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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cli_access.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. c.network.PatchHTTPClient(client)
  68. resp, err := client.Get("https://ifconfig.co") // nolint: bodyclose, noctx
  69. if err != nil {
  70. return nil
  71. }
  72. if resp.StatusCode != http.StatusOK {
  73. return nil
  74. }
  75. defer exhaustResponse(resp)
  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 *cliCommandAccess) makeResponseURLs(ip net.IP, cli *CLI) *runAccessResponseURLs {
  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.port.Value(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 := &runAccessResponseURLs{
  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.makeResponseQRCode(rv.TgURL)
  110. rv.TmeQrCode = c.makeResponseQRCode(rv.TmeURL)
  111. return rv
  112. }
  113. func (c *cliCommandAccess) makeResponseQRCode(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. }