-
Notifications
You must be signed in to change notification settings - Fork 301
/
upstream.go
315 lines (285 loc) · 8.62 KB
/
upstream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (C) 2020-2021, IrineSistiana
//
// This file is part of mosdns.
//
// mosdns is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mosdns is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package upstream
import (
"context"
"crypto/tls"
"fmt"
"github.com/IrineSistiana/mosdns/v3/dispatcher/pkg/dnsutils"
"github.com/lucas-clemente/quic-go"
"github.com/miekg/dns"
"go.uber.org/zap"
"golang.org/x/net/http2"
"io"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
generalWriteTimeout = time.Second * 1
tlsHandshakeTimeout = time.Second * 5
)
var (
nopLogger = zap.NewNop()
)
// Upstream represents a DNS upstream.
type Upstream interface {
// ExchangeContext exchanges query message m to the upstream, and returns
// response. It MUST NOT keep or modify m.
ExchangeContext(ctx context.Context, m *dns.Msg) (*dns.Msg, error)
// CloseIdleConnections closes any connections in the Upstream which
// now sitting idle. It does not interrupt any connections currently in use.
CloseIdleConnections()
io.Closer
}
type Opt struct {
// DialAddr specifies the address the upstream will
// actually dial to.
// Not implemented for doh upstreams with http/3.
DialAddr string
// Socks5 specifies the socks5 proxy server that the upstream
// will connect though.
// Not implemented for udp upstreams and doh upstreams with http/3.
Socks5 string
// SoMark specifies the mark for each packet sent through this upstream.
// Not implemented for doh upstreams with http/3
SoMark int
// IdleTimeout specifies the idle timeout for long-connections.
// Available for TCP, DoT, DoH.
// If negative, TCP, DoT will not reuse connections.
// Default: TCP, DoT: 10s , DoH: 30s.
IdleTimeout time.Duration
// EnablePipeline enables query pipelining support as RFC 7766 6.2.1.1 suggested.
// Available for TCP, DoT upstream with IdleTimeout >= 0.
EnablePipeline bool
// EnableHTTP3 enables HTTP/3 protocol for DoH upstream.
EnableHTTP3 bool
// MaxConns limits the total number of connections, including connections
// in the dialing states.
// Implemented for TCP/DoT pipeline enabled upstreams and DoH upstreams.
// Default is 1.
MaxConns int
// TLSConfig specifies the tls.Config that the TLS client will use.
// Available for DoT, DoH upstreams.
TLSConfig *tls.Config
// Logger specifies the logger that the upstream will use.
Logger *zap.Logger
}
func NewUpstream(addr string, opt *Opt) (Upstream, error) {
if opt == nil {
opt = new(Opt)
}
// parse protocol and server addr
if !strings.Contains(addr, "://") {
addr = "udp://" + addr
}
addrURL, err := url.Parse(addr)
if err != nil {
return nil, fmt.Errorf("invalid server address, %w", err)
}
switch addrURL.Scheme {
case "", "udp":
dialAddr := getDialAddrWithPort(addrURL.Host, opt.DialAddr, 53)
ut := &Transport{
Logger: opt.Logger,
DialFunc: func(ctx context.Context) (net.Conn, error) {
d := net.Dialer{
Control: getSetMarkFunc(opt.SoMark),
}
return d.DialContext(ctx, "udp", dialAddr)
},
WriteFunc: dnsutils.WriteMsgToUDP,
ReadFunc: func(c io.Reader) (*dns.Msg, int, error) {
return dnsutils.ReadMsgFromUDP(c, 4096)
},
EnablePipeline: true,
MaxConns: opt.MaxConns,
IdleTimeout: time.Second * 60,
}
tt := &Transport{
Logger: opt.Logger,
DialFunc: func(ctx context.Context) (net.Conn, error) {
d := net.Dialer{
Control: getSetMarkFunc(opt.SoMark),
}
return d.DialContext(ctx, "tcp", dialAddr)
},
WriteFunc: dnsutils.WriteMsgToTCP,
ReadFunc: dnsutils.ReadMsgFromTCP,
}
return &udpWithFallback{
u: ut,
t: tt,
}, nil
case "tcp":
dialAddr := getDialAddrWithPort(addrURL.Host, opt.DialAddr, 53)
t := &Transport{
Logger: opt.Logger,
DialFunc: func(ctx context.Context) (net.Conn, error) {
return dialTCP(ctx, dialAddr, opt.Socks5, opt.SoMark)
},
WriteFunc: dnsutils.WriteMsgToTCP,
ReadFunc: dnsutils.ReadMsgFromTCP,
IdleTimeout: opt.IdleTimeout,
EnablePipeline: opt.EnablePipeline,
MaxConns: opt.MaxConns,
}
return t, nil
case "tls":
var tlsConfig *tls.Config
if opt.TLSConfig != nil {
tlsConfig = opt.TLSConfig.Clone()
} else {
tlsConfig = new(tls.Config)
}
if len(tlsConfig.ServerName) == 0 {
tlsConfig.ServerName = tryRemovePort(addrURL.Host)
}
dialAddr := getDialAddrWithPort(addrURL.Host, opt.DialAddr, 853)
t := &Transport{
Logger: opt.Logger,
DialFunc: func(ctx context.Context) (net.Conn, error) {
conn, err := dialTCP(ctx, dialAddr, opt.Socks5, opt.SoMark)
if err != nil {
return nil, err
}
tlsConn := tls.Client(conn, tlsConfig)
if err := tlsConn.HandshakeContext(ctx); err != nil {
tlsConn.Close()
return nil, err
}
return tlsConn, nil
},
WriteFunc: dnsutils.WriteMsgToTCP,
ReadFunc: dnsutils.ReadMsgFromTCP,
IdleTimeout: opt.IdleTimeout,
EnablePipeline: opt.EnablePipeline,
MaxConns: opt.MaxConns,
}
return t, nil
case "https":
idleConnTimeout := time.Second * 30
if opt.IdleTimeout > 0 {
idleConnTimeout = opt.IdleTimeout
}
maxConn := 2
if opt.MaxConns > 0 {
maxConn = opt.MaxConns
}
dialAddr := getDialAddrWithPort(addrURL.Host, opt.DialAddr, 443)
var t http.RoundTripper
var addonCloser io.Closer // udpConn
if opt.EnableHTTP3 {
lc := net.ListenConfig{Control: getSetMarkFunc(opt.SoMark)}
conn, err := lc.ListenPacket(context.Background(), "udp", "")
if err != nil {
return nil, fmt.Errorf("failed to init udp socket for quic")
}
addonCloser = conn
t = &h3rt{
logger: opt.Logger,
tlsConfig: opt.TLSConfig,
quicConfig: &quic.Config{
TokenStore: quic.NewLRUTokenStore(4, 8),
InitialStreamReceiveWindow: 4 * 1024,
MaxStreamReceiveWindow: 4 * 1024,
InitialConnectionReceiveWindow: 8 * 1024,
MaxConnectionReceiveWindow: 64 * 1024,
},
dialFunc: func(_, _ string, tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) {
ua, err := net.ResolveUDPAddr("udp", dialAddr)
if err != nil {
return nil, err
}
return quic.DialEarly(conn, ua, addrURL.Host, tlsCfg, cfg)
},
}
} else {
t1 := &http.Transport{
DialContext: func(ctx context.Context, network, _ string) (net.Conn, error) { // overwrite server addr
return dialTCP(ctx, dialAddr, opt.Socks5, opt.SoMark)
},
TLSClientConfig: opt.TLSConfig,
TLSHandshakeTimeout: tlsHandshakeTimeout,
IdleConnTimeout: idleConnTimeout,
// MaxConnsPerHost and MaxIdleConnsPerHost should be equal.
// Otherwise, it might seriously affect the efficiency of connection reuse.
MaxConnsPerHost: maxConn,
MaxIdleConnsPerHost: maxConn,
}
t2, err := http2.ConfigureTransports(t1)
if err != nil {
return nil, fmt.Errorf("failed to upgrade http2 support, %w", err)
}
t2.ReadIdleTimeout = time.Second * 30
t2.PingTimeout = time.Second * 5
t = t1
}
return &DoHUpstream{
EndPoint: addr,
Client: &http.Client{Transport: t},
AddOnCloser: addonCloser,
}, nil
default:
return nil, fmt.Errorf("unsupported protocol [%s]", addrURL.Scheme)
}
}
func getDialAddrWithPort(host, dialAddr string, defaultPort int) string {
addr := host
if len(dialAddr) > 0 {
addr = dialAddr
}
_, _, err := net.SplitHostPort(addr)
if err != nil { // no port, add it.
return net.JoinHostPort(strings.Trim(addr, "[]"), strconv.Itoa(defaultPort))
}
return addr
}
func tryRemovePort(s string) string {
host, _, err := net.SplitHostPort(s)
if err != nil {
return s
}
return host
}
type udpWithFallback struct {
u *Transport
t *Transport
}
func (u *udpWithFallback) ExchangeContext(ctx context.Context, q *dns.Msg) (*dns.Msg, error) {
m, err := u.u.ExchangeContext(ctx, q)
if err != nil {
return nil, err
}
if m.Truncated {
u.u.logger().Warn("truncated udp msg received, retrying tcp")
return u.t.ExchangeContext(ctx, q)
}
return m, nil
}
func (u *udpWithFallback) CloseIdleConnections() {
u.u.CloseIdleConnections()
u.t.CloseIdleConnections()
}
func (u *udpWithFallback) Close() error {
u.u.Close()
u.t.Close()
return nil
}