2018-12-05 21:13:29 +08:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-06 23:07:34 +08:00
|
|
|
"crypto/tls"
|
2020-02-17 22:13:15 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-04-20 01:52:51 +08:00
|
|
|
"net/netip"
|
2020-02-15 21:42:46 +08:00
|
|
|
"strings"
|
2018-12-05 21:13:29 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/ca"
|
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2024-03-19 14:44:36 +08:00
|
|
|
"github.com/metacubex/mihomo/log"
|
2020-02-09 17:02:48 +08:00
|
|
|
|
2024-05-31 11:31:17 +08:00
|
|
|
"github.com/metacubex/randv2"
|
2018-12-05 21:13:29 +08:00
|
|
|
D "github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2019-06-28 12:29:08 +08:00
|
|
|
type client struct {
|
|
|
|
*D.Client
|
2022-02-23 02:38:50 +08:00
|
|
|
r *Resolver
|
|
|
|
port string
|
|
|
|
host string
|
2023-10-26 09:07:49 +08:00
|
|
|
iface string
|
2023-04-11 10:29:55 +08:00
|
|
|
proxyAdapter C.ProxyAdapter
|
|
|
|
proxyName 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) 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
|
2022-11-12 20:43:48 +08:00
|
|
|
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 {
|
2022-08-13 13:07:35 +08:00
|
|
|
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)
|
2022-08-13 13:07:35 +08:00
|
|
|
} else if len(ips) == 0 {
|
|
|
|
return nil, fmt.Errorf("%w: %s", resolver.ErrIPNotFound, c.host)
|
2020-02-17 22:13:15 +08:00
|
|
|
}
|
2024-05-31 11:31:17 +08:00
|
|
|
ip = ips[randv2.IntN(len(ips))]
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
|
2021-09-06 23:07:34 +08:00
|
|
|
network := "udp"
|
|
|
|
if strings.HasPrefix(c.Client.Net, "tcp") {
|
|
|
|
network = "tcp"
|
2020-04-24 23:48:55 +08:00
|
|
|
}
|
|
|
|
|
2023-10-10 16:34:33 +08:00
|
|
|
var options []dialer.Option
|
2023-10-26 09:07:49 +08:00
|
|
|
if c.iface != "" {
|
|
|
|
options = append(options, dialer.WithInterface(c.iface))
|
2021-10-05 13:31:19 +08:00
|
|
|
}
|
2022-02-23 02:38:50 +08:00
|
|
|
|
2024-03-23 22:30:19 +08:00
|
|
|
dialHandler := getDialHandler(c.r, c.proxyAdapter, c.proxyName, options...)
|
|
|
|
addr := net.JoinHostPort(ip.String(), c.port)
|
|
|
|
conn, err := dialHandler(ctx, network, addr)
|
2021-09-06 23:07:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
2022-04-20 01:52:51 +08:00
|
|
|
defer func() {
|
|
|
|
_ = conn.Close()
|
|
|
|
}()
|
2020-02-09 17:02:48 +08:00
|
|
|
|
2020-02-14 16:36:20 +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() {
|
2021-09-06 23:07:34 +08:00
|
|
|
if strings.HasSuffix(c.Client.Net, "tls") {
|
2023-09-22 14:45:34 +08:00
|
|
|
conn = tls.Client(conn, ca.GetGlobalTLSConfig(c.Client.TLSConfig))
|
2021-09-06 23:07:34 +08:00
|
|
|
}
|
|
|
|
|
2024-03-19 14:44:36 +08:00
|
|
|
dConn := &D.Conn{
|
2021-09-06 23:07:34 +08:00
|
|
|
Conn: conn,
|
|
|
|
UDPSize: c.Client.UDPSize,
|
|
|
|
TsigSecret: c.Client.TsigSecret,
|
|
|
|
TsigProvider: c.Client.TsigProvider,
|
2024-03-19 14:44:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
msg, _, err := c.Client.ExchangeWithConn(m, dConn)
|
|
|
|
|
|
|
|
// Resolvers MUST resend queries over TCP if they receive a truncated UDP response (with TC=1 set)!
|
|
|
|
if msg != nil && msg.Truncated && c.Client.Net == "" {
|
|
|
|
tcpClient := *c.Client // copy a client
|
|
|
|
tcpClient.Net = "tcp"
|
2024-03-23 22:30:19 +08:00
|
|
|
network = "tcp"
|
2024-03-19 14:44:36 +08:00
|
|
|
log.Debugln("[DNS] Truncated reply from %s:%s for %s over UDP, retrying over TCP", c.host, c.port, m.Question[0].String())
|
2024-03-23 22:30:19 +08:00
|
|
|
dConn.Conn, err = dialHandler(ctx, network, addr)
|
|
|
|
if err != nil {
|
|
|
|
ch <- result{msg, err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = conn.Close()
|
|
|
|
}()
|
2024-03-19 14:44:36 +08:00
|
|
|
msg, _, err = tcpClient.ExchangeWithConn(m, dConn)
|
|
|
|
}
|
2021-09-06 23:07:34 +08:00
|
|
|
|
2020-02-14 16:36:20 +08:00
|
|
|
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
|
|
|
}
|