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.

proxy.go 2.1KB

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