From ade424cbb41cf2cce5118f5a32a840e9e9736f40 Mon Sep 17 00:00:00 2001 From: MetaCubeX Date: Sun, 5 Jun 2022 12:52:29 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=B0=83=E6=95=B4dns=20interface?= =?UTF-8?q?=E4=B8=8Eadapter=E9=83=A8=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dns/client.go | 6 +----- dns/doh.go | 14 ++++---------- dns/doq.go | 12 ++++-------- dns/util.go | 8 +++----- 4 files changed, 12 insertions(+), 28 deletions(-) diff --git a/dns/client.go b/dns/client.go index a7df7fe8..0bbe671f 100644 --- a/dns/client.go +++ b/dns/client.go @@ -56,11 +56,7 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) var conn net.Conn if c.proxyAdapter != "" { - conn, err = dialContextWithProxyAdapter(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...) - } + conn, err = dialContextExtra(ctx, c.proxyAdapter, network, ip, c.port, options...) } else { conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...) } diff --git a/dns/doh.go b/dns/doh.go index 964b3396..36f5d21c 100644 --- a/dns/doh.go +++ b/dns/doh.go @@ -97,17 +97,11 @@ func newDoHClient(url string, r *Resolver, proxyAdapter string) *dohClient { return nil, err } - if proxyAdapter != "" { - var conn net.Conn - conn, err = dialContextWithProxyAdapter(ctx, proxyAdapter, "tcp", ip, port) - if err == errProxyNotFound { - options := []dialer.Option{dialer.WithInterface(proxyAdapter), dialer.WithRoutingMark(0)} - conn, err = dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port), options...) - } - return conn, err + if proxyAdapter == "" { + return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port)) + } else { + return dialContextExtra(ctx, proxyAdapter, "tcp", ip, port) } - - return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port)) }, }, } diff --git a/dns/doq.go b/dns/doq.go index f0e931f0..ffcb001c 100644 --- a/dns/doq.go +++ b/dns/doq.go @@ -167,20 +167,16 @@ func (dc *quicClient) openSession() (quic.Connection, error) { return nil, err } } else { - conn, err := dialContextWithProxyAdapter(context.Background(), dc.proxyAdapter, "udp", ip, port) - if err == errProxyNotFound { - 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 { + conn, err := dialContextExtra(context.Background(), dc.proxyAdapter, "udp", ip, port) + if err != nil { return nil, err } + wrapConn, ok := conn.(*wrapPacketConn) if !ok { return nil, fmt.Errorf("quio create packet failed") } + udp = wrapConn } diff --git a/dns/util.go b/dns/util.go index bbc85bad..3c9c07df 100644 --- a/dns/util.go +++ b/dns/util.go @@ -3,7 +3,6 @@ package dns import ( "context" "crypto/tls" - "errors" "fmt" "net" "net/netip" @@ -20,8 +19,6 @@ import ( 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) { var ttl uint32 switch { @@ -146,10 +143,11 @@ func (wpc *wrapPacketConn) RemoteAddr() net.Addr { 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] 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