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
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

access.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 = getIP(ntw, "tcp4")
  53. }
  54. if ip != nil {
  55. ip = ip.To4()
  56. }
  57. resp.IPv4 = a.makeURLs(conf, ip)
  58. })
  59. wg.Go(func() {
  60. ip := a.PublicIPv6
  61. if ip == nil {
  62. ip = getIP(ntw, "tcp6")
  63. }
  64. if ip != nil {
  65. ip = ip.To16()
  66. }
  67. resp.IPv6 = a.makeURLs(conf, ip)
  68. })
  69. wg.Wait()
  70. encoder := json.NewEncoder(os.Stdout)
  71. encoder.SetEscapeHTML(false)
  72. encoder.SetIndent("", " ")
  73. if err := encoder.Encode(resp); err != nil {
  74. return fmt.Errorf("cannot dump access json: %w", err)
  75. }
  76. return nil
  77. }
  78. func (a *Access) makeURLs(conf *config.Config, ip net.IP) *accessResponseURLs {
  79. if ip == nil {
  80. return nil
  81. }
  82. portNo := a.Port
  83. if portNo == 0 {
  84. portNo = conf.BindTo.Port
  85. }
  86. values := url.Values{}
  87. values.Set("server", ip.String())
  88. values.Set("port", strconv.Itoa(int(portNo)))
  89. if a.Hex {
  90. values.Set("secret", conf.Secret.Hex())
  91. } else {
  92. values.Set("secret", conf.Secret.Base64())
  93. }
  94. urlQuery := values.Encode()
  95. rv := &accessResponseURLs{
  96. IP: ip,
  97. Port: portNo,
  98. TgURL: (&url.URL{
  99. Scheme: "tg",
  100. Host: "proxy",
  101. RawQuery: urlQuery,
  102. }).String(),
  103. TmeURL: (&url.URL{
  104. Scheme: "https",
  105. Host: "t.me",
  106. Path: "proxy",
  107. RawQuery: urlQuery,
  108. }).String(),
  109. }
  110. rv.TgQrCode = utils.MakeQRCodeURL(rv.TgURL)
  111. rv.TmeQrCode = utils.MakeQRCodeURL(rv.TmeURL)
  112. return rv
  113. }