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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package cli
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "net/url"
  7. "os"
  8. "sort"
  9. "strconv"
  10. "sync"
  11. "github.com/dolonet/mtg-multi/internal/config"
  12. "github.com/dolonet/mtg-multi/internal/utils"
  13. "github.com/dolonet/mtg-multi/mtglib"
  14. )
  15. type accessResponseSecret struct {
  16. Hex string `json:"hex"`
  17. Base64 string `json:"base64"`
  18. }
  19. type accessResponse struct {
  20. IPv4 *accessResponseURLs `json:"ipv4,omitempty"`
  21. IPv6 *accessResponseURLs `json:"ipv6,omitempty"`
  22. Secret accessResponseSecret `json:"secret"`
  23. Secrets map[string]accessResponseSecret `json:"secrets,omitempty"`
  24. }
  25. type accessResponseURLs struct {
  26. IP net.IP `json:"ip"`
  27. Port uint `json:"port"`
  28. TgURL string `json:"tg_url"` //nolint: tagliatelle
  29. TgQrCode string `json:"tg_qrcode"` //nolint: tagliatelle
  30. TmeURL string `json:"tme_url"` //nolint: tagliatelle
  31. TmeQrCode string `json:"tme_qrcode"` //nolint: tagliatelle
  32. }
  33. type Access struct {
  34. ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` //nolint: lll
  35. PublicIPv4 net.IP `kong:"help='Public IPv4 address for proxy. By default it is resolved via remote website',name='ipv4',short='i'"` //nolint: lll
  36. PublicIPv6 net.IP `kong:"help='Public IPv6 address for proxy. By default it is resolved via remote website',name='ipv6',short='I'"` //nolint: lll
  37. Port uint `kong:"help='Port number. Default port is taken from configuration file, bind-to parameter',type:'uint',short='p'"` //nolint: lll
  38. Hex bool `kong:"help='Print secret in hex encoding.',short='x'"`
  39. }
  40. func (a *Access) Run(cli *CLI, version string) error {
  41. conf, err := utils.ReadConfig(a.ConfigPath)
  42. if err != nil {
  43. return fmt.Errorf("cannot init config: %w", err)
  44. }
  45. resp := &accessResponse{}
  46. secrets := conf.GetSecrets()
  47. // Sort secret names for deterministic "first secret" selection.
  48. sortedNames := make([]string, 0, len(secrets))
  49. for name := range secrets {
  50. sortedNames = append(sortedNames, name)
  51. }
  52. sort.Strings(sortedNames)
  53. // For backward compatibility, populate the single Secret field with the
  54. // first secret (sorted alphabetically).
  55. if len(sortedNames) > 0 {
  56. first := secrets[sortedNames[0]]
  57. resp.Secret.Base64 = first.Base64()
  58. resp.Secret.Hex = first.Hex()
  59. }
  60. if len(secrets) > 1 {
  61. resp.Secrets = make(map[string]accessResponseSecret, len(secrets))
  62. for name, s := range secrets {
  63. resp.Secrets[name] = accessResponseSecret{
  64. Hex: s.Hex(),
  65. Base64: s.Base64(),
  66. }
  67. }
  68. }
  69. ntw, err := makeNetwork(conf, version)
  70. if err != nil {
  71. return fmt.Errorf("cannot init network: %w", err)
  72. }
  73. wg := &sync.WaitGroup{}
  74. wg.Go(func() {
  75. ip := a.PublicIPv4
  76. if ip == nil {
  77. ip = conf.PublicIPv4.Get(nil)
  78. }
  79. if ip == nil {
  80. ip = getIP(ntw, "tcp4")
  81. }
  82. if ip != nil {
  83. ip = ip.To4()
  84. }
  85. resp.IPv4 = a.makeURLs(conf, ip)
  86. })
  87. wg.Go(func() {
  88. ip := a.PublicIPv6
  89. if ip == nil {
  90. ip = conf.PublicIPv6.Get(nil)
  91. }
  92. if ip == nil {
  93. ip = getIP(ntw, "tcp6")
  94. }
  95. if ip != nil {
  96. ip = ip.To16()
  97. }
  98. resp.IPv6 = a.makeURLs(conf, ip)
  99. })
  100. wg.Wait()
  101. encoder := json.NewEncoder(os.Stdout)
  102. encoder.SetEscapeHTML(false)
  103. encoder.SetIndent("", " ")
  104. if err := encoder.Encode(resp); err != nil {
  105. return fmt.Errorf("cannot dump access json: %w", err)
  106. }
  107. return nil
  108. }
  109. func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
  110. if ip == nil {
  111. return nil
  112. }
  113. portNo := a.Port
  114. if portNo == 0 {
  115. portNo = conf.BindTo.Port
  116. }
  117. values := url.Values{}
  118. values.Set("server", ip.String())
  119. values.Set("port", strconv.Itoa(int(portNo)))
  120. // Use the first available secret (sorted) for URL generation.
  121. secrets := conf.GetSecrets()
  122. names := make([]string, 0, len(secrets))
  123. for name := range secrets {
  124. names = append(names, name)
  125. }
  126. sort.Strings(names)
  127. var firstSecret mtglib.Secret
  128. if len(names) > 0 {
  129. firstSecret = secrets[names[0]]
  130. }
  131. if a.Hex {
  132. values.Set("secret", firstSecret.Hex())
  133. } else {
  134. values.Set("secret", firstSecret.Base64())
  135. }
  136. urlQuery := values.Encode()
  137. rv := &accessResponseURLs{
  138. IP: ip,
  139. Port: portNo,
  140. TgURL: (&url.URL{
  141. Scheme: "tg",
  142. Host: "proxy",
  143. RawQuery: urlQuery,
  144. }).String(),
  145. TmeURL: (&url.URL{
  146. Scheme: "https",
  147. Host: "t.me",
  148. Path: "proxy",
  149. RawQuery: urlQuery,
  150. }).String(),
  151. }
  152. rv.TgQrCode = utils.MakeQRCodeURL(rv.TgURL)
  153. rv.TmeQrCode = utils.MakeQRCodeURL(rv.TmeURL)
  154. return rv
  155. }