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.

access.go 4.1KB

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