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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package cli
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "os"
  9. "strconv"
  10. "sync"
  11. "time"
  12. "github.com/9seconds/mtg/v2/internal/config"
  13. "github.com/9seconds/mtg/v2/internal/utils"
  14. )
  15. type accessResponse struct {
  16. IPv4 *accessResponseURLs `json:"ipv4,omitempty"`
  17. IPv6 *accessResponseURLs `json:"ipv6,omitempty"`
  18. Secret struct {
  19. Hex string `json:"hex"`
  20. Base64 string `json:"base64"`
  21. } `json:"secret"`
  22. }
  23. type accessResponseURLs struct {
  24. IP net.IP `json:"ip"`
  25. Port uint `json:"port"`
  26. TgURL string `json:"tg_url"` //nolint: tagliatelle
  27. TgQrCode string `json:"tg_qrcode"` //nolint: tagliatelle
  28. TmeURL string `json:"tme_url"` //nolint: tagliatelle
  29. TmeQrCode string `json:"tme_qrcode"` //nolint: tagliatelle
  30. }
  31. type Access struct {
  32. ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` //nolint: lll
  33. PublicIPv4 net.IP `kong:"help='Public IPv4 address for proxy. By default it is resolved via remote website',name='ipv4',short='i'"` //nolint: lll
  34. PublicIPv6 net.IP `kong:"help='Public IPv6 address for proxy. By default it is resolved via remote website',name='ipv6',short='I'"` //nolint: lll
  35. Port uint `kong:"help='Port number. Default port is taken from configuration file, bind-to parameter',type:'uint',short='p'"` //nolint: lll
  36. Hex bool `kong:"help='Print secret in hex encoding.',short='x'"`
  37. }
  38. func (a *Access) Run(cli *CLI, version string) error {
  39. conf, err := utils.ReadConfig(a.ConfigPath)
  40. if err != nil {
  41. return fmt.Errorf("cannot init config: %w", err)
  42. }
  43. resp := &accessResponse{}
  44. resp.Secret.Base64 = conf.Secret.Base64()
  45. resp.Secret.Hex = conf.Secret.Hex()
  46. ntw, err := makeNetwork(conf, version)
  47. if err != nil {
  48. return fmt.Errorf("cannot init network: %w", err)
  49. }
  50. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  51. defer cancel()
  52. endpoints := resolvePublicIPEndpoints(conf.Network.PublicIPEndpoints)
  53. wg := &sync.WaitGroup{}
  54. wg.Go(func() {
  55. ip := a.PublicIPv4
  56. if ip == nil {
  57. ip = conf.PublicIPv4.Get(nil)
  58. }
  59. if ip == nil {
  60. ip = getIP(ctx, ntw, "tcp4", endpoints)
  61. }
  62. if ip != nil {
  63. ip = ip.To4()
  64. }
  65. resp.IPv4 = a.makeURLs(conf, ip)
  66. })
  67. wg.Go(func() {
  68. ip := a.PublicIPv6
  69. if ip == nil {
  70. ip = conf.PublicIPv6.Get(nil)
  71. }
  72. if ip == nil {
  73. ip = getIP(ctx, ntw, "tcp6", endpoints)
  74. }
  75. if ip != nil {
  76. ip = ip.To16()
  77. }
  78. resp.IPv6 = a.makeURLs(conf, ip)
  79. })
  80. wg.Wait()
  81. encoder := json.NewEncoder(os.Stdout)
  82. encoder.SetEscapeHTML(false)
  83. encoder.SetIndent("", " ")
  84. if err := encoder.Encode(resp); err != nil {
  85. return fmt.Errorf("cannot dump access json: %w", err)
  86. }
  87. return nil
  88. }
  89. func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
  90. if ip == nil {
  91. return nil
  92. }
  93. portNo := a.Port
  94. if portNo == 0 {
  95. portNo = conf.BindTo.Port
  96. }
  97. values := url.Values{}
  98. values.Set("server", ip.String())
  99. values.Set("port", strconv.Itoa(int(portNo)))
  100. if a.Hex {
  101. values.Set("secret", conf.Secret.Hex())
  102. } else {
  103. values.Set("secret", conf.Secret.Base64())
  104. }
  105. urlQuery := values.Encode()
  106. rv := &accessResponseURLs{
  107. IP: ip,
  108. Port: portNo,
  109. TgURL: (&url.URL{
  110. Scheme: "tg",
  111. Host: "proxy",
  112. RawQuery: urlQuery,
  113. }).String(),
  114. TmeURL: (&url.URL{
  115. Scheme: "https",
  116. Host: "t.me",
  117. Path: "proxy",
  118. RawQuery: urlQuery,
  119. }).String(),
  120. }
  121. rv.TgQrCode = utils.MakeQRCodeURL(rv.TgURL)
  122. rv.TmeQrCode = utils.MakeQRCodeURL(rv.TmeURL)
  123. return rv
  124. }