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:
+37
-22
@@ -60,30 +60,45 @@ 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")
|
||||||
ss := strings.SplitN(s, " ", 2)
|
if len(routeStrs) == 0 {
|
||||||
if len(ss) == 2 {
|
// Fall back to single string (e.g. from URL query parameter routes=0.0.0.0/0)
|
||||||
_, ipNet, _ := net.ParseCIDR(strings.TrimSpace(ss[0]))
|
if s := mdutil.GetString(md, "routes", "tun.routes"); s != "" {
|
||||||
if ipNet == nil {
|
routeStrs = strings.Split(s, ",")
|
||||||
continue
|
|
||||||
}
|
|
||||||
gw := net.ParseIP(ss[1])
|
|
||||||
if gw == nil {
|
|
||||||
gw = config.Gateway
|
|
||||||
}
|
|
||||||
|
|
||||||
gateway := ""
|
|
||||||
if gw != nil {
|
|
||||||
gateway = gw.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
l.routes = append(l.routes, &router.Route{
|
|
||||||
Net: ipNet,
|
|
||||||
Dst: ipNet.String(),
|
|
||||||
Gateway: gateway,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, s := range routeStrs {
|
||||||
|
s = strings.TrimSpace(s)
|
||||||
|
if s == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ss := strings.SplitN(s, " ", 2)
|
||||||
|
var cidr, gwStr string
|
||||||
|
if len(ss) == 2 {
|
||||||
|
cidr, gwStr = strings.TrimSpace(ss[0]), ss[1]
|
||||||
|
} else {
|
||||||
|
cidr = s
|
||||||
|
}
|
||||||
|
_, ipNet, _ := net.ParseCIDR(cidr)
|
||||||
|
if ipNet == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
gw := net.ParseIP(gwStr)
|
||||||
|
if gw == nil {
|
||||||
|
gw = config.Gateway
|
||||||
|
}
|
||||||
|
|
||||||
|
gateway := ""
|
||||||
|
if gw != nil {
|
||||||
|
gateway = gw.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.routes = append(l.routes, &router.Route{
|
||||||
|
Net: ipNet,
|
||||||
|
Dst: ipNet.String(),
|
||||||
|
Gateway: gateway,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range strings.Split(mdutil.GetString(md, "dns", "tun.dns"), ",") {
|
for _, v := range strings.Split(mdutil.GetString(md, "dns", "tun.dns"), ",") {
|
||||||
if ip := net.ParseIP(strings.TrimSpace(v)); ip != nil {
|
if ip := net.ParseIP(strings.TrimSpace(v)); ip != nil {
|
||||||
|
|||||||
+37
-22
@@ -57,30 +57,45 @@ 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")
|
||||||
ss := strings.SplitN(s, " ", 2)
|
if len(routeStrs) == 0 {
|
||||||
if len(ss) == 2 {
|
// Fall back to single string (e.g. from URL query parameter routes=0.0.0.0/0)
|
||||||
_, ipNet, _ := net.ParseCIDR(strings.TrimSpace(ss[0]))
|
if s := mdutil.GetString(md, "routes", "tun.routes"); s != "" {
|
||||||
if ipNet == nil {
|
routeStrs = strings.Split(s, ",")
|
||||||
continue
|
|
||||||
}
|
|
||||||
gw := net.ParseIP(ss[1])
|
|
||||||
if gw == nil {
|
|
||||||
gw = config.Gateway
|
|
||||||
}
|
|
||||||
|
|
||||||
gateway := ""
|
|
||||||
if gw != nil {
|
|
||||||
gateway = gw.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
l.routes = append(l.routes, &router.Route{
|
|
||||||
Net: ipNet,
|
|
||||||
Dst: ipNet.String(),
|
|
||||||
Gateway: gateway,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, s := range routeStrs {
|
||||||
|
s = strings.TrimSpace(s)
|
||||||
|
if s == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ss := strings.SplitN(s, " ", 2)
|
||||||
|
var cidr, gwStr string
|
||||||
|
if len(ss) == 2 {
|
||||||
|
cidr, gwStr = strings.TrimSpace(ss[0]), ss[1]
|
||||||
|
} else {
|
||||||
|
cidr = s
|
||||||
|
}
|
||||||
|
_, ipNet, _ := net.ParseCIDR(cidr)
|
||||||
|
if ipNet == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
gw := net.ParseIP(gwStr)
|
||||||
|
if gw == nil {
|
||||||
|
gw = config.Gateway
|
||||||
|
}
|
||||||
|
|
||||||
|
gateway := ""
|
||||||
|
if gw != nil {
|
||||||
|
gateway = gw.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.routes = append(l.routes, &router.Route{
|
||||||
|
Net: ipNet,
|
||||||
|
Dst: ipNet.String(),
|
||||||
|
Gateway: gateway,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
for _, v := range strings.Split(mdutil.GetString(md, "dns", "tun.dns"), ",") {
|
for _, v := range strings.Split(mdutil.GetString(md, "dns", "tun.dns"), ",") {
|
||||||
if ip := net.ParseIP(strings.TrimSpace(v)); ip != nil {
|
if ip := net.ParseIP(strings.TrimSpace(v)); ip != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user