feat(route): allow empty listening port in port specification

Support the ":proxy" format where only the proxy port is specified.
When the listening port part is empty, it defaults to 0 instead of
returning a parse error.
This commit is contained in:
yusing
2026-02-18 14:37:23 +08:00
parent bb757b2432
commit 115fba4ff4
2 changed files with 13 additions and 2 deletions

View File

@@ -27,7 +27,11 @@ func (p *Port) Parse(v string) (err error) {
p.Proxy, err = strconv.Atoi(v) p.Proxy, err = strconv.Atoi(v)
case 2: case 2:
var err2 error var err2 error
p.Listening, err = strconv.Atoi(parts[0]) if parts[0] == "" {
p.Listening = 0
} else {
p.Listening, err = strconv.Atoi(parts[0])
}
p.Proxy, err2 = strconv.Atoi(parts[1]) p.Proxy, err2 = strconv.Atoi(parts[1])
err = gperr.Join(err, err2) err = gperr.Join(err, err2)
default: default:

View File

@@ -10,7 +10,6 @@ var invalidPorts = []string{
"", "",
"123:", "123:",
"0:", "0:",
":1234",
"qwerty", "qwerty",
"asdfgh:asdfgh", "asdfgh:asdfgh",
"1234:asdfgh", "1234:asdfgh",
@@ -78,6 +77,14 @@ func TestPortValid(t *testing.T) {
Proxy: 5678, Proxy: 5678,
}, },
}, },
{
name: "valid_lp_empty",
inputs: ":1234",
expect: Port{
Listening: 0,
Proxy: 1234,
},
},
{ {
name: "valid_p", name: "valid_p",
inputs: "5678", inputs: "5678",