1
0

config_args.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // 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. func GetConfPath(dir ...string) (confPath string) {
  66. if settings.NginxSettings.ConfigDir == "" {
  67. out := getNginxV()
  68. r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
  69. match := r.FindStringSubmatch(out)
  70. if len(match) < 1 {
  71. logger.Error("nginx.GetConfPath len(match) < 1")
  72. return ""
  73. }
  74. confPath = match[1]
  75. } else {
  76. confPath = settings.NginxSettings.ConfigDir
  77. }
  78. confPath = resolvePath(confPath)
  79. joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
  80. if !helper.IsUnderDirectory(joined, confPath) {
  81. return confPath
  82. }
  83. return joined
  84. }
  85. func GetConfEntryPath() (path string) {
  86. if settings.NginxSettings.ConfigPath == "" {
  87. out := getNginxV()
  88. r, _ := regexp.Compile("--conf-path=(.*.conf)")
  89. match := r.FindStringSubmatch(out)
  90. if len(match) < 1 {
  91. logger.Error("nginx.GetConfEntryPath len(match) < 1")
  92. return ""
  93. }
  94. path = match[1]
  95. } else {
  96. path = settings.NginxSettings.ConfigPath
  97. }
  98. return resolvePath(path)
  99. }
  100. func GetPIDPath() (path string) {
  101. if settings.NginxSettings.PIDPath == "" {
  102. out := getNginxV()
  103. r, _ := regexp.Compile("--pid-path=(.*.pid)")
  104. match := r.FindStringSubmatch(out)
  105. if len(match) < 1 {
  106. logger.Error("nginx.GetPIDPath len(match) < 1")
  107. return ""
  108. }
  109. path = match[1]
  110. } else {
  111. path = settings.NginxSettings.PIDPath
  112. }
  113. return resolvePath(path)
  114. }
  115. func GetSbinPath() (path string) {
  116. out := getNginxV()
  117. r, _ := regexp.Compile("--sbin-path=(\\S+)")
  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. return resolvePath(path)
  125. }
  126. func GetAccessLogPath() (path string) {
  127. if settings.NginxSettings.AccessLogPath == "" {
  128. out := getNginxV()
  129. r, _ := regexp.Compile("--http-log-path=(\\S+)")
  130. match := r.FindStringSubmatch(out)
  131. if len(match) < 1 {
  132. logger.Error("nginx.GetAccessLogPath len(match) < 1")
  133. return ""
  134. }
  135. path = match[1]
  136. } else {
  137. path = settings.NginxSettings.AccessLogPath
  138. }
  139. return resolvePath(path)
  140. }
  141. func GetErrorLogPath() string {
  142. if settings.NginxSettings.ErrorLogPath == "" {
  143. out := getNginxV()
  144. r, _ := regexp.Compile("--error-log-path=(\\S+)")
  145. match := r.FindStringSubmatch(out)
  146. if len(match) < 1 {
  147. logger.Error("nginx.GetErrorLogPath len(match) < 1")
  148. return ""
  149. }
  150. return resolvePath(match[1])
  151. } else {
  152. return resolvePath(settings.NginxSettings.ErrorLogPath)
  153. }
  154. }
  155. // GetModulesPath returns the nginx modules path
  156. func GetModulesPath() string {
  157. // First try to get from nginx -V output
  158. stdOut, stdErr := execCommand(getNginxExePath(), "-V")
  159. if stdErr != nil {
  160. return ""
  161. }
  162. if stdOut != "" {
  163. // Look for --modules-path in the output
  164. if strings.Contains(stdOut, "--modules-path=") {
  165. parts := strings.Split(stdOut, "--modules-path=")
  166. if len(parts) > 1 {
  167. // Extract the path
  168. path := strings.Split(parts[1], " ")[0]
  169. // Remove quotes if present
  170. path = strings.Trim(path, "\"")
  171. return resolvePath(path)
  172. }
  173. }
  174. }
  175. // Default path if not found
  176. if runtime.GOOS == "windows" {
  177. return resolvePath("modules")
  178. }
  179. return resolvePath("/usr/lib/nginx/modules")
  180. }