Files
1Panel/agent/app/service/nginx_utils.go

363 lines
11 KiB
Go

package service
import (
"fmt"
"os"
"path"
"strings"
"time"
"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/dto/response"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/cmd/server/nginx_conf"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/cmd"
"github.com/1Panel-dev/1Panel/agent/utils/files"
"github.com/1Panel-dev/1Panel/agent/utils/nginx"
"github.com/1Panel-dev/1Panel/agent/utils/nginx/components"
"github.com/1Panel-dev/1Panel/agent/utils/nginx/parser"
)
func getNginxFull(website *model.Website) (dto.NginxFull, error) {
var nginxFull dto.NginxFull
nginxInstall, err := getAppInstallByKey("openresty")
if err != nil {
return nginxFull, err
}
nginxFull.Install = nginxInstall
nginxFull.Dir = path.Join(global.Dir.AppInstallDir, constant.AppOpenresty, nginxInstall.Name)
nginxFull.ConfigDir = path.Join(nginxFull.Dir, "conf")
nginxFull.ConfigFile = "nginx.conf"
nginxFull.SiteDir = path.Join(nginxFull.Dir, "www")
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 nginxFull, err
}
config, err := parser.NewStringParser(string(content)).Parse()
if err != nil {
return dto.NginxFull{}, err
}
config.FilePath = nginxConfig.FilePath
nginxConfig.OldContent = string(content)
nginxConfig.Config = config
nginxFull.RootConfig = nginxConfig
if website != nil {
nginxFull.Website = *website
var siteNginxConfig dto.NginxConfig
siteConfigPath := GetWebsiteConfigPath(*website)
siteNginxConfig.FilePath = siteConfigPath
siteNginxContent, err := os.ReadFile(siteConfigPath)
if err != nil {
return nginxFull, err
}
siteConfig, err := parser.NewStringParser(string(siteNginxContent)).Parse()
if err != nil {
return dto.NginxFull{}, err
}
siteConfig.FilePath = siteConfigPath
siteNginxConfig.Config = siteConfig
siteNginxConfig.OldContent = string(siteNginxContent)
nginxFull.SiteConfig = siteNginxConfig
}
return nginxFull, nil
}
func getNginxParamsByKeys(scope string, keys []string, website *model.Website) ([]response.NginxParam, error) {
nginxFull, err := getNginxFull(website)
if err != nil {
return nil, err
}
var res []response.NginxParam
var block components.IBlock
if scope == constant.NginxScopeHttp {
httpBlock := nginxFull.RootConfig.Config.FindHttp()
if httpBlock == nil {
return nil, fmt.Errorf("nginx HTTP configuration block is missing")
}
block = httpBlock
} else {
servers := nginxFull.SiteConfig.Config.FindServers()
if len(servers) == 0 {
return nil, fmt.Errorf("website nginx server block is missing")
}
block = servers[0]
}
for _, key := range keys {
var dirs []components.IDirective
if scope == constant.NginxScopeHttp {
dirs = findDirectNginxDirectives(block, key)
} else {
dirs = block.FindDirectives(key)
}
for _, dir := range dirs {
nginxParam := response.NginxParam{
Name: dir.GetName(),
Params: dir.GetParameters(),
}
res = append(res, nginxParam)
}
if len(dirs) == 0 {
nginxParam := response.NginxParam{
Name: key,
Params: []string{},
}
res = append(res, nginxParam)
}
}
return res, nil
}
// findDirectNginxDirectives returns the directives set directly in the block.
// FindDirectives also descends into server and location blocks, whose values
// do not apply to the block itself, so reading http settings through it would
// report a site value as the global one.
func findDirectNginxDirectives(block components.IBlock, name string) []components.IDirective {
var dirs []components.IDirective
for _, dir := range block.GetDirectives() {
if dir.GetName() == name {
dirs = append(dirs, dir)
}
}
return dirs
}
// fillNginxIncludedHTTPParams completes the params nginx.conf leaves unset
// with the value an included file sets at its top level, which nginx applies
// at http level too. Values inside server or location blocks belong to a
// site and are skipped. Source records the file, so the settings page shows
// where the value is set instead of offering to write a second copy into
// nginx.conf, which nginx would reject as a duplicate.
func fillNginxIncludedHTTPParams(params []response.NginxParam, paths []string) []response.NginxParam {
missing := make(map[string]int)
for i, param := range params {
if len(param.Params) == 0 {
missing[param.Name] = i
}
}
for _, filePath := range paths {
if len(missing) == 0 {
break
}
content, err := os.ReadFile(filePath)
if err != nil || !nginxContentMentionsAny(string(content), missing) {
continue
}
config, err := parseUserNginxConfig(string(content))
if err != nil {
continue
}
for _, dir := range config.GetDirectives() {
index, ok := missing[dir.GetName()]
if !ok || dir.GetBlock() != nil {
continue
}
params[index].Params = dir.GetParameters()
params[index].Source = filePath
delete(missing, dir.GetName())
}
}
return params
}
// nginxContentMentionsAny skips parsing the many site configs that cannot
// set any of the names.
func nginxContentMentionsAny(content string, names map[string]int) bool {
for name := range names {
if strings.Contains(content, name) {
return true
}
}
return false
}
// parseUserNginxConfig parses a file the user may have left with a syntax
// error. The parser panics on one instead of returning it.
func parseUserNginxConfig(content string) (config *components.Config, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("parse nginx config: %v", r)
}
}()
return parser.NewStringParser(content).Parse()
}
func updateDefaultServerConfig(enable bool) error {
nginxInstall, err := getAppInstallByKey("openresty")
if err != nil {
return err
}
defaultConfigPath := path.Join(nginxInstall.GetPath(), "conf", "default", "00.default.conf")
content, err := os.ReadFile(defaultConfigPath)
if err != nil {
return err
}
defaultConfig, err := parser.NewStringParser(string(content)).Parse()
if err != nil {
return err
}
defaultConfig.FilePath = defaultConfigPath
defaultServer := defaultConfig.FindServers()[0]
includeSSL := false
for _, dir := range defaultServer.GetDirectives() {
if dir.GetName() == "ssl_reject_handshake" && dir.GetParameters()[0] == "on" {
defaultServer.RemoveDirective("ssl_reject_handshake", []string{"on"})
}
if dir.GetName() == "include" && dir.GetParameters()[0] == "/usr/local/openresty/nginx/conf/ssl/root_ssl.conf" {
includeSSL = true
}
}
updateDefaultServer(defaultServer, nginxInstall.HttpPort, nginxInstall.HttpsPort, enable, includeSSL)
if err = nginx.WriteConfig(defaultConfig, nginx.IndentedStyle); err != nil {
return err
}
return nginxCheckAndReload(string(content), defaultConfigPath, nginxInstall.ContainerName)
}
func updateNginxConfig(scope string, params []dto.NginxParam, 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 _, 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(config.OldContent, config.FilePath, nginxFull.Install.ContainerName)
}
func deleteNginxConfig(scope string, params []dto.NginxParam, 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
}
server, isServer := block.(*components.Server)
wasStopped := isServer && server.HasStopPage()
for _, param := range params {
block.RemoveDirective(param.Name, param.Params)
}
if wasStopped {
server.EnableStopPage()
}
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 {
var (
newConfig = &components.Config{}
err error
)
updateScope := "in"
switch scope {
case dto.SSL:
newConfig, err = parser.NewStringParser(string(nginx_conf.SSL)).Parse()
case dto.CACHE:
newConfig, err = parser.NewStringParser(string(nginx_conf.Cache)).Parse()
case dto.ProxyCache:
newConfig, err = parser.NewStringParser(string(nginx_conf.ProxyCache)).Parse()
}
if err != nil {
return nil
}
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 {
cmdMgr := cmd.NewCommandMgr(cmd.WithTimeout(20 * time.Second))
if operate == constant.NginxCheck {
return cmdMgr.Run("docker", "exec", "-i", containerName, "nginx", "-t")
}
return cmdMgr.Run("docker", "exec", "-i", containerName, "nginx", "-s", "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), constant.DirPerm)
return err
}
if err := opNginx(containerName, constant.NginxReload); err != nil {
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), constant.DirPerm)
return err
}
return nil
}
func updateDefaultServer(server *components.Server, httpPort int, httpsPort int, defaultServer bool, ssl bool) {
server.UpdateListen(fmt.Sprintf("%d", httpPort), defaultServer)
server.UpdateListen(fmt.Sprintf("[::]:%d", httpPort), defaultServer)
if ssl {
server.UpdateListen(fmt.Sprintf("%d", httpsPort), defaultServer, "ssl")
server.UpdateListen(fmt.Sprintf("[::]:%d", httpsPort), defaultServer, "ssl")
server.UpdateListen(fmt.Sprintf("%d", httpsPort), defaultServer, "quic", "reuseport")
server.UpdateListen(fmt.Sprintf("[::]:%d", httpsPort), defaultServer, "quic", "reuseport")
}
}