|
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+// Network contains a default implementation of the network.
|
|
|
2
|
+//
|
|
|
3
|
+// Please see mtglib.Network interface to get some basic idea behind
|
|
|
4
|
+// this abstraction.
|
|
|
5
|
+//
|
|
|
6
|
+// Some notable feature of this implementation:
|
|
|
7
|
+//
|
|
|
8
|
+// 1. It detaches dialer from a network. Dialer is something which
|
|
|
9
|
+// implements a real dialer and network completes it with more higher
|
|
|
10
|
+// level details.
|
|
|
11
|
+//
|
|
|
12
|
+// 2. It uses only TCP connections. Even for DNS it uses DNS-Over-HTTPS
|
|
|
13
|
+//
|
|
|
14
|
+// 3. It has some simple implementation of DNS cache which is good
|
|
|
15
|
+// enough for our purpose.
|
|
|
16
|
+//
|
|
|
17
|
+// 4. It sets uses SO_REUSEPORT port if applicable.
|
|
1
|
18
|
package network
|
|
2
|
19
|
|
|
3
|
20
|
import (
|
|
|
@@ -8,23 +25,59 @@ import (
|
|
8
|
25
|
)
|
|
9
|
26
|
|
|
10
|
27
|
const (
|
|
11
|
|
- DefaultTimeout = 10 * time.Second
|
|
|
28
|
+ // DefaultTimeout is a default timeout for establishing TCP
|
|
|
29
|
+ // connection.
|
|
|
30
|
+ DefaultTimeout = 10 * time.Second
|
|
|
31
|
+
|
|
|
32
|
+ // DefaultHTTPTimeout defines a default timeout for making HTTP
|
|
|
33
|
+ // request.
|
|
12
|
34
|
DefaultHTTPTimeout = 10 * time.Second
|
|
13
|
|
- DefaultBufferSize = 16 * 1024 // 16 kib
|
|
14
|
35
|
|
|
|
36
|
+ // DefaultBufferSize defines a TCP buffer size. Both read and write, so
|
|
|
37
|
+ // for real size, please multiply this number by 2.
|
|
|
38
|
+ DefaultBufferSize = 16 * 1024 // 16 kib
|
|
|
39
|
+
|
|
|
40
|
+ // ProxyDialerOpenThreshold is used for load balancing SOCKS5 dialer
|
|
|
41
|
+ // only.
|
|
|
42
|
+ //
|
|
|
43
|
+ // This dialer uses circuit breaker with of 3 stages: OPEN,
|
|
|
44
|
+ // HALF_OPEN and CLOSED. If state is CLOSED, all requests go in
|
|
|
45
|
+ // a normal mode. If you get more that ProxyDialerOpenThreshold
|
|
|
46
|
+ // errors, circuit breaker goes into OPEN mode.
|
|
|
47
|
+ //
|
|
|
48
|
+ // When circuit breaker is in OPEN mode, it forbids all request to
|
|
|
49
|
+ // a given proxy. But after ProxyDialerHalfOpenTimeout it gives a
|
|
|
50
|
+ // second chance and opens an access for a SINGLE request. If this
|
|
|
51
|
+ // request success, then circuit breaker closes, otherwise opens
|
|
|
52
|
+ // again.
|
|
|
53
|
+ //
|
|
|
54
|
+ // When circuit breaker is closed, it clears an error states each
|
|
|
55
|
+ // ProxyDialerResetFailuresTimeout.
|
|
15
|
56
|
ProxyDialerOpenThreshold = 5
|
|
16
|
57
|
ProxyDialerHalfOpenTimeout = time.Minute
|
|
17
|
58
|
ProxyDialerResetFailuresTimeout = 10 * time.Second
|
|
18
|
59
|
|
|
|
60
|
+ // DefaultDOHHostname defines a default IP address for DOH host.
|
|
|
61
|
+ // Since mtg is simple, please pass IP address here. We do not
|
|
|
62
|
+ // have bootstrap servers here embedded.
|
|
19
|
63
|
DefaultDOHHostname = "9.9.9.9"
|
|
20
|
|
- DNSTimeout = 5 * time.Second
|
|
|
64
|
+
|
|
|
65
|
+ // DNSTimeout defines a timeout for DNS queries.
|
|
|
66
|
+ DNSTimeout = 5 * time.Second
|
|
21
|
67
|
)
|
|
22
|
68
|
|
|
23
|
69
|
var (
|
|
24
|
|
- ErrCircuitBreakerOpened = errors.New("circuit breaker is opened")
|
|
|
70
|
+ // ErrCircuitBreakerOpened is returned when proxy is being accessed
|
|
|
71
|
+ // but circuit breaker is opened.
|
|
|
72
|
+ ErrCircuitBreakerOpened = errors.New("circuit breaker is opened")
|
|
|
73
|
+
|
|
|
74
|
+ // ErrCannotDialWithAllProxies is returned when load balancing
|
|
|
75
|
+ // client is trying to access proxies but all of them are failed.
|
|
25
|
76
|
ErrCannotDialWithAllProxies = errors.New("cannot dial with all proxies")
|
|
26
|
77
|
)
|
|
27
|
78
|
|
|
|
79
|
+// Dialer defines an interface which is required to bootstrap a network
|
|
|
80
|
+// instance from.
|
|
28
|
81
|
type Dialer interface {
|
|
29
|
82
|
Dial(network, address string) (net.Conn, error)
|
|
30
|
83
|
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|