From a782cf5cf8c330376b5ae1e4a30d492309f495fa Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Tue, 21 Jan 2020 20:34:27 +0800 Subject: [PATCH] fix parseIPRoutes --- cmd/gost/cfg.go | 7 ++++++- gost.go | 2 +- tuntap.go | 9 ++++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/gost/cfg.go b/cmd/gost/cfg.go index d64feaa..ad44de0 100644 --- a/cmd/gost/cfg.go +++ b/cmd/gost/cfg.go @@ -301,7 +301,12 @@ func parseIPRoutes(s string) (routes []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] != "" { _, route.Dest, _ = net.ParseCIDR(strings.TrimSpace(ss[0])) if route.Dest == nil { diff --git a/gost.go b/gost.go index dae0ae0..5e8762a 100644 --- a/gost.go +++ b/gost.go @@ -20,7 +20,7 @@ import ( ) // 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. var Debug bool diff --git a/tuntap.go b/tuntap.go index 84b1bbb..eda5180 100644 --- a/tuntap.go +++ b/tuntap.go @@ -230,6 +230,9 @@ func (h *tunHandler) initTunnelConn(pc net.PacketConn) (net.PacketConn, error) { } 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 { if route.Dest.Contains(dst) && route.Gateway != nil { 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 } @@ -304,6 +304,9 @@ func (h *tunHandler) transportTun(tun net.Conn, conn net.PacketConn, raddr net.A return nil } + if Debug { + log.Logf("[tun] find route: %s -> %s", dst, addr) + } if _, err := conn.WriteTo(b[:n], addr); err != nil { return err }