mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2026-11-17 00:06:00 +00:00
refactor(website): refactor stop website index show
This commit is contained in:
@@ -272,9 +272,14 @@ func deleteNginxConfig(scope string, params []dto.NginxParam, website *model.Web
|
||||
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
|
||||
|
||||
@@ -1428,7 +1428,7 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) (err
|
||||
server.UpdateDirective("index", []string{"index.html index.php index.htm default.php default.htm default.html"})
|
||||
}
|
||||
}
|
||||
if req.RuntimeID > 0 && site.Status != constant.WebStopped {
|
||||
if req.RuntimeID > 0 && (site.Status != constant.WebStopped || server.HasStopPage()) {
|
||||
updated := false
|
||||
for _, location := range server.FindDirectives("location") {
|
||||
if strings.Join(location.GetParameters(), " ") == "~ [^/]\\.php(/|$)" && location.GetBlock() != nil {
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -1096,6 +1097,9 @@ func buildWebsiteSSLParams(alias string, req request.WebsiteHTTPSOp, redirectPor
|
||||
}
|
||||
|
||||
func applyWebsiteSSLConfig(server *components.Server, website model.Website, plan websiteTLSPlan, req request.WebsiteHTTPSOp) {
|
||||
if server.HasStopPage() {
|
||||
defer server.EnableStopPage()
|
||||
}
|
||||
for _, port := range plan.httpsPorts {
|
||||
portStr := strconv.Itoa(port)
|
||||
server.RemoveListenByBind(portStr)
|
||||
@@ -1255,108 +1259,28 @@ func opWebsite(website *model.Website, operate string) error {
|
||||
}
|
||||
server := servers[0]
|
||||
if operate == constant.StopWeb {
|
||||
proxyInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
|
||||
server.RemoveDirective("include", []string{proxyInclude})
|
||||
rewriteInclude := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.Alias)
|
||||
server.RemoveDirective("include", []string{rewriteInclude})
|
||||
redirectInclude := getWebsiteRedirectInclude(*website)
|
||||
server.RemoveDirective("include", []string{redirectInclude})
|
||||
|
||||
switch website.Type {
|
||||
case constant.Deployment:
|
||||
server.RemoveDirective("location", []string{"/"})
|
||||
case constant.Runtime:
|
||||
runtime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(website.RuntimeID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if runtime.Type == constant.RuntimePHP {
|
||||
server.RemoveDirective("location", []string{"~", "[^/]\\.php(/|$)"})
|
||||
} else {
|
||||
server.RemoveDirective("location", []string{"/"})
|
||||
changed := false
|
||||
for _, siteServer := range servers {
|
||||
if siteServer.EnableStopPage() {
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
server.UpdateRoot("/usr/share/nginx/html/stop")
|
||||
website.Status = constant.WebStopped
|
||||
if !changed {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if operate == constant.StartWeb {
|
||||
absoluteIncludeDir := GetSitePath(*website, SiteProxyDir)
|
||||
fileOp := files.NewFileOp()
|
||||
if fileOp.Stat(absoluteIncludeDir) && !files.IsEmptyDir(absoluteIncludeDir) {
|
||||
proxyInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
|
||||
server.UpdateDirective("include", []string{proxyInclude})
|
||||
for _, siteServer := range servers {
|
||||
siteServer.DisableStopPage()
|
||||
}
|
||||
rewriteInclude := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.Alias)
|
||||
absoluteRewritePath := GetSitePath(*website, SiteReWritePath)
|
||||
if fileOp.Stat(absoluteRewritePath) {
|
||||
server.UpdateDirective("include", []string{rewriteInclude})
|
||||
}
|
||||
redirectInclude := getWebsiteRedirectInclude(*website)
|
||||
absoluteRedirectPath := GetSitePath(*website, SiteRedirectDir)
|
||||
if fileOp.Stat(absoluteRedirectPath) {
|
||||
server.UpdateDirective("include", []string{redirectInclude})
|
||||
}
|
||||
rootIndex := path.Join("/www/sites", website.Alias, "index")
|
||||
if website.SiteDir != "/" {
|
||||
rootIndex = path.Join(rootIndex, website.SiteDir)
|
||||
}
|
||||
switch website.Type {
|
||||
case constant.Deployment:
|
||||
server.RemoveDirective("root", nil)
|
||||
appInstall, err := appInstallRepo.GetFirst(repo.WithByID(website.AppInstallID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proxy, err := getAppInstallProxyPass(&appInstall)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
server.UpdateRootProxy([]string{proxy})
|
||||
case constant.Static:
|
||||
server.UpdateRoot(rootIndex)
|
||||
server.UpdateRootLocation()
|
||||
case constant.Proxy:
|
||||
server.RemoveDirective("root", nil)
|
||||
case constant.Runtime:
|
||||
server.UpdateRoot(rootIndex)
|
||||
localPath := ""
|
||||
runtime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(website.RuntimeID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if runtime.Type == constant.RuntimePHP {
|
||||
if website.ProxyType == constant.RuntimeProxyUnix || website.ProxyType == constant.RuntimeProxyTcp {
|
||||
localPath = path.Join(rootIndex, "index.php")
|
||||
}
|
||||
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
|
||||
} else {
|
||||
proxy := normalizeProxyPass(website.Proxy)
|
||||
server.UpdateRootProxy([]string{proxy})
|
||||
}
|
||||
case constant.Subsite:
|
||||
parentWebsite, err := websiteRepo.GetFirst(repo.WithByID(website.ParentWebsiteID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
website.Proxy = parentWebsite.Proxy
|
||||
rootIndex = path.Join("/www/sites", parentWebsite.Alias, "index", website.SiteDir)
|
||||
if parentWebsite.Type == constant.Runtime {
|
||||
parentRuntime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(parentWebsite.RuntimeID))
|
||||
if err != nil {
|
||||
// Older agents stopped sites by replacing the root and deleting routes.
|
||||
for _, directive := range server.Directives {
|
||||
if directive.GetName() == "root" && slices.Equal(directive.GetParameters(), []string{components.StopPageRoot}) {
|
||||
if err := restoreLegacyWebsiteRoutes(server, website); err != nil {
|
||||
return err
|
||||
}
|
||||
website.RuntimeID = parentRuntime.ID
|
||||
if parentRuntime.Type == constant.RuntimePHP {
|
||||
server.UpdateRoot(rootIndex)
|
||||
localPath := ""
|
||||
if parentRuntime.Resource == constant.ResourceLocal {
|
||||
localPath = path.Join(rootIndex, "index.php")
|
||||
}
|
||||
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
|
||||
}
|
||||
}
|
||||
if parentWebsite.Type == constant.Static {
|
||||
server.UpdateRoot(rootIndex)
|
||||
break
|
||||
}
|
||||
}
|
||||
website.Status = constant.WebRunning
|
||||
@@ -1373,6 +1297,89 @@ func opWebsite(website *model.Website, operate string) error {
|
||||
return nginxCheckAndReload(nginxInstall.SiteConfig.OldContent, config.FilePath, nginxInstall.Install.ContainerName)
|
||||
}
|
||||
|
||||
func restoreLegacyWebsiteRoutes(server *components.Server, website *model.Website) error {
|
||||
absoluteIncludeDir := GetSitePath(*website, SiteProxyDir)
|
||||
fileOp := files.NewFileOp()
|
||||
if fileOp.Stat(absoluteIncludeDir) && !files.IsEmptyDir(absoluteIncludeDir) {
|
||||
proxyInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
|
||||
server.UpdateDirective("include", []string{proxyInclude})
|
||||
}
|
||||
rewriteInclude := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.Alias)
|
||||
absoluteRewritePath := GetSitePath(*website, SiteReWritePath)
|
||||
if fileOp.Stat(absoluteRewritePath) {
|
||||
server.UpdateDirective("include", []string{rewriteInclude})
|
||||
}
|
||||
redirectInclude := getWebsiteRedirectInclude(*website)
|
||||
absoluteRedirectPath := GetSitePath(*website, SiteRedirectDir)
|
||||
if fileOp.Stat(absoluteRedirectPath) {
|
||||
server.UpdateDirective("include", []string{redirectInclude})
|
||||
}
|
||||
rootIndex := path.Join("/www/sites", website.Alias, "index")
|
||||
if website.SiteDir != "/" {
|
||||
rootIndex = path.Join(rootIndex, website.SiteDir)
|
||||
}
|
||||
switch website.Type {
|
||||
case constant.Deployment:
|
||||
server.RemoveDirective("root", nil)
|
||||
appInstall, err := appInstallRepo.GetFirst(repo.WithByID(website.AppInstallID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proxy, err := getAppInstallProxyPass(&appInstall)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
server.UpdateRootProxy([]string{proxy})
|
||||
case constant.Static:
|
||||
server.UpdateRoot(rootIndex)
|
||||
server.UpdateRootLocation()
|
||||
case constant.Proxy:
|
||||
server.RemoveDirective("root", nil)
|
||||
case constant.Runtime:
|
||||
server.UpdateRoot(rootIndex)
|
||||
localPath := ""
|
||||
runtime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(website.RuntimeID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if runtime.Type == constant.RuntimePHP {
|
||||
if website.ProxyType == constant.RuntimeProxyUnix || website.ProxyType == constant.RuntimeProxyTcp {
|
||||
localPath = path.Join(rootIndex, "index.php")
|
||||
}
|
||||
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
|
||||
} else {
|
||||
proxy := normalizeProxyPass(website.Proxy)
|
||||
server.UpdateRootProxy([]string{proxy})
|
||||
}
|
||||
case constant.Subsite:
|
||||
parentWebsite, err := websiteRepo.GetFirst(repo.WithByID(website.ParentWebsiteID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
website.Proxy = parentWebsite.Proxy
|
||||
rootIndex = path.Join("/www/sites", parentWebsite.Alias, "index", website.SiteDir)
|
||||
if parentWebsite.Type == constant.Runtime {
|
||||
parentRuntime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(parentWebsite.RuntimeID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
website.RuntimeID = parentRuntime.ID
|
||||
if parentRuntime.Type == constant.RuntimePHP {
|
||||
server.UpdateRoot(rootIndex)
|
||||
localPath := ""
|
||||
if parentRuntime.Resource == constant.ResourceLocal {
|
||||
localPath = path.Join(rootIndex, "index.php")
|
||||
}
|
||||
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
|
||||
}
|
||||
}
|
||||
if parentWebsite.Type == constant.Static {
|
||||
server.UpdateRoot(rootIndex)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func changeIPV6(website model.Website, enable bool) error {
|
||||
nginxFull, err := getNginxFull(&website)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package components
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -291,6 +293,91 @@ func (s *Server) UpdateRoot(path string) {
|
||||
s.UpdateDirective("root", []string{path})
|
||||
}
|
||||
|
||||
const StopPageRoot = "/usr/share/nginx/html/stop"
|
||||
const stopPageComment = "# 1Panel managed stop page"
|
||||
|
||||
const stopPagePattern = `"^/(?![.]well-known/acme-challenge(?:/|$))"`
|
||||
|
||||
// EnableStopPage reports whether the configuration needs to be written.
|
||||
func (s *Server) EnableStopPage() bool {
|
||||
httpsOnly := slices.ContainsFunc(s.Directives, isHTTP2HTTPSRedirect)
|
||||
if s.HasStopPage() && stopPageRewrite(s.Directives[0]) != nil &&
|
||||
(s.Directives[0].GetName() == "if") == httpsOnly {
|
||||
return false
|
||||
}
|
||||
s.DisableStopPage()
|
||||
// Includes resolve inside OpenResty. Give each activation its own exact
|
||||
// location instead of claiming a fixed URI from the site's namespace.
|
||||
uri := "/.1panel-stop/" + rand.Text() + "/index.html"
|
||||
var rewrite IDirective = &Directive{
|
||||
Name: "rewrite", Comment: stopPageComment,
|
||||
Parameters: []string{stopPagePattern, uri, "last"},
|
||||
}
|
||||
if httpsOnly {
|
||||
// Let HTTP requests follow the site's existing redirect, including its port.
|
||||
rewrite = &Directive{Name: "if", Parameters: []string{"(", "$scheme", "=", "https", ")"},
|
||||
Block: &Block{Directives: []IDirective{rewrite}}}
|
||||
}
|
||||
location := &Directive{
|
||||
Name: "location", Parameters: []string{"=", uri},
|
||||
Block: &Block{Directives: []IDirective{
|
||||
&Directive{Name: "root", Parameters: []string{StopPageRoot}},
|
||||
&Directive{Name: "try_files", Parameters: []string{"/index.html", "=404"}},
|
||||
}},
|
||||
}
|
||||
s.Directives = append([]IDirective{rewrite, location}, s.Directives...)
|
||||
return true
|
||||
}
|
||||
|
||||
func stopPageRewrite(dir IDirective) IDirective {
|
||||
if dir.GetName() == "if" && dir.GetBlock() != nil {
|
||||
children := dir.GetBlock().GetDirectives()
|
||||
if len(children) != 1 || strings.Join(dir.GetParameters(), "") != "($scheme=https)" {
|
||||
return nil
|
||||
}
|
||||
dir = children[0]
|
||||
}
|
||||
if dir.GetName() == "rewrite" && dir.GetComment() == stopPageComment && len(dir.GetParameters()) == 3 {
|
||||
return dir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) DisableStopPage() {
|
||||
for _, dir := range s.Directives {
|
||||
if rewrite := stopPageRewrite(dir); rewrite != nil {
|
||||
uri := rewrite.GetParameters()[1]
|
||||
s.Directives = slices.DeleteFunc(s.Directives, func(d IDirective) bool {
|
||||
return d == dir || d.GetName() == "location" && slices.Equal(d.GetParameters(), []string{"=", uri})
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) HasStopPage() bool {
|
||||
for _, dir := range s.Directives {
|
||||
rewrite := stopPageRewrite(dir)
|
||||
if rewrite == nil {
|
||||
continue
|
||||
}
|
||||
params := rewrite.GetParameters()
|
||||
if params[0] != stopPagePattern || params[2] != "last" {
|
||||
return false
|
||||
}
|
||||
for _, location := range s.Directives {
|
||||
if location.GetName() != "location" || !slices.Equal(location.GetParameters(), []string{"=", params[1]}) || location.GetBlock() == nil {
|
||||
continue
|
||||
}
|
||||
children := location.GetBlock().GetDirectives()
|
||||
return len(children) == 2 && children[0].GetName() == "root" &&
|
||||
slices.Equal(children[0].GetParameters(), []string{StopPageRoot}) &&
|
||||
children[1].GetName() == "try_files" && slices.Equal(children[1].GetParameters(), []string{"/index.html", "=404"})
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) UpdateRootProxyForAi(proxy []string) {
|
||||
newDir := Directive{
|
||||
Name: "location",
|
||||
|
||||
Reference in New Issue
Block a user