fix(listener/tun): parse routes from URL query string and bare CIDR (issue #735)
mdutil.GetStrings only matches []string and []any — a plain string from a URL query parameter (routes=0.0.0.0/0) returned nil, silently dropping the route. Added GetString fallback with comma-split, and bare-CIDR support that defaults to config.Gateway. Same fix applied to both tun and tungo listeners.
This commit is contained in:
@@ -60,14 +60,30 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
for _, s := range mdutil.GetStrings(md, "routes", "tun.routes") {
|
||||
routeStrs := mdutil.GetStrings(md, "routes", "tun.routes")
|
||||
if len(routeStrs) == 0 {
|
||||
// Fall back to single string (e.g. from URL query parameter routes=0.0.0.0/0)
|
||||
if s := mdutil.GetString(md, "routes", "tun.routes"); s != "" {
|
||||
routeStrs = strings.Split(s, ",")
|
||||
}
|
||||
}
|
||||
for _, s := range routeStrs {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
ss := strings.SplitN(s, " ", 2)
|
||||
var cidr, gwStr string
|
||||
if len(ss) == 2 {
|
||||
_, ipNet, _ := net.ParseCIDR(strings.TrimSpace(ss[0]))
|
||||
cidr, gwStr = strings.TrimSpace(ss[0]), ss[1]
|
||||
} else {
|
||||
cidr = s
|
||||
}
|
||||
_, ipNet, _ := net.ParseCIDR(cidr)
|
||||
if ipNet == nil {
|
||||
continue
|
||||
}
|
||||
gw := net.ParseIP(ss[1])
|
||||
gw := net.ParseIP(gwStr)
|
||||
if gw == nil {
|
||||
gw = config.Gateway
|
||||
}
|
||||
@@ -83,7 +99,6 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
Gateway: gateway,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range strings.Split(mdutil.GetString(md, "dns", "tun.dns"), ",") {
|
||||
if ip := net.ParseIP(strings.TrimSpace(v)); ip != nil {
|
||||
|
||||
@@ -57,14 +57,30 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
for _, s := range mdutil.GetStrings(md, "routes", "tun.routes") {
|
||||
routeStrs := mdutil.GetStrings(md, "routes", "tun.routes")
|
||||
if len(routeStrs) == 0 {
|
||||
// Fall back to single string (e.g. from URL query parameter routes=0.0.0.0/0)
|
||||
if s := mdutil.GetString(md, "routes", "tun.routes"); s != "" {
|
||||
routeStrs = strings.Split(s, ",")
|
||||
}
|
||||
}
|
||||
for _, s := range routeStrs {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
ss := strings.SplitN(s, " ", 2)
|
||||
var cidr, gwStr string
|
||||
if len(ss) == 2 {
|
||||
_, ipNet, _ := net.ParseCIDR(strings.TrimSpace(ss[0]))
|
||||
cidr, gwStr = strings.TrimSpace(ss[0]), ss[1]
|
||||
} else {
|
||||
cidr = s
|
||||
}
|
||||
_, ipNet, _ := net.ParseCIDR(cidr)
|
||||
if ipNet == nil {
|
||||
continue
|
||||
}
|
||||
gw := net.ParseIP(ss[1])
|
||||
gw := net.ParseIP(gwStr)
|
||||
if gw == nil {
|
||||
gw = config.Gateway
|
||||
}
|
||||
@@ -80,7 +96,6 @@ func (l *tunListener) parseMetadata(md mdata.Metadata) (err error) {
|
||||
Gateway: gateway,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range strings.Split(mdutil.GetString(md, "dns", "tun.dns"), ",") {
|
||||
if ip := net.ParseIP(strings.TrimSpace(v)); ip != nil {
|
||||
|
||||
Reference in New Issue
Block a user