Browse Source

Add configuration option allow-fallback-on-unknown-dc

tags/v2.1.2^2
9seconds 4 years ago
parent
commit
cd29f3e20b

+ 13
- 0
example.config.toml View File

@@ -56,6 +56,19 @@ domain-fronting-port = 443
56 56
 # time range of this parameter.
57 57
 tolerate-time-skewness = "5s"
58 58
 
59
+# Telegram has a concept of DC. You can think about DC as a number of a cluster
60
+# with a certain purpose. Some clusters serve media, some - messages, some rule
61
+# channels and so on. But sometimes unknown DC number is requested by client.
62
+# It could be a bug or some global reconfiguration of the Telegram.
63
+#
64
+# By default, proxy rejects such requests. But it is also possible to fallback
65
+# this request to any DC. Telegram works in a way that any DC is able to serve
66
+# any request but sacrificing a latency.
67
+#
68
+# If this setting is disabled (default), mtg will reject a connection.
69
+# Otherwise, chose a new DC.
70
+allow-fallback-on-unknown-dc = false
71
+
59 72
 # network defines different network-related settings
60 73
 [network]
61 74
 # please be aware that mtg needs to do some external requests. For

+ 2
- 0
internal/cli/run_proxy.go View File

@@ -184,6 +184,8 @@ func runProxy(conf *config.Config, version string) error {
184 184
 		BufferSize:         conf.TCPBuffer.Get(mtglib.DefaultBufferSize),
185 185
 		DomainFrontingPort: conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
186 186
 		PreferIP:           conf.PreferIP.Get(mtglib.DefaultPreferIP),
187
+
188
+		AllowFallbackOnUnknownDC: conf.AllowFallbackOnUnknownDC.Get(false),
187 189
 	}
188 190
 
189 191
 	proxy, err := mtglib.NewProxy(opts)

+ 1
- 0
internal/cli/simple_run.go View File

@@ -71,6 +71,7 @@ func (s *SimpleRun) Run(cli *CLI, version string) error { // nolint: cyclop
71 71
 	}
72 72
 
73 73
 	conf.Debug.Value = s.Debug
74
+	conf.AllowFallbackOnUnknownDC.Value = true
74 75
 	conf.Defense.AntiReplay.Enabled.Value = true
75 76
 
76 77
 	if err := conf.Validate(); err != nil {

+ 10
- 9
internal/config/config.go View File

@@ -9,15 +9,16 @@ import (
9 9
 )
10 10
 
11 11
 type Config struct {
12
-	Debug                TypeBool        `json:"debug"`
13
-	Secret               mtglib.Secret   `json:"secret"`
14
-	BindTo               TypeHostPort    `json:"bindTo"`
15
-	TCPBuffer            TypeBytes       `json:"tcpBuffer"`
16
-	PreferIP             TypePreferIP    `json:"preferIp"`
17
-	DomainFrontingPort   TypePort        `json:"domainFrontingPort"`
18
-	TolerateTimeSkewness TypeDuration    `json:"tolerateTimeSkewness"`
19
-	Concurrency          TypeConcurrency `json:"concurrency"`
20
-	Defense              struct {
12
+	Debug                    TypeBool        `json:"debug"`
13
+	AllowFallbackOnUnknownDC TypeBool        `json:"allowFallbackOnUnknownDc"`
14
+	Secret                   mtglib.Secret   `json:"secret"`
15
+	BindTo                   TypeHostPort    `json:"bindTo"`
16
+	TCPBuffer                TypeBytes       `json:"tcpBuffer"`
17
+	PreferIP                 TypePreferIP    `json:"preferIp"`
18
+	DomainFrontingPort       TypePort        `json:"domainFrontingPort"`
19
+	TolerateTimeSkewness     TypeDuration    `json:"tolerateTimeSkewness"`
20
+	Concurrency              TypeConcurrency `json:"concurrency"`
21
+	Defense                  struct {
21 22
 		AntiReplay struct {
22 23
 			Enabled   TypeBool      `json:"enabled"`
23 24
 			MaxSize   TypeBytes     `json:"maxSize"`

+ 10
- 9
internal/config/parse.go View File

@@ -9,15 +9,16 @@ import (
9 9
 )
10 10
 
11 11
 type tomlConfig struct {
12
-	Debug                bool   `toml:"debug" json:"debug,omitempty"`
13
-	Secret               string `toml:"secret" json:"secret"`
14
-	BindTo               string `toml:"bind-to" json:"bindTo"`
15
-	TCPBuffer            string `toml:"tcp-buffer" json:"tcpBuffer,omitempty"`
16
-	PreferIP             string `toml:"prefer-ip" json:"preferIp,omitempty"`
17
-	DomainFrontingPort   uint   `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
18
-	TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
19
-	Concurrency          uint   `toml:"concurrency" json:"concurrency,omitempty"`
20
-	Defense              struct {
12
+	Debug                    bool   `toml:"debug" json:"debug,omitempty"`
13
+	AllowFallbackOnUnknownDC bool   `toml:"allow-fallback-on-unknown-dc" json:"allowFallbackOnUnknownDc,omitempty"`
14
+	Secret                   string `toml:"secret" json:"secret"`
15
+	BindTo                   string `toml:"bind-to" json:"bindTo"`
16
+	TCPBuffer                string `toml:"tcp-buffer" json:"tcpBuffer,omitempty"`
17
+	PreferIP                 string `toml:"prefer-ip" json:"preferIp,omitempty"`
18
+	DomainFrontingPort       uint   `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
19
+	TolerateTimeSkewness     string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
20
+	Concurrency              uint   `toml:"concurrency" json:"concurrency,omitempty"`
21
+	Defense                  struct {
21 22
 		AntiReplay struct {
22 23
 			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
23 24
 			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`

+ 20
- 18
mtglib/proxy.go View File

@@ -23,11 +23,12 @@ type Proxy struct {
23 23
 	ctxCancel       context.CancelFunc
24 24
 	streamWaitGroup sync.WaitGroup
25 25
 
26
-	tolerateTimeSkewness time.Duration
27
-	bufferSize           int
28
-	domainFrontingPort   int
29
-	workerPool           *ants.PoolWithFunc
30
-	telegram             *telegram.Telegram
26
+	allowFallbackOnUnknownDC bool
27
+	tolerateTimeSkewness     time.Duration
28
+	bufferSize               int
29
+	domainFrontingPort       int
30
+	workerPool               *ants.PoolWithFunc
31
+	telegram                 *telegram.Telegram
31 32
 
32 33
 	secret          Secret
33 34
 	network         Network
@@ -209,7 +210,7 @@ func (p *Proxy) doObfuscated2Handshake(ctx *streamContext) error {
209 210
 func (p *Proxy) doTelegramCall(ctx *streamContext) error {
210 211
 	dc := ctx.dc
211 212
 
212
-	if !p.telegram.IsKnownDC(dc) {
213
+	if p.allowFallbackOnUnknownDC && !p.telegram.IsKnownDC(dc) {
213 214
 		dc = p.telegram.GetFallbackDC()
214 215
 		ctx.logger = ctx.logger.BindInt("fallback_dc", dc)
215 216
 
@@ -285,18 +286,19 @@ func NewProxy(opts ProxyOpts) (*Proxy, error) {
285 286
 
286 287
 	ctx, cancel := context.WithCancel(context.Background())
287 288
 	proxy := &Proxy{
288
-		ctx:                  ctx,
289
-		ctxCancel:            cancel,
290
-		secret:               opts.Secret,
291
-		network:              opts.Network,
292
-		antiReplayCache:      opts.AntiReplayCache,
293
-		ipBlocklist:          opts.IPBlocklist,
294
-		eventStream:          opts.EventStream,
295
-		logger:               opts.getLogger("proxy"),
296
-		domainFrontingPort:   opts.getDomainFrontingPort(),
297
-		tolerateTimeSkewness: opts.getTolerateTimeSkewness(),
298
-		bufferSize:           opts.getBufferSize(),
299
-		telegram:             tg,
289
+		ctx:                      ctx,
290
+		ctxCancel:                cancel,
291
+		secret:                   opts.Secret,
292
+		network:                  opts.Network,
293
+		antiReplayCache:          opts.AntiReplayCache,
294
+		ipBlocklist:              opts.IPBlocklist,
295
+		eventStream:              opts.EventStream,
296
+		logger:                   opts.getLogger("proxy"),
297
+		domainFrontingPort:       opts.getDomainFrontingPort(),
298
+		tolerateTimeSkewness:     opts.getTolerateTimeSkewness(),
299
+		bufferSize:               opts.getBufferSize(),
300
+		allowFallbackOnUnknownDC: opts.AllowFallbackOnUnknownDC,
301
+		telegram:                 tg,
300 302
 	}
301 303
 
302 304
 	pool, err := ants.NewPoolWithFunc(opts.getConcurrency(),

+ 10
- 0
mtglib/proxy_opts.go View File

@@ -90,6 +90,16 @@ type ProxyOpts struct {
90 90
 	// This is an optional setting.
91 91
 	DomainFrontingPort uint
92 92
 
93
+	// AllowFallbackOnUnknownDC defines how proxy behaves if unknown DC was
94
+	// requested. If this setting is set to false, then such connection
95
+	// will be rejected. Otherwise, proxy will chose any DC.
96
+	//
97
+	// Telegram is designed in a way that any DC can serve any request,
98
+	// the problem is a latency.
99
+	//
100
+	// This is an optional setting.
101
+	AllowFallbackOnUnknownDC bool
102
+
93 103
 	// UseTestDCs defines if we have to connect to production or to staging
94 104
 	// DCs of Telegram.
95 105
 	//

Loading…
Cancel
Save