tasks.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package self_check
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/helper"
  4. "github.com/0xJacky/Nginx-UI/internal/nginx"
  5. "github.com/0xJacky/Nginx-UI/internal/translation"
  6. "github.com/0xJacky/Nginx-UI/settings"
  7. "github.com/elliotchance/orderedmap/v3"
  8. "github.com/uozi-tech/cosy"
  9. )
  10. type Task struct {
  11. Key string
  12. Name *translation.Container
  13. Description *translation.Container
  14. CheckFunc func() error
  15. FixFunc func() error
  16. }
  17. type ReportStatus string
  18. const (
  19. ReportStatusSuccess ReportStatus = "success"
  20. ReportStatusWarning ReportStatus = "warning"
  21. ReportStatusError ReportStatus = "error"
  22. )
  23. type Report struct {
  24. Key string `json:"key"`
  25. Name *translation.Container `json:"name"`
  26. Description *translation.Container `json:"description,omitempty"`
  27. Fixable bool `json:"fixable"`
  28. Err *cosy.Error `json:"err,omitempty"`
  29. Status ReportStatus `json:"status"`
  30. }
  31. type Reports []*Report
  32. var selfCheckTasks = []*Task{
  33. {
  34. Key: "Directory-Sites",
  35. Name: translation.C("Sites directory exists"),
  36. Description: translation.C("Check if the " +
  37. "sites-available and sites-enabled directories are " +
  38. "under the nginx configuration directory"),
  39. CheckFunc: CheckSitesDirectory,
  40. FixFunc: FixSitesDirectory,
  41. },
  42. {
  43. Key: "NginxConf-Sites-Enabled",
  44. Name: translation.C("Nginx.conf includes sites-enabled directory"),
  45. Description: translation.C("Check if the nginx.conf includes the " +
  46. "sites-enabled directory"),
  47. CheckFunc: CheckNginxConfIncludeSites,
  48. FixFunc: FixNginxConfIncludeSites,
  49. },
  50. {
  51. Key: "Directory-ConfD",
  52. Name: translation.C("Conf.d directory exists"),
  53. Description: translation.C("Check if the conf.d directory is under the nginx configuration directory"),
  54. CheckFunc: CheckConfDirectory,
  55. FixFunc: FixConfDirectory,
  56. },
  57. {
  58. Key: "NginxConf-ConfD-Include",
  59. Name: translation.C("Nginx.conf includes conf.d directory"),
  60. Description: translation.C("Check if the nginx.conf includes the " +
  61. "conf.d directory"),
  62. CheckFunc: CheckNginxConfIncludeConfD,
  63. FixFunc: FixNginxConfIncludeConfD,
  64. },
  65. {
  66. Key: "NginxConf-Directory",
  67. Name: translation.C("Nginx configuration directory exists"),
  68. Description: translation.C("Check if the nginx configuration directory exists"),
  69. CheckFunc: CheckConfigDir,
  70. },
  71. {
  72. Key: "NginxConf-Entry-File",
  73. Name: translation.C("Nginx configuration entry file exists"),
  74. Description: translation.C("Check if the nginx configuration entry file exists"),
  75. CheckFunc: CheckConfigEntryFile,
  76. },
  77. {
  78. Key: "NginxPID-Path",
  79. Name: translation.C("Nginx PID path exists"),
  80. Description: translation.C("Check if the nginx PID path exists. " +
  81. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained, an error will be reported. " +
  82. "In this case, you need to modify the configuration file to specify the Nginx PID path." +
  83. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#pidpath"),
  84. CheckFunc: CheckPIDPath,
  85. },
  86. {
  87. Key: "NginxSbin-Path",
  88. Name: translation.C("Nginx sbin path exists"),
  89. Description: translation.C("Check if the nginx sbin path exists"),
  90. CheckFunc: CheckSbinPath,
  91. },
  92. {
  93. Key: "NginxAccessLog-Path",
  94. Name: translation.C("Nginx access log path exists"),
  95. Description: translation.C("Check if the nginx access log path exists. " +
  96. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained or the obtained path does not point to a valid, " +
  97. "existing file, an error will be reported. In this case, you need to modify the configuration file to specify the access log path." +
  98. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#accesslogpath"),
  99. CheckFunc: CheckAccessLogPath,
  100. },
  101. {
  102. Key: "NginxErrorLog-Path",
  103. Name: translation.C("Nginx error log path exists"),
  104. Description: translation.C("Check if the nginx error log path exists. " +
  105. "By default, this path is obtained from 'nginx -V'. If it cannot be obtained or the obtained path does not point to a valid, " +
  106. "existing file, an error will be reported. In this case, you need to modify the configuration file to specify the error log path. " +
  107. "Refer to the docs for more details: https://nginxui.com/zh_CN/guide/config-nginx.html#errorlogpath"),
  108. CheckFunc: CheckErrorLogPath,
  109. },
  110. }
  111. var selfCheckTaskMap = orderedmap.NewOrderedMap[string, *Task]()
  112. func Init() {
  113. if nginx.IsModuleLoaded(nginx.ModuleStream) {
  114. selfCheckTasks = append(selfCheckTasks, &Task{
  115. Key: "Directory-Streams",
  116. Name: translation.C("Streams directory exists"),
  117. Description: translation.C("Check if the " +
  118. "streams-available and streams-enabled directories are " +
  119. "under the nginx configuration directory"),
  120. CheckFunc: CheckStreamDirectory,
  121. FixFunc: FixStreamDirectory,
  122. }, &Task{
  123. Key: "NginxConf-Streams-Enabled",
  124. Name: translation.C("Nginx.conf includes streams-enabled directory"),
  125. Description: translation.C("Check if the nginx.conf includes the " +
  126. "streams-enabled directory"),
  127. CheckFunc: CheckNginxConfIncludeStreams,
  128. FixFunc: FixNginxConfIncludeStreams,
  129. })
  130. }
  131. if helper.InNginxUIOfficialDocker() {
  132. selfCheckTasks = append(selfCheckTasks, &Task{
  133. Name: translation.C("Docker socket exists"),
  134. Description: translation.C("Check if /var/run/docker.sock exists. " +
  135. "If you are using Nginx UI Official " +
  136. "Docker Image, please make sure the docker socket is mounted like this: `-" +
  137. "v /var/run/docker.sock:/var/run/docker.sock`. " +
  138. "Nginx UI official image uses /var/run/docker.sock to communicate with the host Docker Engine via Docker Client API. " +
  139. "This feature is used to control Nginx in another container and perform container replacement rather than binary replacement " +
  140. "during OTA upgrades of Nginx UI to ensure container dependencies are also upgraded. " +
  141. "If you don't need this feature, please add the environment variable NGINX_UI_IGNORE_DOCKER_SOCKET=true to the container."),
  142. CheckFunc: CheckDockerSocket,
  143. })
  144. }
  145. if settings.NginxLogSettings.IndexingEnabled {
  146. selfCheckTasks = append(selfCheckTasks, &Task{
  147. Key: "GeoLite-DB",
  148. Name: translation.C("GeoLite2 database available"),
  149. Description: translation.C("Check if the GeoLite2 database is available when log indexing is enabled. " +
  150. "The GeoLite2 database is required for geographic IP analysis in log indexing. " +
  151. "You can download it from the Preference page or manually place GeoLite2-City.mmdb in the same directory as app.ini"),
  152. CheckFunc: CheckGeoLiteDB,
  153. FixFunc: FixGeoLiteDB,
  154. })
  155. }
  156. for _, task := range selfCheckTasks {
  157. selfCheckTaskMap.Set(task.Key, task)
  158. }
  159. }