mirror of
https://github.com/acepanel/panel.git
synced 2026-02-07 05:47:21 +08:00
feat: 代码优化
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/acepanel/panel/pkg/webserver/types"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// proxyFilePattern 匹配代理配置文件名 (200-299)
|
||||
@@ -222,11 +223,7 @@ func generateProxyConfig(proxy types.Proxy) string {
|
||||
sb.WriteString(fmt.Sprintf(" ProxyPassReverse %s %s\n", location, proxy.Pass))
|
||||
|
||||
// Host 配置
|
||||
if proxy.Host != "" {
|
||||
sb.WriteString(fmt.Sprintf(" RequestHeader set Host \"%s\"\n", proxy.Host))
|
||||
} else {
|
||||
sb.WriteString(" ProxyPreserveHost On\n")
|
||||
}
|
||||
sb.WriteString(lo.If(proxy.Host != "", fmt.Sprintf(" RequestHeader set Host \"%s\"\n", proxy.Host)).Else(" ProxyPreserveHost On\n"))
|
||||
|
||||
// SSL/SNI 配置
|
||||
if proxy.SNI != "" || strings.HasPrefix(proxy.Pass, "https://") {
|
||||
@@ -424,19 +421,11 @@ func generateBalancerConfig(upstream types.Upstream) string {
|
||||
|
||||
// 服务器列表
|
||||
for addr, options := range upstream.Servers {
|
||||
if options != "" {
|
||||
sb.WriteString(fmt.Sprintf(" BalancerMember %s %s\n", addr, options))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf(" BalancerMember %s\n", addr))
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf(" BalancerMember %s\n", lo.If(options != "", addr+" "+options).Else(addr)))
|
||||
}
|
||||
|
||||
// 负载均衡方法
|
||||
lbMethod := "byrequests" // 默认轮询
|
||||
if upstream.Algo != "" {
|
||||
lbMethod = upstream.Algo
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf(" ProxySet lbmethod=%s\n", lbMethod))
|
||||
sb.WriteString(fmt.Sprintf(" ProxySet lbmethod=%s\n", lo.If(upstream.Algo != "", upstream.Algo).Else("byrequests")))
|
||||
|
||||
// 连接池配置
|
||||
if upstream.Keepalive > 0 {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/acepanel/panel/pkg/webserver/types"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// redirectFilePattern 匹配重定向配置文件名 (100-199)
|
||||
@@ -192,10 +193,7 @@ func clearRedirectFiles(siteDir string) error {
|
||||
|
||||
// generateRedirectConfig 生成重定向配置内容
|
||||
func generateRedirectConfig(redirect types.Redirect) string {
|
||||
statusCode := redirect.StatusCode
|
||||
if statusCode == 0 {
|
||||
statusCode = 308 // 默认使用 308 永久重定向
|
||||
}
|
||||
statusCode := lo.If(redirect.StatusCode == 0, 308).Else(redirect.StatusCode)
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString("# Auto-generated by AcePanel. DO NOT EDIT MANUALLY!\n")
|
||||
@@ -206,17 +204,11 @@ func generateRedirectConfig(redirect types.Redirect) string {
|
||||
sb.WriteString(fmt.Sprintf("# URL redirect: %s -> %s\n", redirect.From, redirect.To))
|
||||
if redirect.KeepURI {
|
||||
// 使用 RedirectMatch 保持 URI
|
||||
from := redirect.From
|
||||
if !strings.HasPrefix(from, "^") {
|
||||
from = "^" + from
|
||||
}
|
||||
from := lo.If(strings.HasPrefix(redirect.From, "^"), redirect.From).Else("^" + redirect.From)
|
||||
if !strings.HasSuffix(from, "(.*)$") && !strings.HasSuffix(from, "$") {
|
||||
from = from + "(.*)$"
|
||||
}
|
||||
to := redirect.To
|
||||
if !strings.HasSuffix(to, "$1") {
|
||||
to = to + "$1"
|
||||
}
|
||||
to := lo.If(strings.HasSuffix(redirect.To, "$1"), redirect.To).Else(redirect.To + "$1")
|
||||
sb.WriteString(fmt.Sprintf("RedirectMatch %d %s %s\n", statusCode, from, to))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("Redirect %d %s %s\n", statusCode, redirect.From, redirect.To))
|
||||
@@ -228,11 +220,7 @@ func generateRedirectConfig(redirect types.Redirect) string {
|
||||
sb.WriteString("RewriteEngine on\n")
|
||||
escapedHost := strings.ReplaceAll(redirect.From, ".", `\.`)
|
||||
sb.WriteString(fmt.Sprintf("RewriteCond %%{HTTP_HOST} ^%s$ [NC]\n", escapedHost))
|
||||
if redirect.KeepURI {
|
||||
sb.WriteString(fmt.Sprintf("RewriteRule ^(.*)$ %s$1 [R=%d,L]\n", redirect.To, statusCode))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("RewriteRule ^(.*)$ %s [R=%d,L]\n", redirect.To, statusCode))
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf("RewriteRule ^(.*)$ %s [R=%d,L]\n", redirect.To+lo.If(redirect.KeepURI, "$1").Else(""), statusCode))
|
||||
|
||||
case types.RedirectType404:
|
||||
// 404 重定向
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/acepanel/panel/pkg/webserver/types"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// StaticVhost 纯静态虚拟主机
|
||||
@@ -584,16 +585,9 @@ func (v *baseVhost) BasicAuth() map[string]string {
|
||||
}
|
||||
|
||||
func (v *baseVhost) SetBasicAuth(auth map[string]string) error {
|
||||
realm := auth["realm"]
|
||||
userFile := auth["user_file"]
|
||||
|
||||
if realm == "" {
|
||||
realm = "Restricted"
|
||||
}
|
||||
|
||||
v.vhost.SetDirective("AuthType", "Basic")
|
||||
v.vhost.SetDirective("AuthName", fmt.Sprintf(`"%s"`, realm))
|
||||
v.vhost.SetDirective("AuthUserFile", userFile)
|
||||
v.vhost.SetDirective("AuthName", fmt.Sprintf(`"%s"`, lo.If(auth["realm"] != "", auth["realm"]).Else("Restricted")))
|
||||
v.vhost.SetDirective("AuthUserFile", auth["user_file"])
|
||||
v.vhost.SetDirective("Require", "valid-user")
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user