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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

access.go 4.1KB

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