Skip to content

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
merged 9 commits into from
Feb 27, 2025
Prev Previous commit
Next Next commit
PortMatcher code review feedback
  • Loading branch information
aaronlehmann committed Feb 26, 2025
commit 69432d7b318f26ddf85b05358b5e1270b304a96d
18 changes: 7 additions & 11 deletions src/main/kotlin/com/coder/jetbrains/matcher/PortMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ class PortMatcher(private val rule: String) {
data class RegexPort(val pattern: Regex) : MatchRule()
}

private val parsedRule: MatchRule

init {
parsedRule = parseRule(rule)
}
private val parsedRule: MatchRule = parseRule(rule)

fun matches(port: Int): Boolean {
return when (parsedRule) {
Expand All @@ -33,12 +29,10 @@ class PortMatcher(private val rule: String) {
MatchRule.SinglePort(port)
}
// Try parsing as port range (e.g., "40000-55000")
portPart.matches("^\\d+-\\d+$".toRegex()) -> {
portPart.matches("^\\d+\\s*-\\s*\\d+$".toRegex()) -> {
val (start, end) = portPart.split('-')
.map { it.trim().toInt() }
validatePort(start)
validatePort(end)
require(start <= end) { "Invalid port range: start must be less than or equal to end" }
validatePortRange(start, end)
MatchRule.PortRange(start, end)
}
// If not a single port or range, treat as regex
Expand All @@ -56,7 +50,9 @@ class PortMatcher(private val rule: String) {
require(port in 0..65535) { "Port number must be between 0 and 65535, got: $port" }
}

companion object {
const val MAX_PORT = 65535
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" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ class PortMatcherTest {
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]")
Expand Down