-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto-forward all workspace open ports when using Latest JetBrains IDEs
- Loading branch information
Showing
9 changed files
with
324 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...jetbrains/backend-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/GitpodPortsService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package io.gitpod.jetbrains.remote | ||
|
||
import com.intellij.openapi.diagnostic.thisLogger | ||
import com.jetbrains.rd.util.URI | ||
import org.apache.http.client.utils.URIBuilder | ||
import java.util.Optional | ||
import java.util.regex.Pattern | ||
|
||
class GitpodPortsService { | ||
companion object { | ||
/** Host used by forwarded ports on JetBrains Client. */ | ||
const val FORWARDED_PORT_HOST = "127.0.0.1" | ||
} | ||
private val hostToClientForwardedPortMap: MutableMap<Int, Int> = mutableMapOf() | ||
|
||
fun isForwarded(hostPort: Int): Boolean = hostToClientForwardedPortMap.containsKey(hostPort) | ||
|
||
private fun getForwardedPort(hostPort: Int): Optional<Int> = Optional.ofNullable(hostToClientForwardedPortMap[hostPort]) | ||
|
||
fun setForwardedPort(hostPort: Int, clientPort: Int) { | ||
hostToClientForwardedPortMap[hostPort] = clientPort | ||
} | ||
|
||
fun removeForwardedPort(hostPort: Int) { | ||
hostToClientForwardedPortMap.remove(hostPort) | ||
} | ||
|
||
fun getLocalHostUriFromHostPort(hostPort: Int): URI { | ||
val optionalForwardedPort = getForwardedPort(hostPort) | ||
|
||
val port = if (optionalForwardedPort.isPresent) { | ||
optionalForwardedPort.get() | ||
} else { | ||
thisLogger().warn( | ||
"gitpod: Tried to get the forwarded port of $hostPort, which was not forwarded. " + | ||
"Returning $hostPort itself." | ||
) | ||
hostPort | ||
} | ||
|
||
return URIBuilder() | ||
.setScheme("http") | ||
.setHost(FORWARDED_PORT_HOST) | ||
.setPort(port) | ||
.build() | ||
} | ||
|
||
interface LocalHostUriMetadata { | ||
val address: String | ||
val port: Int | ||
} | ||
|
||
fun extractLocalHostUriMetaDataForPortMapping(uri: URI): Optional<LocalHostUriMetadata> { | ||
if (uri.scheme != "http" && uri.scheme != "https") return Optional.empty() | ||
|
||
val localhostMatch = Pattern.compile("^(localhost|127(?:\\.[0-9]+){0,2}\\.[0-9]+|0+(?:\\.0+){0,2}\\.0+|\\[(?:0*:)*?:?0*1?])(?::(\\d+))?\$").matcher(uri.authority) | ||
|
||
if (!localhostMatch.find()) return Optional.empty() | ||
|
||
var address = localhostMatch.group(1) | ||
if (address.startsWith('[') && address.endsWith(']')) { | ||
address = address.substring(1, address.length - 2) | ||
} | ||
|
||
var port = 443 | ||
try { | ||
port = localhostMatch.group(2).toInt() | ||
} catch (throwable: Throwable){ | ||
if (uri.scheme == "http") port = 80 | ||
} | ||
|
||
return Optional.of(object: LocalHostUriMetadata { | ||
override val address = address | ||
override val port = port | ||
}) | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
...d-plugin/src/main/kotlin/io/gitpod/jetbrains/remote/latest/GitpodPortForwardingService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package io.gitpod.jetbrains.remote.latest | ||
|
||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.diagnostic.thisLogger | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.remoteDev.util.onTerminationOrNow | ||
import com.intellij.util.application | ||
import com.jetbrains.codeWithMe.model.RdPortType | ||
import com.jetbrains.rd.platform.util.lifetime | ||
import com.jetbrains.rd.util.lifetime.LifetimeStatus | ||
import com.jetbrains.rdserver.portForwarding.ForwardedPortInfo | ||
import com.jetbrains.rdserver.portForwarding.PortForwardingManager | ||
import com.jetbrains.rdserver.portForwarding.remoteDev.PortEventsProcessor | ||
import io.gitpod.jetbrains.remote.GitpodManager | ||
import io.gitpod.jetbrains.remote.GitpodPortsService | ||
import io.gitpod.supervisor.api.Status | ||
import io.gitpod.supervisor.api.StatusServiceGrpc | ||
import io.grpc.stub.ClientCallStreamObserver | ||
import io.grpc.stub.ClientResponseObserver | ||
import io.ktor.utils.io.* | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.TimeUnit | ||
|
||
@Suppress("UnstableApiUsage") | ||
class GitpodPortForwardingService(private val project: Project) { | ||
companion object { | ||
const val FORWARDED_PORT_LABEL = "gitpod" | ||
} | ||
|
||
private val portsService = service<GitpodPortsService>() | ||
|
||
init { start() } | ||
|
||
private fun start() { | ||
if (application.isHeadlessEnvironment) return | ||
|
||
observePortsListWhileProjectIsOpen() | ||
} | ||
|
||
private fun observePortsListWhileProjectIsOpen() = application.executeOnPooledThread { | ||
while (project.lifetime.status == LifetimeStatus.Alive) { | ||
try { | ||
observePortsList().get() | ||
} catch (throwable: Throwable) { | ||
when (throwable) { | ||
is InterruptedException, is CancellationException -> break | ||
else -> thisLogger().error( | ||
"gitpod: Got an error while trying to get ports list from Supervisor. " + | ||
"Going to try again in a second.", | ||
throwable | ||
) | ||
} | ||
} | ||
|
||
TimeUnit.SECONDS.sleep(1) | ||
} | ||
} | ||
|
||
private fun observePortsList(): CompletableFuture<Void> { | ||
val completableFuture = CompletableFuture<Void>() | ||
|
||
val statusServiceStub = StatusServiceGrpc.newStub(GitpodManager.supervisorChannel) | ||
|
||
val portsStatusRequest = Status.PortsStatusRequest.newBuilder().setObserve(true).build() | ||
|
||
val portsStatusResponseObserver = object : | ||
ClientResponseObserver<Status.PortsStatusRequest, Status.PortsStatusResponse> { | ||
override fun beforeStart(request: ClientCallStreamObserver<Status.PortsStatusRequest>) { | ||
project.lifetime.onTerminationOrNow { request.cancel("gitpod: Project terminated.", null) } | ||
} | ||
override fun onNext(response: Status.PortsStatusResponse) { | ||
application.invokeLater { updateForwardedPortsList(response) } | ||
} | ||
override fun onCompleted() { completableFuture.complete(null) } | ||
override fun onError(throwable: Throwable) { completableFuture.completeExceptionally(throwable) } | ||
} | ||
|
||
statusServiceStub.portsStatus(portsStatusRequest, portsStatusResponseObserver) | ||
|
||
return completableFuture | ||
} | ||
|
||
private fun updateForwardedPortsList(response: Status.PortsStatusResponse) { | ||
val portForwardingManager = PortForwardingManager.getInstance(project) | ||
val forwardedPortsList = portForwardingManager.getForwardedPortsWithLabel(FORWARDED_PORT_LABEL) | ||
|
||
for (port in response.portsList) { | ||
val hostPort = port.localPort | ||
val isServed = port.served | ||
|
||
if (isServed && !forwardedPortsList.containsKey(hostPort)) { | ||
val portEventsProcessor = object : PortEventsProcessor { | ||
override fun onPortForwarded(hostPort: Int, clientPort: Int) { | ||
portsService.setForwardedPort(hostPort, clientPort) | ||
thisLogger().info("gitpod: Forwarded port $hostPort to client's port $clientPort.") | ||
} | ||
|
||
override fun onPortForwardingEnded(hostPort: Int) { | ||
thisLogger().info("gitpod: Finished forwarding port $hostPort.") | ||
} | ||
|
||
override fun onPortForwardingFailed(hostPort: Int, reason: String) { | ||
thisLogger().error("gitpod: Failed to forward port $hostPort: $reason") | ||
} | ||
} | ||
|
||
val portInfo = ForwardedPortInfo( | ||
hostPort, | ||
RdPortType.HTTP, | ||
FORWARDED_PORT_LABEL, | ||
emptyList(), | ||
portEventsProcessor | ||
) | ||
|
||
portForwardingManager.forwardPort(portInfo) | ||
} | ||
|
||
if (!isServed && forwardedPortsList.containsKey(hostPort)) { | ||
portForwardingManager.removePort(hostPort) | ||
portsService.removeForwardedPort(hostPort) | ||
thisLogger().info("gitpod: Stopped forwarding port $hostPort.") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.