Explorar el Código

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

tags/v2.1.2^2
9seconds hace 4 años
padre
commit
cd29f3e20b

+ 13
- 0
example.config.toml Ver fichero

56
 # time range of this parameter.
56
 # time range of this parameter.
57
 tolerate-time-skewness = "5s"
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
 # network defines different network-related settings
72
 # network defines different network-related settings
60
 [network]
73
 [network]
61
 # please be aware that mtg needs to do some external requests. For
74
 # please be aware that mtg needs to do some external requests. For

+ 2
- 0
internal/cli/run_proxy.go Ver fichero

184
 		BufferSize:         conf.TCPBuffer.Get(mtglib.DefaultBufferSize),
184
 		BufferSize:         conf.TCPBuffer.Get(mtglib.DefaultBufferSize),
185
 		DomainFrontingPort: conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
185
 		DomainFrontingPort: conf.DomainFrontingPort.Get(mtglib.DefaultDomainFrontingPort),
186
 		PreferIP:           conf.PreferIP.Get(mtglib.DefaultPreferIP),
186
 		PreferIP:           conf.PreferIP.Get(mtglib.DefaultPreferIP),
187
+
188
+		AllowFallbackOnUnknownDC: conf.AllowFallbackOnUnknownDC.Get(false),
187
 	}
189
 	}
188
 
190
 
189
 	proxy, err := mtglib.NewProxy(opts)
191
 	proxy, err := mtglib.NewProxy(opts)

+ 1
- 0
internal/cli/simple_run.go Ver fichero

71
 	}
71
 	}
72
 
72
 
73
 	conf.Debug.Value = s.Debug
73
 	conf.Debug.Value = s.Debug
74
+	conf.AllowFallbackOnUnknownDC.Value = true
74
 	conf.Defense.AntiReplay.Enabled.Value = true
75
 	conf.Defense.AntiReplay.Enabled.Value = true
75
 
76
 
76
 	if err := conf.Validate(); err != nil {
77
 	if err := conf.Validate(); err != nil {

+ 10
- 9
internal/config/config.go Ver fichero

9
 )
9
 )
10
 
10
 
11
 type Config struct {
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
 		AntiReplay struct {
22
 		AntiReplay struct {
22
 			Enabled   TypeBool      `json:"enabled"`
23
 			Enabled   TypeBool      `json:"enabled"`
23
 			MaxSize   TypeBytes     `json:"maxSize"`
24
 			MaxSize   TypeBytes     `json:"maxSize"`

+ 10
- 9
internal/config/parse.go Ver fichero

9
 )
9
 )
10
 
10
 
11
 type tomlConfig struct {
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
 		AntiReplay struct {
22
 		AntiReplay struct {
22
 			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
23
 			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
23
 			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`
24
 			MaxSize   string  `toml:"max-size" json:"maxSize,omitempty"`

+ 20
- 18
mtglib/proxy.go Ver fichero

23
 	ctxCancel       context.CancelFunc
23
 	ctxCancel       context.CancelFunc
24
 	streamWaitGroup sync.WaitGroup
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
 	secret          Secret
33
 	secret          Secret
33
 	network         Network
34
 	network         Network
209
 func (p *Proxy) doTelegramCall(ctx *streamContext) error {
210
 func (p *Proxy) doTelegramCall(ctx *streamContext) error {
210
 	dc := ctx.dc
211
 	dc := ctx.dc
211
 
212
 
212
-	if !p.telegram.IsKnownDC(dc) {
213
+	if p.allowFallbackOnUnknownDC && !p.telegram.IsKnownDC(dc) {
213
 		dc = p.telegram.GetFallbackDC()
214
 		dc = p.telegram.GetFallbackDC()
214
 		ctx.logger = ctx.logger.BindInt("fallback_dc", dc)
215
 		ctx.logger = ctx.logger.BindInt("fallback_dc", dc)
215
 
216
 
285
 
286
 
286
 	ctx, cancel := context.WithCancel(context.Background())
287
 	ctx, cancel := context.WithCancel(context.Background())
287
 	proxy := &Proxy{
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
 	pool, err := ants.NewPoolWithFunc(opts.getConcurrency(),
304
 	pool, err := ants.NewPoolWithFunc(opts.getConcurrency(),

+ 10
- 0
mtglib/proxy_opts.go Ver fichero

90
 	// This is an optional setting.
90
 	// This is an optional setting.
91
 	DomainFrontingPort uint
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
 	// UseTestDCs defines if we have to connect to production or to staging
103
 	// UseTestDCs defines if we have to connect to production or to staging
94
 	// DCs of Telegram.
104
 	// DCs of Telegram.
95
 	//
105
 	//

Loading…
Cancelar
Guardar