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

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