config_args.go 4.3 KB

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