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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. proxyBind = proxyCommand.Flag("bind",
  33. "Host:Port to bind proxy to.").
  34. Short('b').
  35. Envar("MTG_BIND").
  36. Default("0.0.0.0:3128").
  37. TCP()
  38. proxyPublicIPv4 = proxyCommand.Flag("public-ipv4",
  39. "Which IPv4 host:port to use.").
  40. Short('4').
  41. Envar("MTG_IPV4").
  42. TCP()
  43. proxyPublicIPv6 = proxyCommand.Flag("public-ipv6",
  44. "Which IPv6 host:port to use.").
  45. Short('6').
  46. Envar("MTG_IPV6").
  47. TCP()
  48. proxyStatsBind = proxyCommand.Flag("stats-bind",
  49. "Which Host:Port to bind stats server to.").
  50. Short('t').
  51. Envar("MTG_STATS_BIND").
  52. Default("127.0.0.1:3129").
  53. TCP()
  54. proxyStatsNamespace = proxyCommand.Flag("stats-namespace",
  55. "Which namespace to use for Prometheus.").
  56. Envar("MTG_STATS_NAMESPACE").
  57. Default("mtg").
  58. String()
  59. proxyStatsdAddress = proxyCommand.Flag("statsd-addr",
  60. "Host:port of statsd server").
  61. Envar("MTG_STATSD_ADDR").
  62. TCP()
  63. proxyStatsdNetwork = proxyCommand.Flag("statsd-network",
  64. "Which network is used to work with statsd. Only 'tcp' and 'udp' are supported.").
  65. Envar("MTG_STATSD_NETWORK").
  66. Default("udp").
  67. Enum("udp", "tcp")
  68. proxyStatsdTagsFormat = proxyCommand.Flag("statsd-tags-format",
  69. "Which tag format should we use to send stats metrics. Valid options are 'datadog' and 'influxdb'.").
  70. Envar("MTG_STATSD_TAGS_FORMAT").
  71. Default("influxdb").
  72. Enum("datadog", "influxdb")
  73. proxyStatsdTags = proxyCommand.Flag("statsd-tags",
  74. "Tags to use for working with statsd (specified as 'key=value').").
  75. Envar("MTG_STATSD_TAGS").
  76. StringMap()
  77. proxyWriteBufferSize = proxyCommand.Flag("write-buffer",
  78. "Write buffer size in bytes. You can think about it as a buffer from client to Telegram.").
  79. Short('w').
  80. Envar("MTG_BUFFER_WRITE").
  81. Default("65536KB").
  82. Bytes()
  83. proxyReadBufferSize = proxyCommand.Flag("read-buffer",
  84. "Read buffer size in bytes. You can think about it as a buffer from Telegram to client.").
  85. Short('r').
  86. Envar("MTG_BUFFER_READ").
  87. Default("131072KB").
  88. Bytes()
  89. proxyAntiReplayMaxSize = proxyCommand.Flag("anti-replay-max-size",
  90. "Max size of antireplay cache in megabytes.").
  91. Envar("MTG_ANTIREPLAY_MAXSIZE").
  92. Default("128").
  93. Int()
  94. proxyAntiReplayEvictionTime = proxyCommand.Flag("anti-replay-eviction-time",
  95. "Eviction time period for obfuscated2 handshakes").
  96. Envar("MTG_ANTIREPLAY_EVICTIONTIME").
  97. Default("168h").
  98. Duration()
  99. proxySecret = proxyCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
  100. proxyAdtag = proxyCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
  101. )
  102. func main() {
  103. rand.Seed(time.Now().UTC().UnixNano())
  104. app.Version(version)
  105. app.HelpFlag.Short('h')
  106. if err := utils.SetLimits(); err != nil {
  107. cli.Fatal(err)
  108. }
  109. switch kingpin.MustParse(app.Parse(os.Args[1:])) {
  110. case generateSecretCommand.FullCommand():
  111. cli.Generate(*generateSecretType)
  112. case proxyCommand.FullCommand():
  113. err := config.Init(
  114. config.Opt{Option: config.OptionTypeDebug, Value: *proxyDebug},
  115. config.Opt{Option: config.OptionTypeVerbose, Value: *proxyVerbose},
  116. config.Opt{Option: config.OptionTypeBind, Value: *proxyBind},
  117. config.Opt{Option: config.OptionTypePublicIPv4, Value: *proxyPublicIPv4},
  118. config.Opt{Option: config.OptionTypePublicIPv6, Value: *proxyPublicIPv6},
  119. config.Opt{Option: config.OptionTypeStatsBind, Value: *proxyStatsBind},
  120. config.Opt{Option: config.OptionTypeStatsNamespace, Value: *proxyStatsNamespace},
  121. config.Opt{Option: config.OptionTypeStatsdAddress, Value: *proxyStatsdAddress},
  122. config.Opt{Option: config.OptionTypeStatsdNetwork, Value: *proxyStatsdNetwork},
  123. config.Opt{Option: config.OptionTypeStatsdTagsFormat, Value: *proxyStatsdTagsFormat},
  124. config.Opt{Option: config.OptionTypeStatsdTags, Value: *proxyStatsdTags},
  125. config.Opt{Option: config.OptionTypeWriteBufferSize, Value: *proxyWriteBufferSize},
  126. config.Opt{Option: config.OptionTypeReadBufferSize, Value: *proxyReadBufferSize},
  127. config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *proxyAntiReplayMaxSize},
  128. config.Opt{Option: config.OptionTypeAntiReplayEvictionTime, Value: *proxyAntiReplayEvictionTime},
  129. config.Opt{Option: config.OptionTypeSecret, Value: *proxySecret},
  130. config.Opt{Option: config.OptionTypeAdtag, Value: *proxyAdtag},
  131. )
  132. if err != nil {
  133. cli.Fatal(err)
  134. }
  135. if err := cli.Proxy(); err != nil {
  136. cli.Fatal(err)
  137. }
  138. }
  139. }