2019-06-28 12:29:08 +08:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-07-03 21:59:47 +08:00
|
|
|
"crypto/tls"
|
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"
|
2022-07-03 21:59:47 +08:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
|
|
"github.com/lucas-clemente/quic-go/http3"
|
2019-06-28 12:29:08 +08:00
|
|
|
D "github.com/miekg/dns"
|
2022-07-03 21:59:47 +08:00
|
|
|
"go.uber.org/atomic"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2019-06-28 12:29:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// dotMimeType is the DoH mimetype that should be used.
|
|
|
|
dotMimeType = "application/dns-message"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dohClient struct {
|
2021-11-17 16:03:47 +08:00
|
|
|
url string
|
|
|
|
proxyAdapter string
|
|
|
|
transport *http.Transport
|
2022-07-03 21:59:47 +08:00
|
|
|
h3Transport *http3.RoundTripper
|
|
|
|
supportH3 *atomic.Bool
|
|
|
|
firstTest *atomic.Bool
|
2019-06-28 12:29:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *dohClient) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
|
|
|
return dc.ExchangeContext(context.Background(), m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *dohClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
2021-10-07 22:57:55 +08:00
|
|
|
// https://datatracker.ietf.org/doc/html/rfc8484#section-4.1
|
|
|
|
// In order to maximize cache friendliness, SHOULD use a DNS ID of 0 in every DNS request.
|
|
|
|
newM := *m
|
|
|
|
newM.Id = 0
|
|
|
|
req, err := dc.newRequest(&newM)
|
2019-06-28 12:29:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req = req.WithContext(ctx)
|
2021-10-07 22:57:55 +08:00
|
|
|
msg, err = dc.doRequest(req)
|
2021-10-11 21:05:38 +08:00
|
|
|
if err == nil {
|
|
|
|
msg.Id = m.Id
|
|
|
|
}
|
2021-10-07 22:57:55 +08:00
|
|
|
return
|
2019-06-28 12:29:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// newRequest returns a new DoH request given a dns.Msg.
|
|
|
|
func (dc *dohClient) newRequest(m *D.Msg) (*http.Request, error) {
|
|
|
|
buf, err := m.Pack()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-01 13:43:26 +08:00
|
|
|
req, err := http.NewRequest(http.MethodPost, dc.url, bytes.NewReader(buf))
|
2019-06-28 12:29:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return req, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("content-type", dotMimeType)
|
|
|
|
req.Header.Set("accept", dotMimeType)
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2022-07-03 21:59:47 +08:00
|
|
|
func (dc *dohClient) doRequest(req *http.Request) (msg *D.Msg, err error) {
|
|
|
|
if dc.supportH3.Load() {
|
|
|
|
msg, err = dc.doRequestWithTransport(req, dc.h3Transport)
|
|
|
|
if err != nil {
|
|
|
|
if dc.firstTest.CAS(true, false) {
|
|
|
|
dc.supportH3.Store(false)
|
|
|
|
_ = dc.h3Transport.Close()
|
|
|
|
dc.h3Transport = nil
|
|
|
|
}
|
2022-07-03 23:01:49 +08:00
|
|
|
} else {
|
|
|
|
if dc.firstTest.CAS(true, false) {
|
|
|
|
dc.supportH3.Store(true)
|
2022-07-04 20:38:07 +08:00
|
|
|
dc.transport.CloseIdleConnections()
|
2022-07-03 23:01:49 +08:00
|
|
|
dc.transport = nil
|
|
|
|
}
|
2022-07-03 21:59:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg, err = dc.doRequestWithTransport(req, dc.transport)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *dohClient) doRequestWithTransport(req *http.Request, transport http.RoundTripper) (*D.Msg, error) {
|
|
|
|
client := &http.Client{Transport: transport}
|
2019-06-28 12:29:08 +08:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2022-07-03 21:59:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-28 12:29:08 +08:00
|
|
|
}
|
2022-07-03 21:59:47 +08:00
|
|
|
|
2019-06-28 12:29:08 +08:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2021-10-09 20:35:06 +08:00
|
|
|
buf, err := io.ReadAll(resp.Body)
|
2019-06-28 12:29:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-17 16:03:47 +08:00
|
|
|
msg := &D.Msg{}
|
2019-06-28 12:29:08 +08:00
|
|
|
err = msg.Unpack(buf)
|
|
|
|
return msg, err
|
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
|
2022-07-03 21:59:47 +08:00
|
|
|
func newDoHClient(url string, r *Resolver, preferH3 bool, proxyAdapter string) *dohClient {
|
2020-02-15 21:42:46 +08:00
|
|
|
return &dohClient{
|
2021-11-17 16:03:47 +08:00
|
|
|
url: url,
|
|
|
|
proxyAdapter: proxyAdapter,
|
2020-02-15 21:42:46 +08:00
|
|
|
transport: &http.Transport{
|
2020-06-01 13:43:26 +08:00
|
|
|
ForceAttemptHTTP2: true,
|
2020-02-15 21:42:46 +08:00
|
|
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-01 21:20:44 +08:00
|
|
|
ip, err := resolver.ResolveIPWithResolver(host, r)
|
2020-02-15 21:42:46 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-05 12:52:29 +08:00
|
|
|
if proxyAdapter == "" {
|
|
|
|
return dialer.DialContext(ctx, "tcp", net.JoinHostPort(ip.String(), port))
|
|
|
|
} else {
|
|
|
|
return dialContextExtra(ctx, proxyAdapter, "tcp", ip, port)
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
2020-02-15 21:42:46 +08:00
|
|
|
},
|
|
|
|
},
|
2022-07-03 21:59:47 +08:00
|
|
|
|
|
|
|
h3Transport: &http3.RoundTripper{
|
|
|
|
Dial: func(ctx context.Context, network, addr string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlyConnection, error) {
|
|
|
|
host, port, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := resolver.ResolveIPWithResolver(host, r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if proxyAdapter == "" {
|
|
|
|
return quic.DialAddrEarlyContext(ctx, net.JoinHostPort(ip.String(), port), tlsCfg, cfg)
|
|
|
|
} else {
|
|
|
|
if conn, err := dialContextExtra(ctx, proxyAdapter, "udp", ip, port); err == nil {
|
|
|
|
portInt, err := strconv.Atoi(port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
udpAddr := net.UDPAddr{
|
|
|
|
IP: net.ParseIP(ip.String()),
|
|
|
|
Port: portInt,
|
|
|
|
}
|
|
|
|
|
|
|
|
return quic.DialEarlyContext(ctx, conn.(net.PacketConn), &udpAddr, host, tlsCfg, cfg)
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
supportH3: atomic.NewBool(preferH3),
|
|
|
|
firstTest: atomic.NewBool(true),
|
2020-02-15 21:42:46 +08:00
|
|
|
}
|
|
|
|
}
|