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.

main.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. //go:generate scripts/generate_version.sh
  3. import (
  4. "encoding/json"
  5. "io"
  6. "os"
  7. "go.uber.org/zap"
  8. "go.uber.org/zap/zapcore"
  9. kingpin "gopkg.in/alecthomas/kingpin.v2"
  10. "github.com/9seconds/mtg/config"
  11. "github.com/9seconds/mtg/proxy"
  12. )
  13. var (
  14. app = kingpin.New("mtg", "Simple MTPROTO proxy.")
  15. debug = app.Flag("debug", "Run in debug mode.").
  16. Short('d').
  17. Envar("MTG_DEBUG").
  18. Bool()
  19. verbose = app.Flag("verbose", "Run in verbose mode.").
  20. Short('v').
  21. Envar("MTG_VERBOSE").
  22. Bool()
  23. bindIP = app.Flag("bind-ip", "Which IP to bind to.").
  24. Short('b').
  25. Envar("MTG_IP").
  26. Default("127.0.0.1").
  27. IP()
  28. bindPort = app.Flag("bind-port", "Which port to bind to.").
  29. Short('p').
  30. Envar("MTG_PORT").
  31. Default("3128").
  32. Uint16()
  33. publicIPv4 = app.Flag("public-ipv4", "Which IPv4 address is public.").
  34. Short('4').
  35. Envar("MTG_IPV4").
  36. IP()
  37. publicIPv4Port = app.Flag("public-ipv4-port", "Which IPv4 port is public. Default is 'bind-port' value.").
  38. Envar("MTG_IPV4_PORT").
  39. Uint16()
  40. publicIPv6 = app.Flag("public-ipv6", "Which IPv6 address is public.").
  41. Short('6').
  42. Envar("MTG_IPV6").
  43. IP()
  44. publicIPv6Port = app.Flag("public-ipv6-port", "Which IPv6 port is public. Default is 'bind-port' value.").
  45. Envar("MTG_IPV6_PORT").
  46. Uint16()
  47. statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
  48. Short('t').
  49. Envar("MTG_STATS_IP").
  50. Default("127.0.0.1").
  51. IP()
  52. statsPort = app.Flag("stats-port", "Which port bind stats to.").
  53. Short('q').
  54. Envar("MTG_STATS_PORT").
  55. Default("3129").
  56. Uint16()
  57. readTimeout = app.Flag("read-timeout", "Socket read timeout.").
  58. Short('r').
  59. Envar("MTG_READ_TIMEOUT").
  60. Default("30s").
  61. Duration()
  62. writeTimeout = app.Flag("write-timeout", "Socket write timeout.").
  63. Short('w').
  64. Envar("MTG_WRITE_TIMEOUT").
  65. Default("30s").
  66. Duration()
  67. secret = app.Arg("secret", "Secret of this proxy.").Required().String()
  68. )
  69. func main() {
  70. app.Version(version)
  71. kingpin.MustParse(app.Parse(os.Args[1:]))
  72. conf, err := config.NewConfig(*debug, *verbose,
  73. *bindIP, *bindPort,
  74. *publicIPv4, *publicIPv4Port,
  75. *publicIPv6, *publicIPv6Port,
  76. *statsIP, *statsPort,
  77. *readTimeout, *writeTimeout,
  78. *secret,
  79. )
  80. if err != nil {
  81. usage(err.Error())
  82. }
  83. atom := zap.NewAtomicLevel()
  84. if conf.Debug {
  85. atom.SetLevel(zapcore.DebugLevel)
  86. } else if conf.Verbose {
  87. atom.SetLevel(zapcore.InfoLevel)
  88. } else {
  89. atom.SetLevel(zapcore.ErrorLevel)
  90. }
  91. encoderCfg := zap.NewProductionEncoderConfig()
  92. logger := zap.New(zapcore.NewCore(
  93. zapcore.NewJSONEncoder(encoderCfg),
  94. zapcore.Lock(os.Stderr),
  95. atom,
  96. )).Sugar()
  97. stat := proxy.NewStats(conf)
  98. go stat.Serve()
  99. srv := proxy.NewServer(conf, logger, stat)
  100. printURLs(conf.GetURLs())
  101. if err := srv.Serve(); err != nil {
  102. logger.Fatal(err.Error())
  103. }
  104. }
  105. func printURLs(data interface{}) {
  106. encoder := json.NewEncoder(os.Stdout)
  107. encoder.SetEscapeHTML(false)
  108. encoder.SetIndent("", " ")
  109. err := encoder.Encode(data)
  110. if err != nil {
  111. panic(err)
  112. }
  113. }
  114. func usage(msg string) {
  115. io.WriteString(os.Stderr, msg+"\n") // nolint: errcheck
  116. os.Exit(1)
  117. }