1
0

nginx_conf.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package self_check
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/0xJacky/Nginx-UI/internal/nginx"
  7. "github.com/tufanbarisyildirim/gonginx/config"
  8. "github.com/tufanbarisyildirim/gonginx/dumper"
  9. "github.com/tufanbarisyildirim/gonginx/parser"
  10. )
  11. // CheckNginxConfIncludeSites checks if nginx.conf include sites-enabled
  12. func CheckNginxConfIncludeSites() error {
  13. path := nginx.GetConfEntryPath()
  14. content, err := os.ReadFile(path)
  15. if err != nil {
  16. return ErrFailedToReadNginxConf
  17. }
  18. // parse nginx.conf
  19. p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())
  20. c, err := p.Parse()
  21. if err != nil {
  22. return ErrParseNginxConf
  23. }
  24. // find http block
  25. for _, v := range c.Block.Directives {
  26. if v.GetName() == "http" {
  27. // find include sites-enabled
  28. for _, directive := range v.GetBlock().GetDirectives() {
  29. if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&
  30. directive.GetParameters()[0].Value == nginx.GetConfPath("sites-enabled/*") {
  31. return nil
  32. }
  33. }
  34. return ErrNginxConfNotIncludeSitesEnabled
  35. }
  36. }
  37. return ErrNginxConfNoHttpBlock
  38. }
  39. // CheckNginxConfIncludeStreams checks if nginx.conf include streams-enabled
  40. func CheckNginxConfIncludeStreams() error {
  41. path := nginx.GetConfEntryPath()
  42. content, err := os.ReadFile(path)
  43. if err != nil {
  44. return ErrFailedToReadNginxConf
  45. }
  46. // parse nginx.conf
  47. p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())
  48. c, err := p.Parse()
  49. if err != nil {
  50. return ErrParseNginxConf
  51. }
  52. // find http block
  53. for _, v := range c.Block.Directives {
  54. if v.GetName() == "stream" {
  55. // find include sites-enabled
  56. for _, directive := range v.GetBlock().GetDirectives() {
  57. if directive.GetName() == "include" && len(directive.GetParameters()) > 0 &&
  58. directive.GetParameters()[0].Value == nginx.GetConfPath("streams-enabled/*") {
  59. return nil
  60. }
  61. }
  62. return ErrNginxConfNotIncludeStreamEnabled
  63. }
  64. }
  65. return ErrorNginxConfNoStreamBlock
  66. }
  67. // FixNginxConfIncludeSites attempts to fix nginx.conf include sites-enabled
  68. func FixNginxConfIncludeSites() error {
  69. path := nginx.GetConfEntryPath()
  70. content, err := os.ReadFile(path)
  71. if err != nil {
  72. return ErrFailedToReadNginxConf
  73. }
  74. // create a backup file (+.bak.timestamp)
  75. backupPath := fmt.Sprintf("%s.bak.%d", path, time.Now().Unix())
  76. err = os.WriteFile(backupPath, content, 0644)
  77. if err != nil {
  78. return ErrFailedToCreateBackup
  79. }
  80. // parse nginx.conf
  81. p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())
  82. c, err := p.Parse()
  83. if err != nil {
  84. return ErrParseNginxConf
  85. }
  86. // find http block
  87. for _, v := range c.Block.Directives {
  88. if v.GetName() == "http" {
  89. // add include sites-enabled/* to http block
  90. includeDirective := &config.Directive{
  91. Name: "include",
  92. Parameters: []config.Parameter{{Value: nginx.GetConfPath("sites-enabled/*")}},
  93. }
  94. realBlock := v.GetBlock().(*config.HTTP)
  95. realBlock.Directives = append(realBlock.Directives, includeDirective)
  96. // write to file
  97. return os.WriteFile(path, []byte(dumper.DumpBlock(c.Block, dumper.IndentedStyle)), 0644)
  98. }
  99. }
  100. // if no http block, append http block with include sites-enabled/*
  101. content = append(content, []byte(fmt.Sprintf("\nhttp {\n\tinclude %s;\n}\n", nginx.GetConfPath("sites-enabled/*")))...)
  102. return os.WriteFile(path, content, 0644)
  103. }
  104. // FixNginxConfIncludeStreams attempts to fix nginx.conf include streams-enabled
  105. func FixNginxConfIncludeStreams() error {
  106. path := nginx.GetConfEntryPath()
  107. content, err := os.ReadFile(path)
  108. if err != nil {
  109. return ErrFailedToReadNginxConf
  110. }
  111. // create a backup file (+.bak.timestamp)
  112. backupPath := fmt.Sprintf("%s.bak.%d", path, time.Now().Unix())
  113. err = os.WriteFile(backupPath, content, 0644)
  114. if err != nil {
  115. return ErrFailedToCreateBackup
  116. }
  117. // parse nginx.conf
  118. p := parser.NewStringParser(string(content), parser.WithSkipValidDirectivesErr())
  119. c, err := p.Parse()
  120. if err != nil {
  121. return ErrParseNginxConf
  122. }
  123. // find stream block
  124. for _, v := range c.Block.Directives {
  125. if v.GetName() == "stream" {
  126. // add include streams-enabled/* to stream block
  127. includeDirective := &config.Directive{
  128. Name: "include",
  129. Parameters: []config.Parameter{{Value: nginx.GetConfPath("streams-enabled/*")}},
  130. }
  131. realBlock := v.GetBlock().(*config.Block)
  132. realBlock.Directives = append(realBlock.Directives, includeDirective)
  133. // write to file
  134. return os.WriteFile(path, []byte(dumper.DumpBlock(c.Block, dumper.IndentedStyle)), 0644)
  135. }
  136. }
  137. // if no stream block, append stream block with include streams-enabled/*
  138. content = append(content, []byte(fmt.Sprintf("\nstream {\n\tinclude %s;\n}\n", nginx.GetConfPath("streams-enabled/*")))...)
  139. return os.WriteFile(path, content, 0644)
  140. }