Bläddra i källkod

Add documentation for the objects of mtglib

tags/v2.0.0-rc1
9seconds 5 år sedan
förälder
incheckning
40c6cf294a
2 ändrade filer med 93 tillägg och 17 borttagningar
  1. 14
    7
      mtglib/proxy.go
  2. 79
    10
      mtglib/proxy_opts.go

+ 14
- 7
mtglib/proxy.go Visa fil

@@ -17,6 +17,7 @@ import (
17 17
 	"github.com/panjf2000/ants/v2"
18 18
 )
19 19
 
20
+// Proxy is an MTPROTO proxy structure.
20 21
 type Proxy struct {
21 22
 	ctx             context.Context
22 23
 	ctxCancel       context.CancelFunc
@@ -37,10 +38,13 @@ type Proxy struct {
37 38
 	logger             Logger
38 39
 }
39 40
 
41
+// DomainFrontingAddress returns a host:port pair for a fronting domain.
40 42
 func (p *Proxy) DomainFrontingAddress() string {
41 43
 	return net.JoinHostPort(p.secret.Host, strconv.Itoa(p.domainFrontingPort))
42 44
 }
43 45
 
46
+// ServeConn serves a connection. We do not check IP blocklist and
47
+// concurrency limit here.
44 48
 func (p *Proxy) ServeConn(conn net.Conn) {
45 49
 	p.streamWaitGroup.Add(1)
46 50
 	defer p.streamWaitGroup.Done()
@@ -86,6 +90,7 @@ func (p *Proxy) ServeConn(conn net.Conn) {
86 90
 	}
87 91
 }
88 92
 
93
+// Serve starts a proxy on a given listener.
89 94
 func (p *Proxy) Serve(listener net.Listener) error {
90 95
 	p.streamWaitGroup.Add(1)
91 96
 	defer p.streamWaitGroup.Done()
@@ -93,7 +98,12 @@ func (p *Proxy) Serve(listener net.Listener) error {
93 98
 	for {
94 99
 		conn, err := listener.Accept()
95 100
 		if err != nil {
96
-			return fmt.Errorf("cannot accept a new connection: %w", err)
101
+			select {
102
+			case <-p.ctx.Done():
103
+				return nil
104
+			default:
105
+				return fmt.Errorf("cannot accept a new connection: %w", err)
106
+			}
97 107
 		}
98 108
 
99 109
 		ipAddr := conn.RemoteAddr().(*net.TCPAddr).IP
@@ -117,15 +127,11 @@ func (p *Proxy) Serve(listener net.Listener) error {
117 127
 			logger.Info("connection was concurrency limited")
118 128
 			p.eventStream.Send(p.ctx, NewEventConcurrencyLimited())
119 129
 		}
120
-
121
-		select {
122
-		case <-p.ctx.Done():
123
-			return p.ctx.Err()
124
-		default:
125
-		}
126 130
 	}
127 131
 }
128 132
 
133
+// Shutdown 'gracefully' shutdowns all connections. Please remember that
134
+// it does not close an underlying listener.
129 135
 func (p *Proxy) Shutdown() {
130 136
 	p.ctxCancel()
131 137
 	p.streamWaitGroup.Wait()
@@ -262,6 +268,7 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
262 268
 	}
263 269
 }
264 270
 
271
+// NewProxy makes a new proxy instance.
265 272
 func NewProxy(opts ProxyOpts) (*Proxy, error) {
266 273
 	if err := opts.valid(); err != nil {
267 274
 		return nil, fmt.Errorf("invalid settings: %w", err)

+ 79
- 10
mtglib/proxy_opts.go Visa fil

@@ -2,20 +2,89 @@ package mtglib
2 2
 
3 3
 import "time"
4 4
 
5
+// ProxyOpts is a structure with settings to mtg proxy.
6
+//
7
+// This is not required per se, but this is to shorten function
8
+// signature and give an ability to conveniently provide default values.
5 9
 type ProxyOpts struct {
6
-	Secret             Secret
7
-	Network            Network
8
-	AntiReplayCache    AntiReplayCache
10
+	// Secret defines a secret which should be used by a proxy.
11
+	//
12
+	// This is a mandatory setting.
13
+	Secret Secret
14
+
15
+	// Network defines a network instance which should be used for all
16
+	// network communications made by proxies.
17
+	//
18
+	// This is a mandatory setting.
19
+	Network Network
20
+
21
+	// AntiReplayCache defines an instance of antireplay cache.
22
+	//
23
+	// This is a mandatory setting.
24
+	AntiReplayCache AntiReplayCache
25
+
26
+	// TimeAttackDetector defines an instance of timeattack detector.
27
+	//
28
+	// This is a mandatory setting.
9 29
 	TimeAttackDetector TimeAttackDetector
10
-	IPBlocklist        IPBlocklist
11
-	EventStream        EventStream
12
-	Logger             Logger
13 30
 
14
-	BufferSize         uint
15
-	Concurrency        uint
31
+	// IPBlocklist defines an instance of IP blocklist.
32
+	//
33
+	// This is a mandatory setting.
34
+	IPBlocklist IPBlocklist
35
+
36
+	// EventStream defines an instance of event stream.
37
+	//
38
+	// This ia a mandatory setting.
39
+	EventStream EventStream
40
+
41
+	// Logger defines an instance of the logger.
42
+	//
43
+	// This is a mandatory setting.
44
+	Logger Logger
45
+
46
+	// BufferSize is a size of the copy buffer in bytes.
47
+	//
48
+	// Please remember that we multiply this number in 2, because when
49
+	// we relay between proxies, we have to create 2 intermediate
50
+	// buffers: to and from.
51
+	//
52
+	// This is an optional setting.
53
+	BufferSize uint
54
+
55
+	// Concurrency is a size of the worker pool for connection management.
56
+	//
57
+	// If we have more connections than this number, they are going to be
58
+	// rejected.
59
+	//
60
+	// This is an optional setting.
61
+	Concurrency uint
62
+
63
+	// DomainFrontingPort is a port we use to connect to a fronting
64
+	// domain.
65
+	//
66
+	// This is required because secret does not specify a port. It
67
+	// specifies a hostname only.
68
+	//
69
+	// This is an optional setting.
16 70
 	DomainFrontingPort uint
17
-	IdleTimeout        time.Duration
18
-	PreferIP           string
71
+
72
+	// IdleTimeout is a timeout for relay when we have to break a
73
+	// stream.
74
+	//
75
+	// This is a timeout for any activity. So, if we have any message
76
+	// which will pass to either direction, a timer is reset. If we have
77
+	// no any reads or writes for this timeout, a connection will be
78
+	// aborted.
79
+	//
80
+	// This is an optional setting.
81
+	IdleTimeout time.Duration
82
+
83
+	// PreferIP defines an IP connectivity preference. Valid values are:
84
+	// 'prefer-ipv4', 'prefer-ipv6', 'only-ipv4', 'only-ipv6'.
85
+	//
86
+	// This is an optional setting.
87
+	PreferIP string
19 88
 }
20 89
 
21 90
 func (p ProxyOpts) valid() error {

Laddar…
Avbryt
Spara