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

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