template.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package api
  2. import (
  3. "bufio"
  4. "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
  5. "github.com/0xJacky/Nginx-UI/server/settings"
  6. "github.com/0xJacky/Nginx-UI/template"
  7. "github.com/gin-gonic/gin"
  8. "io"
  9. "net/http"
  10. "path/filepath"
  11. "regexp"
  12. "strings"
  13. )
  14. func GetTemplate(c *gin.Context) {
  15. content := `proxy_set_header Host $host;
  16. proxy_set_header X-Real_IP $remote_addr;
  17. proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
  18. proxy_pass http://127.0.0.1:{{ HTTP01PORT }};
  19. `
  20. content = strings.ReplaceAll(content, "{{ HTTP01PORT }}",
  21. settings.ServerSettings.HTTPChallengePort)
  22. var ngxConfig *nginx.NgxConfig
  23. ngxConfig = &nginx.NgxConfig{
  24. Servers: []*nginx.NgxServer{
  25. {
  26. Directives: []*nginx.NgxDirective{
  27. {
  28. Directive: "listen",
  29. Params: "80",
  30. },
  31. {
  32. Directive: "listen",
  33. Params: "[::]:80",
  34. },
  35. {
  36. Directive: "server_name",
  37. },
  38. {
  39. Directive: "root",
  40. },
  41. {
  42. Directive: "index",
  43. },
  44. },
  45. Locations: []*nginx.NgxLocation{
  46. {
  47. Path: "/.well-known/acme-challenge",
  48. Content: content,
  49. },
  50. },
  51. },
  52. },
  53. }
  54. c.JSON(http.StatusOK, gin.H{
  55. "message": "ok",
  56. "template": ngxConfig.BuildConfig(),
  57. "tokenized": ngxConfig,
  58. })
  59. }
  60. func GetTemplateConfList(c *gin.Context) {
  61. configs, err := template.DistFS.ReadDir("conf")
  62. if err != nil {
  63. ErrHandler(c, err)
  64. return
  65. }
  66. type configItem struct {
  67. Name string `json:"name"`
  68. Description map[string]string `json:"description"`
  69. Author string `json:"author"`
  70. }
  71. var configList []configItem
  72. for _, config := range configs {
  73. func() {
  74. configListItem := configItem{
  75. Description: make(map[string]string),
  76. }
  77. file, _ := template.DistFS.Open(filepath.Join("conf", config.Name()))
  78. defer file.Close()
  79. r := bufio.NewReader(file)
  80. bytes, _, err := r.ReadLine()
  81. if err == io.EOF {
  82. return
  83. }
  84. line := strings.TrimSpace(string(bytes))
  85. if line != "# Nginx UI Template Start" {
  86. return
  87. }
  88. var content string
  89. for {
  90. bytes, _, err = r.ReadLine()
  91. if err == io.EOF {
  92. break
  93. }
  94. line = strings.TrimSpace(string(bytes))
  95. if line == "# Nginx UI Template End" {
  96. break
  97. }
  98. content += line + "\n"
  99. }
  100. re := regexp.MustCompile(`# (\S+): (.*)`)
  101. matches := re.FindAllStringSubmatch(content, -1)
  102. for _, match := range matches {
  103. if len(match) < 3 {
  104. continue
  105. }
  106. key := match[1]
  107. switch {
  108. case key == "Name":
  109. configListItem.Name = match[2]
  110. case key == "Author":
  111. configListItem.Author = match[2]
  112. case strings.Contains(key, "Description"):
  113. re = regexp.MustCompile(`(\w+)\[(\w+)\]`)
  114. matches = re.FindAllStringSubmatch(key, -1)
  115. for _, m := range matches {
  116. if len(m) < 3 {
  117. continue
  118. }
  119. // lang => description
  120. configListItem.Description[m[2]] = match[2]
  121. }
  122. }
  123. }
  124. configList = append(configList, configListItem)
  125. }()
  126. }
  127. c.JSON(http.StatusOK, gin.H{
  128. "data": configList,
  129. })
  130. }