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个字符

access.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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) error {
  36. if err := c.ReadConfig(cli.Access.ConfigPath); 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(0)
  63. client.Transport = &http.Transport{
  64. DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
  65. return c.network.DialContext(ctx, protocol, address)
  66. },
  67. }
  68. c.network.PrepareHTTPClient(client)
  69. req, err := http.NewRequest(http.MethodGet, "https://ifconfig.co", nil)
  70. if err != nil {
  71. panic(err)
  72. }
  73. resp, err := client.Do(req)
  74. if err != nil {
  75. return nil
  76. }
  77. if resp.StatusCode != http.StatusOK {
  78. return nil
  79. }
  80. defer func() {
  81. io.Copy(ioutil.Discard, resp.Body)
  82. resp.Body.Close()
  83. }()
  84. data, err := ioutil.ReadAll(resp.Body)
  85. if err != nil {
  86. return nil
  87. }
  88. return net.ParseIP(strings.TrimSpace(string(data)))
  89. }
  90. func (c *Access) makeURLs(ip net.IP, cli *CLI) *accessResponseURLs {
  91. if ip == nil {
  92. return nil
  93. }
  94. values := url.Values{}
  95. values.Set("server", ip.String())
  96. values.Set("port", strconv.Itoa(int(c.conf.BindTo.PortValue(0))))
  97. if cli.Access.Hex {
  98. values.Set("secret", c.conf.Secret.Hex())
  99. } else {
  100. values.Set("secret", c.conf.Secret.Base64())
  101. }
  102. urlQuery := values.Encode()
  103. rv := &accessResponseURLs{
  104. IP: ip,
  105. TgURL: (&url.URL{
  106. Scheme: "tg",
  107. Host: "proxy",
  108. RawQuery: urlQuery,
  109. }).String(),
  110. TmeURL: (&url.URL{
  111. Scheme: "https",
  112. Host: "t.me",
  113. Path: "proxy",
  114. RawQuery: urlQuery,
  115. }).String(),
  116. }
  117. rv.TgQrCode = c.makeQRCode(rv.TgURL)
  118. rv.TmeQrCode = c.makeQRCode(rv.TmeURL)
  119. return rv
  120. }
  121. func (c *Access) makeQRCode(data string) string {
  122. values := url.Values{}
  123. values.Set("qzone", "4")
  124. values.Set("format", "svg")
  125. values.Set("data", data)
  126. return (&url.URL{
  127. Scheme: "https",
  128. Host: "api.qrserver.com",
  129. Path: "v1/create-qr-code",
  130. RawQuery: values.Encode(),
  131. }).String()
  132. }