mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-31 14:08:06 +08:00
feat: 优化代码
This commit is contained in:
parent
1bd5a180b3
commit
8a13ea9e45
@ -1,12 +1,25 @@
|
||||
package dto
|
||||
|
||||
import "github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
|
||||
import (
|
||||
"github.com/1Panel-dev/1Panel/backend/app/model"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
|
||||
)
|
||||
|
||||
type NginxFull struct {
|
||||
Install model.AppInstall
|
||||
Website model.WebSite
|
||||
ConfigDir string
|
||||
ConfigFile string
|
||||
SiteDir string
|
||||
Dir string
|
||||
RootConfig NginxConfig
|
||||
SiteConfig NginxConfig
|
||||
}
|
||||
|
||||
type NginxConfig struct {
|
||||
FilePath string `json:"filePath"`
|
||||
ContainerName string `json:"containerName"`
|
||||
Config *components.Config `json:"config"`
|
||||
OldContent string `json:"oldContent"`
|
||||
FilePath string `json:"filePath"`
|
||||
Config *components.Config `json:"config"`
|
||||
OldContent string `json:"oldContent"`
|
||||
}
|
||||
|
||||
type NginxConfigReq struct {
|
||||
@ -54,22 +67,15 @@ var ScopeKeyMap = map[NginxKey][]string{
|
||||
HttpPer: {"server_names_hash_bucket_size", "client_header_buffer_size", "client_max_body_size", "keepalive_timeout", "gzip", "gzip_min_length", "gzip_comp_level"},
|
||||
}
|
||||
|
||||
type NginxScope string
|
||||
|
||||
const (
|
||||
NginxHttp NginxScope = "http"
|
||||
NginxServer NginxScope = "server"
|
||||
NginxEvents NginxScope = "events"
|
||||
)
|
||||
|
||||
var RepeatKeys = map[string]struct {
|
||||
var StaticFileKeyMap = map[NginxKey]struct {
|
||||
}{
|
||||
"limit_conn": {},
|
||||
"limit_conn_zone": {},
|
||||
SSL: {},
|
||||
LimitConn: {},
|
||||
}
|
||||
|
||||
type NginxParam struct {
|
||||
Name string `json:"name"`
|
||||
SecondKey string `json:"secondKey"`
|
||||
Params []string `json:"params"`
|
||||
UpdateScope string `json:"scope"`
|
||||
Name string `json:"name"`
|
||||
SecondKey string `json:"secondKey"`
|
||||
Params []string `json:"params"`
|
||||
}
|
||||
|
@ -105,3 +105,8 @@ type WebsiteHTTPSOp struct {
|
||||
PrivateKey string `json:"privateKey"`
|
||||
Certificate string `json:"certificate"`
|
||||
}
|
||||
|
||||
type WebsiteNginxConfig struct {
|
||||
Enable bool `json:"enable"`
|
||||
Params []NginxParam `json:"params"`
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ func (n NginxService) GetConfigByScope(req dto.NginxScopeReq) ([]dto.NginxParam,
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return getHttpConfigByKeys(keys)
|
||||
return getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil)
|
||||
}
|
||||
|
||||
func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error {
|
||||
@ -49,7 +49,7 @@ func (n NginxService) UpdateConfigByScope(req dto.NginxConfigReq) error {
|
||||
if !ok || len(keys) == 0 {
|
||||
return nil
|
||||
}
|
||||
return updateHttpNginxConfig(getNginxParams(req.Params, keys))
|
||||
return updateNginxConfig(constant.NginxScopeHttp, getNginxParams(req.Params, keys), nil)
|
||||
}
|
||||
|
||||
func (n NginxService) GetStatus() (dto.NginxStatus, error) {
|
||||
|
@ -1,48 +1,81 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||
"github.com/1Panel-dev/1Panel/backend/app/model"
|
||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/nginx"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/nginx/parser"
|
||||
"github.com/1Panel-dev/1Panel/cmd/server/nginx_conf"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func getDefaultNginxConfig() (dto.NginxConfig, error) {
|
||||
var nginxConfig dto.NginxConfig
|
||||
|
||||
func getNginxFull(website *model.WebSite) (dto.NginxFull, error) {
|
||||
var nginxFull dto.NginxFull
|
||||
nginxInstall, err := getAppInstallByKey("nginx")
|
||||
if err != nil {
|
||||
return nginxConfig, err
|
||||
return nginxFull, err
|
||||
}
|
||||
nginxFull.Install = nginxInstall
|
||||
nginxFull.Dir = path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name)
|
||||
nginxFull.ConfigDir = path.Join(nginxFull.Dir, "conf")
|
||||
nginxFull.ConfigFile = "nginx.conf"
|
||||
nginxFull.SiteDir = path.Join(nginxFull.Dir, "www")
|
||||
|
||||
configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "nginx.conf")
|
||||
content, err := os.ReadFile(configPath)
|
||||
var nginxConfig dto.NginxConfig
|
||||
nginxConfig.FilePath = path.Join(nginxFull.Dir, "conf", "nginx.conf")
|
||||
content, err := os.ReadFile(path.Join(nginxFull.ConfigDir, nginxFull.ConfigFile))
|
||||
if err != nil {
|
||||
return nginxConfig, err
|
||||
return nginxFull, err
|
||||
}
|
||||
config := parser.NewStringParser(string(content)).Parse()
|
||||
config.FilePath = configPath
|
||||
nginxConfig.Config = config
|
||||
config.FilePath = nginxConfig.FilePath
|
||||
nginxConfig.OldContent = string(content)
|
||||
nginxConfig.ContainerName = nginxInstall.ContainerName
|
||||
nginxConfig.FilePath = configPath
|
||||
nginxConfig.Config = config
|
||||
|
||||
return nginxConfig, nil
|
||||
nginxFull.RootConfig = nginxConfig
|
||||
|
||||
if website != nil {
|
||||
nginxFull.Website = *website
|
||||
var siteNginxConfig dto.NginxConfig
|
||||
nginxFileName := website.Alias + ".conf"
|
||||
siteConfigPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", nginxFileName)
|
||||
siteNginxConfig.FilePath = siteConfigPath
|
||||
siteNginxContent, err := os.ReadFile(siteConfigPath)
|
||||
if err != nil {
|
||||
return nginxFull, err
|
||||
}
|
||||
siteConfig := parser.NewStringParser(string(siteNginxContent)).Parse()
|
||||
siteConfig.FilePath = siteConfigPath
|
||||
siteNginxConfig.Config = siteConfig
|
||||
siteNginxConfig.OldContent = string(siteNginxContent)
|
||||
nginxFull.SiteConfig = siteNginxConfig
|
||||
}
|
||||
|
||||
return nginxFull, nil
|
||||
}
|
||||
|
||||
func getHttpConfigByKeys(keys []string) ([]dto.NginxParam, error) {
|
||||
nginxConfig, err := getDefaultNginxConfig()
|
||||
func getNginxParamsByKeys(scope string, keys []string, website *model.WebSite) ([]dto.NginxParam, error) {
|
||||
nginxFull, err := getNginxFull(website)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config := nginxConfig.Config
|
||||
http := config.FindHttp()
|
||||
|
||||
var res []dto.NginxParam
|
||||
var block components.IBlock
|
||||
if scope == constant.NginxScopeHttp {
|
||||
block = nginxFull.RootConfig.Config.FindHttp()
|
||||
} else {
|
||||
block = nginxFull.SiteConfig.Config.FindServers()[0]
|
||||
}
|
||||
for _, key := range keys {
|
||||
dirs := http.FindDirectives(key)
|
||||
dirs := block.FindDirectives(key)
|
||||
for _, dir := range dirs {
|
||||
nginxParam := dto.NginxParam{
|
||||
Name: dir.GetName(),
|
||||
@ -61,18 +94,123 @@ func getHttpConfigByKeys(keys []string) ([]dto.NginxParam, error) {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func updateHttpNginxConfig(params []dto.NginxParam) error {
|
||||
nginxConfig, err := getDefaultNginxConfig()
|
||||
func updateNginxConfig(scope string, params []dto.NginxParam, website *model.WebSite) error {
|
||||
|
||||
nginxFull, err := getNginxFull(website)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config := nginxConfig.Config
|
||||
http := config.FindHttp()
|
||||
for _, p := range params {
|
||||
http.UpdateDirective(p.Name, p.Params)
|
||||
var block components.IBlock
|
||||
var config dto.NginxConfig
|
||||
if scope == constant.NginxScopeHttp {
|
||||
config = nginxFull.RootConfig
|
||||
block = nginxFull.RootConfig.Config.FindHttp()
|
||||
} else if scope == constant.NginxScopeServer {
|
||||
config = nginxFull.SiteConfig
|
||||
block = nginxFull.SiteConfig.Config.FindServers()[0]
|
||||
} else {
|
||||
config = nginxFull.SiteConfig
|
||||
block = config.Config.Block
|
||||
}
|
||||
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
||||
|
||||
for _, p := range params {
|
||||
if p.UpdateScope == constant.NginxScopeOut {
|
||||
config.Config.UpdateDirective(p.Name, p.Params)
|
||||
} else {
|
||||
block.UpdateDirective(p.Name, p.Params)
|
||||
}
|
||||
}
|
||||
if err := nginx.WriteConfig(config.Config, nginx.IndentedStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
||||
return nginxCheckAndReload(config.OldContent, config.FilePath, nginxFull.Install.ContainerName)
|
||||
}
|
||||
|
||||
func deleteNginxConfig(scope string, keys []string, website *model.WebSite) error {
|
||||
nginxFull, err := getNginxFull(website)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var block components.IBlock
|
||||
var config dto.NginxConfig
|
||||
if scope == constant.NginxScopeHttp {
|
||||
config = nginxFull.RootConfig
|
||||
block = nginxFull.RootConfig.Config.FindHttp()
|
||||
} else if scope == constant.NginxScopeServer {
|
||||
config = nginxFull.SiteConfig
|
||||
block = nginxFull.SiteConfig.Config.FindServers()[0]
|
||||
} else {
|
||||
config = nginxFull.SiteConfig
|
||||
block = config.Config.Block
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
block.RemoveDirective(key, []string{})
|
||||
}
|
||||
|
||||
if err := nginx.WriteConfig(config.Config, nginx.IndentedStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
return nginxCheckAndReload(config.OldContent, config.FilePath, nginxFull.Install.ContainerName)
|
||||
}
|
||||
|
||||
func getNginxParamsFromStaticFile(scope dto.NginxKey, newParams []dto.NginxParam) []dto.NginxParam {
|
||||
newConfig := &components.Config{}
|
||||
updateScope := "in"
|
||||
switch scope {
|
||||
case dto.SSL:
|
||||
newConfig = parser.NewStringParser(string(nginx_conf.SSL)).Parse()
|
||||
case dto.LimitConn:
|
||||
updateScope = constant.NginxScopeOut
|
||||
newConfig = parser.NewStringParser(string(nginx_conf.Limit)).Parse()
|
||||
}
|
||||
for _, dir := range newConfig.GetDirectives() {
|
||||
addParam := dto.NginxParam{
|
||||
Name: dir.GetName(),
|
||||
Params: dir.GetParameters(),
|
||||
UpdateScope: updateScope,
|
||||
}
|
||||
isExist := false
|
||||
for _, newParam := range newParams {
|
||||
if newParam.Name == dir.GetName() {
|
||||
if components.IsRepeatKey(newParam.Name) {
|
||||
if len(newParam.Params) > 0 && newParam.Params[0] == dir.GetParameters()[0] {
|
||||
isExist = true
|
||||
}
|
||||
} else {
|
||||
isExist = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !isExist {
|
||||
newParams = append(newParams, addParam)
|
||||
}
|
||||
}
|
||||
return newParams
|
||||
}
|
||||
|
||||
func opNginx(containerName, operate string) error {
|
||||
nginxCmd := fmt.Sprintf("docker exec -i %s %s", containerName, "nginx -s reload")
|
||||
if operate == constant.NginxCheck {
|
||||
nginxCmd = fmt.Sprintf("docker exec -i %s %s", containerName, "nginx -t")
|
||||
}
|
||||
if out, err := cmd.Exec(nginxCmd); err != nil {
|
||||
return errors.New(out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func nginxCheckAndReload(oldContent string, filePath string, containerName string) error {
|
||||
|
||||
if err := opNginx(containerName, constant.NginxCheck); err != nil {
|
||||
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := opNginx(containerName, constant.NginxReload); err != nil {
|
||||
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -321,7 +321,7 @@ func (w WebsiteService) DeleteWebsiteDomain(domainId uint) error {
|
||||
return websiteDomainRepo.DeleteBy(context.TODO(), commonRepo.WithByID(domainId))
|
||||
}
|
||||
|
||||
func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) ([]dto.NginxParam, error) {
|
||||
func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) (*dto.WebsiteNginxConfig, error) {
|
||||
|
||||
keys, ok := dto.ScopeKeyMap[req.Scope]
|
||||
if !ok || len(keys) == 0 {
|
||||
@ -332,8 +332,15 @@ func (w WebsiteService) GetNginxConfigByScope(req dto.NginxConfigReq) ([]dto.Ngi
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var config dto.WebsiteNginxConfig
|
||||
params, err := getNginxParamsByKeys(constant.NginxScopeServer, keys, &website)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Params = params
|
||||
config.Enable = len(params[0].Params) > 0
|
||||
|
||||
return getNginxConfigByKeys(website, keys)
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func (w WebsiteService) UpdateNginxConfigByScope(req dto.NginxConfigReq) error {
|
||||
@ -347,10 +354,15 @@ func (w WebsiteService) UpdateNginxConfigByScope(req dto.NginxConfigReq) error {
|
||||
return err
|
||||
}
|
||||
if req.Operate == dto.ConfigDel {
|
||||
return deleteNginxConfig(website, constant.NginxScopeServer, keys)
|
||||
return deleteNginxConfig(constant.NginxScopeServer, keys, &website)
|
||||
}
|
||||
|
||||
return updateNginxConfig(website, getNginxParams(req.Params, keys), req.Scope)
|
||||
params := getNginxParams(req.Params, keys)
|
||||
if req.Operate == dto.ConfigNew {
|
||||
if _, ok := dto.StaticFileKeyMap[req.Scope]; ok {
|
||||
params = getNginxParamsFromStaticFile(req.Scope, params)
|
||||
}
|
||||
}
|
||||
return updateNginxConfig(constant.NginxScopeServer, params, &website)
|
||||
}
|
||||
|
||||
func (w WebsiteService) GetWebsiteNginxConfig(websiteId uint) (dto.FileInfo, error) {
|
||||
@ -468,7 +480,7 @@ func (w WebsiteService) OpWebsiteHTTPS(req dto.WebsiteHTTPSOp) (dto.WebsiteHTTPS
|
||||
return dto.WebsiteHTTPS{}, err
|
||||
}
|
||||
|
||||
if err := deleteNginxConfig(website, constant.NginxScopeServer, getKeysFromStaticFile(dto.SSL)); err != nil {
|
||||
if err := deleteNginxConfig(constant.NginxScopeServer, getKeysFromStaticFile(dto.SSL), &website); err != nil {
|
||||
return dto.WebsiteHTTPS{}, err
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||
"github.com/1Panel-dev/1Panel/backend/app/model"
|
||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/nginx"
|
||||
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
|
||||
@ -13,9 +12,7 @@ import (
|
||||
"github.com/1Panel-dev/1Panel/cmd/server/nginx_conf"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@ -90,6 +87,9 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.WebSite)
|
||||
if err := fileOp.CreateDir(path.Join(siteFolder, "data"), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := fileOp.CreateDir(path.Join(siteFolder, "ssl"), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf", "rules"))
|
||||
}
|
||||
@ -125,7 +125,7 @@ func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) e
|
||||
server.UpdateDirective("access_log", []string{path.Join(siteFolder, "log", "access.log")})
|
||||
server.UpdateDirective("access_by_lua_file", []string{path.Join(commonFolder, "waf", "access.lua")})
|
||||
server.UpdateDirective("set", []string{"$RulePath", path.Join(siteFolder, "waf", "rules")})
|
||||
server.UpdateDirective("set", []string{"$logdir", path.Join(siteFolder, "waf", "log")})
|
||||
server.UpdateDirective("set", []string{"$logdir", path.Join(siteFolder, "log")})
|
||||
|
||||
if website.Type == "deployment" {
|
||||
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
||||
@ -150,17 +150,6 @@ func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) e
|
||||
return opNginx(nginxInstall.ContainerName, constant.NginxReload)
|
||||
}
|
||||
|
||||
func opNginx(containerName, operate string) error {
|
||||
nginxCmd := fmt.Sprintf("docker exec -i %s %s", containerName, "nginx -s reload")
|
||||
if operate == constant.NginxCheck {
|
||||
nginxCmd = fmt.Sprintf("docker exec -i %s %s", containerName, "nginx -t")
|
||||
}
|
||||
if out, err := cmd.Exec(nginxCmd); err != nil {
|
||||
return errors.New(out)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func delNginxConfig(website model.WebSite) error {
|
||||
|
||||
nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx"))
|
||||
@ -188,51 +177,14 @@ func delNginxConfig(website model.WebSite) error {
|
||||
return opNginx(nginxInstall.ContainerName, "reload")
|
||||
}
|
||||
|
||||
func nginxCheckAndReload(oldContent string, filePath string, containerName string) error {
|
||||
|
||||
if err := opNginx(containerName, constant.NginxCheck); err != nil {
|
||||
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := opNginx(containerName, constant.NginxReload); err != nil {
|
||||
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getNginxConfig(alias string) (dto.NginxConfig, error) {
|
||||
var nginxConfig dto.NginxConfig
|
||||
|
||||
nginxInstall, err := getAppInstallByKey("nginx")
|
||||
if err != nil {
|
||||
return nginxConfig, err
|
||||
}
|
||||
|
||||
configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", alias+".conf")
|
||||
content, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nginxConfig, err
|
||||
}
|
||||
config := parser.NewStringParser(string(content)).Parse()
|
||||
config.FilePath = configPath
|
||||
nginxConfig.Config = config
|
||||
nginxConfig.OldContent = string(content)
|
||||
nginxConfig.ContainerName = nginxInstall.ContainerName
|
||||
nginxConfig.FilePath = configPath
|
||||
|
||||
return nginxConfig, nil
|
||||
}
|
||||
|
||||
func addListenAndServerName(website model.WebSite, ports []int, domains []string) error {
|
||||
|
||||
nginxConfig, err := getNginxConfig(website.Alias)
|
||||
nginxFull, err := getNginxFull(&website)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
config := nginxConfig.Config
|
||||
nginxConfig := nginxFull.SiteConfig
|
||||
config := nginxFull.SiteConfig.Config
|
||||
server := config.FindServers()[0]
|
||||
for _, port := range ports {
|
||||
server.AddListen(strconv.Itoa(port), false)
|
||||
@ -243,16 +195,17 @@ func addListenAndServerName(website model.WebSite, ports []int, domains []string
|
||||
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
||||
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxFull.Install.ContainerName)
|
||||
}
|
||||
|
||||
func deleteListenAndServerName(website model.WebSite, ports []int, domains []string) error {
|
||||
|
||||
nginxConfig, err := getNginxConfig(website.Alias)
|
||||
nginxFull, err := getNginxFull(&website)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
config := nginxConfig.Config
|
||||
nginxConfig := nginxFull.SiteConfig
|
||||
config := nginxFull.SiteConfig.Config
|
||||
server := config.FindServers()[0]
|
||||
for _, port := range ports {
|
||||
server.DeleteListen(strconv.Itoa(port))
|
||||
@ -264,77 +217,7 @@ func deleteListenAndServerName(website model.WebSite, ports []int, domains []str
|
||||
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
||||
}
|
||||
|
||||
func getNginxConfigByKeys(website model.WebSite, keys []string) ([]dto.NginxParam, error) {
|
||||
nginxConfig, err := getNginxConfig(website.Alias)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config := nginxConfig.Config
|
||||
server := config.FindServers()[0]
|
||||
|
||||
var res []dto.NginxParam
|
||||
for _, key := range keys {
|
||||
dirs := server.FindDirectives(key)
|
||||
for _, dir := range dirs {
|
||||
nginxParam := dto.NginxParam{
|
||||
Name: dir.GetName(),
|
||||
Params: dir.GetParameters(),
|
||||
}
|
||||
res = append(res, nginxParam)
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func updateNginxConfig(website model.WebSite, params []dto.NginxParam, scope dto.NginxKey) error {
|
||||
nginxConfig, err := getNginxConfig(website.Alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config := nginxConfig.Config
|
||||
updateConfig(config, scope)
|
||||
server := config.FindServers()[0]
|
||||
for _, p := range params {
|
||||
server.UpdateDirective(p.Name, p.Params)
|
||||
}
|
||||
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
||||
}
|
||||
|
||||
func updateConfig(config *components.Config, scope dto.NginxKey) {
|
||||
newConfig := &components.Config{}
|
||||
switch scope {
|
||||
case dto.LimitConn:
|
||||
newConfig = parser.NewStringParser(string(nginx_conf.Limit)).Parse()
|
||||
}
|
||||
if reflect.DeepEqual(newConfig, &components.Config{}) {
|
||||
return
|
||||
}
|
||||
|
||||
for _, dir := range newConfig.GetDirectives() {
|
||||
config.UpdateDirective(dir.GetName(), dir.GetParameters())
|
||||
}
|
||||
}
|
||||
|
||||
func getNginxParamsFromStaticFile(scope dto.NginxKey) []dto.NginxParam {
|
||||
var nginxParams []dto.NginxParam
|
||||
newConfig := &components.Config{}
|
||||
switch scope {
|
||||
case dto.SSL:
|
||||
newConfig = parser.NewStringParser(string(nginx_conf.SSL)).Parse()
|
||||
}
|
||||
for _, dir := range newConfig.GetDirectives() {
|
||||
nginxParams = append(nginxParams, dto.NginxParam{
|
||||
Name: dir.GetName(),
|
||||
Params: dir.GetParameters(),
|
||||
})
|
||||
}
|
||||
return nginxParams
|
||||
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxFull.Install.ContainerName)
|
||||
}
|
||||
|
||||
func getKeysFromStaticFile(scope dto.NginxKey) []string {
|
||||
@ -350,30 +233,6 @@ func getKeysFromStaticFile(scope dto.NginxKey) []string {
|
||||
return res
|
||||
}
|
||||
|
||||
func deleteNginxConfig(website model.WebSite, scope string, keys []string) error {
|
||||
nginxConfig, err := getNginxConfig(website.Alias)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config := nginxConfig.Config
|
||||
if scope == constant.NginxScopeHttp {
|
||||
http := config.FindHttp()
|
||||
for _, key := range keys {
|
||||
http.RemoveDirective(key, []string{})
|
||||
}
|
||||
}
|
||||
if scope == constant.NginxScopeServer {
|
||||
server := config.FindServers()[0]
|
||||
for _, key := range keys {
|
||||
server.RemoveDirective(key, []string{})
|
||||
}
|
||||
}
|
||||
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
||||
return err
|
||||
}
|
||||
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
||||
}
|
||||
|
||||
func createPemFile(website model.WebSite, websiteSSL model.WebSiteSSL) error {
|
||||
nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx"))
|
||||
if err != nil {
|
||||
@ -384,7 +243,7 @@ func createPemFile(website model.WebSite, websiteSSL model.WebSiteSSL) error {
|
||||
return err
|
||||
}
|
||||
|
||||
configDir := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "ssl", website.Alias)
|
||||
configDir := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "www", "sites", website.Alias, "ssl")
|
||||
fileOp := files.NewFileOp()
|
||||
|
||||
if !fileOp.Stat(configDir) {
|
||||
@ -418,11 +277,11 @@ func createPemFile(website model.WebSite, websiteSSL model.WebSiteSSL) error {
|
||||
|
||||
func applySSL(website model.WebSite, websiteSSL model.WebSiteSSL) error {
|
||||
|
||||
nginxConfig, err := getNginxConfig(website.Alias)
|
||||
nginxFull, err := getNginxFull(&website)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
config := nginxConfig.Config
|
||||
config := nginxFull.SiteConfig.Config
|
||||
server := config.FindServers()[0]
|
||||
server.UpdateListen("443", false, "ssl")
|
||||
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
||||
@ -432,16 +291,16 @@ func applySSL(website model.WebSite, websiteSSL model.WebSiteSSL) error {
|
||||
if err := createPemFile(website, websiteSSL); err != nil {
|
||||
return err
|
||||
}
|
||||
nginxParams := getNginxParamsFromStaticFile(dto.SSL)
|
||||
nginxParams := getNginxParamsFromStaticFile(dto.SSL, []dto.NginxParam{})
|
||||
for i, param := range nginxParams {
|
||||
if param.Name == "ssl_certificate" {
|
||||
nginxParams[i].Params = []string{path.Join("/etc/nginx/ssl", website.Alias, "fullchain.pem")}
|
||||
nginxParams[i].Params = []string{path.Join("/www", "sites", website.Alias, "ssl", "fullchain.pem")}
|
||||
}
|
||||
if param.Name == "ssl_certificate_key" {
|
||||
nginxParams[i].Params = []string{path.Join("/etc/nginx/ssl", website.Alias, "privkey.pem")}
|
||||
nginxParams[i].Params = []string{path.Join("/www", "sites", website.Alias, "ssl", "privkey.pem")}
|
||||
}
|
||||
}
|
||||
if err := updateNginxConfig(website, nginxParams, dto.SSL); err != nil {
|
||||
if err := updateNginxConfig(constant.NginxScopeServer, nginxParams, &website); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package constant
|
||||
const (
|
||||
NginxScopeServer = "server"
|
||||
NginxScopeHttp = "http"
|
||||
NginxScopeOut = "out"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
var (
|
||||
IndentedStyle = &Style{
|
||||
SpaceBeforeBlocks: true,
|
||||
SpaceBeforeBlocks: false,
|
||||
StartIndent: 0,
|
||||
Indent: 4,
|
||||
}
|
||||
|
@ -110,6 +110,11 @@ export namespace WebSite {
|
||||
params: string[];
|
||||
}
|
||||
|
||||
export interface NginxScopeConfig {
|
||||
enable: boolean;
|
||||
params: NginxParam[];
|
||||
}
|
||||
|
||||
export interface DnsAccount extends CommonModel {
|
||||
name: string;
|
||||
type: string;
|
||||
|
@ -70,7 +70,7 @@ export const CreateDomain = (req: WebSite.DomainCreate) => {
|
||||
};
|
||||
|
||||
export const GetNginxConfig = (req: WebSite.NginxConfigReq) => {
|
||||
return http.post<WebSite.NginxParam[]>(`/websites/config`, req);
|
||||
return http.post<WebSite.NginxScopeConfig>(`/websites/config`, req);
|
||||
};
|
||||
|
||||
export const UpdateNginxConfig = (req: WebSite.NginxConfigReq) => {
|
||||
|
@ -64,9 +64,12 @@ const search = (req: WebSite.NginxConfigReq) => {
|
||||
loading.value = true;
|
||||
GetNginxConfig(req)
|
||||
.then((res) => {
|
||||
if (res.data && res.data.length > 0) {
|
||||
enable.value = true;
|
||||
for (const param of res.data) {
|
||||
if (res.data) {
|
||||
enable.value = res.data.enable;
|
||||
if (res.data.enable == false) {
|
||||
req.operate = 'add';
|
||||
}
|
||||
for (const param of res.data.params) {
|
||||
if (param.name === 'limit_conn') {
|
||||
if (param.params[0] === 'perserver') {
|
||||
form.perserver = Number(param.params[1]);
|
||||
@ -79,9 +82,6 @@ const search = (req: WebSite.NginxConfigReq) => {
|
||||
form.rate = Number(param.params[0].match(/\d+/g));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
enable.value = false;
|
||||
req.operate = 'add';
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
@ -125,7 +125,7 @@ const changeEnable = () => {
|
||||
} else {
|
||||
req.operate = 'add';
|
||||
}
|
||||
submit(limitForm.value);
|
||||
// submit(limitForm.value);
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user