direct: optimize codes

This commit is contained in:
nadoo 2021-07-02 19:09:01 +08:00
parent 7f85f664e3
commit 53fe94251f
3 changed files with 12 additions and 9 deletions

View File

@ -152,12 +152,13 @@ func (c *Client) exchange(qname string, reqBytes []byte, preferTCP bool) (
// use tcp to connect upstream server default
network = "tcp"
dialer := c.proxy.NextDialer(qname + ":53")
dialer := c.proxy.NextDialer(qname + ":0")
// if we are resolving the dialer's domain, then use Direct to avoid dependency loop
// if we are resolving a domain which uses a forwarder `REJECT`, then use `DIRECT` instead
// so we can resolve it correctly.
// TODO: dialer.Addr() == "REJECT", tricky
if strings.Contains(dialer.Addr(), qname) || dialer.Addr() == "REJECT" {
dialer = proxy.Default
if dialer.Addr() == "REJECT" {
dialer = c.proxy.NextDialer("direct:0")
}
// If client uses udp and no forwarders specified, use udp

View File

@ -16,9 +16,6 @@ type Direct struct {
relayTimeout time.Duration
}
// Default dialer.
var Default = &Direct{dialTimeout: time.Second * 3}
func init() {
RegisterDialer("direct", NewDirectDialer)
}
@ -47,7 +44,10 @@ func NewDirect(intface string, dialTimeout, relayTimeout time.Duration) (*Direct
// NewDirectDialer returns a direct dialer.
func NewDirectDialer(s string, d Dialer) (Dialer, error) {
return NewDirect("", time.Duration(3)*time.Second, time.Duration(3)*time.Second)
if d == nil {
return NewDirect("", time.Duration(3)*time.Second, time.Duration(3)*time.Second)
}
return d, nil
}
// Addr returns forwarder's address.

View File

@ -40,9 +40,11 @@ func NewProxy(mainForwarders []string, mainStrategy *Strategy, rules []*Config)
}
}
direct := NewFwdrGroup("", nil, mainStrategy)
rd.domainMap.Store("direct", direct)
// if there's any forwarder defined in main config, make sure they will be accessed directly.
if len(mainForwarders) > 0 {
direct := NewFwdrGroup("", nil, mainStrategy)
for _, f := range rd.main.fwdrs {
addr := strings.Split(f.addr, ",")[0]
host, _, _ := net.SplitHostPort(addr)