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
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

main.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. generateSecretType = generateSecretCommand.Arg("type",
  17. "A type of secret to generate. Valid options are 'simple', 'secured' and 'tls'").
  18. Required().
  19. Enum("simple", "secured", "tls")
  20. proxyCommand = app.Command("proxy",
  21. "Run new proxy instance")
  22. proxyDebug = proxyCommand.Flag("debug",
  23. "Run in debug mode.").
  24. Short('d').
  25. Envar("MTG_DEBUG").
  26. Bool()
  27. proxyVerbose = proxyCommand.Flag("verbose",
  28. "Run in verbose mode.").
  29. Short('v').
  30. Envar("MTG_VERBOSE").
  31. Bool()
  32. proxyBindIP = proxyCommand.Flag("bind-ip",
  33. "Which IP to bind to.").
  34. Short('b').
  35. Envar("MTG_IP").
  36. Default("127.0.0.1").
  37. IP()
  38. proxyBindPort = proxyCommand.Flag("bind-port",
  39. "Which port to bind to.").
  40. Short('p').
  41. Envar("MTG_PORT").
  42. Default("3128").
  43. Uint16()
  44. proxyPublicIPv4 = proxyCommand.Flag("public-ipv4",
  45. "Which IPv4 address is public.").
  46. Short('4').
  47. Envar("MTG_IPV4").
  48. IP()
  49. proxyPublicIPv4Port = proxyCommand.Flag("public-ipv4-port",
  50. "Which IPv4 port is public. Default is 'bind-port' value.").
  51. Envar("MTG_IPV4_PORT").
  52. Uint16()
  53. proxyPublicIPv6 = proxyCommand.Flag("public-ipv6",
  54. "Which IPv6 address is public.").
  55. Short('6').
  56. Envar("MTG_IPV6").
  57. IP()
  58. proxyPublicIPv6Port = proxyCommand.Flag("public-ipv6-port",
  59. "Which IPv6 port is public. Default is 'bind-port' value.").
  60. Envar("MTG_IPV6_PORT").
  61. Uint16()
  62. proxyStatsIP = proxyCommand.Flag("stats-ip",
  63. "Which IP bind stats server to.").
  64. Short('t').
  65. Envar("MTG_STATS_IP").
  66. Default("127.0.0.1").
  67. IP()
  68. proxyStatsPort = proxyCommand.Flag("stats-port",
  69. "Which port bind stats to.").
  70. Short('q').
  71. Envar("MTG_STATS_PORT").
  72. Default("3129").
  73. Uint16()
  74. proxyStatsdIP = proxyCommand.Flag("statsd-ip",
  75. "Which IP should we use for working with statsd.").
  76. Envar("MTG_STATSD_IP").
  77. IP()
  78. proxyStatsdPort = proxyCommand.Flag("statsd-port",
  79. "Which port should we use for working with statsd.").
  80. Envar("MTG_STATSD_PORT").
  81. Default("8125").
  82. Uint16()
  83. proxyStatsdNetwork = proxyCommand.Flag("statsd-network",
  84. "Which network is used to work with statsd. Only 'tcp' and 'udp' are supported.").
  85. Envar("MTG_STATSD_NETWORK").
  86. Default("udp").
  87. Enum("udp", "tcp")
  88. proxyStatsdPrefix = proxyCommand.Flag("statsd-prefix",
  89. "Which bucket prefix should we use for sending stats to statsd.").
  90. Envar("MTG_STATSD_PREFIX").
  91. Default("mtg").
  92. String()
  93. proxyStatsdTagsFormat = proxyCommand.Flag("statsd-tags-format",
  94. "Which tag format should we use to send stats metrics. Valid options are 'datadog' and 'influxdb'.").
  95. Envar("MTG_STATSD_TAGS_FORMAT").
  96. Default("influxdb").
  97. Enum("datadog", "influxdb")
  98. proxyStatsdTags = proxyCommand.Flag("statsd-tags",
  99. "Tags to use for working with statsd (specified as 'key=value').").
  100. Envar("MTG_STATSD_TAGS").
  101. StringMap()
  102. proxyPrometheusPrefix = proxyCommand.Flag("prometheus-prefix",
  103. "Which namespace to use to send stats to Prometheus.").
  104. Envar("MTG_PROMETHEUS_PREFIX").
  105. Default("mtg").
  106. String()
  107. proxyWriteBufferSize = proxyCommand.Flag("write-buffer",
  108. "Write buffer size in bytes. You can think about it as a buffer from client to Telegram.").
  109. Short('w').
  110. Envar("MTG_BUFFER_WRITE").
  111. Default("65536").
  112. Uint32()
  113. proxyReadBufferSize = proxyCommand.Flag("read-buffer",
  114. "Read buffer size in bytes. You can think about it as a buffer from Telegram to client.").
  115. Short('r').
  116. Envar("MTG_BUFFER_READ").
  117. Default("131072").
  118. Uint32()
  119. proxyAntiReplayMaxSize = proxyCommand.Flag("anti-replay-max-size",
  120. "Max size of antireplay cache in megabytes.").
  121. Envar("MTG_ANTIREPLAY_MAXSIZE").
  122. Default("128").
  123. Int()
  124. proxyAntiReplayEvictionTime = proxyCommand.Flag("anti-replay-eviction-time",
  125. "Eviction time period for obfuscated2 handshakes").
  126. Envar("MTG_ANTIREPLAY_EVICTIONTIME").
  127. Default("168h").
  128. Duration()
  129. proxySecret = proxyCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
  130. proxyAdtag = proxyCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
  131. )
  132. func main() {
  133. rand.Seed(time.Now().UTC().UnixNano())
  134. app.Version(version)
  135. app.HelpFlag.Short('h')
  136. if err := utils.SetLimits(); err != nil {
  137. cli.Fatal(err.Error())
  138. }
  139. switch kingpin.MustParse(app.Parse(os.Args[1:])) {
  140. case generateSecretCommand.FullCommand():
  141. cli.Generate(*generateSecretType)
  142. case proxyCommand.FullCommand():
  143. err := config.Init(
  144. config.ConfigOpt{Option: config.OptionTypeDebug, Value: *proxyDebug},
  145. config.ConfigOpt{Option: config.OptionTypeVerbose, Value: *proxyVerbose},
  146. config.ConfigOpt{Option: config.OptionTypeBindIP, Value: *proxyBindIP},
  147. config.ConfigOpt{Option: config.OptionTypeBindPort, Value: *proxyBindPort},
  148. config.ConfigOpt{Option: config.OptionTypePublicIPv4, Value: *proxyPublicIPv4},
  149. config.ConfigOpt{Option: config.OptionTypePublicIPv4Port, Value: *proxyPublicIPv4Port},
  150. config.ConfigOpt{Option: config.OptionTypePublicIPv6, Value: *proxyPublicIPv6},
  151. config.ConfigOpt{Option: config.OptionTypePublicIPv6Port, Value: *proxyPublicIPv6Port},
  152. config.ConfigOpt{Option: config.OptionTypeStatsIP, Value: *proxyStatsIP},
  153. config.ConfigOpt{Option: config.OptionTypeStatsPort, Value: *proxyStatsPort},
  154. config.ConfigOpt{Option: config.OptionTypeStatsdIP, Value: *proxyStatsdIP},
  155. config.ConfigOpt{Option: config.OptionTypeStatsdPort, Value: *proxyStatsdPort},
  156. config.ConfigOpt{Option: config.OptionTypeStatsdNetwork, Value: *proxyStatsdNetwork},
  157. config.ConfigOpt{Option: config.OptionTypeStatsdPrefix, Value: *proxyStatsdPrefix},
  158. config.ConfigOpt{Option: config.OptionTypeStatsdTagsFormat, Value: *proxyStatsdTagsFormat},
  159. config.ConfigOpt{Option: config.OptionTypeStatsdTags, Value: *proxyStatsdTags},
  160. config.ConfigOpt{Option: config.OptionTypePrometheusPrefix, Value: *proxyPrometheusPrefix},
  161. config.ConfigOpt{Option: config.OptionTypeWriteBufferSize, Value: *proxyWriteBufferSize},
  162. config.ConfigOpt{Option: config.OptionTypeReadBufferSize, Value: *proxyReadBufferSize},
  163. config.ConfigOpt{Option: config.OptionTypeAntiReplayMaxSize, Value: *proxyAntiReplayMaxSize},
  164. config.ConfigOpt{Option: config.OptionTypeAntiReplayEvictionTime, Value: *proxyAntiReplayEvictionTime},
  165. config.ConfigOpt{Option: config.OptionTypeSecret, Value: *proxySecret},
  166. config.ConfigOpt{Option: config.OptionTypeAdtag, Value: *proxyAdtag},
  167. )
  168. if err != nil {
  169. cli.Fatal(err.Error())
  170. }
  171. if err := cli.Proxy(); err != nil {
  172. cli.Fatal(err.Error())
  173. }
  174. }
  175. }