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"
|
2021-11-17 08:03:47 +00:00
|
|
|
"fmt"
|
2020-02-17 14:13:15 +00:00
|
|
|
"net"
|
2022-04-11 16:31:04 +00:00
|
|
|
"net/netip"
|
2018-12-05 13:13:29 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
2021-11-17 08:03:47 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2022-04-05 12:23:16 +00:00
|
|
|
func putMsgToCache(c *cache.LruCache[string, *D.Msg], key string, msg *D.Msg) {
|
2020-05-07 07:10:14 +00:00
|
|
|
var ttl uint32
|
2019-10-14 09:13:23 +00:00
|
|
|
switch {
|
|
|
|
case len(msg.Answer) != 0:
|
2020-05-07 07:10:14 +00:00
|
|
|
ttl = msg.Answer[0].Header().Ttl
|
2019-10-14 09:13:23 +00:00
|
|
|
case len(msg.Ns) != 0:
|
2020-05-07 07:10:14 +00:00
|
|
|
ttl = msg.Ns[0].Header().Ttl
|
2019-10-14 09:13:23 +00:00
|
|
|
case len(msg.Extra) != 0:
|
2020-05-07 07:10:14 +00:00
|
|
|
ttl = msg.Extra[0].Header().Ttl
|
2019-10-14 09:13:23 +00:00
|
|
|
default:
|
2020-06-12 15:39:03 +00:00
|
|
|
log.Debugln("[DNS] response msg empty: %#v", msg)
|
2018-12-05 13:13:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-07 07:10:14 +00:00
|
|
|
c.SetWithExpire(key, msg.Copy(), time.Now().Add(time.Second*time.Duration(ttl)))
|
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
|
|
|
|
|
|
|
func isIPRequest(q D.Question) bool {
|
2020-06-12 15:39:03 +00:00
|
|
|
return q.Qclass == D.ClassINET && (q.Qtype == D.TypeA || q.Qtype == D.TypeAAAA)
|
2019-06-28 04:29:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 13:42:46 +00:00
|
|
|
func transform(servers []NameServer, resolver *Resolver) []dnsClient {
|
|
|
|
ret := []dnsClient{}
|
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":
|
2021-11-17 08:03:47 +00:00
|
|
|
ret = append(ret, newDoHClient(s.Addr, resolver, s.ProxyAdapter))
|
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
|
2022-01-27 04:25:53 +00:00
|
|
|
case "quic":
|
|
|
|
ret = append(ret, &quicClient{addr: s.Addr})
|
|
|
|
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{
|
|
|
|
// alpn identifier, see https://tools.ietf.org/html/draft-hoffman-dprive-dns-tls-alpn-00#page-6
|
|
|
|
NextProtos: []string{"dns"},
|
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,
|
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
|
|
|
|
|
|
|
func msgToIP(msg *D.Msg) []net.IP {
|
|
|
|
ips := []net.IP{}
|
|
|
|
|
|
|
|
for _, answer := range msg.Answer {
|
|
|
|
switch ans := answer.(type) {
|
|
|
|
case *D.AAAA:
|
|
|
|
ips = append(ips, ans.AAAA)
|
|
|
|
case *D.A:
|
|
|
|
ips = append(ips, ans.A)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips
|
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
2022-04-11 16:31:04 +00:00
|
|
|
func ipToAddr(ip net.IP) netip.Addr {
|
|
|
|
if ip == nil {
|
|
|
|
return netip.Addr{}
|
|
|
|
}
|
|
|
|
|
|
|
|
l := len(ip)
|
|
|
|
|
|
|
|
if l == 4 {
|
|
|
|
return netip.AddrFrom4(*(*[4]byte)(ip))
|
|
|
|
} else if l == 16 {
|
|
|
|
return netip.AddrFrom16(*(*[16]byte)(ip))
|
|
|
|
} else {
|
|
|
|
return netip.Addr{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
type wrapPacketConn struct {
|
|
|
|
net.PacketConn
|
|
|
|
rAddr net.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wpc *wrapPacketConn) Read(b []byte) (n int, err error) {
|
|
|
|
n, _, err = wpc.PacketConn.ReadFrom(b)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wpc *wrapPacketConn) Write(b []byte) (n int, err error) {
|
|
|
|
return wpc.PacketConn.WriteTo(b, wpc.rAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wpc *wrapPacketConn) RemoteAddr() net.Addr {
|
|
|
|
return wpc.rAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialContextWithProxyAdapter(ctx context.Context, adapterName string, network string, dstIP net.IP, port string, opts ...dialer.Option) (net.Conn, error) {
|
|
|
|
adapter, ok := tunnel.Proxies()[adapterName]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("proxy adapter [%s] not found", adapterName)
|
|
|
|
}
|
|
|
|
|
|
|
|
networkType := C.TCP
|
|
|
|
if network == "udp" {
|
|
|
|
if !adapter.SupportUDP() {
|
|
|
|
return nil, fmt.Errorf("proxy adapter [%s] UDP is not supported", adapterName)
|
|
|
|
}
|
|
|
|
networkType = C.UDP
|
|
|
|
}
|
|
|
|
|
|
|
|
addrType := C.AtypIPv4
|
|
|
|
if dstIP.To4() == nil {
|
|
|
|
addrType = C.AtypIPv6
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := &C.Metadata{
|
|
|
|
NetWork: networkType,
|
|
|
|
AddrType: addrType,
|
|
|
|
Host: "",
|
|
|
|
DstIP: dstIP,
|
|
|
|
DstPort: port,
|
|
|
|
}
|
|
|
|
|
|
|
|
if networkType == C.UDP {
|
|
|
|
packetConn, err := adapter.ListenPacketContext(ctx, metadata, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &wrapPacketConn{
|
|
|
|
PacketConn: packetConn,
|
|
|
|
rAddr: metadata.UDPAddr(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return adapter.DialContext(ctx, metadata, opts...)
|
|
|
|
}
|