resolve_path.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // 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.Error("nginx.GetPrefix len(match) < 1")
  39. nginxPrefix = "/usr/local/nginx"
  40. return nginxPrefix
  41. }
  42. nginxPrefix = resolvePath(match[1])
  43. return nginxPrefix
  44. }
  45. // GetConfPath returns the path of the nginx configuration file
  46. func GetConfPath(dir ...string) (confPath string) {
  47. if settings.NginxSettings.ConfigDir == "" {
  48. out := getNginxV()
  49. r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
  50. match := r.FindStringSubmatch(out)
  51. if len(match) < 1 {
  52. logger.Error("nginx.GetConfPath len(match) < 1")
  53. return ""
  54. }
  55. confPath = match[1]
  56. } else {
  57. confPath = settings.NginxSettings.ConfigDir
  58. }
  59. confPath = resolvePath(confPath)
  60. joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
  61. if !helper.IsUnderDirectory(joined, confPath) {
  62. return confPath
  63. }
  64. return joined
  65. }
  66. // GetConfEntryPath returns the path of the nginx configuration file
  67. func GetConfEntryPath() (path string) {
  68. if settings.NginxSettings.ConfigPath == "" {
  69. out := getNginxV()
  70. r, _ := regexp.Compile("--conf-path=(.*.conf)")
  71. match := r.FindStringSubmatch(out)
  72. if len(match) < 1 {
  73. logger.Error("nginx.GetConfEntryPath len(match) < 1")
  74. return ""
  75. }
  76. path = match[1]
  77. } else {
  78. path = settings.NginxSettings.ConfigPath
  79. }
  80. return resolvePath(path)
  81. }
  82. // GetPIDPath returns the path of the nginx PID file
  83. func GetPIDPath() (path string) {
  84. if settings.NginxSettings.PIDPath == "" {
  85. out := getNginxV()
  86. r, _ := regexp.Compile("--pid-path=(.*.pid)")
  87. match := r.FindStringSubmatch(out)
  88. if len(match) < 1 {
  89. logger.Error("pid path not found in nginx -V output")
  90. return ""
  91. }
  92. path = match[1]
  93. } else {
  94. path = settings.NginxSettings.PIDPath
  95. }
  96. return resolvePath(path)
  97. }
  98. // GetSbinPath returns the path of the nginx executable
  99. func GetSbinPath() (path string) {
  100. return getNginxSbinPath()
  101. }
  102. // GetAccessLogPath returns the path of the nginx access log file
  103. func GetAccessLogPath() (path string) {
  104. path = settings.NginxSettings.AccessLogPath
  105. if path == "" {
  106. out := getNginxV()
  107. r, _ := regexp.Compile(`--http-log-path=(\S+)`)
  108. match := r.FindStringSubmatch(out)
  109. if len(match) > 1 {
  110. path = match[1]
  111. }
  112. if path == "" {
  113. logger.Debug("access log path not found in nginx -V output, try to get from nginx -T output")
  114. path = getAccessLogPathFromNginxT()
  115. }
  116. }
  117. return resolvePath(path)
  118. }
  119. // GetErrorLogPath returns the path of the nginx error log file
  120. func GetErrorLogPath() string {
  121. path := settings.NginxSettings.ErrorLogPath
  122. if path == "" {
  123. out := getNginxV()
  124. r, _ := regexp.Compile(`--error-log-path=(\S+)`)
  125. match := r.FindStringSubmatch(out)
  126. if len(match) > 1 {
  127. path = match[1]
  128. }
  129. if path == "" {
  130. logger.Debug("error log path not found in nginx -V output, try to get from nginx -T output")
  131. path = getErrorLogPathFromNginxT()
  132. }
  133. }
  134. return resolvePath(path)
  135. }
  136. // GetModulesPath returns the path of the nginx modules
  137. func GetModulesPath() string {
  138. // First try to get from nginx -V output
  139. out := getNginxV()
  140. if out != "" {
  141. // Look for --modules-path in the output
  142. if strings.Contains(out, "--modules-path=") {
  143. parts := strings.Split(out, "--modules-path=")
  144. if len(parts) > 1 {
  145. // Extract the path
  146. path := strings.Split(parts[1], " ")[0]
  147. // Remove quotes if present
  148. path = strings.Trim(path, "\"")
  149. return resolvePath(path)
  150. }
  151. }
  152. }
  153. // Default path if not found
  154. if runtime.GOOS == "windows" {
  155. return resolvePath("modules")
  156. }
  157. return resolvePath("/usr/lib/nginx/modules")
  158. }