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文字以内のものにしてください。

main.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package main
  2. //go:generate scripts/generate_version.sh
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "math/rand"
  8. "os"
  9. "syscall"
  10. "time"
  11. "go.uber.org/zap"
  12. "go.uber.org/zap/zapcore"
  13. kingpin "gopkg.in/alecthomas/kingpin.v2"
  14. "github.com/9seconds/mtg/config"
  15. "github.com/9seconds/mtg/mtproto"
  16. "github.com/9seconds/mtg/telegram"
  17. "github.com/juju/errors"
  18. )
  19. var (
  20. app = kingpin.New("mtg", "Simple MTPROTO proxy.")
  21. debug = app.Flag("debug", "Run in debug mode.").
  22. Short('d').
  23. Envar("MTG_DEBUG").
  24. Bool()
  25. verbose = app.Flag("verbose", "Run in verbose mode.").
  26. Short('v').
  27. Envar("MTG_VERBOSE").
  28. Bool()
  29. bindIP = app.Flag("bind-ip", "Which IP to bind to.").
  30. Short('b').
  31. Envar("MTG_IP").
  32. Default("127.0.0.1").
  33. IP()
  34. bindPort = app.Flag("bind-port", "Which port to bind to.").
  35. Short('p').
  36. Envar("MTG_PORT").
  37. Default("3128").
  38. Uint16()
  39. publicIPv4 = app.Flag("public-ipv4", "Which IPv4 address is public.").
  40. Short('4').
  41. Envar("MTG_IPV4").
  42. IP()
  43. publicIPv4Port = app.Flag("public-ipv4-port", "Which IPv4 port is public. Default is 'bind-port' value.").
  44. Envar("MTG_IPV4_PORT").
  45. Uint16()
  46. publicIPv6 = app.Flag("public-ipv6", "Which IPv6 address is public.").
  47. Short('6').
  48. Envar("MTG_IPV6").
  49. IP()
  50. publicIPv6Port = app.Flag("public-ipv6-port", "Which IPv6 port is public. Default is 'bind-port' value.").
  51. Envar("MTG_IPV6_PORT").
  52. Uint16()
  53. statsIP = app.Flag("stats-ip", "Which IP bind stats server to").
  54. Short('t').
  55. Envar("MTG_STATS_IP").
  56. Default("127.0.0.1").
  57. IP()
  58. statsPort = app.Flag("stats-port", "Which port bind stats to.").
  59. Short('q').
  60. Envar("MTG_STATS_PORT").
  61. Default("3129").
  62. Uint16()
  63. secret = app.Arg("secret", "Secret of this proxy.").Required().String()
  64. )
  65. func init() {
  66. rand.Seed(time.Now().UTC().UnixNano())
  67. app.Version(version)
  68. }
  69. func main() {
  70. kingpin.MustParse(app.Parse(os.Args[1:]))
  71. err := setRLimit()
  72. if err != nil {
  73. usage(err.Error())
  74. }
  75. conf, err := config.NewConfig(*debug, *verbose,
  76. *bindIP, *bindPort,
  77. *publicIPv4, *publicIPv4Port,
  78. *publicIPv6, *publicIPv6Port,
  79. *statsIP, *statsPort,
  80. *secret,
  81. )
  82. if err != nil {
  83. usage(err.Error())
  84. }
  85. atom := zap.NewAtomicLevel()
  86. if conf.Debug {
  87. atom.SetLevel(zapcore.DebugLevel)
  88. } else if conf.Verbose {
  89. atom.SetLevel(zapcore.InfoLevel)
  90. } else {
  91. atom.SetLevel(zapcore.ErrorLevel)
  92. }
  93. encoderCfg := zap.NewProductionEncoderConfig()
  94. logger := zap.New(zapcore.NewCore(
  95. zapcore.NewJSONEncoder(encoderCfg),
  96. zapcore.Lock(os.Stderr),
  97. atom,
  98. )).Sugar()
  99. tg := telegram.NewMiddleTelegram(conf, logger)
  100. connOpts := &mtproto.ConnectionOpts{
  101. DC: int16(1),
  102. ConnectionType: mtproto.ConnectionTypeAbridged,
  103. ConnectionProto: mtproto.ConnectionProtocolIPv4,
  104. }
  105. sock, err := tg.Dial(connOpts)
  106. if err != nil {
  107. panic(err)
  108. }
  109. _, err = tg.Init(connOpts, sock)
  110. fmt.Println(err)
  111. // stat := proxy.NewStats(conf)
  112. // go stat.Serve()
  113. // srv := proxy.NewServer(conf, logger, stat)
  114. // printURLs(conf.GetURLs())
  115. // if err := srv.Serve(); err != nil {
  116. // logger.Fatal(err.Error())
  117. // }
  118. }
  119. func setRLimit() (err error) {
  120. rLimit := syscall.Rlimit{}
  121. err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
  122. if err != nil {
  123. err = errors.Annotate(err, "Cannot get rlimit")
  124. return
  125. }
  126. rLimit.Cur = rLimit.Max
  127. err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
  128. if err != nil {
  129. err = errors.Annotate(err, "Cannot set rlimit")
  130. }
  131. return
  132. }
  133. func printURLs(data interface{}) {
  134. encoder := json.NewEncoder(os.Stdout)
  135. encoder.SetEscapeHTML(false)
  136. encoder.SetIndent("", " ")
  137. err := encoder.Encode(data)
  138. if err != nil {
  139. panic(err)
  140. }
  141. }
  142. func usage(msg string) {
  143. io.WriteString(os.Stderr, msg+"\n") // nolint: errcheck
  144. os.Exit(1)
  145. }