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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "github.com/9seconds/mtg/proxy"
  13. "go.uber.org/zap"
  14. "go.uber.org/zap/zapcore"
  15. kingpin "gopkg.in/alecthomas/kingpin.v2"
  16. )
  17. var (
  18. app = kingpin.New("mtg", "Simple MTPROTO proxy.")
  19. debug = app.Flag("debug", "Run in debug mode.").
  20. Short('d').
  21. Envar("MTG_DEBUG").
  22. Bool()
  23. verbose = app.Flag("verbose", "Run in verbose mode.").
  24. Short('v').
  25. Envar("MTG_VERBOSE").
  26. Bool()
  27. bindIP = app.Flag("bind-ip", "Which IP to bind to.").
  28. Short('i').
  29. Envar("MTG_IP").
  30. Default("127.0.0.1").
  31. IP()
  32. bindPort = app.Flag("bind-port", "Which port to bind to.").
  33. Short('p').
  34. Envar("MTG_PORT").
  35. Default("3128").
  36. Uint16()
  37. readTimeout = app.Flag("read-timeout", "Socket read timeout").
  38. Short('r').
  39. Envar("MTG_READ_TIMEOUT").
  40. Default("30s").
  41. Duration()
  42. writeTimeout = app.Flag("write-timeout", "Socket write timeout").
  43. Short('w').
  44. Envar("MTG_WRITE_TIMEOUT").
  45. Default("30s").
  46. Duration()
  47. serverName = app.Flag("server-name",
  48. "Which server name to use. Default is IP address resolved by ipify.").
  49. Short('s').
  50. Envar("MTG_SERVER").
  51. String()
  52. secret = app.Arg("secret", "Secret of this proxy.").String()
  53. )
  54. func main() {
  55. kingpin.MustParse(app.Parse(os.Args[1:]))
  56. secretBytes, err := hex.DecodeString(*secret)
  57. if err != nil {
  58. usage("Secret has to be hexadecimal string.")
  59. }
  60. if *serverName == "" {
  61. resp, err := http.Get("https://api.ipify.org")
  62. if err != nil || resp.StatusCode != http.StatusOK {
  63. usage("Cannot get local IP address.")
  64. }
  65. myIPBytes, err := ioutil.ReadAll(resp.Body)
  66. resp.Body.Close()
  67. if err != nil {
  68. usage("Cannot get local IP address.")
  69. }
  70. *serverName = strings.TrimSpace(string(myIPBytes))
  71. }
  72. atom := zap.NewAtomicLevel()
  73. if *debug {
  74. atom.SetLevel(zapcore.DebugLevel)
  75. } else if *verbose {
  76. atom.SetLevel(zapcore.InfoLevel)
  77. } else {
  78. atom.SetLevel(zapcore.ErrorLevel)
  79. }
  80. encoderCfg := zap.NewProductionEncoderConfig()
  81. logger := zap.New(zapcore.NewCore(
  82. zapcore.NewJSONEncoder(encoderCfg),
  83. zapcore.Lock(os.Stderr),
  84. atom,
  85. )).Sugar()
  86. printURLs()
  87. srv := proxy.NewServer(*bindIP, int(*bindPort), secretBytes, logger,
  88. *readTimeout, *writeTimeout)
  89. if err := srv.Serve(); err != nil {
  90. logger.Fatal(err.Error())
  91. }
  92. }
  93. func usage(msg string) {
  94. io.WriteString(os.Stderr, msg+"\n")
  95. os.Exit(1)
  96. }
  97. func printURLs() {
  98. values := url.Values{}
  99. values.Set("server", *serverName)
  100. values.Set("port", strconv.Itoa(int(*bindPort)))
  101. values.Set("secret", *secret)
  102. tgURL := url.URL{
  103. Scheme: "tg",
  104. Host: "proxy",
  105. RawQuery: values.Encode(),
  106. }
  107. fmt.Println(tgURL.String())
  108. tgURL.Scheme = "https"
  109. tgURL.Host = "t.me"
  110. tgURL.Path = "proxy"
  111. fmt.Println(tgURL.String())
  112. }