mihomo/dns/client.go

121 lines
2.6 KiB
Go
Raw Permalink Normal View History

2018-12-05 21:13:29 +08:00
package dns
import (
"context"
"crypto/tls"
2020-02-17 22:13:15 +08:00
"fmt"
"net"
2022-04-20 01:52:51 +08:00
"net/netip"
"strings"
2018-12-05 21:13:29 +08:00
tlsC "github.com/Dreamacro/clash/component/tls"
"go.uber.org/atomic"
2020-02-09 17:02:48 +08:00
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/resolver"
2020-02-09 17:02:48 +08:00
2018-12-05 21:13:29 +08:00
D "github.com/miekg/dns"
"github.com/zhangyunhao116/fastrand"
2018-12-05 21:13:29 +08:00
)
2019-06-28 12:29:08 +08:00
type client struct {
*D.Client
r *Resolver
port string
host string
iface *atomic.String
proxyAdapter string
2023-01-28 22:33:03 +08:00
addr string
}
var _ dnsClient = (*client)(nil)
// Address implements dnsClient
func (c *client) Address() string {
if len(c.addr) != 0 {
return c.addr
}
schema := "udp"
if strings.HasPrefix(c.Client.Net, "tcp") {
schema = "tcp"
if strings.HasSuffix(c.Client.Net, "tls") {
schema = "tls"
}
}
2023-01-29 11:03:39 +08:00
c.addr = fmt.Sprintf("%s://%s", schema, net.JoinHostPort(c.host, c.port))
2023-01-28 22:33:03 +08:00
return c.addr
2018-12-05 21:13:29 +08:00
}
2021-09-13 23:58:34 +08:00
func (c *client) Exchange(m *D.Msg) (*D.Msg, error) {
2019-06-28 12:29:08 +08:00
return c.ExchangeContext(context.Background(), m)
2018-12-05 21:13:29 +08:00
}
2021-09-13 23:58:34 +08:00
func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) {
var (
2022-04-20 01:52:51 +08:00
ip netip.Addr
2021-09-13 23:58:34 +08:00
err error
)
2020-02-17 22:13:15 +08:00
if c.r == nil {
// a default ip dns
if ip, err = netip.ParseAddr(c.host); err != nil {
2021-09-13 23:58:34 +08:00
return nil, fmt.Errorf("dns %s not a valid ip", c.host)
2020-02-17 22:13:15 +08:00
}
} else {
ips, err := resolver.LookupIPWithResolver(ctx, c.host, c.r)
if err != nil {
2020-02-17 22:13:15 +08:00
return nil, fmt.Errorf("use default dns resolve failed: %w", err)
} else if len(ips) == 0 {
return nil, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, c.host)
2020-02-17 22:13:15 +08:00
}
ip = ips[fastrand.Intn(len(ips))]
}
network := "udp"
if strings.HasPrefix(c.Client.Net, "tcp") {
network = "tcp"
}
options := []dialer.Option{}
if c.iface != nil && c.iface.Load() != "" {
options = append(options, dialer.WithInterface(c.iface.Load()))
}
conn, err := getDialHandler(c.r, c.proxyAdapter, options...)(ctx, network, net.JoinHostPort(ip.String(), c.port))
if err != nil {
return nil, err
}
2022-04-20 01:52:51 +08:00
defer func() {
_ = conn.Close()
}()
2020-02-09 17:02:48 +08:00
// miekg/dns ExchangeContext doesn't respond to context cancel.
// this is a workaround
type result struct {
msg *D.Msg
err error
}
ch := make(chan result, 1)
go func() {
if strings.HasSuffix(c.Client.Net, "tls") {
conn = tls.Client(conn, tlsC.GetGlobalTLSConfig(c.Client.TLSConfig))
}
2021-09-13 23:58:34 +08:00
msg, _, err := c.Client.ExchangeWithConn(m, &D.Conn{
Conn: conn,
UDPSize: c.Client.UDPSize,
TsigSecret: c.Client.TsigSecret,
TsigProvider: c.Client.TsigProvider,
})
ch <- result{msg, err}
}()
select {
case <-ctx.Done():
return nil, ctx.Err()
case ret := <-ch:
return ret.msg, ret.err
}
2018-12-05 21:13:29 +08:00
}