Browse Source

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

Domain fronting config grouping
tags/v2.1.11^2^2
Sergei Arkhipov 2 months ago
parent
commit
d7db8ca98b
No account linked to committer's email address
5 changed files with 85 additions and 30 deletions
  1. 33
    10
      example.config.toml
  2. 3
    3
      internal/cli/run_proxy.go
  3. 28
    1
      internal/config/config.go
  4. 6
    1
      internal/config/parse.go
  5. 15
    15
      mtglib/proxy.go

+ 33
- 10
example.config.toml View File

36
 # All other incoming connections are going to be dropped.
36
 # All other incoming connections are going to be dropped.
37
 concurrency = 8192
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
 # Sometimes you want to enforce mtg to use some types of
39
 # Sometimes you want to enforce mtg to use some types of
47
 # IP connectivity to Telegram. We have 4 modes:
40
 # IP connectivity to Telegram. We have 4 modes:
48
 #   - prefer-ipv6:
41
 #   - prefer-ipv6:
57
 
50
 
58
 # FakeTLS uses domain fronting protection. So it needs to know a port to
51
 # FakeTLS uses domain fronting protection. So it needs to know a port to
59
 # access.
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
 # By default, mtg resolves the fronting hostname (from the secret) via DNS
58
 # By default, mtg resolves the fronting hostname (from the secret) via DNS
63
 # to establish a TCP connection. If DNS resolution of that hostname is blocked,
59
 # to establish a TCP connection. If DNS resolution of that hostname is blocked,
65
 # used for SNI in the TLS handshake.
61
 # used for SNI in the TLS handshake.
66
 #
62
 #
67
 # default value is not set (DNS resolution is used).
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
 # This makes a communication between both fronting website and mtg to use
69
 # This makes a communication between both fronting website and mtg to use
71
 # proxy protocol.
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
 # FakeTLS can compare timestamps to prevent probes. Each message has
76
 # FakeTLS can compare timestamps to prevent probes. Each message has
75
 # encrypted timestamp. So, mtg can compare this timestamp and decide if
77
 # encrypted timestamp. So, mtg can compare this timestamp and decide if
92
 # Otherwise, chose a new DC.
94
 # Otherwise, chose a new DC.
93
 allow-fallback-on-unknown-dc = false
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
 # network defines different network-related settings
118
 # network defines different network-related settings
96
 [network]
119
 [network]
97
 # please be aware that mtg needs to do some external requests. For
120
 # please be aware that mtg needs to do some external requests. For

+ 3
- 3
internal/cli/run_proxy.go View File

251
 		EventStream:     eventStream,
251
 		EventStream:     eventStream,
252
 
252
 
253
 		Secret:                      conf.Secret,
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
 		PreferIP:                    conf.PreferIP.Get(mtglib.DefaultPreferIP),
257
 		PreferIP:                    conf.PreferIP.Get(mtglib.DefaultPreferIP),
258
 
258
 
259
 		AllowFallbackOnUnknownDC: conf.AllowFallbackOnUnknownDC.Get(false),
259
 		AllowFallbackOnUnknownDC: conf.AllowFallbackOnUnknownDC.Get(false),

+ 28
- 1
internal/config/config.go View File

4
 	"bytes"
4
 	"bytes"
5
 	"encoding/json"
5
 	"encoding/json"
6
 	"fmt"
6
 	"fmt"
7
+	"net"
7
 
8
 
8
 	"github.com/9seconds/mtg/v2/mtglib"
9
 	"github.com/9seconds/mtg/v2/mtglib"
9
 )
10
 )
32
 	DomainFrontingProxyProtocol TypeBool        `json:"domainFrontingProxyProtocol"`
33
 	DomainFrontingProxyProtocol TypeBool        `json:"domainFrontingProxyProtocol"`
33
 	TolerateTimeSkewness        TypeDuration    `json:"tolerateTimeSkewness"`
34
 	TolerateTimeSkewness        TypeDuration    `json:"tolerateTimeSkewness"`
34
 	Concurrency                 TypeConcurrency `json:"concurrency"`
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
 		AntiReplay struct {
42
 		AntiReplay struct {
37
 			Optional
43
 			Optional
38
 
44
 
69
 	} `json:"stats"`
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
 func (c *Config) Validate() error {
99
 func (c *Config) Validate() error {
73
 	if !c.Secret.Valid() {
100
 	if !c.Secret.Valid() {
74
 		return fmt.Errorf("invalid secret %s", c.Secret.String())
101
 		return fmt.Errorf("invalid secret %s", c.Secret.String())

+ 6
- 1
internal/config/parse.go View File

20
 	DomainFrontingProxyProtocol bool   `toml:"domain-fronting-proxy-protocol" json:"domainFrontingProxyProtocol,omitempty"`
20
 	DomainFrontingProxyProtocol bool   `toml:"domain-fronting-proxy-protocol" json:"domainFrontingProxyProtocol,omitempty"`
21
 	TolerateTimeSkewness        string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
21
 	TolerateTimeSkewness        string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
22
 	Concurrency                 uint   `toml:"concurrency" json:"concurrency,omitempty"`
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
 		AntiReplay struct {
29
 		AntiReplay struct {
25
 			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
30
 			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
26
 			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`
31
 			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`

+ 15
- 15
mtglib/proxy.go View File

31
 	domainFrontingProxyProtocol bool
31
 	domainFrontingProxyProtocol bool
32
 	workerPool                  *ants.PoolWithFunc
32
 	workerPool                  *ants.PoolWithFunc
33
 	telegram                    *dc.Telegram
33
 	telegram                    *dc.Telegram
34
-	configUpdater            *dc.PublicConfigUpdater
35
-	clientObfuscatror        obfuscation.Obfuscator
34
+	configUpdater               *dc.PublicConfigUpdater
35
+	clientObfuscatror           obfuscation.Obfuscator
36
 
36
 
37
 	secret          Secret
37
 	secret          Secret
38
 	network         Network
38
 	network         Network
321
 	updatersLogger := logger.Named("telegram-updaters")
321
 	updatersLogger := logger.Named("telegram-updaters")
322
 
322
 
323
 	proxy := &Proxy{
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
 		logger:                   logger,
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
 		configUpdater: dc.NewPublicConfigUpdater(
338
 		configUpdater: dc.NewPublicConfigUpdater(
339
 			tg,
339
 			tg,
340
 			updatersLogger.Named("public-config"),
340
 			updatersLogger.Named("public-config"),

Loading…
Cancel
Save