2018-12-05 13:13:29 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-06 15:07:34 +00:00
|
|
|
"crypto/tls"
|
2020-02-17 14:13:15 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-04-19 17:52:51 +00:00
|
|
|
"net/netip"
|
2020-02-15 13:42:46 +00:00
|
|
|
"strings"
|
2018-12-05 13:13:29 +00:00
|
|
|
|
2020-02-09 09:02:48 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2021-04-01 13:20:44 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2023-04-11 02:29:55 +00:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2020-02-09 09:02:48 +00:00
|
|
|
|
2018-12-05 13:13:29 +00:00
|
|
|
D "github.com/miekg/dns"
|
2023-03-06 10:10:14 +00:00
|
|
|
"github.com/zhangyunhao116/fastrand"
|
2023-04-11 02:29:55 +00:00
|
|
|
"go.uber.org/atomic"
|
2018-12-05 13:13:29 +00:00
|
|
|
)
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
type client struct {
|
|
|
|
*D.Client
|
2022-02-22 18:38:50 +00:00
|
|
|
r *Resolver
|
|
|
|
port string
|
|
|
|
host string
|
2022-04-28 14:40:06 +00:00
|
|
|
iface *atomic.String
|
2023-04-11 02:29:55 +00:00
|
|
|
proxyAdapter C.ProxyAdapter
|
|
|
|
proxyName string
|
2023-01-28 14:33:03 +00: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 03:03:39 +00:00
|
|
|
c.addr = fmt.Sprintf("%s://%s", schema, net.JoinHostPort(c.host, c.port))
|
2023-01-28 14:33:03 +00:00
|
|
|
return c.addr
|
2018-12-05 13:13:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:58:34 +00:00
|
|
|
func (c *client) Exchange(m *D.Msg) (*D.Msg, error) {
|
2019-06-28 04:29:08 +00:00
|
|
|
return c.ExchangeContext(context.Background(), m)
|
2018-12-05 13:13:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:58:34 +00:00
|
|
|
func (c *client) ExchangeContext(ctx context.Context, m *D.Msg) (*D.Msg, error) {
|
|
|
|
var (
|
2022-04-19 17:52:51 +00:00
|
|
|
ip netip.Addr
|
2021-09-13 15:58:34 +00:00
|
|
|
err error
|
|
|
|
)
|
2020-02-17 14:13:15 +00:00
|
|
|
if c.r == nil {
|
|
|
|
// a default ip dns
|
2022-11-12 12:43:48 +00:00
|
|
|
if ip, err = netip.ParseAddr(c.host); err != nil {
|
2021-09-13 15:58:34 +00:00
|
|
|
return nil, fmt.Errorf("dns %s not a valid ip", c.host)
|
2020-02-17 14:13:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-13 05:07:35 +00:00
|
|
|
ips, err := resolver.LookupIPWithResolver(ctx, c.host, c.r)
|
|
|
|
if err != nil {
|
2020-02-17 14:13:15 +00:00
|
|
|
return nil, fmt.Errorf("use default dns resolve failed: %w", err)
|
2022-08-13 05:07:35 +00:00
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return nil, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, c.host)
|
2020-02-17 14:13:15 +00:00
|
|
|
}
|
2023-03-06 10:10:14 +00:00
|
|
|
ip = ips[fastrand.Intn(len(ips))]
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
network := "udp"
|
|
|
|
if strings.HasPrefix(c.Client.Net, "tcp") {
|
|
|
|
network = "tcp"
|
2020-04-24 15:48:55 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 05:31:19 +00:00
|
|
|
options := []dialer.Option{}
|
2022-04-28 14:40:06 +00:00
|
|
|
if c.iface != nil && c.iface.Load() != "" {
|
|
|
|
options = append(options, dialer.WithInterface(c.iface.Load()))
|
2021-10-05 05:31:19 +00:00
|
|
|
}
|
2022-02-22 18:38:50 +00:00
|
|
|
|
2023-04-11 02:29:55 +00:00
|
|
|
conn, err := getDialHandler(c.r, c.proxyAdapter, c.proxyName, options...)(ctx, network, net.JoinHostPort(ip.String(), c.port))
|
2021-09-06 15:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-15 13:42:46 +00:00
|
|
|
}
|
2022-04-19 17:52:51 +00:00
|
|
|
defer func() {
|
|
|
|
_ = conn.Close()
|
|
|
|
}()
|
2020-02-09 09:02:48 +00:00
|
|
|
|
2020-02-14 08:36:20 +00: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() {
|
2021-09-06 15:07:34 +00:00
|
|
|
if strings.HasSuffix(c.Client.Net, "tls") {
|
2023-01-14 13:08:06 +00:00
|
|
|
conn = tls.Client(conn, tlsC.GetGlobalTLSConfig(c.Client.TLSConfig))
|
2021-09-06 15:07:34 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 15:58:34 +00:00
|
|
|
msg, _, err := c.Client.ExchangeWithConn(m, &D.Conn{
|
2021-09-06 15:07:34 +00:00
|
|
|
Conn: conn,
|
|
|
|
UDPSize: c.Client.UDPSize,
|
|
|
|
TsigSecret: c.Client.TsigSecret,
|
|
|
|
TsigProvider: c.Client.TsigProvider,
|
|
|
|
})
|
|
|
|
|
2020-02-14 08:36:20 +00:00
|
|
|
ch <- result{msg, err}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
case ret := <-ch:
|
|
|
|
return ret.msg, ret.err
|
|
|
|
}
|
2018-12-05 13:13:29 +00:00
|
|
|
}
|