config_args.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 := execCommand(exePath, "-V")
  38. if err != nil {
  39. logger.Error(err)
  40. return ""
  41. }
  42. return string(out)
  43. }
  44. // getNginxT executes nginx -T and returns the output
  45. func getNginxT() string {
  46. exePath := getNginxExePath()
  47. out, err := execCommand(exePath, "-T")
  48. if err != nil {
  49. logger.Error(err)
  50. return ""
  51. }
  52. return out
  53. }
  54. // Resolves relative paths by joining them with the nginx executable directory on Windows
  55. func resolvePath(path string) string {
  56. if path == "" {
  57. return ""
  58. }
  59. // Handle relative paths on Windows
  60. if runtime.GOOS == "windows" && !filepath.IsAbs(path) {
  61. return filepath.Join(GetNginxExeDir(), path)
  62. }
  63. return path
  64. }
  65. // GetPrefix returns the prefix of the nginx executable
  66. func GetPrefix() string {
  67. out := getNginxV()
  68. r, _ := regexp.Compile(`--prefix=(\S+)`)
  69. match := r.FindStringSubmatch(out)
  70. if len(match) < 1 {
  71. logger.Error("nginx.GetPrefix len(match) < 1")
  72. return "/usr/local/nginx"
  73. }
  74. return resolvePath(match[1])
  75. }
  76. // GetConfPath returns the path to the nginx configuration file
  77. func GetConfPath(dir ...string) (confPath string) {
  78. if settings.NginxSettings.ConfigDir == "" {
  79. out := getNginxV()
  80. r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
  81. match := r.FindStringSubmatch(out)
  82. if len(match) < 1 {
  83. logger.Error("nginx.GetConfPath len(match) < 1")
  84. return ""
  85. }
  86. confPath = match[1]
  87. } else {
  88. confPath = settings.NginxSettings.ConfigDir
  89. }
  90. confPath = resolvePath(confPath)
  91. joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
  92. if !helper.IsUnderDirectory(joined, confPath) {
  93. return confPath
  94. }
  95. return joined
  96. }
  97. // GetConfEntryPath returns the path to the nginx configuration file
  98. func GetConfEntryPath() (path string) {
  99. if settings.NginxSettings.ConfigPath == "" {
  100. out := getNginxV()
  101. r, _ := regexp.Compile("--conf-path=(.*.conf)")
  102. match := r.FindStringSubmatch(out)
  103. if len(match) < 1 {
  104. logger.Error("nginx.GetConfEntryPath len(match) < 1")
  105. return ""
  106. }
  107. path = match[1]
  108. } else {
  109. path = settings.NginxSettings.ConfigPath
  110. }
  111. return resolvePath(path)
  112. }
  113. // GetPIDPath returns the path to the nginx PID file
  114. func GetPIDPath() (path string) {
  115. if settings.NginxSettings.PIDPath == "" {
  116. out := getNginxV()
  117. r, _ := regexp.Compile("--pid-path=(.*.pid)")
  118. match := r.FindStringSubmatch(out)
  119. if len(match) < 1 {
  120. logger.Error("nginx.GetPIDPath len(match) < 1")
  121. return ""
  122. }
  123. path = match[1]
  124. } else {
  125. path = settings.NginxSettings.PIDPath
  126. }
  127. return resolvePath(path)
  128. }
  129. // GetSbinPath returns the path to the nginx executable
  130. func GetSbinPath() (path string) {
  131. out := getNginxV()
  132. r, _ := regexp.Compile(`--sbin-path=(\S+)`)
  133. match := r.FindStringSubmatch(out)
  134. if len(match) < 1 {
  135. logger.Error("nginx.GetPIDPath len(match) < 1")
  136. return ""
  137. }
  138. path = match[1]
  139. return resolvePath(path)
  140. }
  141. // GetAccessLogPath returns the path to the nginx access log file
  142. func GetAccessLogPath() (path string) {
  143. if settings.NginxSettings.AccessLogPath == "" {
  144. out := getNginxV()
  145. r, _ := regexp.Compile(`--http-log-path=(\S+)`)
  146. match := r.FindStringSubmatch(out)
  147. if len(match) < 1 {
  148. logger.Error("nginx.GetAccessLogPath len(match) < 1")
  149. return ""
  150. }
  151. path = match[1]
  152. } else {
  153. path = settings.NginxSettings.AccessLogPath
  154. }
  155. return resolvePath(path)
  156. }
  157. // GetErrorLogPath returns the path to the nginx error log file
  158. func GetErrorLogPath() string {
  159. if settings.NginxSettings.ErrorLogPath == "" {
  160. out := getNginxV()
  161. r, _ := regexp.Compile(`--error-log-path=(\S+)`)
  162. match := r.FindStringSubmatch(out)
  163. if len(match) < 1 {
  164. logger.Error("nginx.GetErrorLogPath len(match) < 1")
  165. return ""
  166. }
  167. return resolvePath(match[1])
  168. } else {
  169. return resolvePath(settings.NginxSettings.ErrorLogPath)
  170. }
  171. }
  172. // GetModulesPath returns the nginx modules path
  173. func GetModulesPath() string {
  174. // First try to get from nginx -V output
  175. stdOut, stdErr := execCommand(getNginxExePath(), "-V")
  176. if stdErr != nil {
  177. return ""
  178. }
  179. if stdOut != "" {
  180. // Look for --modules-path in the output
  181. if strings.Contains(stdOut, "--modules-path=") {
  182. parts := strings.Split(stdOut, "--modules-path=")
  183. if len(parts) > 1 {
  184. // Extract the path
  185. path := strings.Split(parts[1], " ")[0]
  186. // Remove quotes if present
  187. path = strings.Trim(path, "\"")
  188. return resolvePath(path)
  189. }
  190. }
  191. }
  192. // Default path if not found
  193. if runtime.GOOS == "windows" {
  194. return resolvePath("modules")
  195. }
  196. return resolvePath("/usr/lib/nginx/modules")
  197. }