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

cli_access.go 3.2KB

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