|
| 1 | +package dns |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/tls" |
| 7 | + "crypto/x509" |
| 8 | + "encoding/binary" |
| 9 | + "errors" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "net" |
| 13 | + "net/http" |
| 14 | + "net/url" |
| 15 | + "strings" |
| 16 | + "sync" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +// Transports a -dns-server can use (#86). |
| 21 | +const ( |
| 22 | + TransportUDP = "udp" // ip:port; UDP with TCP fallback on truncation |
| 23 | + TransportTLS = "tls" // tls://host[:853]; DNS over TLS (RFC 7858) |
| 24 | + TransportHTTPS = "https" // https://host/path; DNS over HTTPS (RFC 8484) |
| 25 | +) |
| 26 | + |
| 27 | +// Transport names the transport a -dns-server value selects. |
| 28 | +func Transport(server string) string { |
| 29 | + switch { |
| 30 | + case strings.HasPrefix(server, "tls://"): |
| 31 | + return TransportTLS |
| 32 | + case strings.HasPrefix(server, "https://"): |
| 33 | + return TransportHTTPS |
| 34 | + } |
| 35 | + return TransportUDP |
| 36 | +} |
| 37 | + |
| 38 | +// testRootCAs, when set by tests, replaces the system roots for DoT and DoH. |
| 39 | +var testRootCAs *x509.CertPool |
| 40 | + |
| 41 | +// dialer opens one connection per DNS query, the way Go's resolver expects. |
| 42 | +// Go frames queries with a two-byte length prefix on any connection that is |
| 43 | +// not a net.PacketConn, which is exactly the DoT wire format; DoH adapts the |
| 44 | +// same framing to HTTP requests. |
| 45 | +type dialer interface { |
| 46 | + dial(ctx context.Context, network string) (net.Conn, error) |
| 47 | +} |
| 48 | + |
| 49 | +// newDialer returns the dialer for a -dns-server value. It is validated |
| 50 | +// earlier (validate.DNSServer), so errors here fall back to UDP. |
| 51 | +func newDialer(timeout time.Duration, server string) dialer { |
| 52 | + switch Transport(server) { |
| 53 | + case TransportTLS: |
| 54 | + host := strings.TrimPrefix(server, "tls://") |
| 55 | + if _, _, err := net.SplitHostPort(host); err != nil { |
| 56 | + host = net.JoinHostPort(host, "853") |
| 57 | + } |
| 58 | + name, _, _ := net.SplitHostPort(host) |
| 59 | + return &dotDialer{ |
| 60 | + addr: host, |
| 61 | + tls: &tls.Config{ServerName: name, RootCAs: testRootCAs, MinVersion: tls.VersionTLS12}, |
| 62 | + timeout: timeout, |
| 63 | + idle: make(chan idleConn, 16), |
| 64 | + } |
| 65 | + case TransportHTTPS: |
| 66 | + return &dohDialer{ |
| 67 | + url: server, |
| 68 | + client: &http.Client{Transport: &http.Transport{ |
| 69 | + TLSClientConfig: &tls.Config{RootCAs: testRootCAs, MinVersion: tls.VersionTLS12}, |
| 70 | + ForceAttemptHTTP2: true, |
| 71 | + MaxIdleConnsPerHost: 64, |
| 72 | + IdleConnTimeout: 30 * time.Second, |
| 73 | + }}, |
| 74 | + } |
| 75 | + } |
| 76 | + return &udpDialer{addr: server, timeout: timeout} |
| 77 | +} |
| 78 | + |
| 79 | +type udpDialer struct { |
| 80 | + addr string |
| 81 | + timeout time.Duration |
| 82 | +} |
| 83 | + |
| 84 | +func (d *udpDialer) dial(ctx context.Context, network string) (net.Conn, error) { |
| 85 | + nd := net.Dialer{Timeout: d.timeout} |
| 86 | + return nd.DialContext(ctx, network, d.addr) |
| 87 | +} |
| 88 | + |
| 89 | +// dotIdleMax bounds how long a DoT connection is kept for reuse. Servers |
| 90 | +// close idle connections after some seconds; reusing one they already closed |
| 91 | +// would fail a query, so connections older than this are discarded. |
| 92 | +const dotIdleMax = 5 * time.Second |
| 93 | + |
| 94 | +type idleConn struct { |
| 95 | + conn *tls.Conn |
| 96 | + since time.Time |
| 97 | +} |
| 98 | + |
| 99 | +// dotDialer keeps a small pool of TLS connections so most queries skip the |
| 100 | +// handshake. |
| 101 | +type dotDialer struct { |
| 102 | + addr string |
| 103 | + tls *tls.Config |
| 104 | + timeout time.Duration |
| 105 | + idle chan idleConn |
| 106 | +} |
| 107 | + |
| 108 | +func (d *dotDialer) dial(ctx context.Context, _ string) (net.Conn, error) { |
| 109 | + for { |
| 110 | + select { |
| 111 | + case ic := <-d.idle: |
| 112 | + if time.Since(ic.since) > dotIdleMax { |
| 113 | + _ = ic.conn.Close() |
| 114 | + continue |
| 115 | + } |
| 116 | + return &pooledConn{Conn: ic.conn, pool: d}, nil |
| 117 | + default: |
| 118 | + } |
| 119 | + break |
| 120 | + } |
| 121 | + td := tls.Dialer{NetDialer: &net.Dialer{Timeout: d.timeout}, Config: d.tls} |
| 122 | + c, err := td.DialContext(ctx, "tcp", d.addr) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + return &pooledConn{Conn: c.(*tls.Conn), pool: d}, nil |
| 127 | +} |
| 128 | + |
| 129 | +// pooledConn returns its TLS connection to the pool on Close, unless a read |
| 130 | +// or write failed (the stream may then hold a partial message). |
| 131 | +type pooledConn struct { |
| 132 | + *tls.Conn |
| 133 | + pool *dotDialer |
| 134 | + bad bool |
| 135 | +} |
| 136 | + |
| 137 | +func (c *pooledConn) Read(p []byte) (int, error) { |
| 138 | + n, err := c.Conn.Read(p) |
| 139 | + if err != nil { |
| 140 | + c.bad = true |
| 141 | + } |
| 142 | + return n, err |
| 143 | +} |
| 144 | + |
| 145 | +func (c *pooledConn) Write(p []byte) (int, error) { |
| 146 | + n, err := c.Conn.Write(p) |
| 147 | + if err != nil { |
| 148 | + c.bad = true |
| 149 | + } |
| 150 | + return n, err |
| 151 | +} |
| 152 | + |
| 153 | +func (c *pooledConn) Close() error { |
| 154 | + if c.bad { |
| 155 | + return c.Conn.Close() |
| 156 | + } |
| 157 | + _ = c.SetDeadline(time.Time{}) |
| 158 | + select { |
| 159 | + case c.pool.idle <- idleConn{conn: c.Conn, since: time.Now()}: |
| 160 | + return nil |
| 161 | + default: |
| 162 | + return c.Conn.Close() |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// dohDialer turns each query into an HTTPS POST (RFC 8484). The HTTP client |
| 167 | +// keeps connections alive (HTTP/2 where the server offers it), so "dialing" |
| 168 | +// is cheap. |
| 169 | +type dohDialer struct { |
| 170 | + url string |
| 171 | + client *http.Client |
| 172 | +} |
| 173 | + |
| 174 | +func (d *dohDialer) dial(ctx context.Context, _ string) (net.Conn, error) { |
| 175 | + return &dohConn{ctx: ctx, d: d}, nil |
| 176 | +} |
| 177 | + |
| 178 | +// dohConn accepts length-prefixed DNS messages on Write, exchanges each one |
| 179 | +// over HTTPS, and serves the length-prefixed answers on Read. |
| 180 | +type dohConn struct { |
| 181 | + ctx context.Context |
| 182 | + d *dohDialer |
| 183 | + mu sync.Mutex |
| 184 | + in, out bytes.Buffer |
| 185 | + deadline time.Time |
| 186 | +} |
| 187 | + |
| 188 | +func (c *dohConn) Write(p []byte) (int, error) { |
| 189 | + c.mu.Lock() |
| 190 | + defer c.mu.Unlock() |
| 191 | + c.in.Write(p) |
| 192 | + for c.in.Len() >= 2 { |
| 193 | + size := int(binary.BigEndian.Uint16(c.in.Bytes()[:2])) |
| 194 | + if c.in.Len() < 2+size { |
| 195 | + break |
| 196 | + } |
| 197 | + msg := append([]byte(nil), c.in.Next(2 + size)[2:]...) |
| 198 | + answer, err := c.exchange(msg) |
| 199 | + if err != nil { |
| 200 | + return 0, err |
| 201 | + } |
| 202 | + c.out.Write(binary.BigEndian.AppendUint16(nil, uint16(len(answer)))) //nolint:gosec // DNS messages are under 64 KiB |
| 203 | + c.out.Write(answer) |
| 204 | + } |
| 205 | + return len(p), nil |
| 206 | +} |
| 207 | + |
| 208 | +func (c *dohConn) exchange(msg []byte) ([]byte, error) { |
| 209 | + ctx := c.ctx |
| 210 | + if !c.deadline.IsZero() { |
| 211 | + var cancel context.CancelFunc |
| 212 | + ctx, cancel = context.WithDeadline(ctx, c.deadline) |
| 213 | + defer cancel() |
| 214 | + } |
| 215 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.d.url, bytes.NewReader(msg)) |
| 216 | + if err != nil { |
| 217 | + return nil, err |
| 218 | + } |
| 219 | + req.Header.Set("Content-Type", "application/dns-message") |
| 220 | + req.Header.Set("Accept", "application/dns-message") |
| 221 | + resp, err := c.d.client.Do(req) |
| 222 | + if err != nil { |
| 223 | + var ue *url.Error |
| 224 | + if errors.As(err, &ue) && ue.Timeout() { |
| 225 | + return nil, &net.DNSError{Err: "i/o timeout", IsTimeout: true} |
| 226 | + } |
| 227 | + return nil, err |
| 228 | + } |
| 229 | + defer func() { _ = resp.Body.Close() }() |
| 230 | + if resp.StatusCode != http.StatusOK { |
| 231 | + return nil, fmt.Errorf("DNS-over-HTTPS server returned %s", resp.Status) |
| 232 | + } |
| 233 | + return io.ReadAll(io.LimitReader(resp.Body, 65535)) |
| 234 | +} |
| 235 | + |
| 236 | +func (c *dohConn) Read(p []byte) (int, error) { |
| 237 | + c.mu.Lock() |
| 238 | + defer c.mu.Unlock() |
| 239 | + if c.out.Len() == 0 { |
| 240 | + return 0, io.EOF |
| 241 | + } |
| 242 | + return c.out.Read(p) |
| 243 | +} |
| 244 | + |
| 245 | +func (c *dohConn) Close() error { return nil } |
| 246 | +func (c *dohConn) LocalAddr() net.Addr { return dohAddr{} } |
| 247 | +func (c *dohConn) RemoteAddr() net.Addr { return dohAddr{} } |
| 248 | +func (c *dohConn) SetReadDeadline(time.Time) error { return nil } |
| 249 | +func (c *dohConn) SetWriteDeadline(t time.Time) error { return c.SetDeadline(t) } |
| 250 | +func (c *dohConn) SetDeadline(t time.Time) error { |
| 251 | + c.mu.Lock() |
| 252 | + c.deadline = t |
| 253 | + c.mu.Unlock() |
| 254 | + return nil |
| 255 | +} |
| 256 | + |
| 257 | +type dohAddr struct{} |
| 258 | + |
| 259 | +func (dohAddr) Network() string { return "https" } |
| 260 | +func (dohAddr) String() string { return "doh" } |
0 commit comments