From 6f1678feba6c94afa54e190da06ce1388a74de1e Mon Sep 17 00:00:00 2001 From: Page Fault Date: Sun, 24 May 2020 17:36:04 +0000 Subject: [PATCH] fix geosite/geoip router --- proxy/client/client.go | 4 +++- router/mixed/geo.go | 16 +++++++++++----- test/proxy_test.go | 12 ++++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/proxy/client/client.go b/proxy/client/client.go index 17e6b5c..22f80c6 100644 --- a/proxy/client/client.go +++ b/proxy/client/client.go @@ -71,7 +71,7 @@ func (c *Client) handleSocksConn(conn io.ReadWriteCloser) { //notify listenUDP to get ready for relaying udp packets c.associated.Signal() - log.Debug("udp associated to", req) + log.Debug("UDP associated to", req) if err := inboundConn.(protocol.NeedRespond).Respond(); err != nil { log.Error("Failed to repsond") return @@ -94,6 +94,8 @@ func (c *Client) handleSocksConn(conn io.ReadWriteCloser) { log.Error(err) return } + log.Info("Conn tunneling to", req) + log.Debug("Policy", policy) if policy == router.Bypass { outboundConn, err := direct.NewOutboundConnSession(c.ctx, req, c.config) if err != nil { diff --git a/router/mixed/geo.go b/router/mixed/geo.go index fdc921c..9e691ed 100644 --- a/router/mixed/geo.go +++ b/router/mixed/geo.go @@ -29,25 +29,28 @@ func (r *GeoRouter) matchDomain(fulldomain string) bool { if strings.HasSuffix(fulldomain, domain) { idx := strings.Index(fulldomain, domain) if idx == 0 || fulldomain[idx-1] == '.' { + log.Trace("Domain:", fulldomain, "hit domain rule:", domain) return true } } case v2router.Domain_Plain: //keyword if strings.Contains(fulldomain, d.GetValue()) { + log.Trace("Domain:", fulldomain, "hit keyword rule:", d.GetValue()) return true } case v2router.Domain_Regex: matched, err := regexp.Match(d.GetValue(), []byte(fulldomain)) if err != nil { - log.Error("Invalid regex") + log.Error("Invalid regex", d.GetValue()) return false } if matched { + log.Trace("Domain:", fulldomain, "hit regex rule:", d.GetValue()) return true } default: - log.Debug("Unknown type" + d.GetType().String()) + log.Debug("Unknown rule type:" + d.GetType().String()) } } return false @@ -99,11 +102,11 @@ func (r *GeoRouter) routeRequestByIP(domain string) (router.Policy, error) { } func (r *GeoRouter) RouteRequest(req *protocol.Request) (router.Policy, error) { - if r.domains == nil || r.cidrs == nil { - return r.nonMatchPolicy, nil - } switch req.AddressType { case common.DomainName: + if r.domains == nil { + return r.nonMatchPolicy, nil + } domain := string(req.DomainName) if r.strategy == router.IPOnDemand { return r.routeRequestByIP(domain) @@ -116,6 +119,9 @@ func (r *GeoRouter) RouteRequest(req *protocol.Request) (router.Policy, error) { } return r.nonMatchPolicy, nil case common.IPv4, common.IPv6: + if r.cidrs == nil { + return r.nonMatchPolicy, nil + } if r.matchIP(req.IP) { return r.matchPolicy, nil } diff --git a/test/proxy_test.go b/test/proxy_test.go index f64cb96..c53f5ba 100644 --- a/test/proxy_test.go +++ b/test/proxy_test.go @@ -24,6 +24,7 @@ import ( tp "github.com/p4gefau1t/trojan-go/proxy" "github.com/p4gefau1t/trojan-go/proxy/client" "github.com/p4gefau1t/trojan-go/proxy/server" + _ "github.com/p4gefau1t/trojan-go/router/mixed" _ "github.com/p4gefau1t/trojan-go/stat/memory" _ "github.com/p4gefau1t/trojan-go/stat/mysql" "golang.org/x/net/proxy" @@ -386,6 +387,17 @@ func TestRealProxy(t *testing.T) { RunServer(context.Background(), serverConfig) } +func TestRealClient(t *testing.T) { + if os.Getenv("real_test") == "" { + t.Skip("skipping real proxy test") + } + b, err := ioutil.ReadFile("/etc/trojan-go/config.json") + common.Must(err) + config, err := conf.ParseJSON(b) + common.Must(err) + RunClient(context.Background(), config) +} + func TestNormal(t *testing.T) { CheckClientServer(t, getBasicClientConfig(), getBasicServerConfig()) CheckForwardServer(t, getBasicClientConfig(), getBasicServerConfig())