style(proxy,server): 代码风格优化
- headers.go: 添加协议常量 protoHTTP/protoHTTPS - redirect_rewrite.go: 添加模式常量,修正缩进 - proxy_ssl_test.go: 表格测试字段对齐 - server.go: 添加 ServerModeAuto 分支防御性处理 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
d874f97765
commit
470c82d940
@ -10,6 +10,12 @@ import (
|
|||||||
"rua.plus/lolly/internal/netutil"
|
"rua.plus/lolly/internal/netutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 协议常量
|
||||||
|
const (
|
||||||
|
protoHTTP = "http"
|
||||||
|
protoHTTPS = "https"
|
||||||
|
)
|
||||||
|
|
||||||
// ForwardedHeaders 包含 X-Forwarded 系列头信息。
|
// ForwardedHeaders 包含 X-Forwarded 系列头信息。
|
||||||
type ForwardedHeaders struct {
|
type ForwardedHeaders struct {
|
||||||
ClientIP string // 客户端 IP
|
ClientIP string // 客户端 IP
|
||||||
@ -28,9 +34,9 @@ func ExtractForwardedHeaders(ctx *fasthttp.RequestCtx) ForwardedHeaders {
|
|||||||
clientIP := netutil.ExtractClientIP(ctx)
|
clientIP := netutil.ExtractClientIP(ctx)
|
||||||
host := string(ctx.Host())
|
host := string(ctx.Host())
|
||||||
|
|
||||||
proto := "http"
|
proto := protoHTTP
|
||||||
if ctx.IsTLS() {
|
if ctx.IsTLS() {
|
||||||
proto = "https"
|
proto = protoHTTPS
|
||||||
}
|
}
|
||||||
|
|
||||||
return ForwardedHeaders{
|
return ForwardedHeaders{
|
||||||
|
|||||||
@ -10,19 +10,26 @@ import (
|
|||||||
"rua.plus/lolly/internal/variable"
|
"rua.plus/lolly/internal/variable"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RedirectRewrite 模式常量
|
||||||
|
const (
|
||||||
|
redirectModeDefault = "default"
|
||||||
|
redirectModeOff = "off"
|
||||||
|
redirectModeCustom = "custom"
|
||||||
|
)
|
||||||
|
|
||||||
// compiledRule 预编译的改写规则
|
// compiledRule 预编译的改写规则
|
||||||
type compiledRule struct {
|
type compiledRule struct {
|
||||||
pattern *regexp.Regexp // 正则模式,nil 表示非正则匹配
|
pattern *regexp.Regexp // 正则模式,nil 表示非正则匹配
|
||||||
exactMatch string // 精确匹配前缀(用于 prefix 匹配)
|
|
||||||
replacement string // 替换模板(含变量)
|
replacement string // 替换模板(含变量)
|
||||||
|
exactMatch string // 精确匹配前缀(用于 prefix 匹配)
|
||||||
caseInsensitive bool // 正则大小写不敏感(~* 前缀)
|
caseInsensitive bool // 正则大小写不敏感(~* 前缀)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RedirectRewriter Location/Refresh 头改写器
|
// RedirectRewriter Location/Refresh 头改写器
|
||||||
type RedirectRewriter struct {
|
type RedirectRewriter struct {
|
||||||
|
proxyPath string // 用于 default 模式(当前代理路径)
|
||||||
mode string // "default" | "off" | "custom"(空字符串视为 default)
|
mode string // "default" | "off" | "custom"(空字符串视为 default)
|
||||||
rules []compiledRule // 仅 custom 模式预编译
|
rules []compiledRule // 仅 custom 模式预编译
|
||||||
proxyPath string // 用于 default 模式(当前代理路径)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRedirectRewriter 创建改写器
|
// NewRedirectRewriter 创建改写器
|
||||||
@ -32,7 +39,7 @@ func NewRedirectRewriter(cfg *config.RedirectRewriteConfig, proxyPath string) (*
|
|||||||
if cfg == nil {
|
if cfg == nil {
|
||||||
// 未配置时默认启用 default 模式
|
// 未配置时默认启用 default 模式
|
||||||
return &RedirectRewriter{
|
return &RedirectRewriter{
|
||||||
mode: "default",
|
mode: redirectModeDefault,
|
||||||
proxyPath: proxyPath,
|
proxyPath: proxyPath,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
@ -43,7 +50,7 @@ func NewRedirectRewriter(cfg *config.RedirectRewriteConfig, proxyPath string) (*
|
|||||||
}
|
}
|
||||||
|
|
||||||
// custom 模式:预编译规则
|
// custom 模式:预编译规则
|
||||||
if cfg.Mode == "custom" {
|
if cfg.Mode == redirectModeCustom {
|
||||||
rules := make([]compiledRule, 0, len(cfg.Rules))
|
rules := make([]compiledRule, 0, len(cfg.Rules))
|
||||||
for _, rule := range cfg.Rules {
|
for _, rule := range cfg.Rules {
|
||||||
cr := compiledRule{
|
cr := compiledRule{
|
||||||
@ -52,7 +59,7 @@ func NewRedirectRewriter(cfg *config.RedirectRewriteConfig, proxyPath string) (*
|
|||||||
|
|
||||||
if strings.HasPrefix(rule.Pattern, "~") {
|
if strings.HasPrefix(rule.Pattern, "~") {
|
||||||
// 正则模式
|
// 正则模式
|
||||||
patternStr := rule.Pattern
|
var patternStr string
|
||||||
if strings.HasPrefix(rule.Pattern, "~*") {
|
if strings.HasPrefix(rule.Pattern, "~*") {
|
||||||
cr.caseInsensitive = true
|
cr.caseInsensitive = true
|
||||||
patternStr = rule.Pattern[2:]
|
patternStr = rule.Pattern[2:]
|
||||||
@ -80,7 +87,7 @@ func NewRedirectRewriter(cfg *config.RedirectRewriteConfig, proxyPath string) (*
|
|||||||
// Mode 返回当前模式(处理空字符串默认值)
|
// Mode 返回当前模式(处理空字符串默认值)
|
||||||
func (r *RedirectRewriter) Mode() string {
|
func (r *RedirectRewriter) Mode() string {
|
||||||
if r.mode == "" {
|
if r.mode == "" {
|
||||||
return "default"
|
return redirectModeDefault
|
||||||
}
|
}
|
||||||
return r.mode
|
return r.mode
|
||||||
}
|
}
|
||||||
@ -136,13 +143,13 @@ func (r *RedirectRewriter) rewriteURL(headerValue string, ctx *fasthttp.RequestC
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch r.Mode() {
|
switch r.Mode() {
|
||||||
case "off":
|
case redirectModeOff:
|
||||||
return headerValue
|
return headerValue
|
||||||
|
|
||||||
case "custom":
|
case redirectModeCustom:
|
||||||
return r.rewriteCustom(headerValue, ctx)
|
return r.rewriteCustom(headerValue, ctx)
|
||||||
|
|
||||||
case "default", "":
|
case redirectModeDefault, "":
|
||||||
return r.rewriteDefault(headerValue, ctx, targetURL, originalClientHost)
|
return r.rewriteDefault(headerValue, ctx, targetURL, originalClientHost)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -154,6 +161,7 @@ func (r *RedirectRewriter) rewriteURL(headerValue string, ctx *fasthttp.RequestC
|
|||||||
// 使用前缀匹配:如果 headerValue 以 targetURL 开头,替换为 replacement + 原路径后缀
|
// 使用前缀匹配:如果 headerValue 以 targetURL 开头,替换为 replacement + 原路径后缀
|
||||||
// replacement 使用 originalClientHost 构建:"$scheme://originalClientHost/"
|
// replacement 使用 originalClientHost 构建:"$scheme://originalClientHost/"
|
||||||
// 例如:targetURL="http://backend:8000", headerValue="http://backend:8000/api/v2/users"
|
// 例如:targetURL="http://backend:8000", headerValue="http://backend:8000/api/v2/users"
|
||||||
|
//
|
||||||
// → 替换为 "$scheme://originalClientHost/api/v2/users"
|
// → 替换为 "$scheme://originalClientHost/api/v2/users"
|
||||||
func (r *RedirectRewriter) rewriteDefault(headerValue string, ctx *fasthttp.RequestCtx, targetURL string, originalClientHost string) string {
|
func (r *RedirectRewriter) rewriteDefault(headerValue string, ctx *fasthttp.RequestCtx, targetURL string, originalClientHost string) string {
|
||||||
if targetURL == "" {
|
if targetURL == "" {
|
||||||
@ -167,9 +175,9 @@ func (r *RedirectRewriter) rewriteDefault(headerValue string, ctx *fasthttp.Requ
|
|||||||
// 检查剩余部分是否以合法分隔符开头
|
// 检查剩余部分是否以合法分隔符开头
|
||||||
if len(remaining) == 0 || remaining[0] == '/' || remaining[0] == '?' || remaining[0] == '#' {
|
if len(remaining) == 0 || remaining[0] == '/' || remaining[0] == '?' || remaining[0] == '#' {
|
||||||
// 使用客户端原始 host 构建 replacement
|
// 使用客户端原始 host 构建 replacement
|
||||||
scheme := "http"
|
scheme := protoHTTP
|
||||||
if ctx.IsTLS() {
|
if ctx.IsTLS() {
|
||||||
scheme = "https"
|
scheme = protoHTTPS
|
||||||
}
|
}
|
||||||
replacement := scheme + "://" + originalClientHost
|
replacement := scheme + "://" + originalClientHost
|
||||||
return replacement + remaining
|
return replacement + remaining
|
||||||
|
|||||||
@ -3,10 +3,9 @@ package proxy
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/valyala/fasthttp"
|
||||||
"rua.plus/lolly/internal/config"
|
"rua.plus/lolly/internal/config"
|
||||||
"rua.plus/lolly/internal/testutil"
|
"rua.plus/lolly/internal/testutil"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestRedirectRewrite_ExactMatch 测试精确匹配改写
|
// TestRedirectRewrite_ExactMatch 测试精确匹配改写
|
||||||
|
|||||||
@ -438,6 +438,9 @@ func (s *Server) Start() error {
|
|||||||
return s.startVHostMode()
|
return s.startVHostMode()
|
||||||
case config.ServerModeMultiServer:
|
case config.ServerModeMultiServer:
|
||||||
return s.startMultiServerMode()
|
return s.startMultiServerMode()
|
||||||
|
case config.ServerModeAuto:
|
||||||
|
// auto 模式下 GetMode() 会自动推断,此处为防御性处理
|
||||||
|
return s.startSingleMode()
|
||||||
default:
|
default:
|
||||||
// 默认使用单服务器模式
|
// 默认使用单服务器模式
|
||||||
return s.startSingleMode()
|
return s.startSingleMode()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user