From 63d0f73d72ddc9b73e6364ab2c8068eb19c53a01 Mon Sep 17 00:00:00 2001 From: Sousa Chinensis <84500424+sousa-chinensis@users.noreply.github.com> Date: Thu, 20 May 2021 13:33:20 +0000 Subject: [PATCH] Feat: change the default behavior of `IPOnDemand` domain strategy (#329) Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> --- tunnel/router/client.go | 55 +++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/tunnel/router/client.go b/tunnel/router/client.go index 30874a7..7506e8b 100644 --- a/tunnel/router/client.go +++ b/tunnel/router/client.go @@ -131,45 +131,40 @@ type Client struct { } func (c *Client) Route(address *tunnel.Address) int { - policy := -1 - var err error - if c.domainStrategy == IPOnDemand { - address, err = newIPAddress(address) - if err != nil { - return c.defaultPolicy - } - } if address.AddressType == tunnel.DomainName { - for i := 0; i < 3; i++ { + if c.domainStrategy == IPOnDemand { + resolvedIP, err := newIPAddress(address) + if err == nil { + for i := Block; i <= Proxy; i++ { + if matchIP(c.cidrs[i], resolvedIP.IP) { + return i + } + } + } + } + for i := Block; i <= Proxy; i++ { if matchDomain(c.domains[i], address.DomainName) { - policy = i - break + return i + } + } + if c.domainStrategy == IPIfNonMatch { + resolvedIP, err := newIPAddress(address) + if err == nil { + for i := Block; i <= Proxy; i++ { + if matchIP(c.cidrs[i], resolvedIP.IP) { + return i + } + } } } } else { - for i := 0; i < 3; i++ { + for i := Block; i <= Proxy; i++ { if matchIP(c.cidrs[i], address.IP) { - policy = i - break + return i } } } - if policy == -1 && c.domainStrategy == IPIfNonMatch { - address, err = newIPAddress(address) - if err != nil { - return c.defaultPolicy - } - for i := 0; i < 3; i++ { - if matchIP(c.cidrs[i], address.IP) { - policy = i - break - } - } - } - if policy == -1 { - policy = c.defaultPolicy - } - return policy + return c.defaultPolicy } func (c *Client) DialConn(address *tunnel.Address, overlay tunnel.Tunnel) (tunnel.Conn, error) {