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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package cli
  2. import (
  3. "net"
  4. "os"
  5. "time"
  6. "github.com/9seconds/mtg/antireplay"
  7. "github.com/9seconds/mtg/config"
  8. "github.com/9seconds/mtg/faketls"
  9. "github.com/9seconds/mtg/hub"
  10. "github.com/9seconds/mtg/ntp"
  11. "github.com/9seconds/mtg/obfuscated2"
  12. "github.com/9seconds/mtg/proxy"
  13. "github.com/9seconds/mtg/stats"
  14. "github.com/9seconds/mtg/telegram"
  15. "github.com/9seconds/mtg/utils"
  16. "go.uber.org/zap"
  17. "go.uber.org/zap/zapcore"
  18. )
  19. func Proxy() error { //nolint: funlen,cyclop
  20. ctx := utils.GetSignalContext()
  21. atom := zap.NewAtomicLevel()
  22. switch {
  23. case config.C.Debug:
  24. atom.SetLevel(zapcore.DebugLevel)
  25. case config.C.Verbose:
  26. atom.SetLevel(zapcore.InfoLevel)
  27. default:
  28. atom.SetLevel(zapcore.ErrorLevel)
  29. }
  30. encoderCfg := zap.NewProductionEncoderConfig()
  31. logger := zap.New(zapcore.NewCore(
  32. zapcore.NewJSONEncoder(encoderCfg),
  33. zapcore.Lock(os.Stderr),
  34. atom,
  35. ))
  36. zap.ReplaceGlobals(logger)
  37. defer logger.Sync() //nolint: errcheck
  38. if err := config.InitPublicAddress(ctx); err != nil {
  39. Fatal(err)
  40. }
  41. zap.S().Debugw("Configuration", "config", config.Printable())
  42. if config.C.MiddleProxyMode() {
  43. zap.S().Infow("Use middle proxy connection to Telegram")
  44. diff, err := ntp.Fetch()
  45. if err != nil {
  46. Fatal("Cannot fetch time data from NTP")
  47. }
  48. if diff > time.Second {
  49. Fatal("Your local time is skewed and drift is bigger than a second. Please sync your time.")
  50. }
  51. go ntp.AutoUpdate()
  52. } else {
  53. zap.S().Infow("Use direct connection to Telegram")
  54. }
  55. PrintJSONStdout(config.GetURLs())
  56. if err := stats.Init(ctx); err != nil {
  57. Fatal(err)
  58. }
  59. antireplay.Init()
  60. telegram.Init()
  61. hub.Init(ctx)
  62. proxyListener, err := net.Listen("tcp", config.C.Bind.String())
  63. if err != nil {
  64. Fatal(err)
  65. }
  66. go func() {
  67. <-ctx.Done()
  68. proxyListener.Close()
  69. }()
  70. app := &proxy.Proxy{
  71. Logger: zap.S().Named("proxy"),
  72. Context: ctx,
  73. ClientProtocolMaker: obfuscated2.MakeClientProtocol,
  74. }
  75. if config.C.SecretMode == config.SecretModeTLS {
  76. app.ClientProtocolMaker = faketls.MakeClientProtocol
  77. }
  78. app.Serve(proxyListener)
  79. return nil
  80. }