chore: 调整dns interface与adapter部分

This commit is contained in:
MetaCubeX 2022-06-05 12:52:29 +08:00
parent 4ad0294655
commit ade424cbb4
4 changed files with 12 additions and 28 deletions

View file

@ -56,11 +56,7 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error)
var conn net.Conn var conn net.Conn
if c.proxyAdapter != "" { if c.proxyAdapter != "" {
conn, err = dialContextWithProxyAdapter(ctx, c.proxyAdapter, network, ip, c.port, options...) conn, err = dialContextExtra(ctx, c.proxyAdapter, network, ip, c.port, options...)
if err == errProxyNotFound {
options = append(options[:0], dialer.WithInterface(c.proxyAdapter), dialer.WithRoutingMark(0))
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...)
}
} else { } else {
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...) conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...)
} }

View file

@ -97,17 +97,11 @@ func newDoHClient(url string, r *Resolver, proxyAdapter string) *dohClient {
return nil, err return nil, err
} }
if proxyAdapter != "" { if proxyAdapter == "" {
var conn net.Conn return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port))
conn, err = dialContextWithProxyAdapter(ctx, proxyAdapter, "tcp", ip, port) } else {
if err == errProxyNotFound { return dialContextExtra(ctx, proxyAdapter, "tcp", ip, port)
options := []dialer.Option{dialer.WithInterface(proxyAdapter), dialer.WithRoutingMark(0)}
conn, err = dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port), options...)
}
return conn, err
} }
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port))
}, },
}, },
} }

View file

@ -167,20 +167,16 @@ func (dc *quicClient) openSession() (quic.Connection, error) {
return nil, err return nil, err
} }
} else { } else {
conn, err := dialContextWithProxyAdapter(context.Background(), dc.proxyAdapter, "udp", ip, port) conn, err := dialContextExtra(context.Background(), dc.proxyAdapter, "udp", ip, port)
if err == errProxyNotFound { if err != nil {
options := []dialer.Option{dialer.WithInterface(dc.proxyAdapter), dialer.WithRoutingMark(0)}
conn, err = dialContextWithProxyAdapter(context.Background(), dc.proxyAdapter, "udp", ip, port, options...)
if err != nil {
return nil, err
}
} else {
return nil, err return nil, err
} }
wrapConn, ok := conn.(*wrapPacketConn) wrapConn, ok := conn.(*wrapPacketConn)
if !ok { if !ok {
return nil, fmt.Errorf("quio create packet failed") return nil, fmt.Errorf("quio create packet failed")
} }
udp = wrapConn udp = wrapConn
} }

View file

@ -3,7 +3,6 @@ package dns
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"net" "net"
"net/netip" "net/netip"
@ -20,8 +19,6 @@ import (
D "github.com/miekg/dns" D "github.com/miekg/dns"
) )
var errProxyNotFound = errors.New("proxy adapter not found")
func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) { func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) {
var ttl uint32 var ttl uint32
switch { switch {
@ -146,10 +143,11 @@ func (wpc *wrapPacketConn) RemoteAddr() net.Addr {
return wpc.rAddr return wpc.rAddr
} }
func dialContextWithProxyAdapter(ctx context.Context, adapterName string, network string, dstIP netip.Addr, port string, opts ...dialer.Option) (net.Conn, error) { func dialContextExtra(ctx context.Context, adapterName string, network string, dstIP netip.Addr, port string, opts ...dialer.Option) (net.Conn, error) {
adapter, ok := tunnel.Proxies()[adapterName] adapter, ok := tunnel.Proxies()[adapterName]
if !ok { if !ok {
return nil, fmt.Errorf("proxy adapter [%s] not found", adapterName) opts = append(opts, dialer.WithInterface(adapterName))
adapter, _ = tunnel.Proxies()[tunnel.Direct.String()]
} }
networkType := C.TCP networkType := C.TCP