Kaynağa Gözat

Merge pull request #339 from 9seconds/domain-fronting-config-grouping

Domain fronting config grouping
tags/v2.1.11^2^2
Sergei Arkhipov 2 ay önce
ebeveyn
işleme
d7db8ca98b
No account linked to committer's email address

+ 33
- 10
example.config.toml Dosyayı Görüntüle

@@ -36,13 +36,6 @@ bind-to = "0.0.0.0:3128"
36 36
 # All other incoming connections are going to be dropped.
37 37
 concurrency = 8192
38 38
 
39
-# A size of user-space buffer for TCP to use. Since we do 2 connections,
40
-# then we have tcp-buffer * (4 + 2) per each connection: read/write for
41
-# each connection + 2 copy buffers to pump the data between sockets.
42
-#
43
-# Deprecated: this setting is no longer makes any effect.
44
-# tcp-buffer = "4kb"
45
-
46 39
 # Sometimes you want to enforce mtg to use some types of
47 40
 # IP connectivity to Telegram. We have 4 modes:
48 41
 #   - prefer-ipv6:
@@ -57,7 +50,10 @@ prefer-ip = "prefer-ipv6"
57 50
 
58 51
 # FakeTLS uses domain fronting protection. So it needs to know a port to
59 52
 # access.
60
-domain-fronting-port = 443
53
+#
54
+# Deprecated: use [domain-fronting] configuration block. If relevant option
55
+# is defined there, this one would be ignored.
56
+# domain-fronting-port = 443
61 57
 
62 58
 # By default, mtg resolves the fronting hostname (from the secret) via DNS
63 59
 # to establish a TCP connection. If DNS resolution of that hostname is blocked,
@@ -65,11 +61,17 @@ domain-fronting-port = 443
65 61
 # used for SNI in the TLS handshake.
66 62
 #
67 63
 # default value is not set (DNS resolution is used).
68
-# domain-fronting-ip = "142.250.185.112"
64
+#
65
+# Deprecated: use [domain-fronting] configuration block. If relevant option
66
+# is defined there, this one would be ignored.
67
+# domain-fronting-ip = "10.0.0.10"
69 68
 
70 69
 # This makes a communication between both fronting website and mtg to use
71 70
 # proxy protocol.
72
-domain-fronting-proxy-protocol = false
71
+#
72
+# Deprecated: use [domain-fronting] configuration block. If relevant option
73
+# is defined there, this one would be ignored.
74
+# domain-fronting-proxy-protocol = false
73 75
 
74 76
 # FakeTLS can compare timestamps to prevent probes. Each message has
75 77
 # encrypted timestamp. So, mtg can compare this timestamp and decide if
@@ -92,6 +94,27 @@ tolerate-time-skewness = "5s"
92 94
 # Otherwise, chose a new DC.
93 95
 allow-fallback-on-unknown-dc = false
94 96
 
97
+# This section is relevant to communication with fronting domain. Usually
98
+# you do not need to setup anything here but there are plenty of cases, especially
99
+# if you put mtg behind load balancer, when some specific configuration is
100
+# required.
101
+[domain-fronting]
102
+# By default, mtg resolves the fronting hostname (from the secret) via DNS
103
+# to establish a TCP connection. If DNS resolution of that hostname is blocked,
104
+# you can specify an IP address to connect to directly. The hostname is still
105
+# used for SNI in the TLS handshake.
106
+#
107
+# default value is not set (DNS resolution is used).
108
+# ip = "10.10.10.11"
109
+
110
+# FakeTLS uses domain fronting protection. So it needs to know a port to
111
+# access. Default value is 443
112
+# port = 443
113
+
114
+# This makes a communication between both fronting website and mtg to use
115
+# proxy protocol.
116
+# proxy-protocol = false
117
+
95 118
 # network defines different network-related settings
96 119
 [network]
97 120
 # please be aware that mtg needs to do some external requests. For

+ 3
- 3
internal/cli/run_proxy.go Dosyayı Görüntüle

@@ -251,9 +251,9 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen
251 251
 		EventStream:     eventStream,
252 252
 
253 253
 		Secret:                      conf.Secret,
254
-		DomainFrontingPort:          conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
255
-		DomainFrontingIP:            conf.DomainFrontingIP.String(),
256
-		DomainFrontingProxyProtocol: conf.DomainFrontingProxyProtocol.Get(false),
254
+		DomainFrontingPort:          conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort),
255
+		DomainFrontingIP:            conf.GetDomainFrontingIP(nil),
256
+		DomainFrontingProxyProtocol: conf.GetDomainFrontingProxyProtocol(false),
257 257
 		PreferIP:                    conf.PreferIP.Get(mtglib.DefaultPreferIP),
258 258
 
259 259
 		AllowFallbackOnUnknownDC: conf.AllowFallbackOnUnknownDC.Get(false),

+ 28
- 1
internal/config/config.go Dosyayı Görüntüle

@@ -4,6 +4,7 @@ import (
4 4
 	"bytes"
5 5
 	"encoding/json"
6 6
 	"fmt"
7
+	"net"
7 8
 
8 9
 	"github.com/9seconds/mtg/v2/mtglib"
9 10
 )
@@ -32,7 +33,12 @@ type Config struct {
32 33
 	DomainFrontingProxyProtocol TypeBool        `json:"domainFrontingProxyProtocol"`
33 34
 	TolerateTimeSkewness        TypeDuration    `json:"tolerateTimeSkewness"`
34 35
 	Concurrency                 TypeConcurrency `json:"concurrency"`
35
-	Defense                     struct {
36
+	DomainFronting              struct {
37
+		IP            TypeIP   `json:"ip"`
38
+		Port          TypePort `json:"port"`
39
+		ProxyProtocol TypeBool `json:"proxyProtocol"`
40
+	} `json:"domainFronting"`
41
+	Defense struct {
36 42
 		AntiReplay struct {
37 43
 			Optional
38 44
 
@@ -69,6 +75,27 @@ type Config struct {
69 75
 	} `json:"stats"`
70 76
 }
71 77
 
78
+func (c *Config) GetDomainFrontingPort(defaultValue uint) uint {
79
+	if port := c.DomainFronting.Port.Get(0); port != 0 {
80
+		return port
81
+	}
82
+	return c.DomainFrontingPort.Get(defaultValue)
83
+}
84
+
85
+func (c *Config) GetDomainFrontingIP(defaultValue net.IP) string {
86
+	if ip := c.DomainFronting.IP.Get(nil); ip != nil {
87
+		return ip.String()
88
+	}
89
+	if ip := c.DomainFrontingIP.Get(defaultValue); ip != nil {
90
+		return ip.String()
91
+	}
92
+	return ""
93
+}
94
+
95
+func (c *Config) GetDomainFrontingProxyProtocol(defaultValue bool) bool {
96
+	return c.DomainFronting.ProxyProtocol.Get(false) || c.DomainFrontingProxyProtocol.Get(defaultValue)
97
+}
98
+
72 99
 func (c *Config) Validate() error {
73 100
 	if !c.Secret.Valid() {
74 101
 		return fmt.Errorf("invalid secret %s", c.Secret.String())

+ 6
- 1
internal/config/parse.go Dosyayı Görüntüle

@@ -20,7 +20,12 @@ type tomlConfig struct {
20 20
 	DomainFrontingProxyProtocol bool   `toml:"domain-fronting-proxy-protocol" json:"domainFrontingProxyProtocol,omitempty"`
21 21
 	TolerateTimeSkewness        string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
22 22
 	Concurrency                 uint   `toml:"concurrency" json:"concurrency,omitempty"`
23
-	Defense                     struct {
23
+	DomainFronting              struct {
24
+		IP            string `toml:"ip" json:"ip,omitempty"`
25
+		Port          uint   `toml:"port" json:"port,omitempty"`
26
+		ProxyProtocol bool   `toml:"proxy-protocol" json:"proxyProtocol,omitempty"`
27
+	} `toml:"domain-fronting" json:"domainFronting,omitempty"`
28
+	Defense struct {
24 29
 		AntiReplay struct {
25 30
 			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
26 31
 			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`

+ 15
- 15
mtglib/proxy.go Dosyayı Görüntüle

@@ -31,8 +31,8 @@ type Proxy struct {
31 31
 	domainFrontingProxyProtocol bool
32 32
 	workerPool                  *ants.PoolWithFunc
33 33
 	telegram                    *dc.Telegram
34
-	configUpdater            *dc.PublicConfigUpdater
35
-	clientObfuscatror        obfuscation.Obfuscator
34
+	configUpdater               *dc.PublicConfigUpdater
35
+	clientObfuscatror           obfuscation.Obfuscator
36 36
 
37 37
 	secret          Secret
38 38
 	network         Network
@@ -321,20 +321,20 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) {
321 321
 	updatersLogger := logger.Named("telegram-updaters")
322 322
 
323 323
 	proxy := &Proxy{
324
-		ctx:                         ctx,
325
-		ctxCancel:                   cancel,
326
-		secret:                      opts.Secret,
327
-		network:                     opts.Network,
328
-		antiReplayCache:             opts.AntiReplayCache,
329
-		blocklist:                   opts.IPBlocklist,
330
-		allowlist:                   opts.IPAllowlist,
331
-		eventStream:                 opts.EventStream,
324
+		ctx:                      ctx,
325
+		ctxCancel:                cancel,
326
+		secret:                   opts.Secret,
327
+		network:                  opts.Network,
328
+		antiReplayCache:          opts.AntiReplayCache,
329
+		blocklist:                opts.IPBlocklist,
330
+		allowlist:                opts.IPAllowlist,
331
+		eventStream:              opts.EventStream,
332 332
 		logger:                   logger,
333
-		domainFrontingPort:          opts.getDomainFrontingPort(),
334
-		domainFrontingIP:            opts.DomainFrontingIP,
335
-		tolerateTimeSkewness:        opts.getTolerateTimeSkewness(),
336
-		allowFallbackOnUnknownDC:    opts.AllowFallbackOnUnknownDC,
337
-		telegram:                    tg,
333
+		domainFrontingPort:       opts.getDomainFrontingPort(),
334
+		domainFrontingIP:         opts.DomainFrontingIP,
335
+		tolerateTimeSkewness:     opts.getTolerateTimeSkewness(),
336
+		allowFallbackOnUnknownDC: opts.AllowFallbackOnUnknownDC,
337
+		telegram:                 tg,
338 338
 		configUpdater: dc.NewPublicConfigUpdater(
339 339
 			tg,
340 340
 			updatersLogger.Named("public-config"),

Loading…
İptal
Kaydet