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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

main.go 2.2KB

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