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 1.9KB

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