From 0d55b288051afc44d984c06b2c20a92944193b5a Mon Sep 17 00:00:00 2001 From: Skyxim Date: Tue, 21 Jun 2022 22:59:35 +0800 Subject: [PATCH] chore: dns interface name --- dns/util.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/dns/util.go b/dns/util.go index 4ea511a9..7abbbe97 100644 --- a/dns/util.go +++ b/dns/util.go @@ -152,17 +152,9 @@ func (wpc *wrapPacketConn) LocalAddr() net.Addr { } 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 { - opts = append(opts, dialer.WithInterface(adapterName)) - adapter, _ = tunnel.Proxies()[tunnel.Direct.String()] - } - networkType := C.TCP if network == "udp" { - if !adapter.SupportUDP() { - return nil, fmt.Errorf("proxy adapter [%s] UDP is not supported", adapterName) - } + networkType = C.UDP } @@ -179,6 +171,29 @@ func dialContextExtra(ctx context.Context, adapterName string, network string, d DstPort: port, } + adapter, ok := tunnel.Proxies()[adapterName] + if !ok { + opts = append(opts, dialer.WithInterface(adapterName)) + if C.TCP == networkType { + return dialer.DialContext(ctx, network, dstIP.String()+":"+port, opts...) + } else { + packetConn, err := dialer.ListenPacket(ctx, network, dstIP.String()+":"+port, opts...) + if err != nil { + return nil, err + } + + return &wrapPacketConn{ + PacketConn: packetConn, + rAddr: metadata.UDPAddr(), + }, nil + + } + } + + if networkType == C.UDP && !adapter.SupportUDP() { + return nil, fmt.Errorf("proxy adapter [%s] UDP is not supported", adapterName) + } + if networkType == C.UDP { packetConn, err := adapter.ListenPacketContext(ctx, metadata, opts...) if err != nil {