1Panel/backend/app/service/nginx_utils.go

211 lines
6.2 KiB
Go
Raw Normal View History

2022-11-24 02:28:39 +00:00
package service
import (
2022-11-30 16:41:50 +00:00
"errors"
"fmt"
2022-11-24 02:28:39 +00:00
"github.com/1Panel-dev/1Panel/backend/app/dto"
2022-12-13 10:54:46 +00:00
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
2022-11-30 16:41:50 +00:00
"github.com/1Panel-dev/1Panel/backend/app/model"
2022-11-24 02:28:39 +00:00
"github.com/1Panel-dev/1Panel/backend/constant"
2022-11-30 16:41:50 +00:00
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/files"
2022-11-24 02:28:39 +00:00
"github.com/1Panel-dev/1Panel/backend/utils/nginx"
2022-11-30 16:41:50 +00:00
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
2022-11-24 02:28:39 +00:00
"github.com/1Panel-dev/1Panel/backend/utils/nginx/parser"
2022-11-30 16:41:50 +00:00
"github.com/1Panel-dev/1Panel/cmd/server/nginx_conf"
2022-11-24 02:28:39 +00:00
"os"
"path"
2022-11-30 16:41:50 +00:00
"strings"
2022-11-24 02:28:39 +00:00
)
2022-12-13 09:20:13 +00:00
func getNginxFull(website *model.Website) (dto.NginxFull, error) {
2022-11-30 16:41:50 +00:00
var nginxFull dto.NginxFull
nginxInstall, err := getAppInstallByKey("openresty")
2022-11-24 02:28:39 +00:00
if err != nil {
2022-11-30 16:41:50 +00:00
return nginxFull, err
2022-11-24 02:28:39 +00:00
}
2022-11-30 16:41:50 +00:00
nginxFull.Install = nginxInstall
nginxFull.Dir = path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name)
2022-11-30 16:41:50 +00:00
nginxFull.ConfigDir = path.Join(nginxFull.Dir, "conf")
nginxFull.ConfigFile = "nginx.conf"
nginxFull.SiteDir = path.Join(nginxFull.Dir, "www")
2022-11-24 02:28:39 +00:00
2022-11-30 16:41:50 +00:00
var nginxConfig dto.NginxConfig
nginxConfig.FilePath = path.Join(nginxFull.Dir, "conf", "nginx.conf")
content, err := os.ReadFile(path.Join(nginxFull.ConfigDir, nginxFull.ConfigFile))
2022-11-24 02:28:39 +00:00
if err != nil {
2022-11-30 16:41:50 +00:00
return nginxFull, err
2022-11-24 02:28:39 +00:00
}
config := parser.NewStringParser(string(content)).Parse()
2022-11-30 16:41:50 +00:00
config.FilePath = nginxConfig.FilePath
2022-11-24 02:28:39 +00:00
nginxConfig.OldContent = string(content)
2022-11-30 16:41:50 +00:00
nginxConfig.Config = config
nginxFull.RootConfig = nginxConfig
2022-11-24 02:28:39 +00:00
2022-11-30 16:41:50 +00:00
if website != nil {
nginxFull.Website = *website
var siteNginxConfig dto.NginxConfig
nginxFileName := website.Alias + ".conf"
siteConfigPath := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "conf", "conf.d", nginxFileName)
2022-11-30 16:41:50 +00:00
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
2022-11-24 02:28:39 +00:00
}
2022-12-13 10:54:46 +00:00
func getNginxParamsByKeys(scope string, keys []string, website *model.Website) ([]response.NginxParam, error) {
2022-11-30 16:41:50 +00:00
nginxFull, err := getNginxFull(website)
2022-11-24 02:28:39 +00:00
if err != nil {
return nil, err
}
2022-12-13 10:54:46 +00:00
var res []response.NginxParam
2022-11-30 16:41:50 +00:00
var block components.IBlock
if scope == constant.NginxScopeHttp {
block = nginxFull.RootConfig.Config.FindHttp()
} else {
block = nginxFull.SiteConfig.Config.FindServers()[0]
}
2022-11-24 02:28:39 +00:00
for _, key := range keys {
2022-11-30 16:41:50 +00:00
dirs := block.FindDirectives(key)
2022-11-24 02:28:39 +00:00
for _, dir := range dirs {
2022-12-13 10:54:46 +00:00
nginxParam := response.NginxParam{
2022-11-24 02:28:39 +00:00
Name: dir.GetName(),
Params: dir.GetParameters(),
}
res = append(res, nginxParam)
}
if len(dirs) == 0 {
2022-12-13 10:54:46 +00:00
nginxParam := response.NginxParam{
2022-11-24 02:28:39 +00:00
Name: key,
Params: []string{},
}
res = append(res, nginxParam)
}
}
return res, nil
}
2022-12-13 09:20:13 +00:00
func updateNginxConfig(scope string, params []dto.NginxParam, website *model.Website) error {
2022-11-30 16:41:50 +00:00
nginxFull, err := getNginxFull(website)
2022-11-24 02:28:39 +00:00
if err != nil {
return err
}
2022-11-30 16:41:50 +00:00
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
}
2022-11-24 02:28:39 +00:00
for _, p := range params {
2022-11-30 16:41:50 +00:00
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(config.OldContent, config.FilePath, nginxFull.Install.ContainerName)
}
func deleteNginxConfig(scope string, params []dto.NginxParam, website *model.Website) error {
2022-11-30 16:41:50 +00:00
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 _, param := range params {
block.RemoveDirective(param.Name, param.Params)
2022-11-30 16:41:50 +00:00
}
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()
}
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
2022-11-24 02:28:39 +00:00
}
2022-11-30 16:41:50 +00:00
if err := opNginx(containerName, constant.NginxReload); err != nil {
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
2022-11-24 02:28:39 +00:00
return err
}
2022-11-30 16:41:50 +00:00
return nil
2022-11-24 02:28:39 +00:00
}