mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2026-11-28 16:46:56 +08:00
* feat(website): add custom HTTPS redirect port option Allow HTTP-to-HTTPS redirects to omit the local HTTPS port for Cloudflare or other reverse-proxy deployments terminating at 443. Apply the setting to existing websites and update HTTPS settings labels. (cherry picked from commit27c1468405) * feat(website): configure HTTPS redirect port per site Add a per-site HTTPS redirect port policy next to the HTTP-to-HTTPS option. Sites can inherit the global setting, use the Nginx HTTPS port, or force standard port 443, defaulting existing sites to inheritance. This supports reverse-proxied sites that need 443 and directly exposed sites that need 8443 on the same host. Global changes now update only inheriting sites, so adjusting one website does not rewrite other sites' redirect rules. Hide the global custom-port switch when Nginx uses standard port 443. (cherry picked from commit301dc15d9d) * Revert "feat(website): configure HTTPS redirect port per site" This reverts commit83d94f6cf9. The per-site database strategy will be replaced by a config-driven design. * feat(website): make HTTPS redirect port config-driven Read the site redirect port from managed nginx rules instead of storing per-site policy in the database. Only update rules matching the old global port, preserve manual configurations, and avoid rewriting sites when the global redirect setting is unchanged. --------- Co-authored-by: BOX <no@mail.com>
613 lines
19 KiB
Go
613 lines
19 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/task"
|
|
"github.com/1Panel-dev/1Panel/agent/buserr"
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
|
"github.com/subosito/gotenv"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/utils/compose"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/dto/request"
|
|
"github.com/1Panel-dev/1Panel/agent/app/dto/response"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/app/dto"
|
|
"github.com/1Panel-dev/1Panel/agent/app/model"
|
|
"github.com/1Panel-dev/1Panel/agent/constant"
|
|
"github.com/1Panel-dev/1Panel/agent/utils/files"
|
|
"github.com/1Panel-dev/1Panel/agent/utils/nginx"
|
|
"github.com/1Panel-dev/1Panel/agent/utils/nginx/parser"
|
|
)
|
|
|
|
type NginxService struct {
|
|
}
|
|
|
|
type INginxService interface {
|
|
GetNginxConfig() (*response.NginxFile, error)
|
|
GetConfigByScope(req request.NginxScopeReq) (interface{}, error)
|
|
UpdateConfigByScope(req request.NginxConfigUpdate) error
|
|
GetStatus() (response.NginxStatus, error)
|
|
UpdateConfigFile(req request.NginxConfigFileUpdate) error
|
|
|
|
Build(req request.NginxBuildReq) error
|
|
GetModules() (*response.NginxBuildConfig, error)
|
|
UpdateModule(req request.NginxModuleUpdate) error
|
|
|
|
OperateDefaultHTTPs(req request.NginxDefaultHTTPSUpdate) error
|
|
GetDefaultHttpsStatus() (*response.NginxConfigRes, error)
|
|
}
|
|
|
|
func NewINginxService() INginxService {
|
|
return &NginxService{}
|
|
}
|
|
|
|
func (n NginxService) GetNginxConfig() (*response.NginxFile, error) {
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
configPath := path.Join(global.Dir.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, nginxModuleConfDir, "nginx.conf")
|
|
byteContent, err := files.NewFileOp().GetContent(configPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &response.NginxFile{Content: string(byteContent)}, nil
|
|
}
|
|
|
|
func (n NginxService) GetConfigByScope(req request.NginxScopeReq) (interface{}, error) {
|
|
if req.Scope == dto.Brotli {
|
|
return getNginxBrotliParams()
|
|
}
|
|
keys, ok := dto.ScopeKeyMap[req.Scope]
|
|
if !ok || len(keys) == 0 {
|
|
return nil, nil
|
|
}
|
|
params, err := getNginxParamsByKeys(constant.NginxScopeHttp, keys, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
install, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return fillNginxIncludedHTTPParams(params, nginxHTTPUserIncludePaths(install, GetWebSiteRootDir())), nil
|
|
}
|
|
|
|
func (n NginxService) UpdateConfigByScope(req request.NginxConfigUpdate) error {
|
|
if req.Scope == dto.Brotli {
|
|
return updateNginxBrotliParams(getNginxParams(req.Params, dto.BrotliKeys))
|
|
}
|
|
keys, ok := dto.ScopeKeyMap[req.Scope]
|
|
if !ok || len(keys) == 0 {
|
|
return nil
|
|
}
|
|
return updateNginxConfig(constant.NginxScopeHttp, getNginxParams(req.Params, keys), nil)
|
|
}
|
|
|
|
func (n NginxService) GetStatus() (response.NginxStatus, error) {
|
|
httpPort, _, err := getAppInstallPort(constant.AppOpenresty)
|
|
if err != nil {
|
|
return response.NginxStatus{}, err
|
|
}
|
|
url := "http://127.0.0.1/nginx_status"
|
|
if httpPort != 80 {
|
|
url = fmt.Sprintf("http://127.0.0.1:%v/nginx_status", httpPort)
|
|
}
|
|
res, err := http.Get(url)
|
|
if err != nil || res.StatusCode > 300 {
|
|
return response.NginxStatus{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
content, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return response.NginxStatus{}, err
|
|
}
|
|
var status response.NginxStatus
|
|
resArray := strings.Split(string(content), " ")
|
|
active, err := strconv.Atoi(resArray[2])
|
|
if err == nil {
|
|
status.Active = active
|
|
}
|
|
accepts, err := strconv.Atoi(resArray[7])
|
|
if err == nil {
|
|
status.Accepts = accepts
|
|
}
|
|
handled, err := strconv.Atoi(resArray[8])
|
|
if err == nil {
|
|
status.Handled = handled
|
|
}
|
|
requests, err := strconv.Atoi(resArray[9])
|
|
if err == nil {
|
|
status.Requests = requests
|
|
}
|
|
reading, err := strconv.Atoi(resArray[11])
|
|
if err == nil {
|
|
status.Reading = reading
|
|
}
|
|
writing, err := strconv.Atoi(resArray[13])
|
|
if err == nil {
|
|
status.Writing = writing
|
|
}
|
|
waiting, err := strconv.Atoi(resArray[15])
|
|
if err == nil {
|
|
status.Waiting = waiting
|
|
}
|
|
return status, nil
|
|
}
|
|
|
|
func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error {
|
|
fileOp := files.NewFileOp()
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
filePath := path.Join(global.Dir.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, nginxModuleConfDir, "nginx.conf")
|
|
if req.Backup {
|
|
backupPath := path.Join(path.Dir(filePath), "bak")
|
|
if !fileOp.Stat(backupPath) {
|
|
if err := fileOp.CreateDir(backupPath, constant.DirPerm); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
newFile := path.Join(backupPath, "nginx.bak"+"-"+time.Now().Format("2006-01-02-15-04-05"))
|
|
if err := fileOp.Copy(filePath, backupPath); err != nil {
|
|
return err
|
|
}
|
|
if err := fileOp.Rename(path.Join(backupPath, "nginx.conf"), newFile); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
oldContent, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = fileOp.WriteFile(filePath, strings.NewReader(req.Content), constant.DirPerm); err != nil {
|
|
return err
|
|
}
|
|
if status, err := checkContainerStatus(nginxInstall.ContainerName); err == nil && status != "running" {
|
|
if out, err := compose.DownAndUp(nginxInstall.GetComposePath()); err != nil {
|
|
_ = fileOp.SaveFile(filePath, string(oldContent), constant.DirPerm)
|
|
return fmt.Errorf("nginx restart failed: %v", out)
|
|
} else {
|
|
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName)
|
|
}
|
|
}
|
|
return nginxCheckAndReload(string(oldContent), filePath, nginxInstall.ContainerName)
|
|
}
|
|
|
|
func (n NginxService) Build(req request.NginxBuildReq) error {
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
taskName := task.GetTaskName(nginxInstall.Name, task.TaskBuild, task.TaskScopeApp)
|
|
if err = task.CheckTaskIsExecuting(taskName); err != nil {
|
|
return err
|
|
}
|
|
if err = task.CheckScopeTaskIsExecuting(task.TaskScopeApp, nginxInstall.ID); err != nil {
|
|
return err
|
|
}
|
|
buildPath := path.Join(nginxInstall.GetPath(), nginxModuleBuildDir)
|
|
if !files.NewFileOp().Stat(buildPath) {
|
|
return buserr.New("ErrBuildDirNotFound")
|
|
}
|
|
|
|
buildTask, err := task.NewTaskWithOps(nginxInstall.Name, task.TaskBuild, task.TaskScopeApp, req.TaskID, nginxInstall.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
buildTask.AddSubTaskWithOps("", func(t *task.Task) error {
|
|
return executeNginxModuleBuild(nginxInstall, req.Modules, req.Force, req.Mirror, t, true)
|
|
}, nil, 0, 120*time.Minute)
|
|
|
|
go func() {
|
|
_ = buildTask.Execute()
|
|
}()
|
|
return nil
|
|
}
|
|
|
|
func (n NginxService) GetModules() (*response.NginxBuildConfig, error) {
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
modules, err := loadNginxModules(nginxInstall)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
target, targetWarning, targetErr := resolveNginxModuleTarget(nginxInstall)
|
|
if targetWarning != "" {
|
|
global.LOG.Warn(targetWarning)
|
|
}
|
|
runtimeTarget := target
|
|
runtimeTarget.BuilderDigest = ""
|
|
setNginxModuleTargetKey(&runtimeTarget)
|
|
runtimeTargetErr := targetErr
|
|
if runtimeTarget.OpenRestyVersion != "" {
|
|
runtimeTargetErr = nil
|
|
}
|
|
var resList []response.NginxModule
|
|
for _, module := range modules {
|
|
buildStatus := nginxModuleStatusPending
|
|
loadStatus := nginxModuleLoadDisabled
|
|
var artifacts []dto.NginxModuleArtifact
|
|
if module.BuildMode == nginxModuleBuildStatic && runtimeTargetErr == nil {
|
|
if build := findCurrentNginxModuleBuild(module, runtimeTarget); build != nil && build.Status == nginxModuleStatusReady {
|
|
buildStatus = nginxModuleStatusReady
|
|
if module.Enable {
|
|
loadStatus = nginxModuleLoadEnabled
|
|
}
|
|
} else if findLatestNginxModuleBuild(module, runtimeTarget) != nil {
|
|
if module.Enable {
|
|
loadStatus = nginxModuleLoadEnabled
|
|
}
|
|
}
|
|
} else if targetErr == nil {
|
|
if build := findCurrentNginxModuleBuild(module, target); build != nil {
|
|
buildStatus = build.Status
|
|
artifacts = build.Artifacts
|
|
if build.Status == nginxModuleStatusReady {
|
|
if module.Enable {
|
|
loadStatus = nginxModuleLoadEnabled
|
|
}
|
|
}
|
|
} else if latestBuild := findLatestNginxModuleBuild(module, target); latestBuild != nil {
|
|
artifacts = latestBuild.Artifacts
|
|
if module.Enable {
|
|
loadStatus = nginxModuleLoadEnabled
|
|
}
|
|
}
|
|
}
|
|
if module.BuildMode != nginxModuleBuildStatic && module.LastError != "" {
|
|
buildStatus = nginxModuleStatusFailed
|
|
}
|
|
resList = append(resList, response.NginxModule{
|
|
Name: module.Name,
|
|
Custom: module.Custom,
|
|
Script: module.Script,
|
|
Packages: strings.Join(module.Packages, ","),
|
|
Params: module.Params,
|
|
Enable: module.Enable,
|
|
BuildMode: module.BuildMode,
|
|
Provider: module.Provider,
|
|
LoadOrder: module.LoadOrder,
|
|
BuildStatus: buildStatus,
|
|
LoadStatus: loadStatus,
|
|
Artifacts: artifacts,
|
|
LastError: module.LastError,
|
|
})
|
|
}
|
|
envs, err := gotenv.Read(nginxInstall.GetEnvPath())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &response.NginxBuildConfig{
|
|
Mirror: envs["CONTAINER_PACKAGE_URL"],
|
|
DynamicSupported: nginxModuleDynamicSupported(nginxInstall),
|
|
Modules: resList,
|
|
}, nil
|
|
}
|
|
|
|
func applyNginxModuleUpdate(modules []dto.NginxModule, req request.NginxModuleUpdate) ([]dto.NginxModule, *dto.NginxModule, error) {
|
|
switch req.Operate {
|
|
case nginxModuleOperateCreate:
|
|
if err := validateNginxModuleBuildMode(dto.NginxModule{Name: req.Name, BuildMode: req.BuildMode}); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
for _, module := range modules {
|
|
if module.Name == req.Name {
|
|
return nil, nil, buserr.New("ErrNameIsExist")
|
|
}
|
|
}
|
|
modules = append(modules, dto.NginxModule{
|
|
Name: req.Name,
|
|
Custom: true,
|
|
Script: req.Script,
|
|
Packages: strings.Split(req.Packages, ","),
|
|
Params: req.Params,
|
|
Enable: req.Enable,
|
|
BuildMode: req.BuildMode,
|
|
Provider: req.Provider,
|
|
LoadOrder: req.LoadOrder,
|
|
})
|
|
case nginxModuleOperateUpdate:
|
|
for i := range modules {
|
|
if modules[i].Name != req.Name {
|
|
continue
|
|
}
|
|
if modules[i].Custom {
|
|
if err := validateNginxModuleBuildMode(dto.NginxModule{Name: req.Name, BuildMode: req.BuildMode}); err != nil {
|
|
return nil, nil, err
|
|
}
|
|
} else {
|
|
modules[i].Enable = req.Enable
|
|
return modules, nil, nil
|
|
}
|
|
modules[i].Enable = req.Enable
|
|
modules[i].Script = req.Script
|
|
modules[i].Packages = strings.Split(req.Packages, ",")
|
|
modules[i].Params = req.Params
|
|
modules[i].BuildMode = req.BuildMode
|
|
modules[i].Provider = req.Provider
|
|
modules[i].LoadOrder = req.LoadOrder
|
|
return modules, nil, nil
|
|
}
|
|
return nil, nil, fmt.Errorf("OpenResty module %s not found", req.Name)
|
|
case nginxModuleOperateDelete:
|
|
for i := range modules {
|
|
if modules[i].Name != req.Name {
|
|
continue
|
|
}
|
|
if !modules[i].Custom {
|
|
return nil, nil, fmt.Errorf("built-in OpenResty module %s cannot be deleted", req.Name)
|
|
}
|
|
deleted := modules[i]
|
|
modules = append(modules[:i], modules[i+1:]...)
|
|
return modules, &deleted, nil
|
|
}
|
|
return nil, nil, fmt.Errorf("OpenResty module %s not found", req.Name)
|
|
default:
|
|
return nil, nil, fmt.Errorf("unsupported OpenResty module operation %q", req.Operate)
|
|
}
|
|
return modules, nil, nil
|
|
}
|
|
|
|
func (n NginxService) UpdateModule(req request.NginxModuleUpdate) error {
|
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = task.CheckScopeTaskIsExecuting(task.TaskScopeApp, nginxInstall.ID); err != nil {
|
|
return err
|
|
}
|
|
modules, err := loadNginxModules(nginxInstall)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
oldModules := cloneNginxModules(modules)
|
|
modules, deletedModule, err := applyNginxModuleUpdate(modules, req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = saveNginxModules(nginxInstall, modules); err != nil {
|
|
return err
|
|
}
|
|
if err = reconcileDynamicNginxModuleConfig(nginxInstall, modules, true); err != nil {
|
|
_ = saveNginxModules(nginxInstall, oldModules)
|
|
_ = reconcileDynamicNginxModuleConfig(nginxInstall, oldModules, false)
|
|
return err
|
|
}
|
|
if deletedModule != nil {
|
|
_ = removeNginxModuleArtifacts(nginxInstall, *deletedModule)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (n NginxService) OperateDefaultHTTPs(req request.NginxDefaultHTTPSUpdate) error {
|
|
websiteListenMutation.Lock()
|
|
defer websiteListenMutation.Unlock()
|
|
appInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
websites, _ := websiteRepo.List()
|
|
hasDefaultWebsite := false
|
|
for _, website := range websites {
|
|
if website.DefaultServer {
|
|
hasDefaultWebsite = true
|
|
break
|
|
}
|
|
}
|
|
defaultConfigPath := path.Join(appInstall.GetPath(), nginxModuleConfDir, "default", "00.default.conf")
|
|
content, err := os.ReadFile(defaultConfigPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch req.Operate {
|
|
case "enable":
|
|
if req.SSLRejectHandshake {
|
|
defaultWebsite, _ := websiteRepo.GetFirst(websiteRepo.WithDefaultServer())
|
|
if defaultWebsite.ID > 0 {
|
|
return buserr.New("ErrDefaultWebsite")
|
|
}
|
|
}
|
|
if err := handleSSLConfig(&appInstall, hasDefaultWebsite, req.SSLRejectHandshake); err != nil {
|
|
return err
|
|
}
|
|
case "disable":
|
|
defaultConfig, err := parser.NewStringParser(string(content)).Parse()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defaultConfig.FilePath = defaultConfigPath
|
|
defaultServer := defaultConfig.FindServers()[0]
|
|
defaultServer.RemoveListen(fmt.Sprintf("%d", appInstall.HttpsPort))
|
|
defaultServer.RemoveListen(fmt.Sprintf("[::]:%d", appInstall.HttpsPort))
|
|
defaultServer.RemoveDirective("include", []string{"/usr/local/openresty/nginx/conf/ssl/root_ssl.conf"})
|
|
defaultServer.RemoveDirective("http2", []string{"on"})
|
|
defaultServer.RemoveDirective("ssl_reject_handshake", []string{"on"})
|
|
if err = nginx.WriteConfig(defaultConfig, nginx.IndentedStyle); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err = nginxCheckAndReload(string(content), defaultConfigPath, appInstall.ContainerName); err != nil {
|
|
return err
|
|
}
|
|
previousUseCustomPort := getHTTPSRedirectCustomPort()
|
|
if req.HTTPSRedirectCustomPort == nil || *req.HTTPSRedirectCustomPort == previousUseCustomPort {
|
|
return nil
|
|
}
|
|
oldPort := effectiveHTTPSRedirectPort(previousUseCustomPort, appInstall.HttpsPort)
|
|
newPort := effectiveHTTPSRedirectPort(*req.HTTPSRedirectCustomPort, appInstall.HttpsPort)
|
|
if oldPort != newPort {
|
|
if err = updateWebsiteHTTPSRedirectPort(oldPort, newPort); err != nil {
|
|
return errors.Join(err, restoreDefaultHTTPSConfig(appInstall, defaultConfigPath, content))
|
|
}
|
|
}
|
|
value := constant.StatusDisable
|
|
if *req.HTTPSRedirectCustomPort {
|
|
value = constant.StatusEnable
|
|
}
|
|
if err = settingRepo.UpdateOrCreate(constant.HTTPSRedirectCustomPortKey, value); err != nil {
|
|
var rollbackErr error
|
|
if oldPort != newPort {
|
|
rollbackErr = updateWebsiteHTTPSRedirectPort(newPort, oldPort)
|
|
}
|
|
return errors.Join(err, rollbackErr, restoreDefaultHTTPSConfig(appInstall, defaultConfigPath, content))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func restoreDefaultHTTPSConfig(appInstall model.AppInstall, filePath string, oldContent []byte) error {
|
|
currentContent, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if string(currentContent) == string(oldContent) {
|
|
return nil
|
|
}
|
|
if err = os.WriteFile(filePath, oldContent, constant.FilePerm); err != nil {
|
|
return err
|
|
}
|
|
return nginxCheckAndReload(string(currentContent), filePath, appInstall.ContainerName)
|
|
}
|
|
|
|
func (n NginxService) GetDefaultHttpsStatus() (*response.NginxConfigRes, error) {
|
|
appInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defaultConfigPath := path.Join(appInstall.GetPath(), nginxModuleConfDir, "default", "00.default.conf")
|
|
content, err := os.ReadFile(defaultConfigPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defaultConfig, err := parser.NewStringParser(string(content)).Parse()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defaultConfig.FilePath = defaultConfigPath
|
|
defaultServer := defaultConfig.FindServers()[0]
|
|
res := &response.NginxConfigRes{
|
|
HTTPSRedirectCustomPort: getHTTPSRedirectCustomPort(),
|
|
}
|
|
for _, directive := range defaultServer.GetDirectives() {
|
|
if directive.GetName() == "include" && directive.GetParameters()[0] == "/usr/local/openresty/nginx/conf/ssl/root_ssl.conf" {
|
|
res.Https = true
|
|
}
|
|
if directive.GetName() == "ssl_reject_handshake" && directive.GetParameters()[0] == "on" {
|
|
res.Https = true
|
|
res.SSLRejectHandshake = true
|
|
}
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
type websiteHTTPSRedirectConfig struct {
|
|
filePath string
|
|
oldContent []byte
|
|
newContent []byte
|
|
}
|
|
|
|
func updateWebsiteHTTPSRedirectPort(oldPort, newPort int) (err error) {
|
|
if oldPort == newPort {
|
|
return nil
|
|
}
|
|
appInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
websites, err := websiteRepo.List()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var updates []websiteHTTPSRedirectConfig
|
|
for _, website := range websites {
|
|
if website.Type == constant.Stream {
|
|
continue
|
|
}
|
|
filePath := GetWebsiteConfigPath(website)
|
|
content, err := os.ReadFile(filePath)
|
|
if os.IsNotExist(err) {
|
|
continue
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
config, err := parser.NewStringParser(string(content)).Parse()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
servers := config.FindServers()
|
|
if len(servers) == 0 {
|
|
continue
|
|
}
|
|
server := servers[0]
|
|
state := server.HTTPToHTTPSRedirectState()
|
|
if !state.Managed || state.Port != oldPort {
|
|
continue
|
|
}
|
|
server.SetHTTPToHTTPSRedirectPort(newPort)
|
|
config.FilePath = filePath
|
|
newContent := []byte(nginx.DumpConfig(config, nginx.IndentedStyle))
|
|
if string(newContent) == string(content) {
|
|
continue
|
|
}
|
|
updates = append(updates, websiteHTTPSRedirectConfig{
|
|
filePath: filePath,
|
|
oldContent: content,
|
|
newContent: newContent,
|
|
})
|
|
}
|
|
if len(updates) == 0 {
|
|
return nil
|
|
}
|
|
|
|
written := 0
|
|
reloadAttempted := false
|
|
defer func() {
|
|
if err == nil {
|
|
return
|
|
}
|
|
for _, update := range updates[:written] {
|
|
if restoreErr := os.WriteFile(update.filePath, update.oldContent, constant.FilePerm); restoreErr != nil {
|
|
err = errors.Join(err, fmt.Errorf("restore %s: %w", update.filePath, restoreErr))
|
|
}
|
|
}
|
|
if reloadAttempted {
|
|
if checkErr := opNginx(appInstall.ContainerName, constant.NginxCheck); checkErr != nil {
|
|
err = errors.Join(err, fmt.Errorf("check restored nginx configuration: %w", checkErr))
|
|
} else if reloadErr := opNginx(appInstall.ContainerName, constant.NginxReload); reloadErr != nil {
|
|
err = errors.Join(err, fmt.Errorf("reload restored nginx configuration: %w", reloadErr))
|
|
}
|
|
}
|
|
}()
|
|
|
|
for _, update := range updates {
|
|
written++
|
|
if err = os.WriteFile(update.filePath, update.newContent, constant.FilePerm); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
reloadAttempted = true
|
|
if err = opNginx(appInstall.ContainerName, constant.NginxCheck); err != nil {
|
|
return err
|
|
}
|
|
if err = opNginx(appInstall.ContainerName, constant.NginxReload); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|