2022-10-28 17:04:57 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-03 18:02:07 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2022-10-28 17:04:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2022-11-02 15:19:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2022-10-28 17:04:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx"
|
2022-11-07 16:19:05 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
|
2022-10-28 17:04:57 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx/parser"
|
|
|
|
"github.com/1Panel-dev/1Panel/cmd/server/nginx_conf"
|
|
|
|
"github.com/pkg/errors"
|
2022-11-02 15:19:14 +08:00
|
|
|
"gorm.io/gorm"
|
2022-11-03 17:06:48 +08:00
|
|
|
"os"
|
2022-10-28 17:04:57 +08:00
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getDomain(domainStr string, websiteID uint) (model.WebSiteDomain, error) {
|
|
|
|
domain := model.WebSiteDomain{
|
|
|
|
WebSiteID: websiteID,
|
|
|
|
}
|
|
|
|
domainArray := strings.Split(domainStr, ":")
|
|
|
|
if len(domainArray) == 1 {
|
|
|
|
domain.Domain = domainArray[0]
|
2022-11-02 15:19:14 +08:00
|
|
|
domain.Port = 80
|
2022-10-28 17:04:57 +08:00
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
if len(domainArray) > 1 {
|
|
|
|
domain.Domain = domainArray[0]
|
|
|
|
portStr := domainArray[1]
|
|
|
|
portN, err := strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
return model.WebSiteDomain{}, err
|
|
|
|
}
|
|
|
|
domain.Port = portN
|
|
|
|
return domain, nil
|
|
|
|
}
|
|
|
|
return model.WebSiteDomain{}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-02 15:19:14 +08:00
|
|
|
func configDefaultNginx(website *model.WebSite, domains []model.WebSiteDomain) error {
|
2022-10-28 17:04:57 +08:00
|
|
|
|
|
|
|
nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(nginxApp.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nginxFileName := website.PrimaryDomain + ".conf"
|
|
|
|
configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", nginxFileName)
|
|
|
|
|
|
|
|
nginxContent := string(nginx_conf.WebsiteDefault)
|
|
|
|
config := parser.NewStringParser(nginxContent).Parse()
|
|
|
|
servers := config.FindServers()
|
|
|
|
if len(servers) == 0 {
|
|
|
|
return errors.New("nginx config is not valid")
|
|
|
|
}
|
|
|
|
server := servers[0]
|
|
|
|
var serverNames []string
|
|
|
|
for _, domain := range domains {
|
|
|
|
serverNames = append(serverNames, domain.Domain)
|
2022-11-03 17:06:48 +08:00
|
|
|
server.UpdateListen(strconv.Itoa(domain.Port), false)
|
2022-10-28 17:04:57 +08:00
|
|
|
}
|
|
|
|
server.UpdateServerName(serverNames)
|
2022-11-02 15:19:14 +08:00
|
|
|
proxy := fmt.Sprintf("http://%s:%d", appInstall.ServiceName, appInstall.HttpPort)
|
2022-10-28 17:04:57 +08:00
|
|
|
server.UpdateRootProxy([]string{proxy})
|
|
|
|
|
|
|
|
config.FilePath = configPath
|
|
|
|
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := opNginx(nginxInstall.ContainerName, "check"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return opNginx(nginxInstall.ContainerName, "reload")
|
|
|
|
}
|
|
|
|
|
|
|
|
func opNginx(containerName, operate string) error {
|
|
|
|
nginxCmd := fmt.Sprintf("docker exec -i %s %s", containerName, "nginx -s reload")
|
|
|
|
if operate == "check" {
|
|
|
|
nginxCmd = fmt.Sprintf("docker exec -i %s %s", containerName, "nginx -t")
|
|
|
|
}
|
2022-11-08 15:42:31 +08:00
|
|
|
if out, err := cmd.Exec(nginxCmd); err != nil {
|
|
|
|
return errors.New(out)
|
2022-10-28 17:04:57 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-02 15:19:14 +08:00
|
|
|
|
|
|
|
func delNginxConfig(website model.WebSite) error {
|
|
|
|
|
|
|
|
nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nginxInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(nginxApp.ID))
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nginxFileName := website.PrimaryDomain + ".conf"
|
|
|
|
configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", nginxFileName)
|
|
|
|
fileOp := files.NewFileOp()
|
|
|
|
|
|
|
|
if !fileOp.Stat(configPath) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := fileOp.DeleteFile(configPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return opNginx(nginxInstall.ContainerName, "reload")
|
|
|
|
}
|
|
|
|
|
2022-11-03 17:06:48 +08:00
|
|
|
func nginxCheckAndReload(oldContent string, filePath string, containerName string) error {
|
|
|
|
|
|
|
|
if err := opNginx(containerName, "check"); err != nil {
|
|
|
|
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := opNginx(containerName, "reload"); err != nil {
|
|
|
|
_ = files.NewFileOp().WriteFile(filePath, strings.NewReader(oldContent), 0644)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-02 15:19:14 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-03 17:06:48 +08:00
|
|
|
|
2022-11-03 18:02:07 +08:00
|
|
|
func getNginxConfig(primaryDomain string) (dto.NginxConfig, error) {
|
|
|
|
var nginxConfig dto.NginxConfig
|
2022-11-03 17:06:48 +08:00
|
|
|
nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx"))
|
|
|
|
if err != nil {
|
2022-11-03 18:02:07 +08:00
|
|
|
return nginxConfig, err
|
2022-11-03 17:06:48 +08:00
|
|
|
}
|
|
|
|
nginxInstall, err := appInstallRepo.GetFirst(appInstallRepo.WithAppId(nginxApp.ID))
|
|
|
|
if err != nil {
|
2022-11-03 18:02:07 +08:00
|
|
|
return nginxConfig, err
|
2022-11-03 17:06:48 +08:00
|
|
|
}
|
|
|
|
|
2022-11-03 18:02:07 +08:00
|
|
|
configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", primaryDomain+".conf")
|
2022-11-03 17:06:48 +08:00
|
|
|
content, err := os.ReadFile(configPath)
|
|
|
|
if err != nil {
|
2022-11-03 18:02:07 +08:00
|
|
|
return nginxConfig, err
|
2022-11-03 17:06:48 +08:00
|
|
|
}
|
|
|
|
config := parser.NewStringParser(string(content)).Parse()
|
2022-11-03 18:02:07 +08:00
|
|
|
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.PrimaryDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
config := nginxConfig.Config
|
|
|
|
server := config.FindServers()[0]
|
|
|
|
for _, port := range ports {
|
|
|
|
server.AddListen(strconv.Itoa(port), false)
|
|
|
|
}
|
|
|
|
for _, domain := range domains {
|
|
|
|
server.AddServerName(domain)
|
|
|
|
}
|
|
|
|
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteListenAndServerName(website model.WebSite, ports []int, domains []string) error {
|
|
|
|
|
|
|
|
nginxConfig, err := getNginxConfig(website.PrimaryDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
config := nginxConfig.Config
|
2022-11-03 17:06:48 +08:00
|
|
|
server := config.FindServers()[0]
|
|
|
|
for _, port := range ports {
|
|
|
|
server.DeleteListen(strconv.Itoa(port))
|
|
|
|
}
|
|
|
|
for _, domain := range domains {
|
|
|
|
server.DeleteServerName(domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-03 18:02:07 +08:00
|
|
|
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
2022-11-03 17:06:48 +08:00
|
|
|
}
|
2022-11-07 16:19:05 +08:00
|
|
|
|
2022-11-08 15:42:31 +08:00
|
|
|
func getNginxConfigByKeys(website model.WebSite, keys []string) ([]dto.NginxParam, error) {
|
2022-11-07 16:19:05 +08:00
|
|
|
nginxConfig, err := getNginxConfig(website.PrimaryDomain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config := nginxConfig.Config
|
|
|
|
server := config.FindServers()[0]
|
2022-11-08 15:42:31 +08:00
|
|
|
|
|
|
|
var res []dto.NginxParam
|
2022-11-07 16:19:05 +08:00
|
|
|
for _, key := range keys {
|
|
|
|
dirs := server.FindDirectives(key)
|
|
|
|
for _, dir := range dirs {
|
2022-11-08 15:42:31 +08:00
|
|
|
nginxParam := dto.NginxParam{
|
|
|
|
Name: dir.GetName(),
|
|
|
|
Params: dir.GetParameters(),
|
|
|
|
}
|
|
|
|
if isRepeatKey(key) {
|
|
|
|
nginxParam.IsRepeatKey = true
|
|
|
|
nginxParam.SecondKey = dir.GetParameters()[0]
|
|
|
|
}
|
|
|
|
res = append(res, nginxParam)
|
2022-11-07 16:19:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-11-08 15:42:31 +08:00
|
|
|
func updateNginxConfig(website model.WebSite, params []dto.NginxParam, scope dto.NginxScope) error {
|
2022-11-07 16:19:05 +08:00
|
|
|
nginxConfig, err := getNginxConfig(website.PrimaryDomain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config := nginxConfig.Config
|
2022-11-08 15:42:31 +08:00
|
|
|
updateConfig(config, scope)
|
2022-11-07 16:19:05 +08:00
|
|
|
server := config.FindServers()[0]
|
2022-11-08 15:42:31 +08:00
|
|
|
for _, p := range params {
|
2022-11-07 16:19:05 +08:00
|
|
|
newDir := components.Directive{
|
2022-11-08 15:42:31 +08:00
|
|
|
Name: p.Name,
|
|
|
|
Parameters: p.Params,
|
|
|
|
}
|
|
|
|
if p.IsRepeatKey {
|
|
|
|
server.UpdateDirectiveBySecondKey(p.Name, p.SecondKey, newDir)
|
|
|
|
} else {
|
|
|
|
server.UpdateDirectives(p.Name, newDir)
|
2022-11-07 16:19:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
|
|
|
}
|
|
|
|
|
2022-11-08 15:42:31 +08:00
|
|
|
func updateConfig(config *components.Config, scope dto.NginxScope) {
|
|
|
|
if scope == dto.LimitConn {
|
|
|
|
limit := parser.NewStringParser(string(nginx_conf.Limit)).Parse()
|
|
|
|
for _, dir := range limit.GetDirectives() {
|
|
|
|
newDir := components.Directive{
|
|
|
|
Name: dir.GetName(),
|
|
|
|
Parameters: dir.GetParameters(),
|
|
|
|
}
|
|
|
|
config.UpdateDirectiveBySecondKey(dir.GetName(), dir.GetParameters()[0], newDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteNginxConfig(website model.WebSite, keys []string, scope dto.NginxScope) error {
|
|
|
|
nginxConfig, err := getNginxConfig(website.PrimaryDomain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config := nginxConfig.Config
|
|
|
|
config.RemoveDirectives(keys)
|
|
|
|
server := config.FindServers()[0]
|
|
|
|
server.RemoveDirectives(keys)
|
|
|
|
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nginxCheckAndReload(nginxConfig.OldContent, nginxConfig.FilePath, nginxConfig.ContainerName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getParamArray(key string, param interface{}) []string {
|
2022-11-07 16:19:05 +08:00
|
|
|
var res []string
|
|
|
|
switch param.(type) {
|
|
|
|
case string:
|
|
|
|
if key == "index" {
|
|
|
|
res = strings.Split(param.(string), "\n")
|
2022-11-08 15:42:31 +08:00
|
|
|
return res
|
2022-11-07 16:19:05 +08:00
|
|
|
}
|
2022-11-08 15:42:31 +08:00
|
|
|
|
|
|
|
res = strings.Split(param.(string), " ")
|
|
|
|
return res
|
2022-11-07 16:19:05 +08:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2022-11-08 15:42:31 +08:00
|
|
|
|
|
|
|
func handleParamMap(paramMap map[string]string, keys []string) []dto.NginxParam {
|
|
|
|
var nginxParams []dto.NginxParam
|
|
|
|
for k, v := range paramMap {
|
|
|
|
for _, name := range keys {
|
|
|
|
if name == k {
|
|
|
|
param := dto.NginxParam{
|
|
|
|
Name: k,
|
|
|
|
Params: getParamArray(k, v),
|
|
|
|
}
|
|
|
|
if isRepeatKey(k) {
|
|
|
|
param.IsRepeatKey = true
|
|
|
|
param.SecondKey = param.Params[0]
|
|
|
|
}
|
|
|
|
nginxParams = append(nginxParams, param)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nginxParams
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNginxParams(params interface{}, keys []string) []dto.NginxParam {
|
|
|
|
var nginxParams []dto.NginxParam
|
|
|
|
|
|
|
|
switch params.(type) {
|
|
|
|
case map[string]string:
|
|
|
|
return handleParamMap(params.(map[string]string), keys)
|
|
|
|
case []interface{}:
|
|
|
|
|
|
|
|
if mArray, ok := params.([]interface{}); ok {
|
|
|
|
for _, mA := range mArray {
|
|
|
|
if m, ok := mA.(map[string]interface{}); ok {
|
|
|
|
nginxParams = append(nginxParams, handleParamMap(toMapStr(m), keys)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nginxParams
|
|
|
|
}
|
|
|
|
|
|
|
|
func isRepeatKey(key string) bool {
|
|
|
|
|
|
|
|
if _, ok := dto.RepeatKeys[key]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func toMapStr(m map[string]interface{}) map[string]string {
|
|
|
|
ret := make(map[string]string, len(m))
|
|
|
|
for k, v := range m {
|
|
|
|
ret[k] = fmt.Sprint(v)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|