2022-01-27 12:25:53 +08:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2022-04-28 22:21:48 +08:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2022-06-06 21:45:08 +08:00
|
|
|
"github.com/lucas-clemente/quic-go"
|
2022-04-28 22:21:48 +08:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2022-01-27 12:25:53 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
D "github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
2022-06-12 17:53:11 +08:00
|
|
|
const NextProtoDQ = "doq"
|
2022-01-27 12:25:53 +08:00
|
|
|
|
|
|
|
var bytesPool = sync.Pool{New: func() interface{} { return &bytes.Buffer{} }}
|
|
|
|
|
|
|
|
type quicClient struct {
|
|
|
|
addr string
|
2022-04-28 22:21:48 +08:00
|
|
|
r *Resolver
|
2022-04-13 02:32:55 +08:00
|
|
|
session quic.Connection
|
2022-04-28 22:21:48 +08:00
|
|
|
proxyAdapter string
|
2022-01-27 12:25:53 +08:00
|
|
|
sync.RWMutex // protects session and bytesPool
|
|
|
|
}
|
|
|
|
|
2022-04-28 22:21:48 +08:00
|
|
|
func newDOQ(r *Resolver, addr, proxyAdapter string) *quicClient {
|
|
|
|
return &quicClient{
|
|
|
|
addr: addr,
|
|
|
|
r: r,
|
|
|
|
proxyAdapter: proxyAdapter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 12:25:53 +08:00
|
|
|
func (dc *quicClient) Exchange(m *D.Msg) (msg *D.Msg, err error) {
|
|
|
|
return dc.ExchangeContext(context.Background(), m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *quicClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
|
|
|
|
stream, err := dc.openStream(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open new stream to %s", dc.addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := m.Pack()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = stream.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The client MUST send the DNS query over the selected stream, and MUST
|
|
|
|
// indicate through the STREAM FIN mechanism that no further data will
|
|
|
|
// be sent on that stream.
|
|
|
|
// stream.Close() -- closes the write-direction of the stream.
|
|
|
|
_ = stream.Close()
|
|
|
|
|
|
|
|
respBuf := bytesPool.Get().(*bytes.Buffer)
|
|
|
|
defer bytesPool.Put(respBuf)
|
|
|
|
defer respBuf.Reset()
|
|
|
|
|
|
|
|
n, err := respBuf.ReadFrom(stream)
|
|
|
|
if err != nil && n == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := new(D.Msg)
|
|
|
|
err = reply.Unpack(respBuf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return reply, nil
|
|
|
|
}
|
|
|
|
|
2022-04-13 02:32:55 +08:00
|
|
|
func isActive(s quic.Connection) bool {
|
2022-01-27 12:25:53 +08:00
|
|
|
select {
|
|
|
|
case <-s.Context().Done():
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 02:32:55 +08:00
|
|
|
// getSession - opens or returns an existing quic.Connection
|
2022-01-27 12:25:53 +08:00
|
|
|
// useCached - if true and cached session exists, return it right away
|
|
|
|
// otherwise - forcibly creates a new session
|
2022-06-12 21:41:01 +08:00
|
|
|
func (dc *quicClient) getSession(ctx context.Context) (quic.Connection, error) {
|
2022-04-13 02:32:55 +08:00
|
|
|
var session quic.Connection
|
2022-01-27 12:25:53 +08:00
|
|
|
dc.RLock()
|
|
|
|
session = dc.session
|
|
|
|
if session != nil && isActive(session) {
|
|
|
|
dc.RUnlock()
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
if session != nil {
|
|
|
|
// we're recreating the session, let's create a new one
|
|
|
|
_ = session.CloseWithError(0, "")
|
|
|
|
}
|
|
|
|
dc.RUnlock()
|
|
|
|
|
|
|
|
dc.Lock()
|
|
|
|
defer dc.Unlock()
|
|
|
|
|
|
|
|
var err error
|
2022-06-12 21:41:01 +08:00
|
|
|
session, err = dc.openSession(ctx)
|
2022-01-27 12:25:53 +08:00
|
|
|
if err != nil {
|
|
|
|
// This does not look too nice, but QUIC (or maybe quic-go)
|
|
|
|
// doesn't seem stable enough.
|
|
|
|
// Maybe retransmissions aren't fully implemented in quic-go?
|
|
|
|
// Anyways, the simple solution is to make a second try when
|
|
|
|
// it fails to open the QUIC session.
|
2022-06-12 21:41:01 +08:00
|
|
|
session, err = dc.openSession(ctx)
|
2022-01-27 12:25:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dc.session = session
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:41:01 +08:00
|
|
|
func (dc *quicClient) openSession(ctx context.Context) (quic.Connection, error) {
|
2022-01-27 12:25:53 +08:00
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
NextProtos: []string{
|
|
|
|
"http/1.1", "h2", NextProtoDQ,
|
|
|
|
},
|
|
|
|
SessionTicketsDisabled: false,
|
|
|
|
}
|
|
|
|
quicConfig := &quic.Config{
|
|
|
|
ConnectionIDLength: 12,
|
|
|
|
HandshakeIdleTimeout: time.Second * 8,
|
2022-05-02 05:01:07 +08:00
|
|
|
MaxIncomingStreams: 4,
|
|
|
|
MaxIdleTimeout: time.Second * 45,
|
2022-01-27 12:25:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugln("opening session to %s", dc.addr)
|
2022-04-28 22:21:48 +08:00
|
|
|
var (
|
|
|
|
udp net.PacketConn
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
host, port, err := net.SplitHostPort(dc.addr)
|
2022-06-12 17:53:11 +08:00
|
|
|
|
2022-04-28 22:21:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := resolver.ResolveIPv4WithResolver(host, dc.r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := strconv.Atoi(port)
|
|
|
|
udpAddr := net.UDPAddr{IP: ip.AsSlice(), Port: p}
|
|
|
|
|
|
|
|
if dc.proxyAdapter == "" {
|
2022-06-12 21:41:01 +08:00
|
|
|
udp, err = dialer.ListenPacket(ctx, "udp", "")
|
2022-04-28 22:21:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-12 21:41:01 +08:00
|
|
|
conn, err := dialContextExtra(ctx, dc.proxyAdapter, "udp", ip, port)
|
2022-06-05 12:52:29 +08:00
|
|
|
if err != nil {
|
2022-04-28 22:21:48 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-05 12:52:29 +08:00
|
|
|
|
2022-04-28 22:21:48 +08:00
|
|
|
wrapConn, ok := conn.(*wrapPacketConn)
|
|
|
|
if !ok {
|
2022-06-06 21:45:08 +08:00
|
|
|
return nil, fmt.Errorf("quic create packet failed")
|
2022-04-28 22:21:48 +08:00
|
|
|
}
|
2022-06-05 12:52:29 +08:00
|
|
|
|
2022-05-17 21:30:54 +08:00
|
|
|
udp = wrapConn
|
2022-04-28 22:21:48 +08:00
|
|
|
}
|
|
|
|
|
2022-06-12 21:41:01 +08:00
|
|
|
session, err := quic.DialContext(ctx, udp, &udpAddr, host, tlsConfig, quicConfig)
|
2022-01-27 12:25:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open QUIC session: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return session, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dc *quicClient) openStream(ctx context.Context) (quic.Stream, error) {
|
2022-06-12 21:41:01 +08:00
|
|
|
session, err := dc.getSession(ctx)
|
2022-01-27 12:25:53 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// open a new stream
|
|
|
|
return session.OpenStreamSync(ctx)
|
|
|
|
}
|