-
Notifications
You must be signed in to change notification settings - Fork 3
Control automatic port forwarding with a devcontainer.json file #49
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
bcpeinhardt
merged 9 commits into
coder:main
from
aaronlehmann:ports-from-devcontainer-json
Feb 27, 2025
+234
−5
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
12f38a1
Control automatic port forwarding with a devcontainer.json file
aaronlehmann ae1df2f
Validate single port or range endpoint is between 0 and 65535
aaronlehmann 933e1d4
Split devcontainer.json file path out to a settings module
aaronlehmann cf25cbe
Add validation that onAutoForward is a string
aaronlehmann 69432d7
PortMatcher code review feedback
aaronlehmann 3433d28
Use kotlinx.serialization for JSON deserialization
aaronlehmann 2da0908
Add comment about switching kotlinx-serialization-json to compileOnly
aaronlehmann 8d86ee7
Merge branch 'main' into ports-from-devcontainer-json
bcpeinhardt fa0df63
changelog
bcpeinhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/com/coder/jetbrains/matcher/PortMatcher.kt
This file contains hidden or 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,58 @@ | ||
package com.coder.jetbrains.matcher | ||
|
||
class PortMatcher(private val rule: String) { | ||
private sealed class MatchRule { | ||
data class SinglePort(val port: Int) : MatchRule() | ||
data class PortRange(val start: Int, val end: Int) : MatchRule() | ||
data class RegexPort(val pattern: Regex) : MatchRule() | ||
} | ||
|
||
private val parsedRule: MatchRule = parseRule(rule) | ||
|
||
fun matches(port: Int): Boolean { | ||
return when (parsedRule) { | ||
is MatchRule.SinglePort -> port == parsedRule.port | ||
is MatchRule.PortRange -> port in parsedRule.start..parsedRule.end | ||
is MatchRule.RegexPort -> parsedRule.pattern.matches(port.toString()) | ||
} | ||
} | ||
|
||
private fun parseRule(rule: String): MatchRule { | ||
// Remove host part if present (e.g., "localhost:3000" -> "3000") | ||
val portPart = rule.substringAfter(':').takeIf { ':' in rule } ?: rule | ||
|
||
return when { | ||
// Try parsing as single port | ||
portPart.all { it.isDigit() } -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non Blocking Nit/Question: Should we validate the port is between 0 and 65535 for the SinglePort and PortRange parsing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this validation |
||
val port = portPart.toInt() | ||
validatePort(port) | ||
MatchRule.SinglePort(port) | ||
} | ||
// Try parsing as port range (e.g., "40000-55000") | ||
portPart.matches("^\\d+\\s*-\\s*\\d+$".toRegex()) -> { | ||
val (start, end) = portPart.split('-') | ||
.map { it.trim().toInt() } | ||
validatePortRange(start, end) | ||
MatchRule.PortRange(start, end) | ||
} | ||
// If not a single port or range, treat as regex | ||
else -> { | ||
try { | ||
MatchRule.RegexPort(portPart.toRegex()) | ||
} catch (e: Exception) { | ||
throw IllegalArgumentException("Invalid port rule format: $rule") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun validatePort(port: Int) { | ||
require(port in 0..65535) { "Port number must be between 0 and 65535, got: $port" } | ||
} | ||
|
||
private fun validatePortRange(start: Int, end: Int) { | ||
validatePort(start) | ||
validatePort(end) | ||
require(start <= end) { "Invalid port range: start must be less than or equal to end" } | ||
} | ||
} |
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/coder/jetbrains/services/DevContainerConfig.kt
This file contains hidden or 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,24 @@ | ||
package com.coder.jetbrains.services | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class DevContainerConfig( | ||
@SerialName("portsAttributes") | ||
val portsAttributes: Map<String, PortAttributes> = mapOf(), | ||
@SerialName("otherPortsAttributes") | ||
val otherPortsAttributes: OtherPortsAttributes? = null | ||
) | ||
|
||
@Serializable | ||
data class PortAttributes( | ||
@SerialName("onAutoForward") | ||
val onAutoForward: String = "" | ||
) | ||
|
||
@Serializable | ||
data class OtherPortsAttributes( | ||
@SerialName("onAutoForward") | ||
val onAutoForward: String = "" | ||
) |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/coder/jetbrains/settings/CoderBackendSettings.kt
This file contains hidden or 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,10 @@ | ||
package com.coder.jetbrains.settings | ||
|
||
import java.io.File | ||
|
||
object CoderBackendSettings { | ||
fun getDevcontainerFile(): File { | ||
// TODO: make path configurable? | ||
return File(System.getProperty("user.home"), ".cache/JetBrains/devcontainer.json") | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/test/kotlin/com/coder/jetbrains/matcher/PortMatcherTest.kt
This file contains hidden or 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,71 @@ | ||
package com.coder.jetbrains.matcher | ||
|
||
import org.junit.Test | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Assert.assertThrows | ||
|
||
class PortMatcherTest { | ||
|
||
@Test | ||
fun `test single port`() { | ||
val matcher = PortMatcher("3000") | ||
assertTrue(matcher.matches(3000)) | ||
assertFalse(matcher.matches(2999)) | ||
assertFalse(matcher.matches(3001)) | ||
} | ||
|
||
@Test | ||
fun `test host colon port`() { | ||
val matcher = PortMatcher("localhost:3000") | ||
assertTrue(matcher.matches(3000)) | ||
assertFalse(matcher.matches(3001)) | ||
} | ||
|
||
@Test | ||
fun `test port range`() { | ||
val matcher = PortMatcher("40000-55000") | ||
assertFalse(matcher.matches(39999)) | ||
assertTrue(matcher.matches(40000)) | ||
assertTrue(matcher.matches(50000)) | ||
assertTrue(matcher.matches(55000)) | ||
assertFalse(matcher.matches(55001)) | ||
} | ||
|
||
@Test | ||
fun `test port range with whitespace`() { | ||
val matcher = PortMatcher("20021 - 20024") | ||
assertFalse(matcher.matches(20000)) | ||
assertTrue(matcher.matches(20022)) | ||
} | ||
|
||
@Test | ||
fun `test regex`() { | ||
val matcher = PortMatcher("800[1-9]") | ||
assertFalse(matcher.matches(8000)) | ||
assertTrue(matcher.matches(8001)) | ||
assertTrue(matcher.matches(8005)) | ||
assertTrue(matcher.matches(8009)) | ||
assertFalse(matcher.matches(8010)) | ||
} | ||
|
||
@Test | ||
fun `test invalid port numbers`() { | ||
assertThrows(IllegalArgumentException::class.java) { PortMatcher("65536") } | ||
assertThrows(IllegalArgumentException::class.java) { PortMatcher("0-65536") } | ||
assertThrows(IllegalArgumentException::class.java) { PortMatcher("70000") } | ||
bcpeinhardt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Test | ||
fun `test edge case port numbers`() { | ||
// These should work | ||
PortMatcher("0") | ||
PortMatcher("65535") | ||
PortMatcher("0-65535") | ||
|
||
// These combinations should work | ||
val matcher = PortMatcher("0-65535") | ||
assertTrue(matcher.matches(0)) | ||
assertTrue(matcher.matches(65535)) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for changing this.
Since IntelliJ provides it at runtime as well this should be
compileOnly
. Another important thing is that here we need to declare the version that is packaged with minimum supported IntelliJ.We support IntelliJ 2022.3 which uses kotlinx.serialization:1.4.1 so that's the minimum version we should use, and it should only be changed when the minimum supported version of IntelliJ changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's unfortunate because this version is too old to support
allowComments
, anddevcontainer.json
files often include comments (since the reference implementation and VS Code allow them). It's not an issue in my particular case because I'll be stripping comments before injecting this file, but for maximum compatibility it seems like supporting comments would be a good thing.Could we depend on this newer version and add a comment that the dependency can be changed to
compileOnly
once the minimum supported IDE version provideskotlinx.serialization
>= 1.7.0? It looks like that minimum IDE version will be idea/251.14649.49 (which includes JetBrains/intellij-community@548a3c9).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as far as I know it should be possible to shadow the dependencies but please test it. As a rule of thumb is best not to shadow intellij dependencies to avoid any classloader/memory leak issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some context: any jars packaged with the plugin are not exposed to the rest of the IDE or other plugins, the plugin has its own classloader, so it's a good way to make plugin stable in its sandbox
So if we need this serialization library and there's a compatibility issue, it's better to bundle it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the comment about switching to
compileOnly
in the future. I've been testing with IntelliJ 242.23339.11 and it seems to work fine, but just tried with EAP 251.22821.72 and it works with that version as well.Let me know if you think it's better or safer to downgrade the dependency and remove
allowComments
. I'm also fine with that approach.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct , and intellij platform sdk also allows the plugins to reference classes provided by other plugins in which the classloader for those plugin dependencies will be used. Not really the case here, so I agree with your statement. It should be safe to pack the serialization library.
I agree with @kirillk there is no need for a downgrade for now.