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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package main
  2. import (
  3. "math/rand"
  4. "os"
  5. "time"
  6. "github.com/9seconds/mtg/cli"
  7. "github.com/9seconds/mtg/config"
  8. kingpin "gopkg.in/alecthomas/kingpin.v2"
  9. )
  10. var (
  11. app = kingpin.New("mtg", "Simple MTPROTO proxy.")
  12. generateSecretCommand = app.Command("generate-secret",
  13. "Generate new secret")
  14. generateCloakHost = generateSecretCommand.Flag("cloak-host",
  15. "A host to use for TLS cloaking.").
  16. Short('c').
  17. Default("storage.googleapis.com").
  18. String()
  19. generateSecretType = generateSecretCommand.Arg("type",
  20. "A type of secret to generate. Valid options are 'simple', 'secured' and 'tls'").
  21. Required().
  22. Enum("simple", "secured", "tls")
  23. runCommand = app.Command("run",
  24. "Run new proxy instance")
  25. runDebug = runCommand.Flag("debug",
  26. "Run in debug mode.").
  27. Short('d').
  28. Envar("MTG_DEBUG").
  29. Bool()
  30. runVerbose = runCommand.Flag("verbose",
  31. "Run in verbose mode.").
  32. Short('v').
  33. Envar("MTG_VERBOSE").
  34. Bool()
  35. runPreferIP = runCommand.Flag("prefer-ip",
  36. "Prefer this IP protocol if possible. Valid options are 'ipv4' and 'ipv6'").
  37. Envar("MTG_PREFER_DIRECT_IP").
  38. Default("ipv6").
  39. Enum("ipv4", "ipv6")
  40. runBind = runCommand.Flag("bind",
  41. "Host:Port to bind proxy to.").
  42. Short('b').
  43. Envar("MTG_BIND").
  44. Default("0.0.0.0:3128").
  45. TCP()
  46. runPublicIPv4 = runCommand.Flag("public-ipv4",
  47. "Which IPv4 host:port to use.").
  48. Short('4').
  49. Envar("MTG_IPV4").
  50. TCP()
  51. runPublicIPv6 = runCommand.Flag("public-ipv6",
  52. "Which IPv6 host:port to use.").
  53. Short('6').
  54. Envar("MTG_IPV6").
  55. TCP()
  56. runStatsBind = runCommand.Flag("stats-bind",
  57. "Which Host:Port to bind stats server to.").
  58. Short('t').
  59. Envar("MTG_STATS_BIND").
  60. Default("127.0.0.1:3129").
  61. TCP()
  62. runStatsNamespace = runCommand.Flag("stats-namespace",
  63. "Which namespace to use for Prometheus.").
  64. Envar("MTG_STATS_NAMESPACE").
  65. Default("mtg").
  66. String()
  67. runStatsdAddress = runCommand.Flag("statsd-addr",
  68. "Host:port of statsd server").
  69. Envar("MTG_STATSD_ADDR").
  70. TCP()
  71. runStatsdTagsFormat = runCommand.Flag("statsd-tags-format",
  72. "Which tag format should we use to send stats metrics. Valid options are 'datadog' and 'influxdb'.").
  73. Envar("MTG_STATSD_TAGS_FORMAT").
  74. Default("influxdb").
  75. Enum("datadog", "influxdb")
  76. runStatsdTags = runCommand.Flag("statsd-tags",
  77. "Tags to use for working with statsd (specified as 'key=value').").
  78. Envar("MTG_STATSD_TAGS").
  79. StringMap()
  80. runWriteBufferSize = runCommand.Flag("write-buffer",
  81. "Write buffer size. You can think about it as a buffer from client to Telegram.").
  82. Short('w').
  83. Envar("MTG_BUFFER_WRITE").
  84. Default("32KB").
  85. Bytes()
  86. runReadBufferSize = runCommand.Flag("read-buffer",
  87. "Read buffer size. You can think about it as a buffer from Telegram to client.").
  88. Short('r').
  89. Envar("MTG_BUFFER_READ").
  90. Default("32KB").
  91. Bytes()
  92. runTLSCloakPort = runCommand.Flag("cloak-port",
  93. "Port which should be used for host cloaking.").
  94. Envar("MTG_CLOAK_PORT").
  95. Default("443").
  96. Uint16()
  97. runAntiReplayMaxSize = runCommand.Flag("anti-replay-max-size",
  98. "Max size of antireplay cache.").
  99. Envar("MTG_ANTIREPLAY_MAXSIZE").
  100. Default("128MB").
  101. Bytes()
  102. runMultiplexPerConnection = runCommand.Flag("multiplex-per-connection",
  103. "How many clients can share a single connection to Telegram.").
  104. Envar("MTG_MULTIPLEX_PERCONNECTION").
  105. Default("50").
  106. Uint()
  107. runNTPServers = runCommand.Flag("ntp-server",
  108. "A list of NTP servers to use.").
  109. Envar("MTG_NTP_SERVERS").
  110. Default("0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org").
  111. Strings()
  112. runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
  113. runAdtag = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
  114. )
  115. func main() {
  116. rand.Seed(time.Now().UTC().UnixNano())
  117. app.Version(getVersion())
  118. app.HelpFlag.Short('h')
  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.OptionTypePreferIP, Value: *runPreferIP},
  127. config.Opt{Option: config.OptionTypeBind, Value: *runBind},
  128. config.Opt{Option: config.OptionTypePublicIPv4, Value: *runPublicIPv4},
  129. config.Opt{Option: config.OptionTypePublicIPv6, Value: *runPublicIPv6},
  130. config.Opt{Option: config.OptionTypeStatsBind, Value: *runStatsBind},
  131. config.Opt{Option: config.OptionTypeStatsNamespace, Value: *runStatsNamespace},
  132. config.Opt{Option: config.OptionTypeStatsdAddress, Value: *runStatsdAddress},
  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.OptionTypeMultiplexPerConnection, Value: *runMultiplexPerConnection},
  140. config.Opt{Option: config.OptionTypeNTPServers, Value: *runNTPServers},
  141. config.Opt{Option: config.OptionTypeSecret, Value: *runSecret},
  142. config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag},
  143. )
  144. if err != nil {
  145. cli.Fatal(err)
  146. }
  147. if err := cli.Proxy(); err != nil {
  148. cli.Fatal(err)
  149. }
  150. }
  151. }