1
0

resolve_path.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. var (
  12. nginxPrefix string
  13. )
  14. // GetNginxExeDir Returns the directory containing the nginx executable
  15. func GetNginxExeDir() string {
  16. return filepath.Dir(getNginxSbinPath())
  17. }
  18. // Resolves relative paths by joining them with the nginx executable directory on Windows
  19. func resolvePath(path string) string {
  20. if path == "" {
  21. return ""
  22. }
  23. // Handle relative paths on Windows
  24. if runtime.GOOS == "windows" && !filepath.IsAbs(path) {
  25. return filepath.Join(GetNginxExeDir(), path)
  26. }
  27. return path
  28. }
  29. // GetPrefix returns the prefix of the nginx executable
  30. func GetPrefix() string {
  31. if nginxPrefix != "" {
  32. return nginxPrefix
  33. }
  34. out := getNginxV()
  35. r, _ := regexp.Compile(`--prefix=(\S+)`)
  36. match := r.FindStringSubmatch(out)
  37. if len(match) < 1 {
  38. logger.Debug("nginx.GetPrefix len(match) < 1")
  39. if runtime.GOOS == "windows" {
  40. nginxPrefix = GetNginxExeDir()
  41. } else {
  42. nginxPrefix = "/usr/local/nginx"
  43. }
  44. return nginxPrefix
  45. }
  46. nginxPrefix = resolvePath(match[1])
  47. return nginxPrefix
  48. }
  49. // GetConfPath returns the path of the nginx configuration file
  50. func GetConfPath(dir ...string) (confPath string) {
  51. if settings.NginxSettings.ConfigDir == "" {
  52. out := getNginxV()
  53. r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
  54. match := r.FindStringSubmatch(out)
  55. if len(match) < 1 {
  56. logger.Error("nginx.GetConfPath len(match) < 1")
  57. return ""
  58. }
  59. confPath = match[1]
  60. } else {
  61. confPath = settings.NginxSettings.ConfigDir
  62. }
  63. confPath = resolvePath(confPath)
  64. joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
  65. if !helper.IsUnderDirectory(joined, confPath) {
  66. return confPath
  67. }
  68. return joined
  69. }
  70. // GetConfEntryPath returns the path of the nginx configuration file
  71. func GetConfEntryPath() (path string) {
  72. if settings.NginxSettings.ConfigPath == "" {
  73. out := getNginxV()
  74. r, _ := regexp.Compile("--conf-path=(.*.conf)")
  75. match := r.FindStringSubmatch(out)
  76. if len(match) < 1 {
  77. logger.Error("nginx.GetConfEntryPath len(match) < 1")
  78. return ""
  79. }
  80. path = match[1]
  81. } else {
  82. path = settings.NginxSettings.ConfigPath
  83. }
  84. return resolvePath(path)
  85. }
  86. // GetPIDPath returns the path of the nginx PID file
  87. func GetPIDPath() (path string) {
  88. if settings.NginxSettings.PIDPath == "" {
  89. out := getNginxV()
  90. r, _ := regexp.Compile("--pid-path=(.*.pid)")
  91. match := r.FindStringSubmatch(out)
  92. if len(match) < 1 {
  93. logger.Error("pid path not found in nginx -V output")
  94. return ""
  95. }
  96. path = match[1]
  97. } else {
  98. path = settings.NginxSettings.PIDPath
  99. }
  100. return resolvePath(path)
  101. }
  102. // GetSbinPath returns the path of the nginx executable
  103. func GetSbinPath() (path string) {
  104. return getNginxSbinPath()
  105. }
  106. // GetAccessLogPath returns the path of the nginx access log file
  107. func GetAccessLogPath() (path string) {
  108. path = settings.NginxSettings.AccessLogPath
  109. if path == "" {
  110. out := getNginxV()
  111. r, _ := regexp.Compile(`--http-log-path=(\S+)`)
  112. match := r.FindStringSubmatch(out)
  113. if len(match) > 1 {
  114. path = match[1]
  115. resolvedPath := resolvePath(path)
  116. // Check if the matched path exists but is not a regular file
  117. if !isValidRegularFile(resolvedPath) {
  118. logger.Debug("access log path from nginx -V exists but is not a regular file, try to get from nginx -T output", "path", resolvedPath)
  119. fallbackPath := getAccessLogPathFromNginxT()
  120. if fallbackPath != "" {
  121. path = fallbackPath
  122. return path // Already resolved in getAccessLogPathFromNginxT
  123. }
  124. }
  125. }
  126. if path == "" {
  127. logger.Debug("access log path not found in nginx -V output, try to get from nginx -T output")
  128. path = getAccessLogPathFromNginxT()
  129. }
  130. }
  131. return resolvePath(path)
  132. }
  133. // GetErrorLogPath returns the path of the nginx error log file
  134. func GetErrorLogPath() string {
  135. path := settings.NginxSettings.ErrorLogPath
  136. if path == "" {
  137. out := getNginxV()
  138. r, _ := regexp.Compile(`--error-log-path=(\S+)`)
  139. match := r.FindStringSubmatch(out)
  140. if len(match) > 1 {
  141. path = match[1]
  142. resolvedPath := resolvePath(path)
  143. // Check if the matched path exists but is not a regular file
  144. if !isValidRegularFile(resolvedPath) {
  145. logger.Debug("error log path from nginx -V exists but is not a regular file, try to get from nginx -T output", "path", resolvedPath)
  146. fallbackPath := getErrorLogPathFromNginxT()
  147. if fallbackPath != "" {
  148. path = fallbackPath
  149. return path // Already resolved in getErrorLogPathFromNginxT
  150. }
  151. }
  152. }
  153. if path == "" {
  154. logger.Debug("error log path not found in nginx -V output, try to get from nginx -T output")
  155. path = getErrorLogPathFromNginxT()
  156. }
  157. }
  158. return resolvePath(path)
  159. }
  160. // GetModulesPath returns the path of the nginx modules
  161. func GetModulesPath() string {
  162. // First try to get from nginx -V output
  163. out := getNginxV()
  164. if out != "" {
  165. // Look for --modules-path in the output
  166. if strings.Contains(out, "--modules-path=") {
  167. parts := strings.Split(out, "--modules-path=")
  168. if len(parts) > 1 {
  169. // Extract the path
  170. path := strings.Split(parts[1], " ")[0]
  171. // Remove quotes if present
  172. path = strings.Trim(path, "\"")
  173. return resolvePath(path)
  174. }
  175. }
  176. }
  177. // Default path if not found
  178. if runtime.GOOS == "windows" {
  179. return resolvePath("modules")
  180. }
  181. return resolvePath("/usr/lib/nginx/modules")
  182. }