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"
|
2022-07-11 12:37:27 +08:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
2022-04-28 22:40:06 +08:00
|
|
|
"go.uber.org/atomic"
|
2020-02-17 22:13:15 +08:00
|
|
|
"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
|
|
|
|
2020-02-09 17:02:48 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2021-04-01 21:20:44 +08:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
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
|
2022-04-28 22:40:06 +08:00
|
|
|
iface *atomic.String
|
2022-02-23 02:38:50 +08:00
|
|
|
proxyAdapter string
|
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
|
|
|
|
)
|
2022-04-20 01:52:51 +08:00
|
|
|
if ip, err = netip.ParseAddr(c.host); err != nil {
|
2022-03-15 02:55:06 +08:00
|
|
|
if c.r == nil {
|
2021-09-13 23:58:34 +08:00
|
|
|
return nil, fmt.Errorf("dns %s not a valid ip", c.host)
|
2022-03-15 02:55:06 +08:00
|
|
|
} else {
|
|
|
|
if ip, err = resolver.ResolveIPWithResolver(c.host, c.r); err != nil {
|
|
|
|
return nil, fmt.Errorf("use default dns resolve failed: %w", err)
|
|
|
|
}
|
|
|
|
c.host = ip.String()
|
2020-02-17 22:13:15 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-10-05 13:31:19 +08:00
|
|
|
options := []dialer.Option{}
|
2022-04-28 22:40:06 +08:00
|
|
|
if c.iface != nil && c.iface.Load() != "" {
|
|
|
|
options = append(options, dialer.WithInterface(c.iface.Load()))
|
2021-10-05 13:31:19 +08:00
|
|
|
}
|
2022-02-23 02:38:50 +08:00
|
|
|
|
|
|
|
var conn net.Conn
|
2022-06-04 21:18:49 +08:00
|
|
|
if c.proxyAdapter != "" {
|
2022-06-05 12:52:29 +08:00
|
|
|
conn, err = dialContextExtra(ctx, c.proxyAdapter, network, ip, c.port, options...)
|
2022-06-04 21:18:49 +08:00
|
|
|
} else {
|
|
|
|
conn, err = dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), c.port), options...)
|
2022-02-23 02:38:50 +08:00
|
|
|
}
|
|
|
|
|
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") {
|
2022-07-10 20:44:24 +08:00
|
|
|
conn = tls.Client(conn, tlsC.MixinTLSConfig(c.Client.TLSConfig))
|
2021-09-06 23:07:34 +08:00
|
|
|
}
|
|
|
|
|
2021-09-13 23:58:34 +08:00
|
|
|
msg, _, err := c.Client.ExchangeWithConn(m, &D.Conn{
|
2021-09-06 23:07:34 +08:00
|
|
|
Conn: conn,
|
|
|
|
UDPSize: c.Client.UDPSize,
|
|
|
|
TsigSecret: c.Client.TsigSecret,
|
|
|
|
TsigProvider: c.Client.TsigProvider,
|
|
|
|
})
|
|
|
|
|
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
|
|
|
}
|