mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 02:45:49 +02:00
chore: bump version to 2.0beta1
This commit is contained in:
parent
509d059ceb
commit
1a78cebd50
4 changed files with 403 additions and 403 deletions
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "nginx-ui-frontend-next",
|
"name": "nginx-ui-frontend-next",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "1.9.9",
|
"version": "2.0.0-beta1",
|
||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"version":"1.9.9","build_id":134,"total_build":204}
|
{"version":"2.0.0-beta1","build_id":2,"total_build":206}
|
|
@ -1 +1 @@
|
||||||
{"version":"1.9.9","build_id":134,"total_build":204}
|
{"version":"2.0.0-beta1","build_id":2,"total_build":206}
|
|
@ -1,535 +1,535 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/0xJacky/Nginx-UI/server/internal/cert"
|
"github.com/0xJacky/Nginx-UI/server/internal/cert"
|
||||||
"github.com/0xJacky/Nginx-UI/server/internal/config_list"
|
"github.com/0xJacky/Nginx-UI/server/internal/config_list"
|
||||||
"github.com/0xJacky/Nginx-UI/server/internal/helper"
|
"github.com/0xJacky/Nginx-UI/server/internal/helper"
|
||||||
"github.com/0xJacky/Nginx-UI/server/internal/logger"
|
"github.com/0xJacky/Nginx-UI/server/internal/logger"
|
||||||
"github.com/0xJacky/Nginx-UI/server/internal/nginx"
|
"github.com/0xJacky/Nginx-UI/server/internal/nginx"
|
||||||
"github.com/0xJacky/Nginx-UI/server/model"
|
"github.com/0xJacky/Nginx-UI/server/model"
|
||||||
"github.com/0xJacky/Nginx-UI/server/query"
|
"github.com/0xJacky/Nginx-UI/server/query"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sashabaranov/go-openai"
|
"github.com/sashabaranov/go-openai"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetDomains(c *gin.Context) {
|
func GetDomains(c *gin.Context) {
|
||||||
name := c.Query("name")
|
name := c.Query("name")
|
||||||
orderBy := c.Query("order_by")
|
orderBy := c.Query("order_by")
|
||||||
sort := c.DefaultQuery("sort", "desc")
|
sort := c.DefaultQuery("sort", "desc")
|
||||||
|
|
||||||
mySort := map[string]string{
|
mySort := map[string]string{
|
||||||
"enabled": "bool",
|
"enabled": "bool",
|
||||||
"name": "string",
|
"name": "string",
|
||||||
"modify": "time",
|
"modify": "time",
|
||||||
}
|
}
|
||||||
|
|
||||||
configFiles, err := os.ReadDir(nginx.GetConfPath("sites-available"))
|
configFiles, err := os.ReadDir(nginx.GetConfPath("sites-available"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
|
enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
enabledConfigMap := make(map[string]bool)
|
enabledConfigMap := make(map[string]bool)
|
||||||
for i := range enabledConfig {
|
for i := range enabledConfig {
|
||||||
enabledConfigMap[enabledConfig[i].Name()] = true
|
enabledConfigMap[enabledConfig[i].Name()] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
var configs []gin.H
|
var configs []gin.H
|
||||||
|
|
||||||
for i := range configFiles {
|
for i := range configFiles {
|
||||||
file := configFiles[i]
|
file := configFiles[i]
|
||||||
fileInfo, _ := file.Info()
|
fileInfo, _ := file.Info()
|
||||||
if !file.IsDir() {
|
if !file.IsDir() {
|
||||||
if name != "" && !strings.Contains(file.Name(), name) {
|
if name != "" && !strings.Contains(file.Name(), name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
configs = append(configs, gin.H{
|
configs = append(configs, gin.H{
|
||||||
"name": file.Name(),
|
"name": file.Name(),
|
||||||
"size": fileInfo.Size(),
|
"size": fileInfo.Size(),
|
||||||
"modify": fileInfo.ModTime(),
|
"modify": fileInfo.ModTime(),
|
||||||
"enabled": enabledConfigMap[file.Name()],
|
"enabled": enabledConfigMap[file.Name()],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
configs = config_list.Sort(orderBy, sort, mySort[orderBy], configs)
|
configs = config_list.Sort(orderBy, sort, mySort[orderBy], configs)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"data": configs,
|
"data": configs,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type CertificateInfo struct {
|
type CertificateInfo struct {
|
||||||
SubjectName string `json:"subject_name"`
|
SubjectName string `json:"subject_name"`
|
||||||
IssuerName string `json:"issuer_name"`
|
IssuerName string `json:"issuer_name"`
|
||||||
NotAfter time.Time `json:"not_after"`
|
NotAfter time.Time `json:"not_after"`
|
||||||
NotBefore time.Time `json:"not_before"`
|
NotBefore time.Time `json:"not_before"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDomain(c *gin.Context) {
|
func GetDomain(c *gin.Context) {
|
||||||
rewriteName, ok := c.Get("rewriteConfigFileName")
|
rewriteName, ok := c.Get("rewriteConfigFileName")
|
||||||
|
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
|
|
||||||
// for modify filename
|
// for modify filename
|
||||||
if ok {
|
if ok {
|
||||||
name = rewriteName.(string)
|
name = rewriteName.(string)
|
||||||
}
|
}
|
||||||
|
|
||||||
path := nginx.GetConfPath("sites-available", name)
|
path := nginx.GetConfPath("sites-available", name)
|
||||||
file, err := os.Stat(path)
|
file, err := os.Stat(path)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
c.JSON(http.StatusNotFound, gin.H{
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
"message": "file not found",
|
"message": "file not found",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
enabled := true
|
enabled := true
|
||||||
|
|
||||||
if _, err := os.Stat(nginx.GetConfPath("sites-enabled", name)); os.IsNotExist(err) {
|
if _, err := os.Stat(nginx.GetConfPath("sites-enabled", name)); os.IsNotExist(err) {
|
||||||
enabled = false
|
enabled = false
|
||||||
}
|
}
|
||||||
|
|
||||||
g := query.ChatGPTLog
|
g := query.ChatGPTLog
|
||||||
chatgpt, err := g.Where(g.Name.Eq(path)).FirstOrCreate()
|
chatgpt, err := g.Where(g.Name.Eq(path)).FirstOrCreate()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if chatgpt.Content == nil {
|
if chatgpt.Content == nil {
|
||||||
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
|
chatgpt.Content = make([]openai.ChatCompletionMessage, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
s := query.Site
|
s := query.Site
|
||||||
site, err := s.Where(s.Path.Eq(path)).FirstOrInit()
|
site, err := s.Where(s.Path.Eq(path)).FirstOrInit()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
certModel, err := model.FirstCert(name)
|
certModel, err := model.FirstCert(name)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("cert", err)
|
logger.Warn("cert", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if site.Advanced {
|
if site.Advanced {
|
||||||
origContent, err := os.ReadFile(path)
|
origContent, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"modified_at": file.ModTime(),
|
"modified_at": file.ModTime(),
|
||||||
"advanced": site.Advanced,
|
"advanced": site.Advanced,
|
||||||
"enabled": enabled,
|
"enabled": enabled,
|
||||||
"name": name,
|
"name": name,
|
||||||
"config": string(origContent),
|
"config": string(origContent),
|
||||||
"auto_cert": certModel.AutoCert == model.AutoCertEnabled,
|
"auto_cert": certModel.AutoCert == model.AutoCertEnabled,
|
||||||
"chatgpt_messages": chatgpt.Content,
|
"chatgpt_messages": chatgpt.Content,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set("maybe_error", "nginx_config_syntax_error")
|
c.Set("maybe_error", "nginx_config_syntax_error")
|
||||||
config, err := nginx.ParseNgxConfig(path)
|
config, err := nginx.ParseNgxConfig(path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set("maybe_error", "")
|
c.Set("maybe_error", "")
|
||||||
|
|
||||||
certInfoMap := make(map[int]CertificateInfo)
|
certInfoMap := make(map[int]CertificateInfo)
|
||||||
for serverIdx, server := range config.Servers {
|
for serverIdx, server := range config.Servers {
|
||||||
for _, directive := range server.Directives {
|
for _, directive := range server.Directives {
|
||||||
if directive.Directive == "ssl_certificate" {
|
if directive.Directive == "ssl_certificate" {
|
||||||
|
|
||||||
pubKey, err := cert.GetCertInfo(directive.Params)
|
pubKey, err := cert.GetCertInfo(directive.Params)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to get certificate information", err)
|
logger.Error("Failed to get certificate information", err)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
certInfoMap[serverIdx] = CertificateInfo{
|
certInfoMap[serverIdx] = CertificateInfo{
|
||||||
SubjectName: pubKey.Subject.CommonName,
|
SubjectName: pubKey.Subject.CommonName,
|
||||||
IssuerName: pubKey.Issuer.CommonName,
|
IssuerName: pubKey.Issuer.CommonName,
|
||||||
NotAfter: pubKey.NotAfter,
|
NotAfter: pubKey.NotAfter,
|
||||||
NotBefore: pubKey.NotBefore,
|
NotBefore: pubKey.NotBefore,
|
||||||
}
|
}
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set("maybe_error", "nginx_config_syntax_error")
|
c.Set("maybe_error", "nginx_config_syntax_error")
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"modified_at": file.ModTime(),
|
"modified_at": file.ModTime(),
|
||||||
"advanced": site.Advanced,
|
"advanced": site.Advanced,
|
||||||
"enabled": enabled,
|
"enabled": enabled,
|
||||||
"name": name,
|
"name": name,
|
||||||
"config": config.FmtCode(),
|
"config": config.FmtCode(),
|
||||||
"tokenized": config,
|
"tokenized": config,
|
||||||
"auto_cert": certModel.AutoCert == model.AutoCertEnabled,
|
"auto_cert": certModel.AutoCert == model.AutoCertEnabled,
|
||||||
"cert_info": certInfoMap,
|
"cert_info": certInfoMap,
|
||||||
"chatgpt_messages": chatgpt.Content,
|
"chatgpt_messages": chatgpt.Content,
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SaveDomain(c *gin.Context) {
|
func SaveDomain(c *gin.Context) {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
|
|
||||||
if name == "" {
|
if name == "" {
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
"message": "param name is empty",
|
"message": "param name is empty",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var json struct {
|
var json struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Content string `json:"content" binding:"required"`
|
Content string `json:"content" binding:"required"`
|
||||||
Overwrite bool `json:"overwrite"`
|
Overwrite bool `json:"overwrite"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if !BindAndValid(c, &json) {
|
if !BindAndValid(c, &json) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
path := nginx.GetConfPath("sites-available", name)
|
path := nginx.GetConfPath("sites-available", name)
|
||||||
|
|
||||||
if !json.Overwrite && helper.FileExists(path) {
|
if !json.Overwrite && helper.FileExists(path) {
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
"message": "File exists",
|
"message": "File exists",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err := os.WriteFile(path, []byte(json.Content), 0644)
|
err := os.WriteFile(path, []byte(json.Content), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
|
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
|
||||||
// rename the config file if needed
|
// rename the config file if needed
|
||||||
if name != json.Name {
|
if name != json.Name {
|
||||||
newPath := nginx.GetConfPath("sites-available", json.Name)
|
newPath := nginx.GetConfPath("sites-available", json.Name)
|
||||||
s := query.Site
|
s := query.Site
|
||||||
_, err = s.Where(s.Path.Eq(path)).Update(s.Path, newPath)
|
_, err = s.Where(s.Path.Eq(path)).Update(s.Path, newPath)
|
||||||
|
|
||||||
// check if dst file exists, do not rename
|
// check if dst file exists, do not rename
|
||||||
if helper.FileExists(newPath) {
|
if helper.FileExists(newPath) {
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
"message": "File exists",
|
"message": "File exists",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// recreate soft link
|
// recreate soft link
|
||||||
if helper.FileExists(enabledConfigFilePath) {
|
if helper.FileExists(enabledConfigFilePath) {
|
||||||
_ = os.Remove(enabledConfigFilePath)
|
_ = os.Remove(enabledConfigFilePath)
|
||||||
enabledConfigFilePath = nginx.GetConfPath("sites-enabled", json.Name)
|
enabledConfigFilePath = nginx.GetConfPath("sites-enabled", json.Name)
|
||||||
err = os.Symlink(newPath, enabledConfigFilePath)
|
err = os.Symlink(newPath, enabledConfigFilePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Rename(path, newPath)
|
err = os.Rename(path, newPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
name = json.Name
|
name = json.Name
|
||||||
c.Set("rewriteConfigFileName", name)
|
c.Set("rewriteConfigFileName", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
enabledConfigFilePath = nginx.GetConfPath("sites-enabled", name)
|
enabledConfigFilePath = nginx.GetConfPath("sites-enabled", name)
|
||||||
if helper.FileExists(enabledConfigFilePath) {
|
if helper.FileExists(enabledConfigFilePath) {
|
||||||
// Test nginx configuration
|
// Test nginx configuration
|
||||||
output := nginx.TestConf()
|
output := nginx.TestConf()
|
||||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"message": output,
|
"message": output,
|
||||||
"error": "nginx_config_syntax_error",
|
"error": "nginx_config_syntax_error",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
output = nginx.Reload()
|
output = nginx.Reload()
|
||||||
|
|
||||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"message": output,
|
"message": output,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GetDomain(c)
|
GetDomain(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnableDomain(c *gin.Context) {
|
func EnableDomain(c *gin.Context) {
|
||||||
configFilePath := nginx.GetConfPath("sites-available", c.Param("name"))
|
configFilePath := nginx.GetConfPath("sites-available", c.Param("name"))
|
||||||
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", c.Param("name"))
|
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", c.Param("name"))
|
||||||
|
|
||||||
_, err := os.Stat(configFilePath)
|
_, err := os.Stat(configFilePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = os.Stat(enabledConfigFilePath); os.IsNotExist(err) {
|
if _, err = os.Stat(enabledConfigFilePath); os.IsNotExist(err) {
|
||||||
err = os.Symlink(configFilePath, enabledConfigFilePath)
|
err = os.Symlink(configFilePath, enabledConfigFilePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test nginx config, if not pass then disable the site.
|
// Test nginx config, if not pass then disable the site.
|
||||||
output := nginx.TestConf()
|
output := nginx.TestConf()
|
||||||
|
|
||||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||||
_ = os.Remove(enabledConfigFilePath)
|
_ = os.Remove(enabledConfigFilePath)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"message": output,
|
"message": output,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
output = nginx.Reload()
|
output = nginx.Reload()
|
||||||
|
|
||||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"message": output,
|
"message": output,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "ok",
|
"message": "ok",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func DisableDomain(c *gin.Context) {
|
func DisableDomain(c *gin.Context) {
|
||||||
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", c.Param("name"))
|
enabledConfigFilePath := nginx.GetConfPath("sites-enabled", c.Param("name"))
|
||||||
|
|
||||||
_, err := os.Stat(enabledConfigFilePath)
|
_, err := os.Stat(enabledConfigFilePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Remove(enabledConfigFilePath)
|
err = os.Remove(enabledConfigFilePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete auto cert record
|
// delete auto cert record
|
||||||
certModel := model.Cert{Filename: c.Param("name")}
|
certModel := model.Cert{Filename: c.Param("name")}
|
||||||
err = certModel.Remove()
|
err = certModel.Remove()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
output := nginx.Reload()
|
output := nginx.Reload()
|
||||||
|
|
||||||
if nginx.GetLogLevel(output) > nginx.Warn {
|
if nginx.GetLogLevel(output) > nginx.Warn {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
"message": output,
|
"message": output,
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "ok",
|
"message": "ok",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteDomain(c *gin.Context) {
|
func DeleteDomain(c *gin.Context) {
|
||||||
var err error
|
var err error
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
availablePath := nginx.GetConfPath("sites-available", name)
|
availablePath := nginx.GetConfPath("sites-available", name)
|
||||||
enabledPath := nginx.GetConfPath("sites-enabled", name)
|
enabledPath := nginx.GetConfPath("sites-enabled", name)
|
||||||
|
|
||||||
if _, err = os.Stat(availablePath); os.IsNotExist(err) {
|
if _, err = os.Stat(availablePath); os.IsNotExist(err) {
|
||||||
c.JSON(http.StatusNotFound, gin.H{
|
c.JSON(http.StatusNotFound, gin.H{
|
||||||
"message": "site not found",
|
"message": "site not found",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = os.Stat(enabledPath); err == nil {
|
if _, err = os.Stat(enabledPath); err == nil {
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
"message": "site is enabled",
|
"message": "site is enabled",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
certModel := model.Cert{Filename: name}
|
certModel := model.Cert{Filename: name}
|
||||||
_ = certModel.Remove()
|
_ = certModel.Remove()
|
||||||
|
|
||||||
err = os.Remove(availablePath)
|
err = os.Remove(availablePath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "ok",
|
"message": "ok",
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddDomainToAutoCert(c *gin.Context) {
|
func AddDomainToAutoCert(c *gin.Context) {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
|
|
||||||
var json struct {
|
var json struct {
|
||||||
model.Cert
|
model.Cert
|
||||||
Domains []string `json:"domains"`
|
Domains []string `json:"domains"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if !BindAndValid(c, &json) {
|
if !BindAndValid(c, &json) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
certModel, err := model.FirstOrCreateCert(name)
|
certModel, err := model.FirstOrCreateCert(name)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = certModel.Updates(&model.Cert{
|
err = certModel.Updates(&model.Cert{
|
||||||
Name: name,
|
Name: name,
|
||||||
Domains: json.Domains,
|
Domains: json.Domains,
|
||||||
AutoCert: model.AutoCertEnabled,
|
AutoCert: model.AutoCertEnabled,
|
||||||
DnsCredentialID: json.DnsCredentialID,
|
DnsCredentialID: json.DnsCredentialID,
|
||||||
ChallengeMethod: json.ChallengeMethod,
|
ChallengeMethod: json.ChallengeMethod,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, certModel)
|
c.JSON(http.StatusOK, certModel)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveDomainFromAutoCert(c *gin.Context) {
|
func RemoveDomainFromAutoCert(c *gin.Context) {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
certModel, err := model.FirstCert(name)
|
certModel, err := model.FirstCert(name)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = certModel.Updates(&model.Cert{
|
err = certModel.Updates(&model.Cert{
|
||||||
AutoCert: model.AutoCertDisabled,
|
AutoCert: model.AutoCertDisabled,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, nil)
|
c.JSON(http.StatusOK, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DuplicateSite(c *gin.Context) {
|
func DuplicateSite(c *gin.Context) {
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
|
|
||||||
var json struct {
|
var json struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if !BindAndValid(c, &json) {
|
if !BindAndValid(c, &json) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
src := nginx.GetConfPath("sites-available", name)
|
src := nginx.GetConfPath("sites-available", name)
|
||||||
dst := nginx.GetConfPath("sites-available", json.Name)
|
dst := nginx.GetConfPath("sites-available", json.Name)
|
||||||
|
|
||||||
if helper.FileExists(dst) {
|
if helper.FileExists(dst) {
|
||||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||||
"message": "File exists",
|
"message": "File exists",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := helper.CopyFile(src, dst)
|
_, err := helper.CopyFile(src, dst)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"dst": dst,
|
"dst": dst,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func DomainEditByAdvancedMode(c *gin.Context) {
|
func DomainEditByAdvancedMode(c *gin.Context) {
|
||||||
var json struct {
|
var json struct {
|
||||||
Advanced bool `json:"advanced"`
|
Advanced bool `json:"advanced"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if !BindAndValid(c, &json) {
|
if !BindAndValid(c, &json) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
name := c.Param("name")
|
name := c.Param("name")
|
||||||
path := nginx.GetConfPath("sites-available", name)
|
path := nginx.GetConfPath("sites-available", name)
|
||||||
|
|
||||||
s := query.Site
|
s := query.Site
|
||||||
|
|
||||||
_, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
|
_, err := s.Where(s.Path.Eq(path)).FirstOrCreate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = s.Where(s.Path.Eq(path)).Update(s.Advanced, json.Advanced)
|
_, err = s.Where(s.Path.Eq(path)).Update(s.Advanced, json.Advanced)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrHandler(c, err)
|
ErrHandler(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"message": "ok",
|
"message": "ok",
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue