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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

access.go 3.5KB

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