mirror of
https://github.com/p4gefau1t/trojan-go.git
synced 2024-04-21 12:21:34 +00:00
fix geosite/geoip router
This commit is contained in:
@@ -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 {
|
||||
|
||||
+11
-5
@@ -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
|
||||
}
|
||||
|
||||
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user