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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "go.uber.org/zap"
  13. "go.uber.org/zap/zapcore"
  14. kingpin "gopkg.in/alecthomas/kingpin.v2"
  15. "github.com/9seconds/mtg/server"
  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. serverName = app.Flag("server-name",
  38. "Which server name to use. Default is IP address resolved by ipify.").
  39. Short('s').
  40. Envar("MTG_SERVER").
  41. String()
  42. secret = app.Arg("secret", "Secret of this proxy.").String()
  43. )
  44. func main() {
  45. kingpin.MustParse(app.Parse(os.Args[1:]))
  46. secretBytes, err := hex.DecodeString(*secret)
  47. if err != nil {
  48. usage("Secret has to be hexadecimal string.")
  49. }
  50. if *serverName == "" {
  51. resp, err := http.Get("https://api.ipify.org")
  52. if err != nil || resp.StatusCode != http.StatusOK {
  53. usage("Cannot get local IP address.")
  54. }
  55. myIPBytes, err := ioutil.ReadAll(resp.Body)
  56. resp.Body.Close()
  57. if err != nil {
  58. usage("Cannot get local IP address.")
  59. }
  60. *serverName = strings.TrimSpace(string(myIPBytes))
  61. }
  62. atom := zap.NewAtomicLevel()
  63. if *debug {
  64. atom.SetLevel(zapcore.DebugLevel)
  65. } else if *verbose {
  66. atom.SetLevel(zapcore.InfoLevel)
  67. } else {
  68. atom.SetLevel(zapcore.ErrorLevel)
  69. }
  70. encoderCfg := zap.NewProductionEncoderConfig()
  71. logger := zap.New(zapcore.NewCore(
  72. zapcore.NewJSONEncoder(encoderCfg),
  73. zapcore.Lock(os.Stderr),
  74. atom,
  75. )).Sugar()
  76. printURLs()
  77. if err := server.NewServer(*bindIP, int(*bindPort), secretBytes, logger).Serve(); err != nil {
  78. logger.Fatal(err.Error())
  79. }
  80. }
  81. func usage(msg string) {
  82. io.WriteString(os.Stderr, msg+"\n")
  83. os.Exit(1)
  84. }
  85. func printURLs() {
  86. values := url.Values{}
  87. values.Set("server", *serverName)
  88. values.Set("port", strconv.Itoa(int(*bindPort)))
  89. values.Set("secret", *secret)
  90. tgURL := url.URL{
  91. Scheme: "tg",
  92. Host: "proxy",
  93. RawQuery: values.Encode(),
  94. }
  95. fmt.Println(tgURL.String())
  96. tgURL.Scheme = "https"
  97. tgURL.Host = "t.me"
  98. tgURL.Path = "proxy"
  99. fmt.Println(tgURL.String())
  100. }