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

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