@@ -7,11 +7,7 @@ class PortMatcher(private val rule: String) {
7
7
data class RegexPort (val pattern : Regex ) : MatchRule()
8
8
}
9
9
10
- private val parsedRule: MatchRule
11
-
12
- init {
13
- parsedRule = parseRule(rule)
14
- }
10
+ private val parsedRule: MatchRule = parseRule(rule)
15
11
16
12
fun matches (port : Int ): Boolean {
17
13
return when (parsedRule) {
@@ -33,12 +29,10 @@ class PortMatcher(private val rule: String) {
33
29
MatchRule .SinglePort (port)
34
30
}
35
31
// Try parsing as port range (e.g., "40000-55000")
36
- portPart.matches(" ^\\ d+- \\ d+$" .toRegex()) -> {
32
+ portPart.matches(" ^\\ d+\\ s*- \\ s* \\ d+$" .toRegex()) -> {
37
33
val (start, end) = portPart.split(' -' )
38
34
.map { it.trim().toInt() }
39
- validatePort(start)
40
- validatePort(end)
41
- require(start <= end) { " Invalid port range: start must be less than or equal to end" }
35
+ validatePortRange(start, end)
42
36
MatchRule .PortRange (start, end)
43
37
}
44
38
// If not a single port or range, treat as regex
@@ -56,7 +50,9 @@ class PortMatcher(private val rule: String) {
56
50
require(port in 0 .. 65535 ) { " Port number must be between 0 and 65535, got: $port " }
57
51
}
58
52
59
- companion object {
60
- const val MAX_PORT = 65535
53
+ private fun validatePortRange (start : Int , end : Int ) {
54
+ validatePort(start)
55
+ validatePort(end)
56
+ require(start <= end) { " Invalid port range: start must be less than or equal to end" }
61
57
}
62
58
}
0 commit comments