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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

access.go 3.3KB

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