1
0

nginx_log.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package nginx_log
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "regexp"
  7. "github.com/0xJacky/Nginx-UI/internal/cache"
  8. "github.com/0xJacky/Nginx-UI/internal/helper"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/settings"
  11. "github.com/uozi-tech/cosy/logger"
  12. )
  13. // Regular expression for log directives - matches access_log or error_log
  14. var logDirectiveRegex = regexp.MustCompile(`(?m)(access_log|error_log)\s+([^\s;]+)(?:\s+[^;]+)?;`)
  15. // Use init function to automatically register callback
  16. func init() {
  17. // Register the callback directly with the global registry
  18. cache.RegisterCallback(scanForLogDirectives)
  19. }
  20. // scanForLogDirectives scans and parses configuration files for log directives
  21. func scanForLogDirectives(configPath string, content []byte) error {
  22. // Find log directives using regex
  23. matches := logDirectiveRegex.FindAllSubmatch(content, -1)
  24. // Parse log paths
  25. for _, match := range matches {
  26. if len(match) >= 3 {
  27. directiveType := string(match[1]) // "access_log" or "error_log"
  28. logPath := string(match[2]) // Path to log file
  29. // Validate log path
  30. if IsLogPathUnderWhiteList(logPath) && isValidLogPath(logPath) {
  31. logType := "access"
  32. if directiveType == "error_log" {
  33. logType = "error"
  34. }
  35. // Add to cache
  36. AddLogPath(logPath, logType, filepath.Base(logPath))
  37. }
  38. }
  39. }
  40. return nil
  41. }
  42. // GetAllLogs returns all log paths
  43. func GetAllLogs(filters ...func(*NginxLogCache) bool) []*NginxLogCache {
  44. return GetAllLogPaths(filters...)
  45. }
  46. // isValidLogPath checks if a log path is valid:
  47. // 1. It must be a regular file or a symlink to a regular file
  48. // 2. It must not point to a console or special device
  49. // 3. It must be under the whitelist directories
  50. func isValidLogPath(logPath string) bool {
  51. // First check if the path is in the whitelist
  52. if !IsLogPathUnderWhiteList(logPath) {
  53. logger.Warn("Log path is not under whitelist:", logPath)
  54. return false
  55. }
  56. // Check if the path exists
  57. fileInfo, err := os.Lstat(logPath)
  58. if err != nil {
  59. // If the file doesn't exist, it might be created later
  60. // We'll assume it's valid for now
  61. return true
  62. }
  63. // If it's a symlink, follow it
  64. if fileInfo.Mode()&os.ModeSymlink != 0 {
  65. linkTarget, err := os.Readlink(logPath)
  66. if err != nil {
  67. return false
  68. }
  69. // Make the link target path absolute if it's relative
  70. if !filepath.IsAbs(linkTarget) {
  71. linkTarget = filepath.Join(filepath.Dir(logPath), linkTarget)
  72. }
  73. // Check the target file
  74. targetInfo, err := os.Stat(linkTarget)
  75. if err != nil {
  76. return false
  77. }
  78. // Only accept regular files as targets
  79. return targetInfo.Mode().IsRegular()
  80. }
  81. // For non-symlinks, just check if it's a regular file
  82. return fileInfo.Mode().IsRegular()
  83. }
  84. // IsLogPathUnderWhiteList checks if a log path is under one of the paths in LogDirWhiteList
  85. func IsLogPathUnderWhiteList(path string) bool {
  86. cacheKey := fmt.Sprintf("isLogPathUnderWhiteList:%s", path)
  87. res, ok := cache.Get(cacheKey)
  88. // Deep copy the whitelist
  89. logDirWhiteList := append([]string{}, settings.NginxSettings.LogDirWhiteList...)
  90. accessLogPath := nginx.GetAccessLogPath()
  91. errorLogPath := nginx.GetErrorLogPath()
  92. if accessLogPath != "" {
  93. logDirWhiteList = append(logDirWhiteList, filepath.Dir(accessLogPath))
  94. }
  95. if errorLogPath != "" {
  96. logDirWhiteList = append(logDirWhiteList, filepath.Dir(errorLogPath))
  97. }
  98. // No cache, check it
  99. if !ok {
  100. for _, whitePath := range logDirWhiteList {
  101. if helper.IsUnderDirectory(path, whitePath) {
  102. cache.Set(cacheKey, true, 0)
  103. return true
  104. }
  105. }
  106. return false
  107. }
  108. return res.(bool)
  109. }