Selaa lähdekoodia

Refactor some configuration to proxy_opts

tags/v2.0.0-rc1
9seconds 5 vuotta sitten
vanhempi
commit
4c38ea2b11
2 muutettua tiedostoa jossa 78 lisäystä ja 50 poistoa
  1. 13
    50
      mtglib/proxy.go
  2. 65
    0
      mtglib/proxy_opts.go

+ 13
- 50
mtglib/proxy.go Näytä tiedosto

@@ -262,50 +262,12 @@ func (p *Proxy) doDomainFronting(ctx *streamContext, conn *connRewind) {
262 262
 	}
263 263
 }
264 264
 
265
-func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop, funlen
266
-	switch {
267
-	case opts.Network == nil:
268
-		return nil, ErrNetworkIsNotDefined
269
-	case opts.AntiReplayCache == nil:
270
-		return nil, ErrAntiReplayCacheIsNotDefined
271
-	case opts.IPBlocklist == nil:
272
-		return nil, ErrIPBlocklistIsNotDefined
273
-	case opts.EventStream == nil:
274
-		return nil, ErrEventStreamIsNotDefined
275
-	case opts.TimeAttackDetector == nil:
276
-		return nil, ErrTimeAttackDetectorIsNotDefined
277
-	case opts.Logger == nil:
278
-		return nil, ErrLoggerIsNotDefined
279
-	case !opts.Secret.Valid():
280
-		return nil, ErrSecretInvalid
265
+func NewProxy(opts ProxyOpts) (*Proxy, error) {
266
+	if err := opts.valid(); err != nil {
267
+		return nil, fmt.Errorf("invalid settings: %w", err)
281 268
 	}
282 269
 
283
-	preferIP := opts.PreferIP
284
-	if preferIP == "" {
285
-		preferIP = DefaultPreferIP
286
-	}
287
-
288
-	concurrency := opts.Concurrency
289
-	if concurrency == 0 {
290
-		concurrency = DefaultConcurrency
291
-	}
292
-
293
-	idleTimeout := opts.IdleTimeout
294
-	if idleTimeout < 1 {
295
-		idleTimeout = DefaultIdleTimeout
296
-	}
297
-
298
-	bufferSize := opts.BufferSize
299
-	if bufferSize < 1 {
300
-		bufferSize = DefaultBufferSize
301
-	}
302
-
303
-	domainFrontingPort := int(opts.DomainFrontingPort)
304
-	if domainFrontingPort == 0 {
305
-		domainFrontingPort = DefaultDomainFrontingPort
306
-	}
307
-
308
-	tg, err := telegram.New(opts.Network, preferIP)
270
+	tg, err := telegram.New(opts.Network, opts.getPreferIP())
309 271
 	if err != nil {
310 272
 		return nil, fmt.Errorf("cannot build telegram dialer: %w", err)
311 273
 	}
@@ -320,17 +282,18 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) { // nolint: cyclop, funlen
320 282
 		timeAttackDetector: opts.TimeAttackDetector,
321 283
 		ipBlocklist:        opts.IPBlocklist,
322 284
 		eventStream:        opts.EventStream,
323
-		logger:             opts.Logger.Named("proxy"),
324
-		domainFrontingPort: domainFrontingPort,
325
-		idleTimeout:        idleTimeout,
326
-		bufferSize:         int(bufferSize),
285
+		logger:             opts.getLogger("proxy"),
286
+		domainFrontingPort: opts.getDomainFrontingPort(),
287
+		idleTimeout:        opts.getIdleTimeout(),
288
+		bufferSize:         opts.getBufferSize(),
327 289
 		telegram:           tg,
328 290
 	}
329 291
 
330
-	pool, err := ants.NewPoolWithFunc(int(concurrency), func(arg interface{}) {
331
-		proxy.ServeConn(arg.(net.Conn))
332
-	},
333
-		ants.WithLogger(opts.Logger.Named("ants")),
292
+	pool, err := ants.NewPoolWithFunc(opts.getConcurrency(),
293
+		func(arg interface{}) {
294
+			proxy.ServeConn(arg.(net.Conn))
295
+		},
296
+		ants.WithLogger(opts.getLogger("ants")),
334 297
 		ants.WithNonblocking(true))
335 298
 	if err != nil {
336 299
 		panic(err)

+ 65
- 0
mtglib/proxy_opts.go Näytä tiedosto

@@ -17,3 +17,68 @@ type ProxyOpts struct {
17 17
 	IdleTimeout        time.Duration
18 18
 	PreferIP           string
19 19
 }
20
+
21
+func (p ProxyOpts) valid() error {
22
+	switch {
23
+	case p.Network == nil:
24
+		return ErrNetworkIsNotDefined
25
+	case p.AntiReplayCache == nil:
26
+		return ErrAntiReplayCacheIsNotDefined
27
+	case p.IPBlocklist == nil:
28
+		return ErrIPBlocklistIsNotDefined
29
+	case p.EventStream == nil:
30
+		return ErrEventStreamIsNotDefined
31
+	case p.TimeAttackDetector == nil:
32
+		return ErrTimeAttackDetectorIsNotDefined
33
+	case p.Logger == nil:
34
+		return ErrLoggerIsNotDefined
35
+	case !p.Secret.Valid():
36
+		return ErrSecretInvalid
37
+	}
38
+
39
+	return nil
40
+}
41
+
42
+func (p ProxyOpts) getBufferSize() int {
43
+	if p.BufferSize < 1 {
44
+		return DefaultBufferSize
45
+	}
46
+
47
+	return int(p.BufferSize)
48
+}
49
+
50
+func (p ProxyOpts) getConcurrency() int {
51
+	if p.Concurrency == 0 {
52
+		return DefaultConcurrency
53
+	}
54
+
55
+	return int(p.Concurrency)
56
+}
57
+
58
+func (p ProxyOpts) getDomainFrontingPort() int {
59
+	if p.DomainFrontingPort == 0 {
60
+		return DefaultDomainFrontingPort
61
+	}
62
+
63
+	return int(p.DomainFrontingPort)
64
+}
65
+
66
+func (p ProxyOpts) getIdleTimeout() time.Duration {
67
+	if p.IdleTimeout == 0 {
68
+		return DefaultIdleTimeout
69
+	}
70
+
71
+	return p.IdleTimeout
72
+}
73
+
74
+func (p ProxyOpts) getPreferIP() string {
75
+	if p.PreferIP == "" {
76
+		return DefaultPreferIP
77
+	}
78
+
79
+	return p.PreferIP
80
+}
81
+
82
+func (p ProxyOpts) getLogger(name string) Logger {
83
+	return p.Logger.Named(name)
84
+}

Loading…
Peruuta
Tallenna