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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package cli
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "os"
  9. "strconv"
  10. "sync"
  11. "github.com/9seconds/mtg/v2/internal/config"
  12. "github.com/9seconds/mtg/v2/internal/utils"
  13. )
  14. type accessResponse struct {
  15. IPv4 *accessResponseURLs `json:"ipv4,omitempty"`
  16. IPv6 *accessResponseURLs `json:"ipv6,omitempty"`
  17. Secret struct {
  18. Hex string `json:"hex"`
  19. Base64 string `json:"base64"`
  20. } `json:"secret"`
  21. }
  22. type accessResponseURLs struct {
  23. IP net.IP `json:"ip"`
  24. Port uint `json:"port"`
  25. TgURL string `json:"tg_url"` //nolint: tagliatelle
  26. TgQrCode string `json:"tg_qrcode"` //nolint: tagliatelle
  27. TmeURL string `json:"tme_url"` //nolint: tagliatelle
  28. TmeQrCode string `json:"tme_qrcode"` //nolint: tagliatelle
  29. }
  30. type Access struct {
  31. ConfigPath string `kong:"arg,required,type='existingfile',help='Path to the configuration file.',name='config-path'"` //nolint: lll
  32. PublicIPv4 net.IP `kong:"help='Public IPv4 address for proxy. By default it is resolved via remote website',name='ipv4',short='i'"` //nolint: lll
  33. PublicIPv6 net.IP `kong:"help='Public IPv6 address for proxy. By default it is resolved via remote website',name='ipv6',short='I'"` //nolint: lll
  34. Port uint `kong:"help='Port number. Default port is taken from configuration file, bind-to parameter',type:'uint',short='p'"` //nolint: lll
  35. Hex bool `kong:"help='Print secret in hex encoding.',short='x'"`
  36. }
  37. func (a *Access) Run(cli *CLI, version string) error {
  38. conf, err := utils.ReadConfig(a.ConfigPath)
  39. if err != nil {
  40. return fmt.Errorf("cannot init config: %w", err)
  41. }
  42. resp := &accessResponse{}
  43. resp.Secret.Base64 = conf.Secret.Base64()
  44. resp.Secret.Hex = conf.Secret.Hex()
  45. ntw, err := makeNetwork(conf, version)
  46. if err != nil {
  47. return fmt.Errorf("cannot init network: %w", err)
  48. }
  49. ctx, cancel := context.WithTimeout(context.Background(), getIPTimeout)
  50. defer cancel()
  51. wg := &sync.WaitGroup{}
  52. wg.Go(func() {
  53. ip := a.PublicIPv4
  54. if ip == nil {
  55. ip = conf.PublicIPv4.Get(nil)
  56. }
  57. if ip == nil {
  58. ip, _ = getIP(ctx, ntw, "tcp4")
  59. }
  60. if ip != nil {
  61. resp.IPv4 = a.makeURLs(conf, ip.To4())
  62. }
  63. })
  64. wg.Go(func() {
  65. ip := a.PublicIPv6
  66. if ip == nil {
  67. ip = conf.PublicIPv6.Get(nil)
  68. }
  69. if ip == nil {
  70. ip, _ = getIP(ctx, ntw, "tcp6")
  71. }
  72. if ip != nil {
  73. resp.IPv6 = a.makeURLs(conf, ip.To16())
  74. }
  75. })
  76. wg.Wait()
  77. encoder := json.NewEncoder(os.Stdout)
  78. encoder.SetEscapeHTML(false)
  79. encoder.SetIndent("", " ")
  80. if err := encoder.Encode(resp); err != nil {
  81. return fmt.Errorf("cannot dump access json: %w", err)
  82. }
  83. return nil
  84. }
  85. func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
  86. if ip == nil {
  87. return nil
  88. }
  89. portNo := a.Port
  90. if portNo == 0 {
  91. portNo = conf.BindTo.Port
  92. }
  93. values := url.Values{}
  94. values.Set("server", ip.String())
  95. values.Set("port", strconv.Itoa(int(portNo)))
  96. if a.Hex {
  97. values.Set("secret", conf.Secret.Hex())
  98. } else {
  99. values.Set("secret", conf.Secret.Base64())
  100. }
  101. urlQuery := values.Encode()
  102. rv := &accessResponseURLs{
  103. IP: ip,
  104. Port: portNo,
  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 = utils.MakeQRCodeURL(rv.TgURL)
  118. rv.TmeQrCode = utils.MakeQRCodeURL(rv.TmeURL)
  119. return rv
  120. }