Make connection multiplexing configurable

This commit is contained in:
9seconds
2019-11-11 15:03:59 +03:00
parent 34ad883559
commit a7a4da8ee5
3 changed files with 19 additions and 3 deletions
+10
View File
@@ -58,6 +58,8 @@ const (
OptionTypeAntiReplayMaxSize OptionTypeAntiReplayMaxSize
OptionTypeMultiplexPerConnection
OptionTypeSecret OptionTypeSecret
OptionTypeAdtag OptionTypeAdtag
) )
@@ -80,6 +82,8 @@ type Config struct {
AntiReplayMaxSize int64 `json:"anti_replay_max_size"` AntiReplayMaxSize int64 `json:"anti_replay_max_size"`
MultiplexPerConnection int `json:"multiplex_per_connection"`
Debug bool `json:"debug"` Debug bool `json:"debug"`
Verbose bool `json:"verbose"` Verbose bool `json:"verbose"`
StatsdTagsFormat statsd.TagFormat `json:"statsd_tags_format"` StatsdTagsFormat statsd.TagFormat `json:"statsd_tags_format"`
@@ -149,6 +153,8 @@ func Init(options ...Opt) error { // nolint: gocyclo, funlen
C.CloakPort = int(opt.Value.(uint16)) C.CloakPort = int(opt.Value.(uint16))
case OptionTypeAntiReplayMaxSize: case OptionTypeAntiReplayMaxSize:
C.AntiReplayMaxSize = int64(opt.Value.(units.Base2Bytes)) C.AntiReplayMaxSize = int64(opt.Value.(units.Base2Bytes))
case OptionTypeMultiplexPerConnection:
C.MultiplexPerConnection = int(opt.Value.(uint))
case OptionTypeSecret: case OptionTypeSecret:
C.Secret = opt.Value.([]byte) C.Secret = opt.Value.([]byte)
case OptionTypeAdtag: case OptionTypeAdtag:
@@ -173,6 +179,10 @@ func Init(options ...Opt) error { // nolint: gocyclo, funlen
return errors.New("incorrect secret") return errors.New("incorrect secret")
} }
if C.MultiplexPerConnection == 0 {
return errors.New("cannot use 0 clients per connection for multiplexing")
}
if C.CloakHost != "" { if C.CloakHost != "" {
addrs, err := net.LookupHost(C.CloakHost) addrs, err := net.LookupHost(C.CloakHost)
if err != nil { if err != nil {
+3 -3
View File
@@ -3,9 +3,9 @@ package hub
import ( import (
"fmt" "fmt"
"sort" "sort"
)
const connectionListMaxClientsPerConnection = 2 "github.com/9seconds/mtg/config"
)
type connectionList struct { type connectionList struct {
connections []*connection connections []*connection
@@ -16,7 +16,7 @@ func (c *connectionList) Get(conn *ProxyConn) (*connection, error) {
c.gc() c.gc()
} }
if len(c.connections) > 0 && c.connections[0].Len() < connectionListMaxClientsPerConnection { if len(c.connections) > 0 && c.connections[0].Len() < config.C.MultiplexPerConnection {
if err := c.connections[0].Attach(conn); err == nil { if err := c.connections[0].Attach(conn); err == nil {
return c.connections[0], nil return c.connections[0], nil
} }
+6
View File
@@ -108,6 +108,11 @@ var (
Envar("MTG_ANTIREPLAY_MAXSIZE"). Envar("MTG_ANTIREPLAY_MAXSIZE").
Default("128MB"). Default("128MB").
Bytes() Bytes()
runMultiplexPerConnection = runCommand.Flag("multiplex-per-connection",
"How many clients can share a single connection to Telegram.").
Envar("MTG_MULTIPLEX_PERCONNECTION").
Default("50").
Uint()
runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes() runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
runAdtag = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes() runAdtag = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
) )
@@ -141,6 +146,7 @@ func main() {
config.Opt{Option: config.OptionTypeReadBufferSize, Value: *runReadBufferSize}, config.Opt{Option: config.OptionTypeReadBufferSize, Value: *runReadBufferSize},
config.Opt{Option: config.OptionTypeCloakPort, Value: *runTLSCloakPort}, config.Opt{Option: config.OptionTypeCloakPort, Value: *runTLSCloakPort},
config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *runAntiReplayMaxSize}, config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *runAntiReplayMaxSize},
config.Opt{Option: config.OptionTypeMultiplexPerConnection, Value: *runMultiplexPerConnection},
config.Opt{Option: config.OptionTypeSecret, Value: *runSecret}, config.Opt{Option: config.OptionTypeSecret, Value: *runSecret},
config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag}, config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag},
) )