chore: always pass context when resolve dns
This commit is contained in:
parent
dbadf37823
commit
901a47318d
20 changed files with 156 additions and 135 deletions
|
@ -56,7 +56,7 @@ func (h *Hysteria) DialContext(ctx context.Context, metadata *C.Metadata, opts .
|
||||||
return dialer.ListenPacket(ctx, "udp", "", h.Base.DialOptions(opts...)...)
|
return dialer.ListenPacket(ctx, "udp", "", h.Base.DialOptions(opts...)...)
|
||||||
},
|
},
|
||||||
remoteAddr: func(addr string) (net.Addr, error) {
|
remoteAddr: func(addr string) (net.Addr, error) {
|
||||||
return resolveUDPAddrWithPrefer("udp", addr, h.prefer)
|
return resolveUDPAddrWithPrefer(ctx, "udp", addr, h.prefer)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata
|
||||||
return dialer.ListenPacket(ctx, "udp", "", h.Base.DialOptions(opts...)...)
|
return dialer.ListenPacket(ctx, "udp", "", h.Base.DialOptions(opts...)...)
|
||||||
},
|
},
|
||||||
remoteAddr: func(addr string) (net.Addr, error) {
|
remoteAddr: func(addr string) (net.Addr, error) {
|
||||||
return resolveUDPAddrWithPrefer("udp", addr, h.prefer)
|
return resolveUDPAddrWithPrefer(ctx, "udp", addr, h.prefer)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
udpConn, err := h.client.DialUDP(&hdc)
|
udpConn, err := h.client.DialUDP(&hdc)
|
||||||
|
|
|
@ -109,7 +109,7 @@ func (ss *ShadowSocks) ListenPacketContext(ctx context.Context, metadata *C.Meta
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err := resolveUDPAddrWithPrefer("udp", ss.addr, ss.prefer)
|
addr, err := resolveUDPAddrWithPrefer(ctx, "udp", ss.addr, ss.prefer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pc.Close()
|
pc.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -79,7 +79,7 @@ func (ssr *ShadowSocksR) ListenPacketContext(ctx context.Context, metadata *C.Me
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
addr, err := resolveUDPAddrWithPrefer("udp", ssr.addr, ssr.prefer)
|
addr, err := resolveUDPAddrWithPrefer(ctx, "udp", ssr.addr, ssr.prefer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pc.Close()
|
pc.Close()
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -129,7 +129,7 @@ func (ss *Socks5) ListenPacketContext(ctx context.Context, metadata *C.Metadata,
|
||||||
err = errors.New("invalid UDP bind address")
|
err = errors.New("invalid UDP bind address")
|
||||||
return
|
return
|
||||||
} else if bindUDPAddr.IP.IsUnspecified() {
|
} else if bindUDPAddr.IP.IsUnspecified() {
|
||||||
serverAddr, err := resolveUDPAddr("udp", ss.Addr())
|
serverAddr, err := resolveUDPAddr(ctx, "udp", ss.Addr())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package outbound
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
xtls "github.com/xtls/go"
|
xtls "github.com/xtls/go"
|
||||||
"net"
|
"net"
|
||||||
|
@ -63,20 +64,20 @@ func serializesSocksAddr(metadata *C.Metadata) []byte {
|
||||||
return bytes.Join(buf, nil)
|
return bytes.Join(buf, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveUDPAddr(network, address string) (*net.UDPAddr, error) {
|
func resolveUDPAddr(ctx context.Context, network, address string) (*net.UDPAddr, error) {
|
||||||
host, port, err := net.SplitHostPort(address)
|
host, port, err := net.SplitHostPort(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := resolver.ResolveProxyServerHost(host)
|
ip, err := resolver.ResolveProxyServerHost(ctx, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
return net.ResolveUDPAddr(network, net.JoinHostPort(ip.String(), port))
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveUDPAddrWithPrefer(network, address string, prefer C.DNSPrefer) (*net.UDPAddr, error) {
|
func resolveUDPAddrWithPrefer(ctx context.Context, network, address string, prefer C.DNSPrefer) (*net.UDPAddr, error) {
|
||||||
host, port, err := net.SplitHostPort(address)
|
host, port, err := net.SplitHostPort(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -84,12 +85,12 @@ func resolveUDPAddrWithPrefer(network, address string, prefer C.DNSPrefer) (*net
|
||||||
var ip netip.Addr
|
var ip netip.Addr
|
||||||
switch prefer {
|
switch prefer {
|
||||||
case C.IPv4Only:
|
case C.IPv4Only:
|
||||||
ip, err = resolver.ResolveIPv4ProxyServerHost(host)
|
ip, err = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||||
case C.IPv6Only:
|
case C.IPv6Only:
|
||||||
ip, err = resolver.ResolveIPv6ProxyServerHost(host)
|
ip, err = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||||
case C.IPv6Prefer:
|
case C.IPv6Prefer:
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
ips, err = resolver.ResolveAllIPProxyServerHost(host)
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||||
var fallback netip.Addr
|
var fallback netip.Addr
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for _, addr := range ips {
|
for _, addr := range ips {
|
||||||
|
@ -107,7 +108,7 @@ func resolveUDPAddrWithPrefer(network, address string, prefer C.DNSPrefer) (*net
|
||||||
default:
|
default:
|
||||||
// C.IPv4Prefer, C.DualStack and other
|
// C.IPv4Prefer, C.DualStack and other
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
ips, err = resolver.ResolveAllIPProxyServerHost(host)
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||||
var fallback netip.Addr
|
var fallback netip.Addr
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for _, addr := range ips {
|
for _, addr := range ips {
|
||||||
|
|
|
@ -233,7 +233,7 @@ func (v *Vless) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
||||||
func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (v *Vless) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
// vless use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
// vless use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("can't resolve ip")
|
return nil, errors.New("can't resolve ip")
|
||||||
}
|
}
|
||||||
|
|
|
@ -243,7 +243,7 @@ func (v *Vmess) DialContext(ctx context.Context, metadata *C.Metadata, opts ...d
|
||||||
func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, opts ...dialer.Option) (_ C.PacketConn, err error) {
|
||||||
// vmess use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
// vmess use stream-oriented udp with a special address, so we needs a net.UDPAddr
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("can't resolve ip")
|
return nil, errors.New("can't resolve ip")
|
||||||
}
|
}
|
||||||
|
|
|
@ -205,7 +205,7 @@ func (w *WireGuard) DialContext(ctx context.Context, metadata *C.Metadata, opts
|
||||||
}
|
}
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
var addrs []netip.Addr
|
var addrs []netip.Addr
|
||||||
addrs, err = resolver.ResolveAllIP(metadata.Host)
|
addrs, err = resolver.LookupIP(ctx, metadata.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -229,7 +229,7 @@ func (w *WireGuard) ListenPacketContext(ctx context.Context, metadata *C.Metadat
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New("can't resolve ip")
|
return nil, errors.New("can't resolve ip")
|
||||||
}
|
}
|
||||||
|
|
|
@ -158,15 +158,15 @@ func dualStackDialContext(ctx context.Context, network, address string, opt *opt
|
||||||
var ip netip.Addr
|
var ip netip.Addr
|
||||||
if ipv6 {
|
if ipv6 {
|
||||||
if !direct {
|
if !direct {
|
||||||
ip, result.error = resolver.ResolveIPv6ProxyServerHost(host)
|
ip, result.error = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ip, result.error = resolver.ResolveIPv6(host)
|
ip, result.error = resolver.ResolveIPv6(ctx, host)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !direct {
|
if !direct {
|
||||||
ip, result.error = resolver.ResolveIPv4ProxyServerHost(host)
|
ip, result.error = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ip, result.error = resolver.ResolveIPv4(host)
|
ip, result.error = resolver.ResolveIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if result.error != nil {
|
if result.error != nil {
|
||||||
|
@ -219,9 +219,9 @@ func concurrentDualStackDialContext(ctx context.Context, network, address string
|
||||||
|
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
if opt.direct {
|
if opt.direct {
|
||||||
ips, err = resolver.ResolveAllIP(host)
|
ips, err = resolver.LookupIP(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ips, err = resolver.ResolveAllIPProxyServerHost(host)
|
ips, err = resolver.LookupIPProxyServerHost(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -344,15 +344,15 @@ func singleDialContext(ctx context.Context, network string, address string, opt
|
||||||
switch network {
|
switch network {
|
||||||
case "tcp4", "udp4":
|
case "tcp4", "udp4":
|
||||||
if !opt.direct {
|
if !opt.direct {
|
||||||
ip, err = resolver.ResolveIPv4ProxyServerHost(host)
|
ip, err = resolver.ResolveIPv4ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ip, err = resolver.ResolveIPv4(host)
|
ip, err = resolver.ResolveIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if !opt.direct {
|
if !opt.direct {
|
||||||
ip, err = resolver.ResolveIPv6ProxyServerHost(host)
|
ip, err = resolver.ResolveIPv6ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ip, err = resolver.ResolveIPv6(host)
|
ip, err = resolver.ResolveIPv6(ctx, host)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -379,9 +379,9 @@ func concurrentIPv4DialContext(ctx context.Context, network, address string, opt
|
||||||
|
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
if !opt.direct {
|
if !opt.direct {
|
||||||
ips, err = resolver.ResolveAllIPv4ProxyServerHost(host)
|
ips, err = resolver.LookupIPv4ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ips, err = resolver.ResolveAllIPv4(host)
|
ips, err = resolver.LookupIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -399,9 +399,9 @@ func concurrentIPv6DialContext(ctx context.Context, network, address string, opt
|
||||||
|
|
||||||
var ips []netip.Addr
|
var ips []netip.Addr
|
||||||
if !opt.direct {
|
if !opt.direct {
|
||||||
ips, err = resolver.ResolveAllIPv6ProxyServerHost(host)
|
ips, err = resolver.LookupIPv6ProxyServerHost(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
ips, err = resolver.ResolveAllIPv6(host)
|
ips, err = resolver.LookupIPv6(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,17 +1,21 @@
|
||||||
package resolver
|
package resolver
|
||||||
|
|
||||||
import D "github.com/miekg/dns"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
D "github.com/miekg/dns"
|
||||||
|
)
|
||||||
|
|
||||||
var DefaultLocalServer LocalServer
|
var DefaultLocalServer LocalServer
|
||||||
|
|
||||||
type LocalServer interface {
|
type LocalServer interface {
|
||||||
ServeMsg(msg *D.Msg) (*D.Msg, error)
|
ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeMsg with a dns.Msg, return resolve dns.Msg
|
// ServeMsg with a dns.Msg, return resolve dns.Msg
|
||||||
func ServeMsg(msg *D.Msg) (*D.Msg, error) {
|
func ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error) {
|
||||||
if server := DefaultLocalServer; server != nil {
|
if server := DefaultLocalServer; server != nil {
|
||||||
return server.ServeMsg(msg)
|
return server.ServeMsg(ctx, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, ErrIPNotFound
|
return nil, ErrIPNotFound
|
||||||
|
|
|
@ -37,21 +37,21 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Resolver interface {
|
type Resolver interface {
|
||||||
ResolveIP(host string) (ip netip.Addr, err error)
|
LookupIP(ctx context.Context, host string) (ips []netip.Addr, err error)
|
||||||
ResolveIPv4(host string) (ip netip.Addr, err error)
|
LookupIPv4(ctx context.Context, host string) (ips []netip.Addr, err error)
|
||||||
ResolveIPv6(host string) (ip netip.Addr, err error)
|
LookupIPv6(ctx context.Context, host string) (ips []netip.Addr, err error)
|
||||||
ResolveAllIP(host string) (ip []netip.Addr, err error)
|
ResolveIP(ctx context.Context, host string) (ip netip.Addr, err error)
|
||||||
ResolveAllIPv4(host string) (ips []netip.Addr, err error)
|
ResolveIPv4(ctx context.Context, host string) (ip netip.Addr, err error)
|
||||||
ResolveAllIPv6(host string) (ips []netip.Addr, err error)
|
ResolveIPv6(ctx context.Context, host string) (ip netip.Addr, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv4 with a host, return ipv4
|
// ResolveIPv4 with a host, return ipv4
|
||||||
func ResolveIPv4(host string) (netip.Addr, error) {
|
func ResolveIPv4(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
return ResolveIPv4WithResolver(host, DefaultResolver)
|
return ResolveIPv4WithResolver(ctx, host, DefaultResolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveIPv4WithResolver(host string, r Resolver) (netip.Addr, error) {
|
func ResolveIPv4WithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
||||||
if ips, err := ResolveAllIPv4WithResolver(host, r); err == nil {
|
if ips, err := LookupIPv4WithResolver(ctx, host, r); err == nil {
|
||||||
return ips[rand.Intn(len(ips))], nil
|
return ips[rand.Intn(len(ips))], nil
|
||||||
} else {
|
} else {
|
||||||
return netip.Addr{}, err
|
return netip.Addr{}, err
|
||||||
|
@ -59,12 +59,12 @@ func ResolveIPv4WithResolver(host string, r Resolver) (netip.Addr, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv6 with a host, return ipv6
|
// ResolveIPv6 with a host, return ipv6
|
||||||
func ResolveIPv6(host string) (netip.Addr, error) {
|
func ResolveIPv6(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
return ResolveIPv6WithResolver(host, DefaultResolver)
|
return ResolveIPv6WithResolver(ctx, host, DefaultResolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveIPv6WithResolver(host string, r Resolver) (netip.Addr, error) {
|
func ResolveIPv6WithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
||||||
if ips, err := ResolveAllIPv6WithResolver(host, r); err == nil {
|
if ips, err := LookupIPv6WithResolver(ctx, host, r); err == nil {
|
||||||
return ips[rand.Intn(len(ips))], nil
|
return ips[rand.Intn(len(ips))], nil
|
||||||
} else {
|
} else {
|
||||||
return netip.Addr{}, err
|
return netip.Addr{}, err
|
||||||
|
@ -72,56 +72,56 @@ func ResolveIPv6WithResolver(host string, r Resolver) (netip.Addr, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPWithResolver same as ResolveIP, but with a resolver
|
// ResolveIPWithResolver same as ResolveIP, but with a resolver
|
||||||
func ResolveIPWithResolver(host string, r Resolver) (netip.Addr, error) {
|
func ResolveIPWithResolver(ctx context.Context, host string, r Resolver) (netip.Addr, error) {
|
||||||
if ip, err := ResolveIPv4WithResolver(host, r); err == nil {
|
if ip, err := ResolveIPv4WithResolver(ctx, host, r); err == nil {
|
||||||
return ip, nil
|
return ip, nil
|
||||||
} else {
|
} else {
|
||||||
return ResolveIPv6WithResolver(host, r)
|
return ResolveIPv6WithResolver(ctx, host, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIP with a host, return ip
|
// ResolveIP with a host, return ip
|
||||||
func ResolveIP(host string) (netip.Addr, error) {
|
func ResolveIP(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
return ResolveIPWithResolver(host, DefaultResolver)
|
return ResolveIPWithResolver(ctx, host, DefaultResolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv4ProxyServerHost proxies server host only
|
// ResolveIPv4ProxyServerHost proxies server host only
|
||||||
func ResolveIPv4ProxyServerHost(host string) (netip.Addr, error) {
|
func ResolveIPv4ProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
if ip, err := ResolveIPv4WithResolver(host, ProxyServerHostResolver); err != nil {
|
if ip, err := ResolveIPv4WithResolver(ctx, host, ProxyServerHostResolver); err != nil {
|
||||||
return ResolveIPv4(host)
|
return ResolveIPv4(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
return ip, nil
|
return ip, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ResolveIPv4(host)
|
return ResolveIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv6ProxyServerHost proxies server host only
|
// ResolveIPv6ProxyServerHost proxies server host only
|
||||||
func ResolveIPv6ProxyServerHost(host string) (netip.Addr, error) {
|
func ResolveIPv6ProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
if ip, err := ResolveIPv6WithResolver(host, ProxyServerHostResolver); err != nil {
|
if ip, err := ResolveIPv6WithResolver(ctx, host, ProxyServerHostResolver); err != nil {
|
||||||
return ResolveIPv6(host)
|
return ResolveIPv6(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
return ip, nil
|
return ip, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ResolveIPv6(host)
|
return ResolveIPv6(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveProxyServerHost proxies server host only
|
// ResolveProxyServerHost proxies server host only
|
||||||
func ResolveProxyServerHost(host string) (netip.Addr, error) {
|
func ResolveProxyServerHost(ctx context.Context, host string) (netip.Addr, error) {
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
if ip, err := ResolveIPWithResolver(host, ProxyServerHostResolver); err != nil {
|
if ip, err := ResolveIPWithResolver(ctx, host, ProxyServerHostResolver); err != nil {
|
||||||
return ResolveIP(host)
|
return ResolveIP(ctx, host)
|
||||||
} else {
|
} else {
|
||||||
return ip, err
|
return ip, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ResolveIP(host)
|
return ResolveIP(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPv6WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
func LookupIPv6WithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
||||||
if DisableIPv6 {
|
if DisableIPv6 {
|
||||||
return []netip.Addr{}, ErrIPv6Disabled
|
return []netip.Addr{}, ErrIPv6Disabled
|
||||||
}
|
}
|
||||||
|
@ -141,12 +141,10 @@ func ResolveAllIPv6WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if r != nil {
|
if r != nil {
|
||||||
return r.ResolveAllIPv6(host)
|
return r.LookupIPv6(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if DefaultResolver == nil {
|
if DefaultResolver == nil {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
|
|
||||||
defer cancel()
|
|
||||||
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip6", host)
|
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip6", host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []netip.Addr{}, err
|
return []netip.Addr{}, err
|
||||||
|
@ -167,7 +165,7 @@ func ResolveAllIPv6WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
||||||
return []netip.Addr{}, ErrIPNotFound
|
return []netip.Addr{}, ErrIPNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPv4WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
func LookupIPv4WithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
||||||
if node := DefaultHosts.Search(host); node != nil {
|
if node := DefaultHosts.Search(host); node != nil {
|
||||||
if ip := node.Data(); ip.Is4() {
|
if ip := node.Data(); ip.Is4() {
|
||||||
return []netip.Addr{node.Data()}, nil
|
return []netip.Addr{node.Data()}, nil
|
||||||
|
@ -183,12 +181,10 @@ func ResolveAllIPv4WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if r != nil {
|
if r != nil {
|
||||||
return r.ResolveAllIPv4(host)
|
return r.LookupIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if DefaultResolver == nil {
|
if DefaultResolver == nil {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), DefaultDNSTimeout)
|
|
||||||
defer cancel()
|
|
||||||
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
|
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip4", host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []netip.Addr{}, err
|
return []netip.Addr{}, err
|
||||||
|
@ -209,7 +205,7 @@ func ResolveAllIPv4WithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
||||||
return []netip.Addr{}, ErrIPNotFound
|
return []netip.Addr{}, ErrIPNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPWithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
func LookupIPWithResolver(ctx context.Context, host string, r Resolver) ([]netip.Addr, error) {
|
||||||
if node := DefaultHosts.Search(host); node != nil {
|
if node := DefaultHosts.Search(host); node != nil {
|
||||||
return []netip.Addr{node.Data()}, nil
|
return []netip.Addr{node.Data()}, nil
|
||||||
}
|
}
|
||||||
|
@ -221,16 +217,16 @@ func ResolveAllIPWithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
||||||
|
|
||||||
if r != nil {
|
if r != nil {
|
||||||
if DisableIPv6 {
|
if DisableIPv6 {
|
||||||
return r.ResolveAllIPv4(host)
|
return r.LookupIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r.ResolveAllIP(host)
|
return r.LookupIP(ctx, host)
|
||||||
} else if DisableIPv6 {
|
} else if DisableIPv6 {
|
||||||
return ResolveAllIPv4(host)
|
return LookupIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
if DefaultResolver == nil {
|
if DefaultResolver == nil {
|
||||||
ipAddrs, err := net.DefaultResolver.LookupIP(context.Background(), "ip", host)
|
ipAddrs, err := net.DefaultResolver.LookupIP(ctx, "ip", host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []netip.Addr{}, err
|
return []netip.Addr{}, err
|
||||||
} else if len(ipAddrs) == 0 {
|
} else if len(ipAddrs) == 0 {
|
||||||
|
@ -249,35 +245,35 @@ func ResolveAllIPWithResolver(host string, r Resolver) ([]netip.Addr, error) {
|
||||||
return []netip.Addr{}, ErrIPNotFound
|
return []netip.Addr{}, ErrIPNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIP(host string) ([]netip.Addr, error) {
|
func LookupIP(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
return ResolveAllIPWithResolver(host, DefaultResolver)
|
return LookupIPWithResolver(ctx, host, DefaultResolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPv4(host string) ([]netip.Addr, error) {
|
func LookupIPv4(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
return ResolveAllIPv4WithResolver(host, DefaultResolver)
|
return LookupIPv4WithResolver(ctx, host, DefaultResolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPv6(host string) ([]netip.Addr, error) {
|
func LookupIPv6(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
return ResolveAllIPv6WithResolver(host, DefaultResolver)
|
return LookupIPv6WithResolver(ctx, host, DefaultResolver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPv6ProxyServerHost(host string) ([]netip.Addr, error) {
|
func LookupIPv6ProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
return ResolveAllIPv6WithResolver(host, ProxyServerHostResolver)
|
return LookupIPv6WithResolver(ctx, host, ProxyServerHostResolver)
|
||||||
}
|
}
|
||||||
return ResolveAllIPv6(host)
|
return LookupIPv6(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPv4ProxyServerHost(host string) ([]netip.Addr, error) {
|
func LookupIPv4ProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
return ResolveAllIPv4WithResolver(host, ProxyServerHostResolver)
|
return LookupIPv4WithResolver(ctx, host, ProxyServerHostResolver)
|
||||||
}
|
}
|
||||||
return ResolveAllIPv4(host)
|
return LookupIPv4(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ResolveAllIPProxyServerHost(host string) ([]netip.Addr, error) {
|
func LookupIPProxyServerHost(ctx context.Context, host string) ([]netip.Addr, error) {
|
||||||
if ProxyServerHostResolver != nil {
|
if ProxyServerHostResolver != nil {
|
||||||
return ResolveAllIPWithResolver(host, ProxyServerHostResolver)
|
return LookupIPWithResolver(ctx, host, ProxyServerHostResolver)
|
||||||
}
|
}
|
||||||
return ResolveAllIP(host)
|
return LookupIP(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package context
|
package context
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
@ -12,14 +14,18 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DNSContext struct {
|
type DNSContext struct {
|
||||||
|
context.Context
|
||||||
|
|
||||||
id uuid.UUID
|
id uuid.UUID
|
||||||
msg *dns.Msg
|
msg *dns.Msg
|
||||||
tp string
|
tp string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDNSContext(msg *dns.Msg) *DNSContext {
|
func NewDNSContext(ctx context.Context, msg *dns.Msg) *DNSContext {
|
||||||
id, _ := uuid.NewV4()
|
id, _ := uuid.NewV4()
|
||||||
return &DNSContext{
|
return &DNSContext{
|
||||||
|
Context: ctx,
|
||||||
|
|
||||||
id: id,
|
id: id,
|
||||||
msg: msg,
|
msg: msg,
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error)
|
||||||
if c.r == nil {
|
if c.r == nil {
|
||||||
return nil, fmt.Errorf("dns %s not a valid ip", c.host)
|
return nil, fmt.Errorf("dns %s not a valid ip", c.host)
|
||||||
} else {
|
} else {
|
||||||
if ip, err = resolver.ResolveIPWithResolver(c.host, c.r); err != nil {
|
if ip, err = resolver.ResolveIPWithResolver(ctx, c.host, c.r); err != nil {
|
||||||
return nil, fmt.Errorf("use default dns resolve failed: %w", err)
|
return nil, fmt.Errorf("use default dns resolve failed: %w", err)
|
||||||
}
|
}
|
||||||
c.host = ip.String()
|
c.host = ip.String()
|
||||||
|
|
|
@ -347,7 +347,7 @@ func (doq *dnsOverQUIC) openConnection() (conn quic.Connection, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := dialContextExtra(ctx, doq.proxyAdapter, "udp", ipAddr, port)
|
conn, err := dialContextExtra(ctx, doq.proxyAdapter, "udp", ipAddr, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -505,7 +505,7 @@ func getDialHandler(r *Resolver, proxyAdapter string) dialHandler {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ip, err := r.ResolveIP(host)
|
ip, err := r.ResolveIP(ctx, host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -156,7 +156,7 @@ func withResolver(resolver *Resolver) handler {
|
||||||
return handleMsgWithEmptyAnswer(r), nil
|
return handleMsgWithEmptyAnswer(r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, err := resolver.Exchange(r)
|
msg, err := resolver.ExchangeContext(ctx, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugln("[DNS Server] Exchange %s failed: %v", q.String(), err)
|
log.Debugln("[DNS Server] Exchange %s failed: %v", q.String(), err)
|
||||||
return msg, err
|
return msg, err
|
||||||
|
|
10
dns/patch.go
10
dns/patch.go
|
@ -1,14 +1,18 @@
|
||||||
package dns
|
package dns
|
||||||
|
|
||||||
import D "github.com/miekg/dns"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
D "github.com/miekg/dns"
|
||||||
|
)
|
||||||
|
|
||||||
type LocalServer struct {
|
type LocalServer struct {
|
||||||
handler handler
|
handler handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeMsg implement resolver.LocalServer ResolveMsg
|
// ServeMsg implement resolver.LocalServer ResolveMsg
|
||||||
func (s *LocalServer) ServeMsg(msg *D.Msg) (*D.Msg, error) {
|
func (s *LocalServer) ServeMsg(ctx context.Context, msg *D.Msg) (*D.Msg, error) {
|
||||||
return handlerWithContext(s.handler, msg)
|
return handlerWithContext(ctx, s.handler, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalServer(resolver *Resolver, mapper *ResolverEnhancer) *LocalServer {
|
func NewLocalServer(resolver *Resolver, mapper *ResolverEnhancer) *LocalServer {
|
||||||
|
|
|
@ -44,18 +44,18 @@ type Resolver struct {
|
||||||
proxyServer []dnsClient
|
proxyServer []dnsClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) ResolveAllIPPrimaryIPv4(host string) (ips []netip.Addr, err error) {
|
func (r *Resolver) LookupIPPrimaryIPv4(ctx context.Context, host string) (ips []netip.Addr, err error) {
|
||||||
ch := make(chan []netip.Addr, 1)
|
ch := make(chan []netip.Addr, 1)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
ip, err := r.resolveIP(host, D.TypeAAAA)
|
ip, err := r.resolveIP(ctx, host, D.TypeAAAA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ch <- ip
|
ch <- ip
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ips, err = r.resolveIP(host, D.TypeA)
|
ips, err = r.resolveIP(ctx, host, D.TypeA)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -68,11 +68,11 @@ func (r *Resolver) ResolveAllIPPrimaryIPv4(host string) (ips []netip.Addr, err e
|
||||||
return ip, nil
|
return ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) ResolveAllIP(host string) (ips []netip.Addr, err error) {
|
func (r *Resolver) LookupIP(ctx context.Context, host string) (ips []netip.Addr, err error) {
|
||||||
ch := make(chan []netip.Addr, 1)
|
ch := make(chan []netip.Addr, 1)
|
||||||
go func() {
|
go func() {
|
||||||
defer close(ch)
|
defer close(ch)
|
||||||
ip, err := r.resolveIP(host, D.TypeAAAA)
|
ip, err := r.resolveIP(ctx, host, D.TypeAAAA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ func (r *Resolver) ResolveAllIP(host string) (ips []netip.Addr, err error) {
|
||||||
ch <- ip
|
ch <- ip
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ips, err = r.resolveIP(host, D.TypeA)
|
ips, err = r.resolveIP(ctx, host, D.TypeA)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case ipv6s, open := <-ch:
|
case ipv6s, open := <-ch:
|
||||||
|
@ -95,17 +95,17 @@ func (r *Resolver) ResolveAllIP(host string) (ips []netip.Addr, err error) {
|
||||||
return ips, nil
|
return ips, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) ResolveAllIPv4(host string) (ips []netip.Addr, err error) {
|
func (r *Resolver) LookupIPv4(ctx context.Context, host string) (ips []netip.Addr, err error) {
|
||||||
return r.resolveIP(host, D.TypeA)
|
return r.resolveIP(ctx, host, D.TypeA)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) ResolveAllIPv6(host string) (ips []netip.Addr, err error) {
|
func (r *Resolver) LookupIPv6(ctx context.Context, host string) (ips []netip.Addr, err error) {
|
||||||
return r.resolveIP(host, D.TypeAAAA)
|
return r.resolveIP(ctx, host, D.TypeAAAA)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIP request with TypeA and TypeAAAA, priority return TypeA
|
// ResolveIP request with TypeA and TypeAAAA, priority return TypeA
|
||||||
func (r *Resolver) ResolveIP(host string) (ip netip.Addr, err error) {
|
func (r *Resolver) ResolveIP(ctx context.Context, host string) (ip netip.Addr, err error) {
|
||||||
if ips, err := r.ResolveAllIPPrimaryIPv4(host); err == nil {
|
if ips, err := r.LookupIPPrimaryIPv4(ctx, host); err == nil {
|
||||||
return ips[rand.Intn(len(ips))], nil
|
return ips[rand.Intn(len(ips))], nil
|
||||||
} else {
|
} else {
|
||||||
return netip.Addr{}, err
|
return netip.Addr{}, err
|
||||||
|
@ -113,8 +113,8 @@ func (r *Resolver) ResolveIP(host string) (ip netip.Addr, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv4 request with TypeA
|
// ResolveIPv4 request with TypeA
|
||||||
func (r *Resolver) ResolveIPv4(host string) (ip netip.Addr, err error) {
|
func (r *Resolver) ResolveIPv4(ctx context.Context, host string) (ip netip.Addr, err error) {
|
||||||
if ips, err := r.ResolveAllIPv4(host); err == nil {
|
if ips, err := r.LookupIPv4(ctx, host); err == nil {
|
||||||
return ips[rand.Intn(len(ips))], nil
|
return ips[rand.Intn(len(ips))], nil
|
||||||
} else {
|
} else {
|
||||||
return netip.Addr{}, err
|
return netip.Addr{}, err
|
||||||
|
@ -122,8 +122,8 @@ func (r *Resolver) ResolveIPv4(host string) (ip netip.Addr, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolveIPv6 request with TypeAAAA
|
// ResolveIPv6 request with TypeAAAA
|
||||||
func (r *Resolver) ResolveIPv6(host string) (ip netip.Addr, err error) {
|
func (r *Resolver) ResolveIPv6(ctx context.Context, host string) (ip netip.Addr, err error) {
|
||||||
if ips, err := r.ResolveAllIPv6(host); err == nil {
|
if ips, err := r.LookupIPv6(ctx, host); err == nil {
|
||||||
return ips[rand.Intn(len(ips))], nil
|
return ips[rand.Intn(len(ips))], nil
|
||||||
} else {
|
} else {
|
||||||
return netip.Addr{}, err
|
return netip.Addr{}, err
|
||||||
|
@ -305,7 +305,7 @@ func (r *Resolver) ipExchange(ctx context.Context, m *D.Msg) (msg *D.Msg, err er
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resolver) resolveIP(host string, dnsType uint16) (ips []netip.Addr, err error) {
|
func (r *Resolver) resolveIP(ctx context.Context, host string, dnsType uint16) (ips []netip.Addr, err error) {
|
||||||
ip, err := netip.ParseAddr(host)
|
ip, err := netip.ParseAddr(host)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
isIPv4 := ip.Is4()
|
isIPv4 := ip.Is4()
|
||||||
|
@ -321,7 +321,7 @@ func (r *Resolver) resolveIP(host string, dnsType uint16) (ips []netip.Addr, err
|
||||||
query := &D.Msg{}
|
query := &D.Msg{}
|
||||||
query.SetQuestion(D.Fqdn(host), dnsType)
|
query.SetQuestion(D.Fqdn(host), dnsType)
|
||||||
|
|
||||||
msg, err := r.Exchange(query)
|
msg, err := r.ExchangeContext(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []netip.Addr{}, err
|
return []netip.Addr{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package dns
|
package dns
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
stdContext "context"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
@ -25,7 +26,7 @@ type Server struct {
|
||||||
|
|
||||||
// ServeDNS implement D.Handler ServeDNS
|
// ServeDNS implement D.Handler ServeDNS
|
||||||
func (s *Server) ServeDNS(w D.ResponseWriter, r *D.Msg) {
|
func (s *Server) ServeDNS(w D.ResponseWriter, r *D.Msg) {
|
||||||
msg, err := handlerWithContext(s.handler, r)
|
msg, err := handlerWithContext(stdContext.Background(), s.handler, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
D.HandleFailed(w, r)
|
D.HandleFailed(w, r)
|
||||||
return
|
return
|
||||||
|
@ -34,12 +35,12 @@ func (s *Server) ServeDNS(w D.ResponseWriter, r *D.Msg) {
|
||||||
w.WriteMsg(msg)
|
w.WriteMsg(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handlerWithContext(handler handler, msg *D.Msg) (*D.Msg, error) {
|
func handlerWithContext(stdCtx stdContext.Context, handler handler, msg *D.Msg) (*D.Msg, error) {
|
||||||
if len(msg.Question) == 0 {
|
if len(msg.Question) == 0 {
|
||||||
return nil, errors.New("at least one question is required")
|
return nil, errors.New("at least one question is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.NewDNSContext(msg)
|
ctx := context.NewDNSContext(stdCtx, msg)
|
||||||
return handler(ctx, msg)
|
return handler(ctx, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const DefaultDnsReadTimeout = time.Second * 10
|
const DefaultDnsReadTimeout = time.Second * 10
|
||||||
|
const DefaultDnsRelayTimeout = time.Second * 5
|
||||||
|
|
||||||
type ListenerHandler struct {
|
type ListenerHandler struct {
|
||||||
sing.ListenerHandler
|
sing.ListenerHandler
|
||||||
|
@ -69,8 +70,10 @@ func (h *ListenerHandler) NewConnection(ctx context.Context, conn net.Conn, meta
|
||||||
}
|
}
|
||||||
|
|
||||||
err = func() error {
|
err = func() error {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, DefaultDnsRelayTimeout)
|
||||||
|
defer cancel()
|
||||||
inData := buff[:n]
|
inData := buff[:n]
|
||||||
msg, err := RelayDnsPacket(inData)
|
msg, err := RelayDnsPacket(ctx, inData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -117,8 +120,10 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, DefaultDnsRelayTimeout)
|
||||||
|
defer cancel()
|
||||||
inData := buff.Bytes()
|
inData := buff.Bytes()
|
||||||
msg, err := RelayDnsPacket(inData)
|
msg, err := RelayDnsPacket(ctx, inData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
buff.Release()
|
buff.Release()
|
||||||
return
|
return
|
||||||
|
@ -146,13 +151,13 @@ func (h *ListenerHandler) NewPacketConnection(ctx context.Context, conn network.
|
||||||
return h.ListenerHandler.NewPacketConnection(ctx, conn, metadata)
|
return h.ListenerHandler.NewPacketConnection(ctx, conn, metadata)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RelayDnsPacket(payload []byte) ([]byte, error) {
|
func RelayDnsPacket(ctx context.Context, payload []byte) ([]byte, error) {
|
||||||
msg := &D.Msg{}
|
msg := &D.Msg{}
|
||||||
if err := msg.Unpack(payload); err != nil {
|
if err := msg.Unpack(payload); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
r, err := resolver.ServeMsg(msg)
|
r, err := resolver.ServeMsg(ctx, msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m := new(D.Msg)
|
m := new(D.Msg)
|
||||||
m.SetRcode(msg, D.RcodeServerFailure)
|
m.SetRcode(msg, D.RcodeServerFailure)
|
||||||
|
|
|
@ -225,7 +225,7 @@ func handleUDPConn(packet *inbound.PacketAdapter) {
|
||||||
|
|
||||||
// local resolve UDP dns
|
// local resolve UDP dns
|
||||||
if !metadata.Resolved() {
|
if !metadata.Resolved() {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
ip, err := resolver.ResolveIP(context.Background(), metadata.Host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -400,14 +400,18 @@ func match(metadata *C.Metadata) (C.Proxy, C.Rule, error) {
|
||||||
|
|
||||||
for _, rule := range rules {
|
for _, rule := range rules {
|
||||||
if !resolved && shouldResolveIP(rule, metadata) {
|
if !resolved && shouldResolveIP(rule, metadata) {
|
||||||
ip, err := resolver.ResolveIP(metadata.Host)
|
func() {
|
||||||
if err != nil {
|
ctx, cancel := context.WithTimeout(context.Background(), resolver.DefaultDNSTimeout)
|
||||||
log.Debugln("[DNS] resolve %s error: %s", metadata.Host, err.Error())
|
defer cancel()
|
||||||
} else {
|
ip, err := resolver.ResolveIP(ctx, metadata.Host)
|
||||||
log.Debugln("[DNS] %s --> %s", metadata.Host, ip.String())
|
if err != nil {
|
||||||
metadata.DstIP = ip
|
log.Debugln("[DNS] resolve %s error: %s", metadata.Host, err.Error())
|
||||||
}
|
} else {
|
||||||
resolved = true
|
log.Debugln("[DNS] %s --> %s", metadata.Host, ip.String())
|
||||||
|
metadata.DstIP = ip
|
||||||
|
}
|
||||||
|
resolved = true
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !processFound && (alwaysFindProcess || rule.ShouldFindProcess()) {
|
if !processFound && (alwaysFindProcess || rule.ShouldFindProcess()) {
|
||||||
|
|
Loading…
Reference in a new issue