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 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package main
  2. //go:generate scripts/generate_version.sh
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "math/rand"
  8. "os"
  9. "syscall"
  10. "time"
  11. "github.com/juju/errors"
  12. "go.uber.org/zap"
  13. "go.uber.org/zap/zapcore"
  14. kingpin "gopkg.in/alecthomas/kingpin.v2"
  15. "github.com/9seconds/mtg/config"
  16. "github.com/9seconds/mtg/ntp"
  17. "github.com/9seconds/mtg/proxy"
  18. "github.com/9seconds/mtg/stats"
  19. )
  20. var (
  21. app = kingpin.New("mtg", "Simple MTPROTO proxy.")
  22. debug = app.Flag("debug", "Run in debug mode.").
  23. Short('d').
  24. Envar("MTG_DEBUG").
  25. Bool()
  26. verbose = app.Flag("verbose", "Run in verbose mode.").
  27. Short('v').
  28. Envar("MTG_VERBOSE").
  29. Bool()
  30. bindIP = app.Flag("bind-ip", "Which IP to bind to.").
  31. Short('b').
  32. Envar("MTG_IP").
  33. Default("127.0.0.1").
  34. IP()
  35. bindPort = app.Flag("bind-port", "Which port to bind to.").
  36. Short('p').
  37. Envar("MTG_PORT").
  38. Default("3128").
  39. Uint16()
  40. publicIPv4 = app.Flag("public-ipv4", "Which IPv4 address is public.").
  41. Short('4').
  42. Envar("MTG_IPV4").
  43. IP()
  44. publicIPv4Port = app.Flag("public-ipv4-port", "Which IPv4 port is public. Default is 'bind-port' value.").
  45. Envar("MTG_IPV4_PORT").
  46. Uint16()
  47. publicIPv6 = app.Flag("public-ipv6", "Which IPv6 address is public.").
  48. Short('6').
  49. Envar("MTG_IPV6").
  50. IP()
  51. publicIPv6Port = app.Flag("public-ipv6-port", "Which IPv6 port is public. Default is 'bind-port' value.").
  52. Envar("MTG_IPV6_PORT").
  53. Uint16()
  54. statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
  55. Short('t').
  56. Envar("MTG_STATS_IP").
  57. Default("127.0.0.1").
  58. IP()
  59. statsPort = app.Flag("stats-port", "Which port bind stats to.").
  60. Short('q').
  61. Envar("MTG_STATS_PORT").
  62. Default("3129").
  63. Uint16()
  64. secret = app.Arg("secret", "Secret of this proxy.").Required().String()
  65. adtag = app.Arg("adtag", "ADTag of the proxy.").String()
  66. )
  67. func init() {
  68. rand.Seed(time.Now().UTC().UnixNano())
  69. app.Version(version)
  70. }
  71. func main() {
  72. kingpin.MustParse(app.Parse(os.Args[1:]))
  73. err := setRLimit()
  74. if err != nil {
  75. usage(err.Error())
  76. }
  77. conf, err := config.NewConfig(*debug, *verbose,
  78. *bindIP, *bindPort,
  79. *publicIPv4, *publicIPv4Port,
  80. *publicIPv6, *publicIPv6Port,
  81. *statsIP, *statsPort,
  82. *secret, *adtag,
  83. )
  84. if err != nil {
  85. usage(err.Error())
  86. }
  87. atom := zap.NewAtomicLevel()
  88. if conf.Debug {
  89. atom.SetLevel(zapcore.DebugLevel)
  90. } else if conf.Verbose {
  91. atom.SetLevel(zapcore.InfoLevel)
  92. } else {
  93. atom.SetLevel(zapcore.ErrorLevel)
  94. }
  95. encoderCfg := zap.NewProductionEncoderConfig()
  96. logger := zap.New(zapcore.NewCore(
  97. zapcore.NewJSONEncoder(encoderCfg),
  98. zapcore.Lock(os.Stderr),
  99. atom,
  100. ))
  101. zap.ReplaceGlobals(logger)
  102. defer logger.Sync() // nolint: errcheck
  103. printURLs(conf.GetURLs())
  104. if conf.UseMiddleProxy() {
  105. zap.S().Infow("Use middle proxy connection to Telegram")
  106. if diff, err := ntp.Fetch(); err != nil {
  107. zap.S().Warnw("Could not fetch time data from NTP")
  108. } else {
  109. if diff >= time.Second {
  110. usage(fmt.Sprintf("You choose to use middle proxy but your clock drift (%s) is bigger than 1 second. Please, sync your time", diff))
  111. }
  112. go ntp.AutoUpdate()
  113. }
  114. } else {
  115. zap.S().Infow("Use direct connection to Telegram")
  116. }
  117. go stats.Start(conf)
  118. server := proxy.NewProxy(conf)
  119. if err := server.Serve(); err != nil {
  120. zap.S().Fatalw("Server stopped", "error", err)
  121. }
  122. }
  123. func setRLimit() (err error) {
  124. rLimit := syscall.Rlimit{}
  125. err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
  126. if err != nil {
  127. err = errors.Annotate(err, "Cannot get rlimit")
  128. return
  129. }
  130. rLimit.Cur = rLimit.Max
  131. err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
  132. if err != nil {
  133. err = errors.Annotate(err, "Cannot set rlimit")
  134. }
  135. return
  136. }
  137. func printURLs(data interface{}) {
  138. encoder := json.NewEncoder(os.Stdout)
  139. encoder.SetEscapeHTML(false)
  140. encoder.SetIndent("", " ")
  141. err := encoder.Encode(data)
  142. if err != nil {
  143. panic(err)
  144. }
  145. }
  146. func usage(msg string) {
  147. io.WriteString(os.Stderr, msg+"\n") // nolint: errcheck
  148. os.Exit(1)
  149. }