1
0

index.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package cache
  2. import (
  3. "os"
  4. "path/filepath"
  5. "regexp"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/fsnotify/fsnotify"
  11. "github.com/uozi-tech/cosy/logger"
  12. )
  13. // ScanCallback is a function that gets called during config scanning
  14. // It receives the config file path and contents
  15. type ScanCallback func(configPath string, content []byte) error
  16. // Scanner is responsible for scanning and watching nginx config files
  17. type Scanner struct {
  18. watcher *fsnotify.Watcher // File system watcher
  19. scanTicker *time.Ticker // Ticker for periodic scanning
  20. initialized bool // Whether the scanner has been initialized
  21. scanning bool // Whether a scan is currently in progress
  22. scanMutex sync.RWMutex // Mutex for protecting the scanning state
  23. statusChan chan bool // Channel to broadcast scanning status changes
  24. subscribers map[chan bool]struct{} // Set of subscribers
  25. subscriberMux sync.RWMutex // Mutex for protecting the subscribers map
  26. }
  27. // Global variables
  28. var (
  29. // scanner is the singleton instance of Scanner
  30. scanner *Scanner
  31. configScannerInitMux sync.Mutex
  32. // This regex matches: include directives in nginx config files
  33. includeRegex = regexp.MustCompile(`include\s+([^;]+);`)
  34. // Global callbacks that will be executed during config file scanning
  35. scanCallbacks []ScanCallback
  36. scanCallbacksMutex sync.RWMutex
  37. )
  38. func init() {
  39. // Initialize the callbacks slice
  40. scanCallbacks = make([]ScanCallback, 0)
  41. }
  42. // InitScanner initializes the config scanner
  43. func InitScanner() {
  44. if nginx.GetConfPath() == "" {
  45. logger.Error("Nginx config path is not set")
  46. return
  47. }
  48. s := GetScanner()
  49. err := s.Initialize()
  50. if err != nil {
  51. logger.Error("Failed to initialize config scanner:", err)
  52. }
  53. }
  54. // GetScanner returns the singleton instance of Scanner
  55. func GetScanner() *Scanner {
  56. configScannerInitMux.Lock()
  57. defer configScannerInitMux.Unlock()
  58. if scanner == nil {
  59. scanner = &Scanner{
  60. statusChan: make(chan bool, 10), // Buffer to prevent blocking
  61. subscribers: make(map[chan bool]struct{}),
  62. }
  63. // Start broadcaster goroutine
  64. go scanner.broadcastStatus()
  65. }
  66. return scanner
  67. }
  68. // RegisterCallback adds a callback function to be executed during scans
  69. // This function can be called before Scanner is initialized
  70. func RegisterCallback(callback ScanCallback) {
  71. scanCallbacksMutex.Lock()
  72. defer scanCallbacksMutex.Unlock()
  73. scanCallbacks = append(scanCallbacks, callback)
  74. }
  75. // broadcastStatus listens for status changes and broadcasts to all subscribers
  76. func (s *Scanner) broadcastStatus() {
  77. for status := range s.statusChan {
  78. s.subscriberMux.RLock()
  79. for ch := range s.subscribers {
  80. // Non-blocking send to prevent slow subscribers from blocking others
  81. select {
  82. case ch <- status:
  83. default:
  84. // Skip if channel buffer is full
  85. }
  86. }
  87. s.subscriberMux.RUnlock()
  88. }
  89. }
  90. // SubscribeScanningStatus allows a client to subscribe to scanning status changes
  91. func SubscribeScanningStatus() chan bool {
  92. s := GetScanner()
  93. ch := make(chan bool, 5) // Buffer to prevent blocking
  94. // Add to subscribers
  95. s.subscriberMux.Lock()
  96. s.subscribers[ch] = struct{}{}
  97. s.subscriberMux.Unlock()
  98. // Send current status immediately
  99. s.scanMutex.RLock()
  100. currentStatus := s.scanning
  101. s.scanMutex.RUnlock()
  102. // Non-blocking send
  103. select {
  104. case ch <- currentStatus:
  105. default:
  106. }
  107. return ch
  108. }
  109. // UnsubscribeScanningStatus removes a subscriber from receiving status updates
  110. func UnsubscribeScanningStatus(ch chan bool) {
  111. s := GetScanner()
  112. s.subscriberMux.Lock()
  113. delete(s.subscribers, ch)
  114. s.subscriberMux.Unlock()
  115. // Close the channel so the client knows it's unsubscribed
  116. close(ch)
  117. }
  118. // Initialize sets up the scanner and starts watching for file changes
  119. func (s *Scanner) Initialize() error {
  120. if s.initialized {
  121. return nil
  122. }
  123. // Create a new watcher
  124. watcher, err := fsnotify.NewWatcher()
  125. if err != nil {
  126. return err
  127. }
  128. s.watcher = watcher
  129. // Scan for the first time
  130. err = s.ScanAllConfigs()
  131. if err != nil {
  132. return err
  133. }
  134. // Setup watcher for config directory
  135. configDir := filepath.Dir(nginx.GetConfPath())
  136. availableDir := nginx.GetConfPath("sites-available")
  137. enabledDir := nginx.GetConfPath("sites-enabled")
  138. streamAvailableDir := nginx.GetConfPath("stream-available")
  139. streamEnabledDir := nginx.GetConfPath("stream-enabled")
  140. // Watch the main directories
  141. err = s.watcher.Add(configDir)
  142. if err != nil {
  143. logger.Error("Failed to watch config directory:", err)
  144. }
  145. // Watch sites-available and sites-enabled if they exist
  146. if _, err := os.Stat(availableDir); err == nil {
  147. err = s.watcher.Add(availableDir)
  148. if err != nil {
  149. logger.Error("Failed to watch sites-available directory:", err)
  150. }
  151. }
  152. if _, err := os.Stat(enabledDir); err == nil {
  153. err = s.watcher.Add(enabledDir)
  154. if err != nil {
  155. logger.Error("Failed to watch sites-enabled directory:", err)
  156. }
  157. }
  158. // Watch stream-available and stream-enabled if they exist
  159. if _, err := os.Stat(streamAvailableDir); err == nil {
  160. err = s.watcher.Add(streamAvailableDir)
  161. if err != nil {
  162. logger.Error("Failed to watch stream-available directory:", err)
  163. }
  164. }
  165. if _, err := os.Stat(streamEnabledDir); err == nil {
  166. err = s.watcher.Add(streamEnabledDir)
  167. if err != nil {
  168. logger.Error("Failed to watch stream-enabled directory:", err)
  169. }
  170. }
  171. // Start the watcher goroutine
  172. go s.watchForChanges()
  173. // Setup a ticker for periodic scanning (every 5 minutes)
  174. s.scanTicker = time.NewTicker(5 * time.Minute)
  175. go func() {
  176. for range s.scanTicker.C {
  177. err := s.ScanAllConfigs()
  178. if err != nil {
  179. logger.Error("Periodic config scan failed:", err)
  180. }
  181. }
  182. }()
  183. s.initialized = true
  184. return nil
  185. }
  186. // watchForChanges handles the fsnotify events and triggers rescans when necessary
  187. func (s *Scanner) watchForChanges() {
  188. for {
  189. select {
  190. case event, ok := <-s.watcher.Events:
  191. if !ok {
  192. return
  193. }
  194. // Check if this is a relevant event (create, write, rename, remove)
  195. if event.Has(fsnotify.Create) || event.Has(fsnotify.Write) ||
  196. event.Has(fsnotify.Rename) || event.Has(fsnotify.Remove) {
  197. // If it's a directory, add it to the watch list
  198. if event.Has(fsnotify.Create) {
  199. fi, err := os.Stat(event.Name)
  200. if err == nil && fi.IsDir() {
  201. _ = s.watcher.Add(event.Name)
  202. }
  203. }
  204. // Process file changes
  205. if !event.Has(fsnotify.Remove) {
  206. logger.Debug("Config file changed:", event.Name)
  207. // Give the system a moment to finish writing the file
  208. time.Sleep(100 * time.Millisecond)
  209. // Only scan the changed file instead of all configs
  210. err := s.scanSingleFile(event.Name)
  211. if err != nil {
  212. logger.Error("Failed to scan changed file:", err)
  213. }
  214. } else {
  215. // For removed files, we need a full rescan
  216. err := s.ScanAllConfigs()
  217. if err != nil {
  218. logger.Error("Failed to rescan configs after file removal:", err)
  219. }
  220. }
  221. }
  222. case err, ok := <-s.watcher.Errors:
  223. if !ok {
  224. return
  225. }
  226. logger.Error("Watcher error:", err)
  227. }
  228. }
  229. }
  230. // scanSingleFile scans a single file and executes all registered callbacks
  231. func (s *Scanner) scanSingleFile(filePath string) error {
  232. // Set scanning state to true
  233. s.scanMutex.Lock()
  234. wasScanning := s.scanning
  235. s.scanning = true
  236. if !wasScanning {
  237. // Only broadcast if status changed from not scanning to scanning
  238. s.statusChan <- true
  239. }
  240. s.scanMutex.Unlock()
  241. // Ensure we reset scanning state when done
  242. defer func() {
  243. s.scanMutex.Lock()
  244. s.scanning = false
  245. // Broadcast the completion
  246. s.statusChan <- false
  247. s.scanMutex.Unlock()
  248. }()
  249. // Open the file
  250. file, err := os.Open(filePath)
  251. if err != nil {
  252. return err
  253. }
  254. defer file.Close()
  255. // Read the entire file content
  256. content, err := os.ReadFile(filePath)
  257. if err != nil {
  258. return err
  259. }
  260. // Execute all registered callbacks
  261. scanCallbacksMutex.RLock()
  262. for _, callback := range scanCallbacks {
  263. err := callback(filePath, content)
  264. if err != nil {
  265. logger.Error("Callback error for file", filePath, ":", err)
  266. }
  267. }
  268. scanCallbacksMutex.RUnlock()
  269. // Look for include directives to process included files
  270. includeMatches := includeRegex.FindAllSubmatch(content, -1)
  271. for _, match := range includeMatches {
  272. if len(match) >= 2 {
  273. includePath := string(match[1])
  274. // Handle glob patterns in include directives
  275. if strings.Contains(includePath, "*") {
  276. // If it's a relative path, make it absolute based on nginx config dir
  277. if !filepath.IsAbs(includePath) {
  278. configDir := filepath.Dir(nginx.GetConfPath("", ""))
  279. includePath = filepath.Join(configDir, includePath)
  280. }
  281. // Expand the glob pattern
  282. matchedFiles, err := filepath.Glob(includePath)
  283. if err != nil {
  284. logger.Error("Error expanding glob pattern:", includePath, err)
  285. continue
  286. }
  287. // Process each matched file
  288. for _, matchedFile := range matchedFiles {
  289. fileInfo, err := os.Stat(matchedFile)
  290. if err == nil && !fileInfo.IsDir() {
  291. err = s.scanSingleFile(matchedFile)
  292. if err != nil {
  293. logger.Error("Failed to scan included file:", matchedFile, err)
  294. }
  295. }
  296. }
  297. } else {
  298. // Handle single file include
  299. // If it's a relative path, make it absolute based on nginx config dir
  300. if !filepath.IsAbs(includePath) {
  301. configDir := filepath.Dir(nginx.GetConfPath("", ""))
  302. includePath = filepath.Join(configDir, includePath)
  303. }
  304. fileInfo, err := os.Stat(includePath)
  305. if err == nil && !fileInfo.IsDir() {
  306. err = s.scanSingleFile(includePath)
  307. if err != nil {
  308. logger.Error("Failed to scan included file:", includePath, err)
  309. }
  310. }
  311. }
  312. }
  313. }
  314. return nil
  315. }
  316. // ScanAllConfigs scans all nginx config files and executes all registered callbacks
  317. func (s *Scanner) ScanAllConfigs() error {
  318. // Set scanning state to true
  319. s.scanMutex.Lock()
  320. wasScanning := s.scanning
  321. s.scanning = true
  322. if !wasScanning {
  323. // Only broadcast if status changed from not scanning to scanning
  324. s.statusChan <- true
  325. }
  326. s.scanMutex.Unlock()
  327. // Ensure we reset scanning state when done
  328. defer func() {
  329. s.scanMutex.Lock()
  330. s.scanning = false
  331. // Broadcast the completion
  332. s.statusChan <- false
  333. s.scanMutex.Unlock()
  334. }()
  335. // Get the main config file
  336. mainConfigPath := nginx.GetConfPath("", "nginx.conf")
  337. err := s.scanSingleFile(mainConfigPath)
  338. if err != nil {
  339. logger.Error("Failed to scan main config:", err)
  340. }
  341. // Scan sites-available directory
  342. sitesAvailablePath := nginx.GetConfPath("sites-available", "")
  343. sitesAvailableFiles, err := os.ReadDir(sitesAvailablePath)
  344. if err == nil {
  345. for _, file := range sitesAvailableFiles {
  346. if !file.IsDir() {
  347. configPath := filepath.Join(sitesAvailablePath, file.Name())
  348. err := s.scanSingleFile(configPath)
  349. if err != nil {
  350. logger.Error("Failed to scan config:", configPath, err)
  351. }
  352. }
  353. }
  354. }
  355. // Scan stream-available directory if it exists
  356. streamAvailablePath := nginx.GetConfPath("stream-available", "")
  357. streamAvailableFiles, err := os.ReadDir(streamAvailablePath)
  358. if err == nil {
  359. for _, file := range streamAvailableFiles {
  360. if !file.IsDir() {
  361. configPath := filepath.Join(streamAvailablePath, file.Name())
  362. err := s.scanSingleFile(configPath)
  363. if err != nil {
  364. logger.Error("Failed to scan stream config:", configPath, err)
  365. }
  366. }
  367. }
  368. }
  369. return nil
  370. }
  371. // Shutdown cleans up resources used by the scanner
  372. func (s *Scanner) Shutdown() {
  373. if s.watcher != nil {
  374. s.watcher.Close()
  375. }
  376. if s.scanTicker != nil {
  377. s.scanTicker.Stop()
  378. }
  379. // Clean up subscriber resources
  380. s.subscriberMux.Lock()
  381. // Close all subscriber channels
  382. for ch := range s.subscribers {
  383. close(ch)
  384. }
  385. // Clear the map
  386. s.subscribers = make(map[chan bool]struct{})
  387. s.subscriberMux.Unlock()
  388. // Close the status channel
  389. close(s.statusChan)
  390. }
  391. // IsScanningInProgress returns whether a scan is currently in progress
  392. func IsScanningInProgress() bool {
  393. s := GetScanner()
  394. s.scanMutex.RLock()
  395. defer s.scanMutex.RUnlock()
  396. return s.scanning
  397. }