fix parseIPRoutes

This commit is contained in:
ginuerzh 2020-01-21 20:34:27 +08:00
parent e16427c6c8
commit a782cf5cf8
3 changed files with 13 additions and 5 deletions

View File

@ -301,7 +301,12 @@ func parseIPRoutes(s string) (routes []gost.IPRoute) {
} }
var route gost.IPRoute var route gost.IPRoute
ss := strings.Split(line, " ") var ss []string
for _, s := range strings.Split(line, " ") {
if s = strings.TrimSpace(s); s != "" {
ss = append(ss, s)
}
}
if len(ss) > 0 && ss[0] != "" { if len(ss) > 0 && ss[0] != "" {
_, route.Dest, _ = net.ParseCIDR(strings.TrimSpace(ss[0])) _, route.Dest, _ = net.ParseCIDR(strings.TrimSpace(ss[0]))
if route.Dest == nil { if route.Dest == nil {

View File

@ -20,7 +20,7 @@ import (
) )
// Version is the gost version. // Version is the gost version.
const Version = "2.9.1" const Version = "2.9.2-dev"
// Debug is a flag that enables the debug log. // Debug is a flag that enables the debug log.
var Debug bool var Debug bool

View File

@ -230,6 +230,9 @@ func (h *tunHandler) initTunnelConn(pc net.PacketConn) (net.PacketConn, error) {
} }
func (h *tunHandler) findRouteFor(dst net.IP) net.Addr { func (h *tunHandler) findRouteFor(dst net.IP) net.Addr {
if v, ok := h.routes.Load(ipToTunRouteKey(dst)); ok {
return v.(net.Addr)
}
for _, route := range h.options.IPRoutes { for _, route := range h.options.IPRoutes {
if route.Dest.Contains(dst) && route.Gateway != nil { if route.Dest.Contains(dst) && route.Gateway != nil {
if v, ok := h.routes.Load(ipToTunRouteKey(route.Gateway)); ok { if v, ok := h.routes.Load(ipToTunRouteKey(route.Gateway)); ok {
@ -237,9 +240,6 @@ func (h *tunHandler) findRouteFor(dst net.IP) net.Addr {
} }
} }
} }
if v, ok := h.routes.Load(ipToTunRouteKey(dst)); ok {
return v.(net.Addr)
}
return nil return nil
} }
@ -304,6 +304,9 @@ func (h *tunHandler) transportTun(tun net.Conn, conn net.PacketConn, raddr net.A
return nil return nil
} }
if Debug {
log.Logf("[tun] find route: %s -> %s", dst, addr)
}
if _, err := conn.WriteTo(b[:n], addr); err != nil { if _, err := conn.WriteTo(b[:n], addr); err != nil {
return err return err
} }