Skip to content

[JetBrains EAP] Port Forwarding improvements #14408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.ide.BrowserUtil
import com.intellij.openapi.client.ClientSession
import com.intellij.openapi.client.ClientSessionsManager
import com.intellij.openapi.components.service
import com.intellij.openapi.components.serviceOrNull
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.BufferExposingByteArrayOutputStream
Expand All @@ -18,6 +19,7 @@ import com.intellij.util.withFragment
import com.intellij.util.withPath
import com.intellij.util.withQuery
import com.jetbrains.rd.util.URI
import io.gitpod.jetbrains.remote.utils.LocalHostUri
import io.netty.buffer.Unpooled
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.http.FullHttpRequest
Expand All @@ -29,12 +31,12 @@ import org.jetbrains.io.response
import java.io.OutputStreamWriter
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.util.*

@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
class GitpodCLIService : RestService() {

private val manager = service<GitpodManager>()
private val portsService = service<GitpodPortsService>()
private val cliHelperService = service<GitpodCLIHelper>()

override fun getServiceName() = SERVICE_NAME
Expand Down Expand Up @@ -74,29 +76,35 @@ class GitpodCLIService : RestService() {
return "url is missing"
}

val resolvedUrl = resolveExternalUrl(url)

return withClient(request, context) { project ->
var resolvedUrl = url
val uri = URI.create(url)
val localHostUriMetadata = LocalHostUri.extractLocalHostUriMetaDataForPortMapping(uri)
val gitpodPortForwardingService = serviceOrNull<GitpodPortForwardingService>()

if (localHostUriMetadata.isPresent && gitpodPortForwardingService != null) {
var localHostUriFromPort = Optional.empty<URI>()

application.invokeAndWait {
localHostUriFromPort = gitpodPortForwardingService
.getLocalHostUriFromHostPort(localHostUriMetadata.get().port)
}

if (localHostUriFromPort.isPresent) {
resolvedUrl = localHostUriFromPort.get()
.withPath(uri.path)
.withQuery(uri.query)
.withFragment(uri.fragment)
.toString()
}
}

BrowserUtil.browse(resolvedUrl, project)
}
}
return "invalid operation"
}

private fun resolveExternalUrl(url: String): String {
val uri = URI.create(url)
val optionalLocalHostUriMetadata = portsService.extractLocalHostUriMetaDataForPortMapping(uri)

return when {
optionalLocalHostUriMetadata.isEmpty -> url
else -> portsService.getLocalHostUriFromHostPort(optionalLocalHostUriMetadata.get().port)
.withPath(uri.path)
.withQuery(uri.query)
.withFragment(uri.fragment)
.toString()
}
}

private fun withClient(request: FullHttpRequest, context: ChannelHandlerContext, action: suspend (project: Project?) -> Unit): String? {
GlobalScope.launch {
getClientSessionAndProjectAsync().let { (session, project) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.intellij.notification.NotificationType
import com.intellij.openapi.Disposable
import com.intellij.openapi.client.ClientSessionsManager
import com.intellij.openapi.components.service
import com.intellij.openapi.components.serviceOrNull
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.fileEditor.FileEditorManagerEvent
import com.intellij.openapi.fileEditor.FileEditorManagerListener
Expand All @@ -29,14 +30,14 @@ import io.grpc.stub.ClientCallStreamObserver
import io.grpc.stub.ClientResponseObserver
import kotlinx.coroutines.*
import kotlinx.coroutines.future.await
import java.util.*
import java.util.concurrent.CancellationException
import java.util.concurrent.CompletableFuture

@Suppress("UnstableApiUsage", "OPT_IN_USAGE")
class GitpodClientProjectSessionTracker(private val project: Project) : Disposable {

private val manager = service<GitpodManager>()
private val portsService = service<GitpodPortsService>()
private val session = ClientSessionsManager.getProjectSession(project)

private lateinit var info: Info.WorkspaceInfoResponse
Expand Down Expand Up @@ -64,8 +65,12 @@ class GitpodClientProjectSessionTracker(private val project: Project) : Disposab
}

private fun getForwardedPortUrl(port: PortsStatus): String {
val localHostUri = serviceOrNull<GitpodPortForwardingService>()
?.getLocalHostUriFromHostPort(port.localPort)
?: Optional.empty()

return when {
portsService.isForwarded(port.localPort) -> portsService.getLocalHostUriFromHostPort(port.localPort).toString()
localHostUri.isPresent -> localHostUri.get().toString()
else -> port.exposed.url
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
package io.gitpod.jetbrains.remote

import com.intellij.openapi.Disposable
import java.util.Optional
import com.jetbrains.rd.util.URI

interface GitpodGlobalPortForwardingService {
fun monitorPortsOfPid(disposable: Disposable, pid: Long)
interface GitpodPortForwardingService : Disposable {
/** Returns the localhost URI if the given port is forwarded on client. */
fun getLocalHostUriFromHostPort(hostPort: Int): Optional<URI>
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package io.gitpod.jetbrains.remote

// import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.thisLogger
import com.intellij.openapi.project.Project
import com.intellij.util.application
Expand Down Expand Up @@ -35,7 +34,6 @@ class GitpodTerminalService(project: Project) {
private val terminalServiceFutureStub = TerminalServiceGrpc.newFutureStub(GitpodManager.supervisorChannel)
private val terminalServiceStub = TerminalServiceGrpc.newStub(GitpodManager.supervisorChannel)
private val statusServiceStub = StatusServiceGrpc.newStub(GitpodManager.supervisorChannel)
// private val portForwardingService = service<GitpodGlobalPortForwardingService>()

init {
start()
Expand Down Expand Up @@ -187,10 +185,6 @@ class GitpodTerminalService(project: Project) {
exitTaskWhenTerminalWidgetGetsClosed(supervisorTerminal, shellTerminalWidget)

listenForTaskTerminationAndTitleChanges(supervisorTerminal, shellTerminalWidget)

// This works for auto-forwarding ports opened in Gitpod Terminals, but it's currently not useful as we
// have the GitpodPortForwardingService already auto-forwarding all the ports from the workspace.
// portForwardingService.monitorPortsOfPid(shellTerminalWidget, supervisorTerminal.pid)
}

private fun listenForTaskTerminationAndTitleChanges(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.ide.CopyPasteManager
import com.jetbrains.rd.platform.codeWithMe.portForwarding.ClientPortState
import com.jetbrains.rd.platform.codeWithMe.portForwarding.PortConfiguration
import com.jetbrains.rd.platform.codeWithMe.portForwarding.PortForwardingDataKeys
import io.gitpod.jetbrains.remote.GitpodPortsService
import org.apache.http.client.utils.URIBuilder
import java.awt.datatransfer.StringSelection

@Suppress("ComponentNotRegistered", "UnstableApiUsage")
class GitpodCopyUrlAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
e.dataContext.getData(PortForwardingDataKeys.SUGGESTION)?.getSuggestedHostPort()?.let { hostPort ->
service<GitpodPortsService>().getLocalHostUriFromHostPort(hostPort).let { localHostUri ->
CopyPasteManager.getInstance().setContents(StringSelection(localHostUri.toString()))
}
}
(e.dataContext.getData(PortForwardingDataKeys.PORT)
?.configuration as PortConfiguration.PerClientTcpForwarding?)
?.clientPortState?.let {
if (it is ClientPortState.Assigned) {
CopyPasteManager.getInstance().setContents(StringSelection(
URIBuilder()
.setScheme("http")
.setHost(it.clientInterface)
.setPort(it.clientPort)
.build()
.toString())
)
}
}
}

override fun update(e: AnActionEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ import com.intellij.openapi.actionSystem.ActionPlaces
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.components.service
import com.intellij.openapi.ide.CopyPasteManager
import com.intellij.util.application
import com.jetbrains.rd.platform.codeWithMe.portForwarding.PerClientPortForwardingManager
import com.jetbrains.rd.platform.codeWithMe.portForwarding.PortConfiguration
import com.jetbrains.rd.platform.codeWithMe.portForwarding.PortForwardingDataKeys
import io.gitpod.jetbrains.remote.GitpodManager
import io.gitpod.supervisor.api.Status.PortsStatusRequest
import io.gitpod.supervisor.api.StatusServiceGrpc
import kotlinx.coroutines.launch
import java.awt.datatransfer.StringSelection

@Suppress("ComponentNotRegistered", "UnstableApiUsage")
class GitpodCopyWebUrlAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
e.dataContext.getData(PortForwardingDataKeys.SUGGESTION)?.getSuggestedHostPort()?.let { hostPort ->
application.coroutineScope.launch {
getUrlFromPort(hostPort)?.let {
CopyPasteManager.getInstance().setContents(StringSelection(it))
}
(service<PerClientPortForwardingManager>().getPorts(hostPort).firstOrNull {
it.labels.contains(GitpodPortForwardingServiceImpl.EXPOSED_PORT_LABEL)
}?.configuration as PortConfiguration.UrlExposure?)?.exposedUrl?.let {
CopyPasteManager.getInstance().setContents(StringSelection(it))
}
}
}
Expand All @@ -34,17 +32,4 @@ class GitpodCopyWebUrlAction : AnAction() {
}

override fun getActionUpdateThread() = ActionUpdateThread.BGT

private fun getUrlFromPort(port: Number): String? {
val blockingStub = StatusServiceGrpc.newBlockingStub(GitpodManager.supervisorChannel)
val request = PortsStatusRequest.newBuilder().setObserve(false).build()
val response = blockingStub.portsStatus(request)
while (response.hasNext()) {
val portStatusResponse = response.next()
for (portStatus in portStatusResponse.portsList) {
if (portStatus.localPort == port) return portStatus.exposed.url
}
}
return null
}
}

This file was deleted.

Loading