server_resources.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package clashapi
  2. import (
  3. "archive/zip"
  4. "context"
  5. "io"
  6. "net"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "github.com/sagernet/sing-box/adapter"
  12. C "github.com/sagernet/sing-box/constant"
  13. "github.com/sagernet/sing/common"
  14. E "github.com/sagernet/sing/common/exceptions"
  15. M "github.com/sagernet/sing/common/metadata"
  16. "github.com/sagernet/sing/service/filemanager"
  17. )
  18. func (s *Server) checkAndDownloadExternalUI() {
  19. if s.externalUI == "" {
  20. return
  21. }
  22. entries, err := os.ReadDir(s.externalUI)
  23. if err != nil {
  24. os.MkdirAll(s.externalUI, 0o755)
  25. }
  26. if len(entries) == 0 {
  27. err = s.downloadExternalUI()
  28. if err != nil {
  29. s.logger.Error("download external ui error: ", err)
  30. }
  31. }
  32. }
  33. func (s *Server) downloadExternalUI() error {
  34. var downloadURL string
  35. if s.externalUIDownloadURL != "" {
  36. downloadURL = s.externalUIDownloadURL
  37. } else {
  38. downloadURL = "https://github.com/MetaCubeX/Yacd-meta/archive/gh-pages.zip"
  39. }
  40. var detour adapter.Outbound
  41. if s.externalUIDownloadDetour != "" {
  42. outbound, loaded := s.outbound.Outbound(s.externalUIDownloadDetour)
  43. if !loaded {
  44. return E.New("detour outbound not found: ", s.externalUIDownloadDetour)
  45. }
  46. detour = outbound
  47. } else {
  48. outbound := s.outbound.Default()
  49. detour = outbound
  50. }
  51. s.logger.Info("downloading external ui using outbound/", detour.Type(), "[", detour.Tag(), "]")
  52. httpClient := &http.Client{
  53. Transport: &http.Transport{
  54. ForceAttemptHTTP2: true,
  55. TLSHandshakeTimeout: C.TCPTimeout,
  56. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  57. return detour.DialContext(ctx, network, M.ParseSocksaddr(addr))
  58. },
  59. },
  60. }
  61. defer httpClient.CloseIdleConnections()
  62. response, err := httpClient.Get(downloadURL)
  63. if err != nil {
  64. return err
  65. }
  66. defer response.Body.Close()
  67. if response.StatusCode != http.StatusOK {
  68. return E.New("download external ui failed: ", response.Status)
  69. }
  70. err = s.downloadZIP(filepath.Base(downloadURL), response.Body, s.externalUI)
  71. if err != nil {
  72. removeAllInDirectory(s.externalUI)
  73. }
  74. return err
  75. }
  76. func (s *Server) downloadZIP(name string, body io.Reader, output string) error {
  77. tempFile, err := filemanager.CreateTemp(s.ctx, name)
  78. if err != nil {
  79. return err
  80. }
  81. defer os.Remove(tempFile.Name())
  82. _, err = io.Copy(tempFile, body)
  83. tempFile.Close()
  84. if err != nil {
  85. return err
  86. }
  87. reader, err := zip.OpenReader(tempFile.Name())
  88. if err != nil {
  89. return err
  90. }
  91. defer reader.Close()
  92. trimDir := zipIsInSingleDirectory(reader.File)
  93. for _, file := range reader.File {
  94. if file.FileInfo().IsDir() {
  95. continue
  96. }
  97. pathElements := strings.Split(file.Name, "/")
  98. if trimDir {
  99. pathElements = pathElements[1:]
  100. }
  101. saveDirectory := output
  102. if len(pathElements) > 1 {
  103. saveDirectory = filepath.Join(saveDirectory, filepath.Join(pathElements[:len(pathElements)-1]...))
  104. }
  105. err = os.MkdirAll(saveDirectory, 0o755)
  106. if err != nil {
  107. return err
  108. }
  109. savePath := filepath.Join(saveDirectory, pathElements[len(pathElements)-1])
  110. err = downloadZIPEntry(s.ctx, file, savePath)
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. return nil
  116. }
  117. func downloadZIPEntry(ctx context.Context, zipFile *zip.File, savePath string) error {
  118. saveFile, err := filemanager.Create(ctx, savePath)
  119. if err != nil {
  120. return err
  121. }
  122. defer saveFile.Close()
  123. reader, err := zipFile.Open()
  124. if err != nil {
  125. return err
  126. }
  127. defer reader.Close()
  128. return common.Error(io.Copy(saveFile, reader))
  129. }
  130. func removeAllInDirectory(directory string) {
  131. dirEntries, err := os.ReadDir(directory)
  132. if err != nil {
  133. return
  134. }
  135. for _, dirEntry := range dirEntries {
  136. os.RemoveAll(filepath.Join(directory, dirEntry.Name()))
  137. }
  138. }
  139. func zipIsInSingleDirectory(files []*zip.File) bool {
  140. var singleDirectory string
  141. for _, file := range files {
  142. if file.FileInfo().IsDir() {
  143. continue
  144. }
  145. pathElements := strings.Split(file.Name, "/")
  146. if len(pathElements) == 0 {
  147. return false
  148. }
  149. if singleDirectory == "" {
  150. singleDirectory = pathElements[0]
  151. } else if singleDirectory != pathElements[0] {
  152. return false
  153. }
  154. }
  155. return true
  156. }