Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package main
  2. import (
  3. "math/rand"
  4. "os"
  5. "time"
  6. kingpin "gopkg.in/alecthomas/kingpin.v2"
  7. "github.com/9seconds/mtg/cli"
  8. "github.com/9seconds/mtg/config"
  9. "github.com/9seconds/mtg/utils"
  10. )
  11. var version = "dev" // this has to be set by build ld flags
  12. var (
  13. app = kingpin.New("mtg", "Simple MTPROTO proxy.")
  14. generateSecretCommand = app.Command("generate-secret",
  15. "Generate new secret")
  16. generateCloakHost = generateSecretCommand.Flag("cloak-host",
  17. "A host to use for TLS cloaking.").
  18. Short('c').
  19. Default("storage.googleapis.com").
  20. String()
  21. generateSecretType = generateSecretCommand.Arg("type",
  22. "A type of secret to generate. Valid options are 'simple', 'secured' and 'tls'").
  23. Required().
  24. Enum("simple", "secured", "tls")
  25. runCommand = app.Command("run",
  26. "Run new proxy instance")
  27. runDebug = runCommand.Flag("debug",
  28. "Run in debug mode.").
  29. Short('d').
  30. Envar("MTG_DEBUG").
  31. Bool()
  32. runVerbose = runCommand.Flag("verbose",
  33. "Run in verbose mode.").
  34. Short('v').
  35. Envar("MTG_VERBOSE").
  36. Bool()
  37. runBind = runCommand.Flag("bind",
  38. "Host:Port to bind proxy to.").
  39. Short('b').
  40. Envar("MTG_BIND").
  41. Default("0.0.0.0:3128").
  42. TCP()
  43. runPublicIPv4 = runCommand.Flag("public-ipv4",
  44. "Which IPv4 host:port to use.").
  45. Short('4').
  46. Envar("MTG_IPV4").
  47. TCP()
  48. runPublicIPv6 = runCommand.Flag("public-ipv6",
  49. "Which IPv6 host:port to use.").
  50. Short('6').
  51. Envar("MTG_IPV6").
  52. TCP()
  53. runStatsBind = runCommand.Flag("stats-bind",
  54. "Which Host:Port to bind stats server to.").
  55. Short('t').
  56. Envar("MTG_STATS_BIND").
  57. Default("127.0.0.1:3129").
  58. TCP()
  59. runStatsNamespace = runCommand.Flag("stats-namespace",
  60. "Which namespace to use for Prometheus.").
  61. Envar("MTG_STATS_NAMESPACE").
  62. Default("mtg").
  63. String()
  64. runStatsdAddress = runCommand.Flag("statsd-addr",
  65. "Host:port of statsd server").
  66. Envar("MTG_STATSD_ADDR").
  67. TCP()
  68. runStatsdNetwork = runCommand.Flag("statsd-network",
  69. "Which network is used to work with statsd. Only 'tcp' and 'udp' are supported.").
  70. Envar("MTG_STATSD_NETWORK").
  71. Default("udp").
  72. Enum("udp", "tcp")
  73. runStatsdTagsFormat = runCommand.Flag("statsd-tags-format",
  74. "Which tag format should we use to send stats metrics. Valid options are 'datadog' and 'influxdb'.").
  75. Envar("MTG_STATSD_TAGS_FORMAT").
  76. Default("influxdb").
  77. Enum("datadog", "influxdb")
  78. runStatsdTags = runCommand.Flag("statsd-tags",
  79. "Tags to use for working with statsd (specified as 'key=value').").
  80. Envar("MTG_STATSD_TAGS").
  81. StringMap()
  82. runWriteBufferSize = runCommand.Flag("write-buffer",
  83. "Write buffer size in bytes. You can think about it as a buffer from client to Telegram.").
  84. Short('w').
  85. Envar("MTG_BUFFER_WRITE").
  86. Default("65536KB").
  87. Bytes()
  88. runReadBufferSize = runCommand.Flag("read-buffer",
  89. "Read buffer size in bytes. You can think about it as a buffer from Telegram to client.").
  90. Short('r').
  91. Envar("MTG_BUFFER_READ").
  92. Default("131072KB").
  93. Bytes()
  94. runTLSCloakPort = runCommand.Flag("cloak-port",
  95. "Port which should be used for host cloaking.").
  96. Envar("MTG_CLOAK_PORT").
  97. Default("443").
  98. Uint16()
  99. runAntiReplayMaxSize = runCommand.Flag("anti-replay-max-size",
  100. "Max size of antireplay cache in megabytes.").
  101. Envar("MTG_ANTIREPLAY_MAXSIZE").
  102. Default("128").
  103. Int()
  104. runAntiReplayEvictionTime = runCommand.Flag("anti-replay-eviction-time",
  105. "Eviction time period for obfuscated2 handshakes").
  106. Envar("MTG_ANTIREPLAY_EVICTIONTIME").
  107. Default("168h").
  108. Duration()
  109. runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
  110. runAdtag = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
  111. )
  112. func main() {
  113. rand.Seed(time.Now().UTC().UnixNano())
  114. app.Version(version)
  115. app.HelpFlag.Short('h')
  116. if err := utils.SetLimits(); err != nil {
  117. cli.Fatal(err)
  118. }
  119. switch kingpin.MustParse(app.Parse(os.Args[1:])) {
  120. case generateSecretCommand.FullCommand():
  121. cli.Generate(*generateSecretType, *generateCloakHost)
  122. case runCommand.FullCommand():
  123. err := config.Init(
  124. config.Opt{Option: config.OptionTypeDebug, Value: *runDebug},
  125. config.Opt{Option: config.OptionTypeVerbose, Value: *runVerbose},
  126. config.Opt{Option: config.OptionTypeBind, Value: *runBind},
  127. config.Opt{Option: config.OptionTypePublicIPv4, Value: *runPublicIPv4},
  128. config.Opt{Option: config.OptionTypePublicIPv6, Value: *runPublicIPv6},
  129. config.Opt{Option: config.OptionTypeStatsBind, Value: *runStatsBind},
  130. config.Opt{Option: config.OptionTypeStatsNamespace, Value: *runStatsNamespace},
  131. config.Opt{Option: config.OptionTypeStatsdAddress, Value: *runStatsdAddress},
  132. config.Opt{Option: config.OptionTypeStatsdNetwork, Value: *runStatsdNetwork},
  133. config.Opt{Option: config.OptionTypeStatsdTagsFormat, Value: *runStatsdTagsFormat},
  134. config.Opt{Option: config.OptionTypeStatsdTags, Value: *runStatsdTags},
  135. config.Opt{Option: config.OptionTypeWriteBufferSize, Value: *runWriteBufferSize},
  136. config.Opt{Option: config.OptionTypeReadBufferSize, Value: *runReadBufferSize},
  137. config.Opt{Option: config.OptionTypeCloakPort, Value: *runTLSCloakPort},
  138. config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *runAntiReplayMaxSize},
  139. config.Opt{Option: config.OptionTypeAntiReplayEvictionTime, Value: *runAntiReplayEvictionTime},
  140. config.Opt{Option: config.OptionTypeSecret, Value: *runSecret},
  141. config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag},
  142. )
  143. if err != nil {
  144. cli.Fatal(err)
  145. }
  146. if err := cli.Proxy(); err != nil {
  147. cli.Fatal(err)
  148. }
  149. }
  150. }