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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

main.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package main
  2. import (
  3. "math/rand"
  4. "os"
  5. "time"
  6. kingpin "gopkg.in/alecthomas/kingpin.v2"
  7. "mtg/cli"
  8. "mtg/config"
  9. "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. runStatsdTagsFormat = runCommand.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. runStatsdTags = runCommand.Flag("statsd-tags",
  74. "Tags to use for working with statsd (specified as 'key=value').").
  75. Envar("MTG_STATSD_TAGS").
  76. StringMap()
  77. runWriteBufferSize = runCommand.Flag("write-buffer",
  78. "Write buffer size. You can think about it as a buffer from client to Telegram.").
  79. Short('w').
  80. Envar("MTG_BUFFER_WRITE").
  81. Default("64KB").
  82. Bytes()
  83. runReadBufferSize = runCommand.Flag("read-buffer",
  84. "Read buffer size. You can think about it as a buffer from Telegram to client.").
  85. Short('r').
  86. Envar("MTG_BUFFER_READ").
  87. Default("128KB").
  88. Bytes()
  89. runTLSCloakPort = runCommand.Flag("cloak-port",
  90. "Port which should be used for host cloaking.").
  91. Envar("MTG_CLOAK_PORT").
  92. Default("443").
  93. Uint16()
  94. runAntiReplayMaxSize = runCommand.Flag("anti-replay-max-size",
  95. "Max size of antireplay cache.").
  96. Envar("MTG_ANTIREPLAY_MAXSIZE").
  97. Default("128MB").
  98. Bytes()
  99. runMultiplexPerConnection = runCommand.Flag("multiplex-per-connection",
  100. "How many clients can share a single connection to Telegram.").
  101. Envar("MTG_MULTIPLEX_PERCONNECTION").
  102. Default("50").
  103. Uint()
  104. runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
  105. runAdtag = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
  106. )
  107. func main() {
  108. rand.Seed(time.Now().UTC().UnixNano())
  109. app.Version(version)
  110. app.HelpFlag.Short('h')
  111. if err := utils.SetLimits(); err != nil {
  112. cli.Fatal(err)
  113. }
  114. switch kingpin.MustParse(app.Parse(os.Args[1:])) {
  115. case generateSecretCommand.FullCommand():
  116. cli.Generate(*generateSecretType, *generateCloakHost)
  117. case runCommand.FullCommand():
  118. err := config.Init(
  119. config.Opt{Option: config.OptionTypeDebug, Value: *runDebug},
  120. config.Opt{Option: config.OptionTypeVerbose, Value: *runVerbose},
  121. config.Opt{Option: config.OptionTypeBind, Value: *runBind},
  122. config.Opt{Option: config.OptionTypePublicIPv4, Value: *runPublicIPv4},
  123. config.Opt{Option: config.OptionTypePublicIPv6, Value: *runPublicIPv6},
  124. config.Opt{Option: config.OptionTypeStatsBind, Value: *runStatsBind},
  125. config.Opt{Option: config.OptionTypeStatsNamespace, Value: *runStatsNamespace},
  126. config.Opt{Option: config.OptionTypeStatsdAddress, Value: *runStatsdAddress},
  127. config.Opt{Option: config.OptionTypeStatsdTagsFormat, Value: *runStatsdTagsFormat},
  128. config.Opt{Option: config.OptionTypeStatsdTags, Value: *runStatsdTags},
  129. config.Opt{Option: config.OptionTypeWriteBufferSize, Value: *runWriteBufferSize},
  130. config.Opt{Option: config.OptionTypeReadBufferSize, Value: *runReadBufferSize},
  131. config.Opt{Option: config.OptionTypeCloakPort, Value: *runTLSCloakPort},
  132. config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *runAntiReplayMaxSize},
  133. config.Opt{Option: config.OptionTypeMultiplexPerConnection, Value: *runMultiplexPerConnection},
  134. config.Opt{Option: config.OptionTypeSecret, Value: *runSecret},
  135. config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag},
  136. )
  137. if err != nil {
  138. cli.Fatal(err)
  139. }
  140. if err := cli.Proxy(); err != nil {
  141. cli.Fatal(err)
  142. }
  143. }
  144. }