-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialer.go
334 lines (295 loc) · 9.54 KB
/
dialer.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package chained
import (
"bufio"
"context"
"fmt"
"io"
"io/ioutil"
"math"
"net"
"net/http"
"net/url"
"sync/atomic"
"time"
"github.com/dustin/go-humanize"
servertiming "github.com/mitchellh/go-server-timing"
"github.com/getlantern/bufconn"
"github.com/getlantern/errors"
"github.com/getlantern/idletiming"
gp "github.com/getlantern/proxy/v3"
"github.com/getlantern/flashlight/v7/balancer"
"github.com/getlantern/flashlight/v7/bandwidth"
"github.com/getlantern/flashlight/v7/common"
"github.com/getlantern/flashlight/v7/domainrouting"
"github.com/getlantern/flashlight/v7/ops"
)
const (
minCheckInterval = 10 * time.Second
maxCheckInterval = 15 * time.Minute
dialCoreCheckInterval = 30 * time.Second
)
var (
// IdleTimeout closes connections idle for a period to avoid dangling
// connections. Web applications tend to contact servers in 1 minute
// interval or below. 65 seconds is long enough to avoid interrupt normal
// connections but shorter than the idle timeout on the server to avoid
// running into closed connection problems.
IdleTimeout = 65 * time.Second
// errUpstream is an error that indicates there was a problem upstream of a
// proxy. Such errors are not counted as failures but do allow failover to
// other proxies.
errUpstream = errors.New("Upstream error")
)
func (p *proxy) SupportsAddr(network, addr string) bool {
if p.allowedDomains == nil {
// this proxy doesn't filter allowed domains
return true
}
domain, _, err := net.SplitHostPort(addr)
if err != nil {
log.Debugf("Unable to split addr %v: %v", addr, err)
return false
}
rule := p.allowedDomains.RuleFor(domain)
return rule == domainrouting.MustProxy
}
func (p *proxy) Stop() {
log.Tracef("Stopping dialer %s", p.Label())
p.closeOnce.Do(func() {
close(p.closeCh)
p.impl.close()
})
}
func (p *proxy) Attempts() int64 {
return atomic.LoadInt64(&p.attempts)
}
func (p *proxy) Successes() int64 {
return atomic.LoadInt64(&p.successes)
}
func (p *proxy) ConsecSuccesses() int64 {
return atomic.LoadInt64(&p.consecSuccesses)
}
func (p *proxy) Failures() int64 {
return atomic.LoadInt64(&p.failures)
}
func (p *proxy) ConsecFailures() int64 {
return atomic.LoadInt64(&p.consecFailures)
}
func (p *proxy) Succeeding() bool {
return p.ConsecFailures() == 0 ||
(p.EstSuccessRate() > 0.5 && p.consecReadSuccesses.Get() > 0)
}
func (p *proxy) DataSent() uint64 {
return atomic.LoadUint64(&p.dataSent)
}
func (p *proxy) DataRecv() uint64 {
return atomic.LoadUint64(&p.dataRecv)
}
func (p *proxy) NumPreconnecting() int {
return p.numPreconnecting()
}
func (p *proxy) NumPreconnected() int {
return p.numPreconnected()
}
func (p *proxy) WriteStats(w io.Writer) {
estRTT := p.EstRTT().Seconds()
estBandwidth := p.EstBandwidth()
probeSuccesses, probeSuccessKBs, probeFailures, probeFailedKBs := p.ProbeStats()
_, _ = fmt.Fprintf(w, "%s P:%3d R:%3d A: %5d S: %5d CS: %3d F: %5d CF: %3d R: %4.3f L: %4.0fms B: %6.2fMbps T: %7s/%7s P: %3d(%3dkb)/%3d(%3dkb)\n",
p.JustifiedLabel(),
p.NumPreconnected(),
p.NumPreconnecting(),
p.Attempts(),
p.Successes(), p.ConsecSuccesses(),
p.Failures(), p.ConsecFailures(),
p.EstSuccessRate(),
estRTT*1000, estBandwidth,
humanize.Bytes(p.DataSent()), humanize.Bytes(p.DataRecv()),
probeSuccesses, probeSuccessKBs, probeFailures, probeFailedKBs)
if impl, ok := p.impl.(*multipathImpl); ok {
for _, line := range impl.FormatStats() {
_, _ = fmt.Fprintf(w, "\t%s\n", line)
}
}
}
// DialContext dials using provided context
func (p *proxy) DialContext(ctx context.Context, network, addr string) (conn net.Conn, isUpstreamError bool, err error) {
op := ops.Begin("dial_for_balancer").
ChainedProxy(p.Name(), p.Addr(), p.Protocol(), p.Network(), p.multiplexed).
Set("dial_type", network)
defer op.End()
log.Debugf("Dialing origin address %s for proxy %s", addr, p.Label())
conn, err = p.dialOrigin(op, ctx, p, network, addr)
if err != nil {
op.Set("idled", idletiming.IsIdled(conn))
op.FailIf(err)
if err != errUpstream {
p.MarkFailure()
}
return nil, err == errUpstream, err
}
if network == balancer.NetworkConnect {
// only mark success if we did a CONNECT request because that involves a
// full round-trip to/from the proxy
p.markSuccess()
}
return conn, false, nil
}
func (p *proxy) markSuccess() {
p.emaSuccessRate.Update(1)
atomic.AddInt64(&p.attempts, 1)
atomic.AddInt64(&p.successes, 1)
newCS := atomic.AddInt64(&p.consecSuccesses, 1)
log.Tracef("Dialer %s consecutive successes: %d -> %d", p.Label(), newCS-1, newCS)
// only when state is changing
if newCS <= 2 {
atomic.StoreInt64(&p.consecFailures, 0)
}
}
func (p *proxy) MarkFailure() {
p.emaSuccessRate.Update(0)
atomic.AddInt64(&p.attempts, 1)
atomic.AddInt64(&p.failures, 1)
atomic.StoreInt64(&p.consecSuccesses, 0)
newCF := atomic.AddInt64(&p.consecFailures, 1)
log.Tracef("Dialer %s consecutive failures: %d -> %d", p.Label(), newCF-1, newCF)
return
}
// defaultDialOrigin implements the method from serverConn. With standard proxies, this
// involves sending either a CONNECT request or a GET request to initiate a
// persistent connection to the upstream proxy.
func defaultDialOrigin(op *ops.Op, ctx context.Context, p *proxy, network, addr string) (net.Conn, error) {
conn, err := p.reportedDial(func(op *ops.Op) (net.Conn, error) {
return p.impl.dialServer(op, ctx)
})
if err != nil {
log.Debugf("Unable to dial server %v: %s", p.Label(), err)
return nil, err
}
conn, err = overheadWrapper(true)(conn, op.FailIf(err))
var timeout time.Duration
if deadline, set := ctx.Deadline(); set {
conn.SetDeadline(deadline)
// Set timeout based on our given deadline, minus a 2 second fudge factor
timeUntilDeadline := deadline.Sub(time.Now())
timeout = timeUntilDeadline - 2*time.Second
if timeout < 0 {
log.Errorf("Not enough time left for server to dial upstream within %v, return errUpstream immediately", timeUntilDeadline)
return nil, errUpstream
}
}
// Look for our special hacked "connect" transport used to signal
// that we should send a CONNECT request and tunnel all traffic through
// that.
switch network {
case balancer.NetworkConnect:
log.Trace("Sending CONNECT request")
bconn := bufconn.Wrap(conn)
conn = bconn
err = p.sendCONNECT(op, addr, bconn, timeout)
case balancer.NetworkPersistent:
log.Trace("Sending GET request to establish persistent HTTP connection")
err = p.initPersistentConnection(addr, conn)
}
if err != nil {
conn.Close()
return nil, err
}
// Unset the deadline to avoid affecting later read/write on the connection.
conn.SetDeadline(time.Time{})
return conn, nil
}
func (p *proxy) onRequest(req *http.Request) {
p.AdaptRequest(req)
common.AddCommonHeaders(p.user, req)
// Request BBR metrics
req.Header.Set("X-BBR", "y")
}
func (p *proxy) onFinish(op *ops.Op) {
op.ChainedProxy(p.Name(), p.Addr(), p.Protocol(), p.Network(), p.multiplexed)
}
func (p *proxy) sendCONNECT(op *ops.Op, addr string, conn bufconn.Conn, timeout time.Duration) error {
reqTime := time.Now()
req, err := p.buildCONNECTRequest(addr, timeout)
if err != nil {
return fmt.Errorf("Unable to construct CONNECT request: %s", err)
}
err = req.Write(conn)
if err != nil {
return fmt.Errorf("Unable to write CONNECT request: %s", err)
}
r := conn.Head()
err = p.checkCONNECTResponse(op, r, req, reqTime)
return err
}
func (p *proxy) buildCONNECTRequest(addr string, timeout time.Duration) (*http.Request, error) {
req, err := http.NewRequest(http.MethodConnect, "/", nil)
if err != nil {
return nil, err
}
req.URL = &url.URL{
Host: addr,
}
req.Host = addr
if timeout != 0 {
req.Header.Set(common.ProxyDialTimeoutHeader, fmt.Sprint(int(math.Ceil(timeout.Seconds()*1000))))
}
p.onRequest(req)
return req, nil
}
func (p *proxy) checkCONNECTResponse(op *ops.Op, r *bufio.Reader, req *http.Request, reqTime time.Time) error {
resp, err := http.ReadResponse(r, req)
if err != nil {
return errors.New("Error reading CONNECT response: %s", err)
}
if !sameStatusCodeClass(http.StatusOK, resp.StatusCode) {
var body []byte
if resp.Body != nil {
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
}
log.Errorf("Bad status code on CONNECT response %d: %v", resp.StatusCode, string(body))
return errUpstream
}
rtt := time.Since(reqTime)
timing := resp.Header.Get(servertiming.HeaderKey)
if timing != "" {
header, err := servertiming.ParseHeader(timing)
if err != nil {
log.Errorf("Fail to parse Server-Timing header in CONNECT response: %v", err)
} else {
// dialupstream is the only metric for now, but more may be added later.
for _, metric := range header.Metrics {
if metric.Name == gp.MetricDialUpstream {
adjustedRTT := rtt - metric.Duration
op.Set("connect_time_total", rtt)
op.Set("connect_time_server", metric.Duration)
op.Set("connect_time_rtt", adjustedRTT)
p.updateEstRTT(adjustedRTT)
}
}
}
}
p.collectBBRInfo(reqTime, resp)
bandwidth.Track(resp)
return nil
}
func sameStatusCodeClass(statusCode1 int, statusCode2 int) bool {
// HTTP response status code "classes" come in ranges of 100.
const classRange = 100
// These are all integers, so division truncates.
return statusCode1/classRange == statusCode2/classRange
}
func (p *proxy) initPersistentConnection(addr string, conn net.Conn) error {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%v", addr), nil)
if err != nil {
return err
}
p.onRequest(req)
req.Header.Set("X-Lantern-Persistent", "true")
writeErr := req.Write(conn)
if writeErr != nil {
return fmt.Errorf("Unable to write initial request: %v", writeErr)
}
return nil
}