resolve_path.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package nginx
  2. import (
  3. "path/filepath"
  4. "regexp"
  5. "runtime"
  6. "strings"
  7. "github.com/0xJacky/Nginx-UI/internal/helper"
  8. "github.com/0xJacky/Nginx-UI/settings"
  9. "github.com/uozi-tech/cosy/logger"
  10. )
  11. // Returns the directory containing the nginx executable
  12. func GetNginxExeDir() string {
  13. return filepath.Dir(getNginxSbinPath())
  14. }
  15. // Resolves relative paths by joining them with the nginx executable directory on Windows
  16. func resolvePath(path string) string {
  17. if path == "" {
  18. return ""
  19. }
  20. // Handle relative paths on Windows
  21. if runtime.GOOS == "windows" && !filepath.IsAbs(path) {
  22. return filepath.Join(GetNginxExeDir(), path)
  23. }
  24. return path
  25. }
  26. // GetPrefix returns the prefix of the nginx executable
  27. func GetPrefix() string {
  28. out := getNginxV()
  29. r, _ := regexp.Compile(`--prefix=(\S+)`)
  30. match := r.FindStringSubmatch(out)
  31. if len(match) < 1 {
  32. logger.Error("nginx.GetPrefix len(match) < 1")
  33. return "/usr/local/nginx"
  34. }
  35. return resolvePath(match[1])
  36. }
  37. // GetConfPath returns the path of the nginx configuration file
  38. func GetConfPath(dir ...string) (confPath string) {
  39. if settings.NginxSettings.ConfigDir == "" {
  40. out := getNginxV()
  41. r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
  42. match := r.FindStringSubmatch(out)
  43. if len(match) < 1 {
  44. logger.Error("nginx.GetConfPath len(match) < 1")
  45. return ""
  46. }
  47. confPath = match[1]
  48. } else {
  49. confPath = settings.NginxSettings.ConfigDir
  50. }
  51. confPath = resolvePath(confPath)
  52. joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
  53. if !helper.IsUnderDirectory(joined, confPath) {
  54. return confPath
  55. }
  56. return joined
  57. }
  58. // GetConfEntryPath returns the path of the nginx configuration file
  59. func GetConfEntryPath() (path string) {
  60. if settings.NginxSettings.ConfigPath == "" {
  61. out := getNginxV()
  62. r, _ := regexp.Compile("--conf-path=(.*.conf)")
  63. match := r.FindStringSubmatch(out)
  64. if len(match) < 1 {
  65. logger.Error("nginx.GetConfEntryPath len(match) < 1")
  66. return ""
  67. }
  68. path = match[1]
  69. } else {
  70. path = settings.NginxSettings.ConfigPath
  71. }
  72. return resolvePath(path)
  73. }
  74. // GetPIDPath returns the path of the nginx PID file
  75. func GetPIDPath() (path string) {
  76. if settings.NginxSettings.PIDPath == "" {
  77. out := getNginxV()
  78. r, _ := regexp.Compile("--pid-path=(.*.pid)")
  79. match := r.FindStringSubmatch(out)
  80. if len(match) < 1 {
  81. logger.Error("pid path not found in nginx -V output")
  82. return ""
  83. }
  84. path = match[1]
  85. } else {
  86. path = settings.NginxSettings.PIDPath
  87. }
  88. return resolvePath(path)
  89. }
  90. // GetSbinPath returns the path of the nginx executable
  91. func GetSbinPath() (path string) {
  92. return getNginxSbinPath()
  93. }
  94. // GetAccessLogPath returns the path of the nginx access log file
  95. func GetAccessLogPath() (path string) {
  96. path = settings.NginxSettings.AccessLogPath
  97. if path == "" {
  98. out := getNginxV()
  99. r, _ := regexp.Compile(`--http-log-path=(\S+)`)
  100. match := r.FindStringSubmatch(out)
  101. if len(match) > 1 {
  102. path = match[1]
  103. }
  104. if path == "" {
  105. logger.Debug("access log path not found in nginx -V output, try to get from nginx -T output")
  106. path = getAccessLogPathFromNginxT()
  107. }
  108. }
  109. return resolvePath(path)
  110. }
  111. // GetErrorLogPath returns the path of the nginx error log file
  112. func GetErrorLogPath() string {
  113. path := settings.NginxSettings.ErrorLogPath
  114. if path == "" {
  115. out := getNginxV()
  116. r, _ := regexp.Compile(`--error-log-path=(\S+)`)
  117. match := r.FindStringSubmatch(out)
  118. if len(match) > 1 {
  119. path = match[1]
  120. }
  121. if path == "" {
  122. logger.Debug("error log path not found in nginx -V output, try to get from nginx -T output")
  123. path = getErrorLogPathFromNginxT()
  124. }
  125. }
  126. return resolvePath(path)
  127. }
  128. // GetModulesPath returns the path of the nginx modules
  129. func GetModulesPath() string {
  130. // First try to get from nginx -V output
  131. out := getNginxV()
  132. if out != "" {
  133. // Look for --modules-path in the output
  134. if strings.Contains(out, "--modules-path=") {
  135. parts := strings.Split(out, "--modules-path=")
  136. if len(parts) > 1 {
  137. // Extract the path
  138. path := strings.Split(parts[1], " ")[0]
  139. // Remove quotes if present
  140. path = strings.Trim(path, "\"")
  141. return resolvePath(path)
  142. }
  143. }
  144. }
  145. // Default path if not found
  146. if runtime.GOOS == "windows" {
  147. return resolvePath("modules")
  148. }
  149. return resolvePath("/usr/lib/nginx/modules")
  150. }