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
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

access.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.Go(func() {
  56. ip := a.PublicIPv4
  57. if ip == nil {
  58. ip = a.getIP(ntw, "tcp4")
  59. }
  60. if ip != nil {
  61. ip = ip.To4()
  62. }
  63. resp.IPv4 = a.makeURLs(conf, ip)
  64. })
  65. wg.Go(func() {
  66. ip := a.PublicIPv6
  67. if ip == nil {
  68. ip = a.getIP(ntw, "tcp6")
  69. }
  70. if ip != nil {
  71. ip = ip.To16()
  72. }
  73. resp.IPv6 = a.makeURLs(conf, ip)
  74. })
  75. wg.Wait()
  76. encoder := json.NewEncoder(os.Stdout)
  77. encoder.SetEscapeHTML(false)
  78. encoder.SetIndent("", " ")
  79. if err := encoder.Encode(resp); err != nil {
  80. return fmt.Errorf("cannot dump access json: %w", err)
  81. }
  82. return nil
  83. }
  84. func (a *Access) getIP(ntw mtglib.Network, protocol string) net.IP {
  85. dialer := ntw.NativeDialer()
  86. client := ntw.MakeHTTPClient(func(ctx context.Context, network, address string) (essentials.Conn, error) {
  87. conn, err := dialer.DialContext(ctx, protocol, address)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return essentials.WrapNetConn(conn), err
  92. })
  93. req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) //nolint: noctx
  94. if err != nil {
  95. panic(err)
  96. }
  97. req.Header.Add("Accept", "text/plain")
  98. resp, err := client.Do(req)
  99. if err != nil {
  100. return nil
  101. }
  102. if resp.StatusCode != http.StatusOK {
  103. return nil
  104. }
  105. defer func() {
  106. io.Copy(io.Discard, resp.Body) //nolint: errcheck
  107. resp.Body.Close() //nolint: errcheck
  108. }()
  109. data, err := io.ReadAll(resp.Body)
  110. if err != nil {
  111. return nil
  112. }
  113. return net.ParseIP(strings.TrimSpace(string(data)))
  114. }
  115. func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
  116. if ip == nil {
  117. return nil
  118. }
  119. portNo := a.Port
  120. if portNo == 0 {
  121. portNo = conf.BindTo.Port
  122. }
  123. values := url.Values{}
  124. values.Set("server", ip.String())
  125. values.Set("port", strconv.Itoa(int(portNo)))
  126. if a.Hex {
  127. values.Set("secret", conf.Secret.Hex())
  128. } else {
  129. values.Set("secret", conf.Secret.Base64())
  130. }
  131. urlQuery := values.Encode()
  132. rv := &accessResponseURLs{
  133. IP: ip,
  134. Port: portNo,
  135. TgURL: (&url.URL{
  136. Scheme: "tg",
  137. Host: "proxy",
  138. RawQuery: urlQuery,
  139. }).String(),
  140. TmeURL: (&url.URL{
  141. Scheme: "https",
  142. Host: "t.me",
  143. Path: "proxy",
  144. RawQuery: urlQuery,
  145. }).String(),
  146. }
  147. rv.TgQrCode = utils.MakeQRCodeURL(rv.TgURL)
  148. rv.TmeQrCode = utils.MakeQRCodeURL(rv.TmeURL)
  149. return rv
  150. }