Przeglądaj źródła

Add documentation for network

tags/v2.0.0-rc1
9seconds 5 lat temu
rodzic
commit
372c703ad6
6 zmienionych plików z 104 dodań i 4 usunięć
  1. 24
    0
      mtglib/init.go
  2. 5
    0
      network/default.go
  3. 57
    4
      network/init.go
  4. 10
    0
      network/load_balanced_socks5.go
  5. 4
    0
      network/network.go
  6. 4
    0
      network/socks5.go

+ 24
- 0
mtglib/init.go Wyświetl plik

@@ -27,9 +27,33 @@ const (
27 27
 	DefaultPreferIP           = "prefer-ipv6"
28 28
 )
29 29
 
30
+// Network defines a knowledge how to work with a network. It may sound
31
+// fun but it encapsulates all the knowledge how to properly establish
32
+// connections to remote hosts and configure HTTP clients.
33
+//
34
+// For example, if you want to use SOCKS5 proxy, you probably want to
35
+// have all traffic routed to this proxy: telegram connections, http
36
+// requests and so on. This knowledge is encapsulated into instances of
37
+// such interface.
38
+//
39
+// mtglib uses Network for:
40
+//
41
+// 1. Dialing to Telegram
42
+//
43
+// 2. Dialing to front domain
44
+//
45
+// 3. Doing HTTP requests (for example, for FireHOL ipblocklist).
30 46
 type Network interface {
47
+	// Dial establishes context-free TCP connections.
31 48
 	Dial(network, address string) (net.Conn, error)
49
+
50
+	// DialContext dials using a context. This is a preferrable
51
+	// way of establishing TCP connections.
32 52
 	DialContext(ctx context.Context, network, address string) (net.Conn, error)
53
+
54
+	// MakeHTTPClient build an HTTP client with given dial function. If
55
+	// nothing is provided, then DialContext of this interface is going
56
+	// to be used.
33 57
 	MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error)) *http.Client
34 58
 }
35 59
 

+ 5
- 0
network/default.go Wyświetl plik

@@ -60,6 +60,11 @@ func (d *defaultDialer) DialContext(ctx context.Context, network, address string
60 60
 	return tcpConn, nil
61 61
 }
62 62
 
63
+// NewDefaultDialer build a new dialer which dials bypassing proxies
64
+// etc.
65
+//
66
+// The most default one you can imagine. But it has tunes TCP
67
+// connections and setups SO_REUSEPORT.
63 68
 func NewDefaultDialer(timeout time.Duration, bufferSize int) (Dialer, error) {
64 69
 	switch {
65 70
 	case timeout < 0:

+ 57
- 4
network/init.go Wyświetl plik

@@ -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)

+ 10
- 0
network/load_balanced_socks5.go Wyświetl plik

@@ -32,6 +32,16 @@ func (l loadBalancedSocks5Dialer) DialContext(ctx context.Context, network, addr
32 32
 	return nil, ErrCannotDialWithAllProxies
33 33
 }
34 34
 
35
+// NewLoadBalancedSocks5Dialer builds a new load balancing SOCKS5
36
+// dialer.
37
+//
38
+// The main difference from one which is made by NewSocks5Dialer is that
39
+// we actually have a list of these proxies. When dial is requested,
40
+// any proxy is picked and used. If proxy fails for some reason, we try
41
+// another one.
42
+//
43
+// So, it is mostly useful if you have some routes with proxies which
44
+// are not always online or having buggy network.
35 45
 func NewLoadBalancedSocks5Dialer(baseDialer Dialer, proxyURLs []*url.URL) (Dialer, error) {
36 46
 	dialers := make([]Dialer, 0, len(proxyURLs))
37 47
 

+ 4
- 0
network/network.go Wyświetl plik

@@ -115,6 +115,10 @@ func (n *network) dnsResolve(protocol, address string) ([]string, error) {
115 115
 	return ips, nil
116 116
 }
117 117
 
118
+// NewNetwork assembles an mtglib.Network compatible structure
119
+// based on a dialer and given params.
120
+//
121
+// It brings simple DNS cache and DNS-Over-HTTPS when necessary.
118 122
 func NewNetwork(dialer Dialer,
119 123
 	userAgent, dohHostname string,
120 124
 	httpTimeout time.Duration) (mtglib.Network, error) {

+ 4
- 0
network/socks5.go Wyświetl plik

@@ -7,6 +7,10 @@ import (
7 7
 	"golang.org/x/net/proxy"
8 8
 )
9 9
 
10
+// NewSocks5Dialer build a new dialer from a given one (so, in theory
11
+// you can chain here). Proxy parameters are passed with URI in a form of:
12
+//
13
+//     socks5://[user:[password]]@host:port
10 14
 func NewSocks5Dialer(baseDialer Dialer, proxyURL *url.URL) (Dialer, error) {
11 15
 	rv, err := proxy.FromURL(proxyURL, baseDialer)
12 16
 	if err != nil {

Ładowanie…
Anuluj
Zapisz