mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
parent
d5fa42da3c
commit
c2a3cb622e
2 changed files with 162 additions and 149 deletions
|
@ -1,192 +1,192 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
||||
"github.com/0xJacky/Nginx-UI/server/settings"
|
||||
templ "github.com/0xJacky/Nginx-UI/template"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tufanbarisyildirim/gonginx/parser"
|
||||
"io"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
"bufio"
|
||||
"bytes"
|
||||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
||||
"github.com/0xJacky/Nginx-UI/server/settings"
|
||||
templ "github.com/0xJacky/Nginx-UI/template"
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tufanbarisyildirim/gonginx/parser"
|
||||
"io"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type TVariable struct {
|
||||
Type string `json:"type"`
|
||||
Name map[string]string `json:"name"`
|
||||
Value interface{} `json:"value"`
|
||||
Type string `json:"type"`
|
||||
Name map[string]string `json:"name"`
|
||||
Value interface{} `json:"value"`
|
||||
}
|
||||
|
||||
type ConfigInfoItem struct {
|
||||
Name string `json:"name"`
|
||||
Description map[string]string `json:"description"`
|
||||
Author string `json:"author"`
|
||||
Filename string `json:"filename"`
|
||||
Variables map[string]TVariable `json:"variables"`
|
||||
Name string `json:"name"`
|
||||
Description map[string]string `json:"description"`
|
||||
Author string `json:"author"`
|
||||
Filename string `json:"filename"`
|
||||
Variables map[string]TVariable `json:"variables"`
|
||||
}
|
||||
|
||||
func GetTemplateInfo(path, name string) (configListItem ConfigInfoItem) {
|
||||
configListItem = ConfigInfoItem{
|
||||
Description: make(map[string]string),
|
||||
Filename: name,
|
||||
}
|
||||
configListItem = ConfigInfoItem{
|
||||
Description: make(map[string]string),
|
||||
Filename: name,
|
||||
}
|
||||
|
||||
file, _ := templ.DistFS.Open(filepath.Join(path, name))
|
||||
defer file.Close()
|
||||
r := bufio.NewReader(file)
|
||||
bytes, _, err := r.ReadLine()
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
line := strings.TrimSpace(string(bytes))
|
||||
file, _ := templ.DistFS.Open(filepath.Join(path, name))
|
||||
defer file.Close()
|
||||
r := bufio.NewReader(file)
|
||||
bytes, _, err := r.ReadLine()
|
||||
if err == io.EOF {
|
||||
return
|
||||
}
|
||||
line := strings.TrimSpace(string(bytes))
|
||||
|
||||
if line != "# Nginx UI Template Start" {
|
||||
return
|
||||
}
|
||||
var content string
|
||||
for {
|
||||
bytes, _, err = r.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
line = strings.TrimSpace(string(bytes))
|
||||
if line == "# Nginx UI Template End" {
|
||||
break
|
||||
}
|
||||
content += line + "\n"
|
||||
}
|
||||
if line != "# Nginx UI Template Start" {
|
||||
return
|
||||
}
|
||||
var content string
|
||||
for {
|
||||
bytes, _, err = r.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
line = strings.TrimSpace(string(bytes))
|
||||
if line == "# Nginx UI Template End" {
|
||||
break
|
||||
}
|
||||
content += line + "\n"
|
||||
}
|
||||
|
||||
_, err = toml.Decode(content, &configListItem)
|
||||
if err != nil {
|
||||
log.Println("toml.Decode", err.Error())
|
||||
}
|
||||
return
|
||||
_, err = toml.Decode(content, &configListItem)
|
||||
if err != nil {
|
||||
log.Println("toml.Decode", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type ConfigDetail struct {
|
||||
Custom string `json:"custom"`
|
||||
nginx.NgxServer
|
||||
Custom string `json:"custom"`
|
||||
nginx.NgxServer
|
||||
}
|
||||
|
||||
func ParseTemplate(path, name string, bindData map[string]TVariable) (c ConfigDetail, err error) {
|
||||
file, err := templ.DistFS.Open(filepath.Join(path, name))
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error tokenized template")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
file, err := templ.DistFS.Open(filepath.Join(path, name))
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error tokenized template")
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
r := bufio.NewReader(file)
|
||||
var flag bool
|
||||
custom := ""
|
||||
content := ""
|
||||
for {
|
||||
bytes, _, err := r.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
orig := string(bytes)
|
||||
line := strings.TrimSpace(orig)
|
||||
switch {
|
||||
case line == "# Nginx UI Custom Start":
|
||||
flag = true
|
||||
case line == "# Nginx UI Custom End":
|
||||
flag = false
|
||||
case flag == true:
|
||||
custom += orig + "\n"
|
||||
case flag == false:
|
||||
content += orig + "\n"
|
||||
}
|
||||
}
|
||||
r := bufio.NewReader(file)
|
||||
var flag bool
|
||||
custom := ""
|
||||
content := ""
|
||||
for {
|
||||
lineBytes, _, err := r.ReadLine()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
orig := string(lineBytes)
|
||||
line := strings.TrimSpace(orig)
|
||||
switch {
|
||||
case line == "# Nginx UI Custom Start":
|
||||
flag = true
|
||||
case line == "# Nginx UI Custom End":
|
||||
flag = false
|
||||
case flag == true:
|
||||
custom += orig + "\n"
|
||||
case flag == false:
|
||||
content += orig + "\n"
|
||||
}
|
||||
}
|
||||
|
||||
data := gin.H{
|
||||
"HTTPPORT": settings.ServerSettings.HttpPort,
|
||||
"HTTP01PORT": settings.ServerSettings.HTTPChallengePort,
|
||||
}
|
||||
data := gin.H{
|
||||
"HTTPPORT": settings.ServerSettings.HttpPort,
|
||||
"HTTP01PORT": settings.ServerSettings.HTTPChallengePort,
|
||||
}
|
||||
|
||||
for k, v := range bindData {
|
||||
data[k] = v.Value
|
||||
}
|
||||
for k, v := range bindData {
|
||||
data[k] = v.Value
|
||||
}
|
||||
|
||||
t, err := template.New(name).Parse(custom)
|
||||
t, err := template.New(name).Parse(custom)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error parse template.custom")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error parse template.custom")
|
||||
return
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
var buf bytes.Buffer
|
||||
|
||||
err = t.Execute(&buf, data)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error execute template")
|
||||
return
|
||||
}
|
||||
err = t.Execute(&buf, data)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error execute template")
|
||||
return
|
||||
}
|
||||
|
||||
custom = strings.TrimSpace(buf.String())
|
||||
custom = strings.TrimSpace(buf.String())
|
||||
|
||||
templatePart := strings.Split(content, "# Nginx UI Template End")
|
||||
if len(templatePart) < 2 {
|
||||
return
|
||||
}
|
||||
templatePart := strings.Split(content, "# Nginx UI Template End")
|
||||
if len(templatePart) < 2 {
|
||||
return
|
||||
}
|
||||
|
||||
content = templatePart[1]
|
||||
content = templatePart[1]
|
||||
|
||||
t, err = template.New(name).Parse(content)
|
||||
t, err = template.New(name).Parse(content)
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error parse template")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error parse template")
|
||||
return
|
||||
}
|
||||
|
||||
buf.Reset()
|
||||
buf.Reset()
|
||||
|
||||
err = t.Execute(&buf, data)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error execute template")
|
||||
return
|
||||
}
|
||||
err = t.Execute(&buf, data)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error execute template")
|
||||
return
|
||||
}
|
||||
|
||||
content = buf.String()
|
||||
content = buf.String()
|
||||
|
||||
p := parser.NewStringParser(content)
|
||||
config := p.Parse()
|
||||
c.Custom = custom
|
||||
for _, d := range config.GetDirectives() {
|
||||
switch d.GetName() {
|
||||
case nginx.Location:
|
||||
l := &nginx.NgxLocation{
|
||||
Path: strings.Join(d.GetParameters(), " "),
|
||||
}
|
||||
l.ParseLocation(d, 0)
|
||||
c.NgxServer.Locations = append(c.NgxServer.Locations, l)
|
||||
default:
|
||||
dir := &nginx.NgxDirective{
|
||||
Directive: d.GetName(),
|
||||
}
|
||||
dir.ParseDirective(d, 0)
|
||||
c.NgxServer.Directives = append(c.NgxServer.Directives, dir)
|
||||
}
|
||||
}
|
||||
return
|
||||
p := parser.NewStringParser(content)
|
||||
config := p.Parse()
|
||||
c.Custom = custom
|
||||
for _, d := range config.GetDirectives() {
|
||||
switch d.GetName() {
|
||||
case nginx.Location:
|
||||
l := &nginx.NgxLocation{
|
||||
Path: strings.Join(d.GetParameters(), " "),
|
||||
}
|
||||
l.ParseLocation(d, 0)
|
||||
c.NgxServer.Locations = append(c.NgxServer.Locations, l)
|
||||
default:
|
||||
dir := &nginx.NgxDirective{
|
||||
Directive: d.GetName(),
|
||||
}
|
||||
dir.ParseDirective(d, 0)
|
||||
c.NgxServer.Directives = append(c.NgxServer.Directives, dir)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetTemplateList(path string) (configList []ConfigInfoItem, err error) {
|
||||
configs, err := templ.DistFS.ReadDir(path)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error get template list")
|
||||
return
|
||||
}
|
||||
configs, err := templ.DistFS.ReadDir(path)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "error get template list")
|
||||
return
|
||||
}
|
||||
|
||||
for _, config := range configs {
|
||||
configList = append(configList, GetTemplateInfo(path, config.Name()))
|
||||
}
|
||||
for _, config := range configs {
|
||||
configList = append(configList, GetTemplateInfo(path, config.Name()))
|
||||
}
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
|
|
@ -2,5 +2,18 @@
|
|||
name = "HTTP to HTTPS"
|
||||
author = "@0xJacky"
|
||||
description = { en = "HTTP force redirect to HTTPS Config", zh_CN = "HTTP 强制跳转 HTTPS 配置"}
|
||||
|
||||
[variables.host]
|
||||
type = "string"
|
||||
name = { en = "Host", zh_CN = "主机"}
|
||||
value = ""
|
||||
# Nginx UI Template End
|
||||
|
||||
{{- if .host }}
|
||||
if ($host == {{ .host }}) {
|
||||
return 307 https://$server_name$request_uri;
|
||||
}
|
||||
return 404;
|
||||
{{ else }}
|
||||
return 307 https://$server_name$request_uri;
|
||||
{{- end }}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue