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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. )
  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"`
  27. TgQrCode string `json:"tg_qrcode"`
  28. TmeURL string `json:"tme_url"`
  29. TmeQrCode string `json:"tme_qrcode"`
  30. }
  31. type Access struct {
  32. base
  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 (c *Access) Run(cli *CLI, version string) error {
  39. if err := c.ReadConfig(version); err != nil {
  40. return fmt.Errorf("cannot init config: %w", err)
  41. }
  42. return c.Execute(cli)
  43. }
  44. func (c *Access) Execute(cli *CLI) error {
  45. resp := &accessResponse{}
  46. resp.Secret.Base64 = c.Config.Secret.Base64()
  47. resp.Secret.Hex = c.Config.Secret.Hex()
  48. wg := &sync.WaitGroup{}
  49. wg.Add(2) // nolint: gomnd
  50. go func() {
  51. defer wg.Done()
  52. ip := cli.Access.PublicIPv4
  53. if ip == nil {
  54. ip = c.getIP("tcp4")
  55. }
  56. if ip != nil {
  57. ip = ip.To4()
  58. }
  59. resp.IPv4 = c.makeURLs(ip, cli)
  60. }()
  61. go func() {
  62. defer wg.Done()
  63. ip := cli.Access.PublicIPv6
  64. if ip == nil {
  65. ip = c.getIP("tcp6")
  66. }
  67. if ip != nil {
  68. ip = ip.To16()
  69. }
  70. resp.IPv6 = c.makeURLs(ip, cli)
  71. }()
  72. wg.Wait()
  73. encoder := json.NewEncoder(os.Stdout)
  74. encoder.SetEscapeHTML(false)
  75. encoder.SetIndent("", " ")
  76. if err := encoder.Encode(resp); err != nil {
  77. return fmt.Errorf("cannot dump access json: %w", err)
  78. }
  79. return nil
  80. }
  81. func (c *Access) getIP(protocol string) net.IP {
  82. client := c.Network.MakeHTTPClient(func(ctx context.Context, network, address string) (net.Conn, error) {
  83. return c.Network.DialContext(ctx, protocol, address) // nolint: wrapcheck
  84. })
  85. req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil) // nolint: noctx
  86. if err != nil {
  87. panic(err)
  88. }
  89. req.Header.Add("Accept", "text/plain")
  90. resp, err := client.Do(req)
  91. if err != nil {
  92. return nil
  93. }
  94. if resp.StatusCode != http.StatusOK {
  95. return nil
  96. }
  97. defer func() {
  98. io.Copy(io.Discard, resp.Body) // nolint: errcheck
  99. resp.Body.Close()
  100. }()
  101. data, err := io.ReadAll(resp.Body)
  102. if err != nil {
  103. return nil
  104. }
  105. return net.ParseIP(strings.TrimSpace(string(data)))
  106. }
  107. func (c *Access) makeURLs(ip net.IP, cli *CLI) *accessResponseURLs {
  108. if ip == nil {
  109. return nil
  110. }
  111. portNo := cli.Access.Port
  112. if portNo == 0 {
  113. portNo = c.Config.BindTo.PortValue(0)
  114. }
  115. values := url.Values{}
  116. values.Set("server", ip.String())
  117. values.Set("port", strconv.Itoa(int(portNo)))
  118. if cli.Access.Hex {
  119. values.Set("secret", c.Config.Secret.Hex())
  120. } else {
  121. values.Set("secret", c.Config.Secret.Base64())
  122. }
  123. urlQuery := values.Encode()
  124. rv := &accessResponseURLs{
  125. IP: ip,
  126. Port: portNo,
  127. TgURL: (&url.URL{
  128. Scheme: "tg",
  129. Host: "proxy",
  130. RawQuery: urlQuery,
  131. }).String(),
  132. TmeURL: (&url.URL{
  133. Scheme: "https",
  134. Host: "t.me",
  135. Path: "proxy",
  136. RawQuery: urlQuery,
  137. }).String(),
  138. }
  139. rv.TgQrCode = c.makeQRCode(rv.TgURL)
  140. rv.TmeQrCode = c.makeQRCode(rv.TmeURL)
  141. return rv
  142. }
  143. func (c *Access) makeQRCode(data string) string {
  144. values := url.Values{}
  145. values.Set("qzone", "4")
  146. values.Set("format", "svg")
  147. values.Set("data", data)
  148. return (&url.URL{
  149. Scheme: "https",
  150. Host: "api.qrserver.com",
  151. Path: "v1/create-qr-code",
  152. RawQuery: values.Encode(),
  153. }).String()
  154. }