config_args.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 GetConfEntryPath() (path string) {
  38. if settings.NginxSettings.ConfigPath == "" {
  39. out := getNginxV()
  40. r, _ := regexp.Compile("--conf-path=(.*.conf)")
  41. match := r.FindStringSubmatch(out)
  42. if len(match) < 1 {
  43. logger.Error("nginx.GetConfEntryPath len(match) < 1")
  44. return ""
  45. }
  46. path = match[1]
  47. } else {
  48. path = settings.NginxSettings.ConfigPath
  49. }
  50. return
  51. }
  52. func GetPIDPath() (path string) {
  53. if settings.NginxSettings.PIDPath == "" {
  54. out := getNginxV()
  55. r, _ := regexp.Compile("--pid-path=(.*.pid)")
  56. match := r.FindStringSubmatch(out)
  57. if len(match) < 1 {
  58. logger.Error("nginx.GetPIDPath len(match) < 1")
  59. return ""
  60. }
  61. path = match[1]
  62. } else {
  63. path = settings.NginxSettings.PIDPath
  64. }
  65. return
  66. }
  67. func GetSbinPath() (path string) {
  68. out := getNginxV()
  69. r, _ := regexp.Compile("--sbin-path=(\\S+)")
  70. match := r.FindStringSubmatch(out)
  71. if len(match) < 1 {
  72. logger.Error("nginx.GetPIDPath len(match) < 1")
  73. return ""
  74. }
  75. path = match[1]
  76. return
  77. }
  78. func GetAccessLogPath() (path string) {
  79. if settings.NginxSettings.AccessLogPath == "" {
  80. out := getNginxV()
  81. r, _ := regexp.Compile("--http-log-path=(\\S+)")
  82. match := r.FindStringSubmatch(out)
  83. if len(match) < 1 {
  84. logger.Error("nginx.GetAccessLogPath len(match) < 1")
  85. return ""
  86. }
  87. path = match[1]
  88. } else {
  89. path = settings.NginxSettings.AccessLogPath
  90. }
  91. return
  92. }
  93. func GetErrorLogPath() string {
  94. if settings.NginxSettings.ErrorLogPath == "" {
  95. out := getNginxV()
  96. r, _ := regexp.Compile("--error-log-path=(\\S+)")
  97. match := r.FindStringSubmatch(out)
  98. if len(match) < 1 {
  99. logger.Error("nginx.GetErrorLogPath len(match) < 1")
  100. return ""
  101. }
  102. return match[1]
  103. } else {
  104. return settings.NginxSettings.ErrorLogPath
  105. }
  106. }