Skip to content

Commit c535aa5

Browse files
committed
refactor(website): refactor stop website index show
1 parent e5c1987 commit c535aa5

4 files changed

Lines changed: 194 additions & 95 deletions

File tree

‎agent/app/service/nginx_utils.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,14 @@ func deleteNginxConfig(scope string, params []dto.NginxParam, website *model.Web
272272
block = config.Config.Block
273273
}
274274

275+
server, isServer := block.(*components.Server)
276+
wasStopped := isServer && server.HasStopPage()
275277
for _, param := range params {
276278
block.RemoveDirective(param.Name, param.Params)
277279
}
280+
if wasStopped {
281+
server.EnableStopPage()
282+
}
278283

279284
if err := nginx.WriteConfig(config.Config, nginx.IndentedStyle); err != nil {
280285
return err

‎agent/app/service/website.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ func (w WebsiteService) ChangePHPVersion(req request.WebsitePHPVersionReq) (err
14281428
server.UpdateDirective("index", []string{"index.html index.php index.htm default.php default.htm default.html"})
14291429
}
14301430
}
1431-
if req.RuntimeID > 0 && site.Status != constant.WebStopped {
1431+
if req.RuntimeID > 0 && (site.Status != constant.WebStopped || server.HasStopPage()) {
14321432
updated := false
14331433
for _, location := range server.FindDirectives("location") {
14341434
if strings.Join(location.GetParameters(), " ") == "~ [^/]\\.php(/|$)" && location.GetBlock() != nil {

‎agent/app/service/website_utils.go‎

Lines changed: 101 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"path"
1515
"path/filepath"
16+
"slices"
1617
"sort"
1718
"strconv"
1819
"strings"
@@ -1096,6 +1097,9 @@ func buildWebsiteSSLParams(alias string, req request.WebsiteHTTPSOp, redirectPor
10961097
}
10971098

10981099
func applyWebsiteSSLConfig(server *components.Server, website model.Website, plan websiteTLSPlan, req request.WebsiteHTTPSOp) {
1100+
if server.HasStopPage() {
1101+
defer server.EnableStopPage()
1102+
}
10991103
for _, port := range plan.httpsPorts {
11001104
portStr := strconv.Itoa(port)
11011105
server.RemoveListenByBind(portStr)
@@ -1255,108 +1259,28 @@ func opWebsite(website *model.Website, operate string) error {
12551259
}
12561260
server := servers[0]
12571261
if operate == constant.StopWeb {
1258-
proxyInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
1259-
server.RemoveDirective("include", []string{proxyInclude})
1260-
rewriteInclude := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.Alias)
1261-
server.RemoveDirective("include", []string{rewriteInclude})
1262-
redirectInclude := getWebsiteRedirectInclude(*website)
1263-
server.RemoveDirective("include", []string{redirectInclude})
1264-
1265-
switch website.Type {
1266-
case constant.Deployment:
1267-
server.RemoveDirective("location", []string{"/"})
1268-
case constant.Runtime:
1269-
runtime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(website.RuntimeID))
1270-
if err != nil {
1271-
return err
1272-
}
1273-
if runtime.Type == constant.RuntimePHP {
1274-
server.RemoveDirective("location", []string{"~", "[^/]\\.php(/|$)"})
1275-
} else {
1276-
server.RemoveDirective("location", []string{"/"})
1262+
changed := false
1263+
for _, siteServer := range servers {
1264+
if siteServer.EnableStopPage() {
1265+
changed = true
12771266
}
12781267
}
1279-
server.UpdateRoot("/usr/share/nginx/html/stop")
12801268
website.Status = constant.WebStopped
1269+
if !changed {
1270+
return nil
1271+
}
12811272
}
12821273
if operate == constant.StartWeb {
1283-
absoluteIncludeDir := GetSitePath(*website, SiteProxyDir)
1284-
fileOp := files.NewFileOp()
1285-
if fileOp.Stat(absoluteIncludeDir) && !files.IsEmptyDir(absoluteIncludeDir) {
1286-
proxyInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
1287-
server.UpdateDirective("include", []string{proxyInclude})
1288-
}
1289-
rewriteInclude := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.Alias)
1290-
absoluteRewritePath := GetSitePath(*website, SiteReWritePath)
1291-
if fileOp.Stat(absoluteRewritePath) {
1292-
server.UpdateDirective("include", []string{rewriteInclude})
1274+
for _, siteServer := range servers {
1275+
siteServer.DisableStopPage()
12931276
}
1294-
redirectInclude := getWebsiteRedirectInclude(*website)
1295-
absoluteRedirectPath := GetSitePath(*website, SiteRedirectDir)
1296-
if fileOp.Stat(absoluteRedirectPath) {
1297-
server.UpdateDirective("include", []string{redirectInclude})
1298-
}
1299-
rootIndex := path.Join("/www/sites", website.Alias, "index")
1300-
if website.SiteDir != "/" {
1301-
rootIndex = path.Join(rootIndex, website.SiteDir)
1302-
}
1303-
switch website.Type {
1304-
case constant.Deployment:
1305-
server.RemoveDirective("root", nil)
1306-
appInstall, err := appInstallRepo.GetFirst(repo.WithByID(website.AppInstallID))
1307-
if err != nil {
1308-
return err
1309-
}
1310-
proxy, err := getAppInstallProxyPass(&appInstall)
1311-
if err != nil {
1312-
return err
1313-
}
1314-
server.UpdateRootProxy([]string{proxy})
1315-
case constant.Static:
1316-
server.UpdateRoot(rootIndex)
1317-
server.UpdateRootLocation()
1318-
case constant.Proxy:
1319-
server.RemoveDirective("root", nil)
1320-
case constant.Runtime:
1321-
server.UpdateRoot(rootIndex)
1322-
localPath := ""
1323-
runtime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(website.RuntimeID))
1324-
if err != nil {
1325-
return err
1326-
}
1327-
if runtime.Type == constant.RuntimePHP {
1328-
if website.ProxyType == constant.RuntimeProxyUnix || website.ProxyType == constant.RuntimeProxyTcp {
1329-
localPath = path.Join(rootIndex, "index.php")
1330-
}
1331-
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
1332-
} else {
1333-
proxy := normalizeProxyPass(website.Proxy)
1334-
server.UpdateRootProxy([]string{proxy})
1335-
}
1336-
case constant.Subsite:
1337-
parentWebsite, err := websiteRepo.GetFirst(repo.WithByID(website.ParentWebsiteID))
1338-
if err != nil {
1339-
return err
1340-
}
1341-
website.Proxy = parentWebsite.Proxy
1342-
rootIndex = path.Join("/www/sites", parentWebsite.Alias, "index", website.SiteDir)
1343-
if parentWebsite.Type == constant.Runtime {
1344-
parentRuntime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(parentWebsite.RuntimeID))
1345-
if err != nil {
1277+
// Older agents stopped sites by replacing the root and deleting routes.
1278+
for _, directive := range server.Directives {
1279+
if directive.GetName() == "root" && slices.Equal(directive.GetParameters(), []string{components.StopPageRoot}) {
1280+
if err := restoreLegacyWebsiteRoutes(server, website); err != nil {
13461281
return err
13471282
}
1348-
website.RuntimeID = parentRuntime.ID
1349-
if parentRuntime.Type == constant.RuntimePHP {
1350-
server.UpdateRoot(rootIndex)
1351-
localPath := ""
1352-
if parentRuntime.Resource == constant.ResourceLocal {
1353-
localPath = path.Join(rootIndex, "index.php")
1354-
}
1355-
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
1356-
}
1357-
}
1358-
if parentWebsite.Type == constant.Static {
1359-
server.UpdateRoot(rootIndex)
1283+
break
13601284
}
13611285
}
13621286
website.Status = constant.WebRunning
@@ -1373,6 +1297,89 @@ func opWebsite(website *model.Website, operate string) error {
13731297
return nginxCheckAndReload(nginxInstall.SiteConfig.OldContent, config.FilePath, nginxInstall.Install.ContainerName)
13741298
}
13751299

1300+
func restoreLegacyWebsiteRoutes(server *components.Server, website *model.Website) error {
1301+
absoluteIncludeDir := GetSitePath(*website, SiteProxyDir)
1302+
fileOp := files.NewFileOp()
1303+
if fileOp.Stat(absoluteIncludeDir) && !files.IsEmptyDir(absoluteIncludeDir) {
1304+
proxyInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
1305+
server.UpdateDirective("include", []string{proxyInclude})
1306+
}
1307+
rewriteInclude := fmt.Sprintf("/www/sites/%s/rewrite/%s.conf", website.Alias, website.Alias)
1308+
absoluteRewritePath := GetSitePath(*website, SiteReWritePath)
1309+
if fileOp.Stat(absoluteRewritePath) {
1310+
server.UpdateDirective("include", []string{rewriteInclude})
1311+
}
1312+
redirectInclude := getWebsiteRedirectInclude(*website)
1313+
absoluteRedirectPath := GetSitePath(*website, SiteRedirectDir)
1314+
if fileOp.Stat(absoluteRedirectPath) {
1315+
server.UpdateDirective("include", []string{redirectInclude})
1316+
}
1317+
rootIndex := path.Join("/www/sites", website.Alias, "index")
1318+
if website.SiteDir != "/" {
1319+
rootIndex = path.Join(rootIndex, website.SiteDir)
1320+
}
1321+
switch website.Type {
1322+
case constant.Deployment:
1323+
server.RemoveDirective("root", nil)
1324+
appInstall, err := appInstallRepo.GetFirst(repo.WithByID(website.AppInstallID))
1325+
if err != nil {
1326+
return err
1327+
}
1328+
proxy, err := getAppInstallProxyPass(&appInstall)
1329+
if err != nil {
1330+
return err
1331+
}
1332+
server.UpdateRootProxy([]string{proxy})
1333+
case constant.Static:
1334+
server.UpdateRoot(rootIndex)
1335+
server.UpdateRootLocation()
1336+
case constant.Proxy:
1337+
server.RemoveDirective("root", nil)
1338+
case constant.Runtime:
1339+
server.UpdateRoot(rootIndex)
1340+
localPath := ""
1341+
runtime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(website.RuntimeID))
1342+
if err != nil {
1343+
return err
1344+
}
1345+
if runtime.Type == constant.RuntimePHP {
1346+
if website.ProxyType == constant.RuntimeProxyUnix || website.ProxyType == constant.RuntimeProxyTcp {
1347+
localPath = path.Join(rootIndex, "index.php")
1348+
}
1349+
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
1350+
} else {
1351+
proxy := normalizeProxyPass(website.Proxy)
1352+
server.UpdateRootProxy([]string{proxy})
1353+
}
1354+
case constant.Subsite:
1355+
parentWebsite, err := websiteRepo.GetFirst(repo.WithByID(website.ParentWebsiteID))
1356+
if err != nil {
1357+
return err
1358+
}
1359+
website.Proxy = parentWebsite.Proxy
1360+
rootIndex = path.Join("/www/sites", parentWebsite.Alias, "index", website.SiteDir)
1361+
if parentWebsite.Type == constant.Runtime {
1362+
parentRuntime, err := runtimeRepo.GetFirst(context.Background(), repo.WithByID(parentWebsite.RuntimeID))
1363+
if err != nil {
1364+
return err
1365+
}
1366+
website.RuntimeID = parentRuntime.ID
1367+
if parentRuntime.Type == constant.RuntimePHP {
1368+
server.UpdateRoot(rootIndex)
1369+
localPath := ""
1370+
if parentRuntime.Resource == constant.ResourceLocal {
1371+
localPath = path.Join(rootIndex, "index.php")
1372+
}
1373+
server.UpdatePHPProxy([]string{website.Proxy}, localPath)
1374+
}
1375+
}
1376+
if parentWebsite.Type == constant.Static {
1377+
server.UpdateRoot(rootIndex)
1378+
}
1379+
}
1380+
return nil
1381+
}
1382+
13761383
func changeIPV6(website model.Website, enable bool) error {
13771384
nginxFull, err := getNginxFull(&website)
13781385
if err != nil {

‎agent/utils/nginx/components/server.go‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package components
22

33
import (
4+
"crypto/rand"
45
"errors"
56
"fmt"
7+
"slices"
68
"strconv"
79
"strings"
810
)
@@ -291,6 +293,91 @@ func (s *Server) UpdateRoot(path string) {
291293
s.UpdateDirective("root", []string{path})
292294
}
293295

296+
const StopPageRoot = "/usr/share/nginx/html/stop"
297+
const stopPageComment = "# 1Panel managed stop page"
298+
299+
const stopPagePattern = `"^/(?![.]well-known/acme-challenge(?:/|$))"`
300+
301+
// EnableStopPage reports whether the configuration needs to be written.
302+
func (s *Server) EnableStopPage() bool {
303+
httpsOnly := slices.ContainsFunc(s.Directives, isHTTP2HTTPSRedirect)
304+
if s.HasStopPage() && stopPageRewrite(s.Directives[0]) != nil &&
305+
(s.Directives[0].GetName() == "if") == httpsOnly {
306+
return false
307+
}
308+
s.DisableStopPage()
309+
// Includes resolve inside OpenResty. Give each activation its own exact
310+
// location instead of claiming a fixed URI from the site's namespace.
311+
uri := "/.1panel-stop/" + rand.Text() + "/index.html"
312+
var rewrite IDirective = &Directive{
313+
Name: "rewrite", Comment: stopPageComment,
314+
Parameters: []string{stopPagePattern, uri, "last"},
315+
}
316+
if httpsOnly {
317+
// Let HTTP requests follow the site's existing redirect, including its port.
318+
rewrite = &Directive{Name: "if", Parameters: []string{"(", "$scheme", "=", "https", ")"},
319+
Block: &Block{Directives: []IDirective{rewrite}}}
320+
}
321+
location := &Directive{
322+
Name: "location", Parameters: []string{"=", uri},
323+
Block: &Block{Directives: []IDirective{
324+
&Directive{Name: "root", Parameters: []string{StopPageRoot}},
325+
&Directive{Name: "try_files", Parameters: []string{"/index.html", "=404"}},
326+
}},
327+
}
328+
s.Directives = append([]IDirective{rewrite, location}, s.Directives...)
329+
return true
330+
}
331+
332+
func stopPageRewrite(dir IDirective) IDirective {
333+
if dir.GetName() == "if" && dir.GetBlock() != nil {
334+
children := dir.GetBlock().GetDirectives()
335+
if len(children) != 1 || strings.Join(dir.GetParameters(), "") != "($scheme=https)" {
336+
return nil
337+
}
338+
dir = children[0]
339+
}
340+
if dir.GetName() == "rewrite" && dir.GetComment() == stopPageComment && len(dir.GetParameters()) == 3 {
341+
return dir
342+
}
343+
return nil
344+
}
345+
346+
func (s *Server) DisableStopPage() {
347+
for _, dir := range s.Directives {
348+
if rewrite := stopPageRewrite(dir); rewrite != nil {
349+
uri := rewrite.GetParameters()[1]
350+
s.Directives = slices.DeleteFunc(s.Directives, func(d IDirective) bool {
351+
return d == dir || d.GetName() == "location" && slices.Equal(d.GetParameters(), []string{"=", uri})
352+
})
353+
return
354+
}
355+
}
356+
}
357+
358+
func (s *Server) HasStopPage() bool {
359+
for _, dir := range s.Directives {
360+
rewrite := stopPageRewrite(dir)
361+
if rewrite == nil {
362+
continue
363+
}
364+
params := rewrite.GetParameters()
365+
if params[0] != stopPagePattern || params[2] != "last" {
366+
return false
367+
}
368+
for _, location := range s.Directives {
369+
if location.GetName() != "location" || !slices.Equal(location.GetParameters(), []string{"=", params[1]}) || location.GetBlock() == nil {
370+
continue
371+
}
372+
children := location.GetBlock().GetDirectives()
373+
return len(children) == 2 && children[0].GetName() == "root" &&
374+
slices.Equal(children[0].GetParameters(), []string{StopPageRoot}) &&
375+
children[1].GetName() == "try_files" && slices.Equal(children[1].GetParameters(), []string{"/index.html", "=404"})
376+
}
377+
}
378+
return false
379+
}
380+
294381
func (s *Server) UpdateRootProxyForAi(proxy []string) {
295382
newDir := Directive{
296383
Name: "location",

0 commit comments

Comments
 (0)