config_args.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package nginx
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/helper"
  4. "github.com/0xJacky/Nginx-UI/settings"
  5. "github.com/uozi-tech/cosy/logger"
  6. "os/exec"
  7. "path/filepath"
  8. "regexp"
  9. )
  10. func getNginxV() string {
  11. out, err := exec.Command("nginx", "-V").CombinedOutput()
  12. if err != nil {
  13. logger.Error(err)
  14. return ""
  15. }
  16. return string(out)
  17. }
  18. func GetConfPath(dir ...string) (confPath string) {
  19. if settings.NginxSettings.ConfigDir == "" {
  20. out := getNginxV()
  21. r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
  22. match := r.FindStringSubmatch(out)
  23. if len(match) < 1 {
  24. logger.Error("nginx.GetConfPath len(match) < 1")
  25. return ""
  26. }
  27. confPath = match[1]
  28. } else {
  29. confPath = settings.NginxSettings.ConfigDir
  30. }
  31. joined := filepath.Clean(filepath.Join(confPath, filepath.Join(dir...)))
  32. if !helper.IsUnderDirectory(joined, confPath) {
  33. return confPath
  34. }
  35. return joined
  36. }
  37. func GetPIDPath() (path string) {
  38. if settings.NginxSettings.PIDPath == "" {
  39. out := getNginxV()
  40. r, _ := regexp.Compile("--pid-path=(.*.pid)")
  41. match := r.FindStringSubmatch(out)
  42. if len(match) < 1 {
  43. logger.Error("nginx.GetPIDPath len(match) < 1")
  44. return ""
  45. }
  46. path = match[1]
  47. } else {
  48. path = settings.NginxSettings.PIDPath
  49. }
  50. return path
  51. }
  52. func GetSbinPath() (path string) {
  53. out := getNginxV()
  54. r, _ := regexp.Compile("--sbin-path=(\\S+)")
  55. match := r.FindStringSubmatch(out)
  56. if len(match) < 1 {
  57. logger.Error("nginx.GetPIDPath len(match) < 1")
  58. return ""
  59. }
  60. path = match[1]
  61. return path
  62. }
  63. func GetAccessLogPath() (path string) {
  64. if settings.NginxSettings.AccessLogPath == "" {
  65. out := getNginxV()
  66. r, _ := regexp.Compile("--http-log-path=(\\S+)")
  67. match := r.FindStringSubmatch(out)
  68. if len(match) < 1 {
  69. logger.Error("nginx.GetAccessLogPath len(match) < 1")
  70. return ""
  71. }
  72. path = match[1]
  73. } else {
  74. path = settings.NginxSettings.AccessLogPath
  75. }
  76. return path
  77. }
  78. func GetErrorLogPath() string {
  79. if settings.NginxSettings.ErrorLogPath == "" {
  80. out := getNginxV()
  81. r, _ := regexp.Compile("--error-log-path=(\\S+)")
  82. match := r.FindStringSubmatch(out)
  83. if len(match) < 1 {
  84. logger.Error("nginx.GetErrorLogPath len(match) < 1")
  85. return ""
  86. }
  87. return match[1]
  88. } else {
  89. return settings.NginxSettings.ErrorLogPath
  90. }
  91. }