2018-12-05 13:13:29 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
2021-11-17 08:03:47 +00:00
|
|
|
"context"
|
2019-06-28 04:29:08 +00:00
|
|
|
"crypto/tls"
|
2022-08-24 13:36:19 +00:00
|
|
|
"errors"
|
2021-11-17 08:03:47 +00:00
|
|
|
"fmt"
|
2020-02-17 14:13:15 +00:00
|
|
|
"net"
|
2022-04-19 17:52:51 +00:00
|
|
|
"net/netip"
|
2023-08-09 05:51:02 +00:00
|
|
|
"strconv"
|
2022-05-15 05:16:45 +00:00
|
|
|
"strings"
|
2018-12-05 13:13:29 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
2022-12-19 13:34:07 +00:00
|
|
|
N "github.com/Dreamacro/clash/common/net"
|
2022-04-19 17:52:51 +00:00
|
|
|
"github.com/Dreamacro/clash/common/nnip"
|
2022-08-24 13:36:19 +00:00
|
|
|
"github.com/Dreamacro/clash/common/picker"
|
2021-11-17 08:03:47 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2022-11-12 12:43:48 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2021-11-17 08:03:47 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-12-05 13:13:29 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2021-11-17 08:03:47 +00:00
|
|
|
"github.com/Dreamacro/clash/tunnel"
|
2018-12-05 13:13:29 +00:00
|
|
|
|
|
|
|
D "github.com/miekg/dns"
|
2023-06-06 01:45:05 +00:00
|
|
|
"github.com/samber/lo"
|
2018-12-05 13:13:29 +00:00
|
|
|
)
|
|
|
|
|
2022-11-12 03:14:51 +00:00
|
|
|
const (
|
|
|
|
MaxMsgSize = 65535
|
|
|
|
)
|
|
|
|
|
2023-06-06 01:45:05 +00:00
|
|
|
func minimalTTL(records []D.RR) uint32 {
|
2023-09-17 09:18:35 +00:00
|
|
|
minObj := lo.MinBy(records, func(r1 D.RR, r2 D.RR) bool {
|
2023-06-06 01:45:05 +00:00
|
|
|
return r1.Header().Ttl < r2.Header().Ttl
|
2023-09-17 09:18:35 +00:00
|
|
|
})
|
|
|
|
if minObj != nil {
|
|
|
|
return minObj.Header().Ttl
|
|
|
|
}
|
|
|
|
return 0
|
2023-06-06 01:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateTTL(records []D.RR, ttl uint32) {
|
|
|
|
if len(records) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delta := minimalTTL(records) - ttl
|
|
|
|
for i := range records {
|
|
|
|
records[i].Header().Ttl = lo.Clamp(records[i].Header().Ttl-delta, 1, records[i].Header().Ttl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 12:23:16 +00:00
|
|
|
func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) {
|
2023-09-17 09:18:35 +00:00
|
|
|
putMsgToCacheWithExpire(c, key, msg, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putMsgToCacheWithExpire(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg, sec uint32) {
|
|
|
|
if sec == 0 {
|
|
|
|
if sec = minimalTTL(msg.Answer); sec == 0 {
|
|
|
|
if sec = minimalTTL(msg.Ns); sec == 0 {
|
|
|
|
sec = minimalTTL(msg.Extra)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sec == 0 {
|
2023-01-11 01:53:56 +00:00
|
|
|
return
|
|
|
|
}
|
2023-09-17 09:18:35 +00:00
|
|
|
|
2023-09-21 00:28:05 +00:00
|
|
|
if sec > 120 {
|
|
|
|
sec = 120 // at least 2 minutes to cache
|
|
|
|
}
|
2023-09-17 09:18:35 +00:00
|
|
|
|
2018-12-05 13:13:29 +00:00
|
|
|
}
|
|
|
|
|
2023-09-17 09:18:35 +00:00
|
|
|
c.SetWithExpire(key, msg.Copy(), time.Now().Add(time.Duration(sec)*time.Second))
|
2019-02-23 12:31:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setMsgTTL(msg *D.Msg, ttl uint32) {
|
|
|
|
for _, answer := range msg.Answer {
|
|
|
|
answer.Header().Ttl = ttl
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ns := range msg.Ns {
|
|
|
|
ns.Header().Ttl = ttl
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, extra := range msg.Extra {
|
|
|
|
extra.Header().Ttl = ttl
|
|
|
|
}
|
2018-12-05 13:13:29 +00:00
|
|
|
}
|
2019-06-28 04:29:08 +00:00
|
|
|
|
2023-06-06 01:45:05 +00:00
|
|
|
func updateMsgTTL(msg *D.Msg, ttl uint32) {
|
|
|
|
updateTTL(msg.Answer, ttl)
|
|
|
|
updateTTL(msg.Ns, ttl)
|
|
|
|
updateTTL(msg.Extra, ttl)
|
|
|
|
}
|
|
|
|
|
2019-06-28 04:29:08 +00:00
|
|
|
func isIPRequest(q D.Question) bool {
|
2023-03-12 07:00:59 +00:00
|
|
|
return q.Qclass == D.ClassINET && (q.Qtype == D.TypeA || q.Qtype == D.TypeAAAA || q.Qtype == D.TypeCNAME)
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 06:03:49 +00:00
|
|
|
func transform(servers []NameServer, resolver *Resolver) []dnsClient {
|
2023-06-11 15:01:45 +00:00
|
|
|
ret := make([]dnsClient, 0, len(servers))
|
2019-06-28 04:29:08 +00:00
|
|
|
for _, s := range servers {
|
2021-09-06 15:07:34 +00:00
|
|
|
switch s.Net {
|
|
|
|
case "https":
|
2023-04-11 02:29:55 +00:00
|
|
|
ret = append(ret, newDoHClient(s.Addr, resolver, s.PreferH3, s.Params, s.ProxyAdapter, s.ProxyName))
|
2019-06-28 04:29:08 +00:00
|
|
|
continue
|
2021-09-06 15:07:34 +00:00
|
|
|
case "dhcp":
|
|
|
|
ret = append(ret, newDHCPClient(s.Addr))
|
|
|
|
continue
|
2023-04-26 07:57:55 +00:00
|
|
|
case "system":
|
|
|
|
clients, err := loadSystemResolver()
|
|
|
|
if err != nil {
|
2023-05-01 04:41:36 +00:00
|
|
|
log.Errorln("[DNS:system] load system resolver failed: %s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(clients) == 0 {
|
|
|
|
log.Errorln("[DNS:system] no nameserver found in system")
|
2023-04-26 07:57:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret = append(ret, clients...)
|
|
|
|
continue
|
2023-06-11 15:01:45 +00:00
|
|
|
case "rcode":
|
|
|
|
ret = append(ret, newRCodeClient(s.Addr))
|
|
|
|
continue
|
2022-01-27 04:25:53 +00:00
|
|
|
case "quic":
|
2023-04-11 02:29:55 +00:00
|
|
|
if doq, err := newDoQ(resolver, s.Addr, s.ProxyAdapter, s.ProxyName); err == nil {
|
2022-11-12 03:14:51 +00:00
|
|
|
ret = append(ret, doq)
|
2022-11-12 12:43:48 +00:00
|
|
|
} else {
|
|
|
|
log.Fatalln("DoQ format error: %v", err)
|
2022-11-12 03:14:51 +00:00
|
|
|
}
|
2022-01-27 04:25:53 +00:00
|
|
|
continue
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 14:13:15 +00:00
|
|
|
host, port, _ := net.SplitHostPort(s.Addr)
|
2019-06-28 04:29:08 +00:00
|
|
|
ret = append(ret, &client{
|
|
|
|
Client: &D.Client{
|
|
|
|
Net: s.Net,
|
|
|
|
TLSConfig: &tls.Config{
|
2020-04-17 03:29:59 +00:00
|
|
|
ServerName: host,
|
2019-06-28 04:29:08 +00:00
|
|
|
},
|
|
|
|
UDPSize: 4096,
|
2020-01-10 06:13:44 +00:00
|
|
|
Timeout: 5 * time.Second,
|
2019-06-28 04:29:08 +00:00
|
|
|
},
|
2021-11-17 08:03:47 +00:00
|
|
|
port: port,
|
|
|
|
host: host,
|
|
|
|
iface: s.Interface,
|
|
|
|
r: resolver,
|
|
|
|
proxyAdapter: s.ProxyAdapter,
|
2023-04-11 02:29:55 +00:00
|
|
|
proxyName: s.ProxyName,
|
2019-06-28 04:29:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2020-10-12 16:15:49 +00:00
|
|
|
|
|
|
|
func handleMsgWithEmptyAnswer(r *D.Msg) *D.Msg {
|
|
|
|
msg := &D.Msg{}
|
|
|
|
msg.Answer = []D.RR{}
|
|
|
|
|
|
|
|
msg.SetRcode(r, D.RcodeSuccess)
|
|
|
|
msg.Authoritative = true
|
|
|
|
msg.RecursionAvailable = true
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
2021-01-23 06:49:46 +00:00
|
|
|
|
2022-04-19 17:52:51 +00:00
|
|
|
func msgToIP(msg *D.Msg) []netip.Addr {
|
|
|
|
ips := []netip.Addr{}
|
2021-01-23 06:49:46 +00:00
|
|
|
|
|
|
|
for _, answer := range msg.Answer {
|
|
|
|
switch ans := answer.(type) {
|
|
|
|
case *D.AAAA:
|
2022-04-19 17:52:51 +00:00
|
|
|
ips = append(ips, nnip.IpToAddr(ans.AAAA))
|
2021-01-23 06:49:46 +00:00
|
|
|
case *D.A:
|
2022-04-19 17:52:51 +00:00
|
|
|
ips = append(ips, nnip.IpToAddr(ans.A))
|
2021-01-23 06:49:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips
|
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
2022-05-15 05:16:45 +00:00
|
|
|
func msgToDomain(msg *D.Msg) string {
|
|
|
|
if len(msg.Question) > 0 {
|
|
|
|
return strings.TrimRight(msg.Question[0].Name, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-12-07 12:01:44 +00:00
|
|
|
type dialHandler func(ctx context.Context, network, addr string) (net.Conn, error)
|
2022-06-21 14:59:35 +00:00
|
|
|
|
2023-04-11 02:29:55 +00:00
|
|
|
func getDialHandler(r *Resolver, proxyAdapter C.ProxyAdapter, proxyName string, opts ...dialer.Option) dialHandler {
|
2022-12-07 12:01:44 +00:00
|
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
2023-04-11 02:29:55 +00:00
|
|
|
if len(proxyName) == 0 && proxyAdapter == nil {
|
2022-12-07 12:01:44 +00:00
|
|
|
opts = append(opts, dialer.WithResolver(r))
|
|
|
|
return dialer.DialContext(ctx, network, addr, opts...)
|
|
|
|
} else {
|
2022-12-13 04:38:46 +00:00
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-09 05:51:02 +00:00
|
|
|
uintPort, err := strconv.ParseUint(port, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 02:29:55 +00:00
|
|
|
if proxyAdapter == nil {
|
|
|
|
var ok bool
|
|
|
|
proxyAdapter, ok = tunnel.Proxies()[proxyName]
|
|
|
|
if !ok {
|
|
|
|
opts = append(opts, dialer.WithInterface(proxyName))
|
|
|
|
}
|
2022-12-13 04:38:46 +00:00
|
|
|
}
|
2023-04-11 02:29:55 +00:00
|
|
|
|
2022-12-13 04:38:46 +00:00
|
|
|
if strings.Contains(network, "tcp") {
|
|
|
|
// tcp can resolve host by remote
|
|
|
|
metadata := &C.Metadata{
|
|
|
|
NetWork: C.TCP,
|
|
|
|
Host: host,
|
2023-08-09 05:51:02 +00:00
|
|
|
DstPort: uint16(uintPort),
|
2022-12-13 04:38:46 +00:00
|
|
|
}
|
2023-04-11 02:29:55 +00:00
|
|
|
if proxyAdapter != nil {
|
2023-04-12 03:09:31 +00:00
|
|
|
if proxyAdapter.IsL3Protocol(metadata) { // L3 proxy should resolve domain before to avoid loopback
|
|
|
|
dstIP, err := resolver.ResolveIPWithResolver(ctx, host, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metadata.Host = ""
|
|
|
|
metadata.DstIP = dstIP
|
|
|
|
}
|
2023-04-11 02:29:55 +00:00
|
|
|
return proxyAdapter.DialContext(ctx, metadata, opts...)
|
2022-12-13 04:38:46 +00:00
|
|
|
}
|
|
|
|
opts = append(opts, dialer.WithResolver(r))
|
|
|
|
return dialer.DialContext(ctx, network, addr, opts...)
|
|
|
|
} else {
|
|
|
|
// udp must resolve host first
|
|
|
|
dstIP, err := resolver.ResolveIPWithResolver(ctx, host, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metadata := &C.Metadata{
|
|
|
|
NetWork: C.UDP,
|
|
|
|
Host: "",
|
|
|
|
DstIP: dstIP,
|
2023-08-09 05:51:02 +00:00
|
|
|
DstPort: uint16(uintPort),
|
2022-12-13 04:38:46 +00:00
|
|
|
}
|
2023-04-11 02:29:55 +00:00
|
|
|
if proxyAdapter == nil {
|
2022-12-13 04:38:46 +00:00
|
|
|
return dialer.DialContext(ctx, network, addr, opts...)
|
|
|
|
}
|
|
|
|
|
2023-04-11 02:29:55 +00:00
|
|
|
if !proxyAdapter.SupportUDP() {
|
2022-12-13 04:38:46 +00:00
|
|
|
return nil, fmt.Errorf("proxy adapter [%s] UDP is not supported", proxyAdapter)
|
|
|
|
}
|
|
|
|
|
2023-04-11 02:29:55 +00:00
|
|
|
packetConn, err := proxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
2022-12-13 04:38:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-19 13:34:07 +00:00
|
|
|
return N.NewBindPacketConn(packetConn, metadata.UDPAddr()), nil
|
2022-12-13 04:38:46 +00:00
|
|
|
}
|
2022-12-07 12:01:44 +00:00
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
}
|
2022-12-07 12:01:44 +00:00
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
2023-04-11 02:29:55 +00:00
|
|
|
func listenPacket(ctx context.Context, proxyAdapter C.ProxyAdapter, proxyName string, network string, addr string, r *Resolver, opts ...dialer.Option) (net.PacketConn, error) {
|
2022-12-07 12:01:44 +00:00
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-17 08:03:47 +00:00
|
|
|
}
|
2023-08-09 05:51:02 +00:00
|
|
|
uintPort, err := strconv.ParseUint(port, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 02:29:55 +00:00
|
|
|
if proxyAdapter == nil {
|
|
|
|
var ok bool
|
|
|
|
proxyAdapter, ok = tunnel.Proxies()[proxyName]
|
|
|
|
if !ok {
|
|
|
|
opts = append(opts, dialer.WithInterface(proxyName))
|
|
|
|
}
|
2022-12-07 12:01:44 +00:00
|
|
|
}
|
2022-06-21 14:59:35 +00:00
|
|
|
|
2022-12-13 04:38:46 +00:00
|
|
|
// udp must resolve host first
|
|
|
|
dstIP, err := resolver.ResolveIPWithResolver(ctx, host, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metadata := &C.Metadata{
|
|
|
|
NetWork: C.UDP,
|
|
|
|
Host: "",
|
|
|
|
DstIP: dstIP,
|
2023-08-09 05:51:02 +00:00
|
|
|
DstPort: uint16(uintPort),
|
2022-12-13 04:38:46 +00:00
|
|
|
}
|
2023-04-11 02:29:55 +00:00
|
|
|
if proxyAdapter == nil {
|
2022-12-13 04:38:46 +00:00
|
|
|
return dialer.ListenPacket(ctx, dialer.ParseNetwork(network, dstIP), "", opts...)
|
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
2023-04-11 02:29:55 +00:00
|
|
|
if !proxyAdapter.SupportUDP() {
|
2022-12-13 04:38:46 +00:00
|
|
|
return nil, fmt.Errorf("proxy adapter [%s] UDP is not supported", proxyAdapter)
|
2021-11-17 08:03:47 +00:00
|
|
|
}
|
2022-12-13 04:38:46 +00:00
|
|
|
|
2023-04-11 02:29:55 +00:00
|
|
|
return proxyAdapter.ListenPacketContext(ctx, metadata, opts...)
|
2021-11-17 08:03:47 +00:00
|
|
|
}
|
2022-11-12 12:43:48 +00:00
|
|
|
|
2023-06-11 12:58:51 +00:00
|
|
|
func batchExchange(ctx context.Context, clients []dnsClient, m *D.Msg) (msg *D.Msg, cache bool, err error) {
|
2023-07-14 01:55:43 +00:00
|
|
|
cache = true
|
2022-11-12 12:43:48 +00:00
|
|
|
fast, ctx := picker.WithTimeout[*D.Msg](ctx, resolver.DefaultDNSTimeout)
|
2023-07-14 01:55:43 +00:00
|
|
|
defer fast.Close()
|
2023-01-28 14:33:03 +00:00
|
|
|
domain := msgToDomain(m)
|
2022-08-24 13:36:19 +00:00
|
|
|
for _, client := range clients {
|
2023-07-14 01:55:43 +00:00
|
|
|
if _, isRCodeClient := client.(rcodeClient); isRCodeClient {
|
|
|
|
msg, err = client.Exchange(m)
|
|
|
|
return msg, false, err
|
|
|
|
}
|
2023-06-12 10:01:13 +00:00
|
|
|
client := client // shadow define client to ensure the value captured by the closure will not be changed in the next loop
|
2022-11-19 02:35:45 +00:00
|
|
|
fast.Go(func() (*D.Msg, error) {
|
2023-06-11 15:01:45 +00:00
|
|
|
log.Debugln("[DNS] resolve %s from %s", domain, client.Address())
|
|
|
|
m, err := client.ExchangeContext(ctx, m)
|
2022-08-24 13:36:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-06-11 12:58:51 +00:00
|
|
|
} else if cache && (m.Rcode == D.RcodeServerFailure || m.Rcode == D.RcodeRefused) {
|
|
|
|
// currently, cache indicates whether this msg was from a RCode client,
|
|
|
|
// so we would ignore RCode errors from RCode clients.
|
2023-07-14 01:55:43 +00:00
|
|
|
return nil, errors.New("server failure: " + D.RcodeToString[m.Rcode])
|
2022-08-24 13:36:19 +00:00
|
|
|
}
|
2023-06-11 15:01:45 +00:00
|
|
|
log.Debugln("[DNS] %s --> %s, from %s", domain, msgToIP(m), client.Address())
|
2022-08-24 13:36:19 +00:00
|
|
|
return m, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-07-14 01:55:43 +00:00
|
|
|
msg = fast.Wait()
|
|
|
|
if msg == nil {
|
|
|
|
err = errors.New("all DNS requests failed")
|
2022-08-24 13:36:19 +00:00
|
|
|
if fErr := fast.Error(); fErr != nil {
|
2023-07-02 01:59:18 +00:00
|
|
|
err = fmt.Errorf("%w, first error: %w", err, fErr)
|
2022-08-24 13:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|