Просмотр исходного кода

Introduce [domain-fronting] config

tags/v2.1.11^2^2
9seconds 2 месяцев назад
Родитель
Сommit
1cb225f52c
5 измененных файлов: 85 добавлений и 23 удалений
  1. 33
    3
      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
- 3
example.config.toml Просмотреть файл

50
 
50
 
51
 # 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
52
 # access.
52
 # access.
53
-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
54
 
57
 
55
 # 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
56
 # 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,
58
 # used for SNI in the TLS handshake.
61
 # used for SNI in the TLS handshake.
59
 #
62
 #
60
 # default value is not set (DNS resolution is used).
63
 # default value is not set (DNS resolution is used).
61
-# 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"
62
 
68
 
63
 # 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
64
 # proxy protocol.
70
 # proxy protocol.
65
-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
66
 
75
 
67
 # FakeTLS can compare timestamps to prevent probes. Each message has
76
 # FakeTLS can compare timestamps to prevent probes. Each message has
68
 # encrypted timestamp. So, mtg can compare this timestamp and decide if
77
 # encrypted timestamp. So, mtg can compare this timestamp and decide if
85
 # Otherwise, chose a new DC.
94
 # Otherwise, chose a new DC.
86
 allow-fallback-on-unknown-dc = false
95
 allow-fallback-on-unknown-dc = false
87
 
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
+
88
 # network defines different network-related settings
118
 # network defines different network-related settings
89
 [network]
119
 [network]
90
 # 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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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 Просмотреть файл

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"),

Загрузка…
Отмена
Сохранить