mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 00:49:22 +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
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/acepanel/panel/pkg/webserver/types"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// redirectFilePattern 匹配重定向配置文件名 (100-199)
|
||||
@@ -179,10 +180,8 @@ 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)
|
||||
uriSuffix := lo.If(redirect.KeepURI, "$request_uri").Else("")
|
||||
|
||||
var sb strings.Builder
|
||||
sb.WriteString("# Auto-generated by AcePanel. DO NOT EDIT MANUALLY!\n")
|
||||
@@ -192,22 +191,14 @@ func generateRedirectConfig(redirect types.Redirect) string {
|
||||
// URL 重定向
|
||||
sb.WriteString(fmt.Sprintf("# URL redirect: %s -> %s\n", redirect.From, redirect.To))
|
||||
sb.WriteString(fmt.Sprintf("location = %s {\n", redirect.From))
|
||||
if redirect.KeepURI {
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s$request_uri;\n", statusCode, redirect.To))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s;\n", statusCode, redirect.To))
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s%s;\n", statusCode, redirect.To, uriSuffix))
|
||||
sb.WriteString("}\n")
|
||||
|
||||
case types.RedirectTypeHost:
|
||||
// Host 重定向
|
||||
sb.WriteString(fmt.Sprintf("# Host redirect: %s -> %s\n", redirect.From, redirect.To))
|
||||
sb.WriteString(fmt.Sprintf("if ($host = \"%s\") {\n", redirect.From))
|
||||
if redirect.KeepURI {
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s$request_uri;\n", statusCode, redirect.To))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s;\n", statusCode, redirect.To))
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s%s;\n", statusCode, redirect.To, uriSuffix))
|
||||
sb.WriteString("}\n")
|
||||
|
||||
case types.RedirectType404:
|
||||
@@ -215,11 +206,7 @@ func generateRedirectConfig(redirect types.Redirect) string {
|
||||
sb.WriteString(fmt.Sprintf("# 404 redirect -> %s\n", redirect.To))
|
||||
sb.WriteString("error_page 404 = @redirect_404;\n")
|
||||
sb.WriteString("location @redirect_404 {\n")
|
||||
if redirect.KeepURI {
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s$request_uri;\n", statusCode, redirect.To))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s;\n", statusCode, redirect.To))
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf(" return %d %s%s;\n", statusCode, redirect.To, uriSuffix))
|
||||
sb.WriteString("}\n")
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/acepanel/panel/pkg/webserver/types"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
// upstreamFilePattern 匹配 upstream 配置文件名 (100-XXX-name.conf)
|
||||
@@ -217,11 +218,7 @@ func generateUpstreamConfig(upstream types.Upstream) string {
|
||||
|
||||
// 服务器列表
|
||||
for addr, options := range upstream.Servers {
|
||||
if options != "" {
|
||||
sb.WriteString(fmt.Sprintf(" server %s %s;\n", addr, options))
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf(" server %s;\n", addr))
|
||||
}
|
||||
sb.WriteString(fmt.Sprintf(" server %s;\n", lo.If(options != "", addr+" "+options).Else(addr)))
|
||||
}
|
||||
|
||||
// keepalive 连接数
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/tufanbarisyildirim/gonginx/config"
|
||||
|
||||
"github.com/acepanel/panel/pkg/webserver/types"
|
||||
@@ -406,14 +407,7 @@ func (v *baseVhost) RemoveConfig(name string, typ string) error {
|
||||
|
||||
func (v *baseVhost) SSL() bool {
|
||||
directive, err := v.parser.FindOne("server.ssl_certificate")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if len(v.parser.parameters2Slices(directive.GetParameters())) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
return err == nil && len(v.parser.parameters2Slices(directive.GetParameters())) != 0
|
||||
}
|
||||
|
||||
func (v *baseVhost) SSLConfig() *types.SSLConfig {
|
||||
@@ -695,21 +689,14 @@ func (v *baseVhost) SetBasicAuth(auth map[string]string) error {
|
||||
_ = v.parser.Clear("server.auth_basic")
|
||||
_ = v.parser.Clear("server.auth_basic_user_file")
|
||||
|
||||
realm := auth["realm"]
|
||||
userFile := auth["user_file"]
|
||||
|
||||
if realm == "" {
|
||||
realm = "Restricted"
|
||||
}
|
||||
|
||||
return v.parser.Set("server", []*config.Directive{
|
||||
{
|
||||
Name: "auth_basic",
|
||||
Parameters: []config.Parameter{{Value: realm}},
|
||||
Parameters: []config.Parameter{{Value: lo.If(auth["realm"] != "", auth["realm"]).Else("Restricted")}},
|
||||
},
|
||||
{
|
||||
Name: "auth_basic_user_file",
|
||||
Parameters: []config.Parameter{{Value: userFile}},
|
||||
Parameters: []config.Parameter{{Value: auth["user_file"]}},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user