From 4b6bc056bc1695f224122a4fdb5f8bc44f54d073 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 20 Apr 2026 16:04:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(config):=20=E6=B7=BB=E5=8A=A0=20location?= =?UTF-8?q?=5Ftype=20=E9=85=8D=E7=BD=AE=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 验证 location_type 枚举值有效性 - regex/regex_caseless 类型时验证 path 为有效正则 - named 类型时验证 location_name 必填 Co-Authored-By: Claude Opus 4.7 --- internal/config/validate.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/internal/config/validate.go b/internal/config/validate.go index 70c6769..06520d9 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -494,6 +494,33 @@ func validateProxy(p *ProxyConfig) error { return fmt.Errorf("redirect_rewrite: %w", err) } + // 验证 location_type 和 path 组合 + validLocationTypes := []string{"", "exact", "prefix_priority", "regex", "regex_caseless", "prefix", "named"} + if p.LocationType != "" { + if err := ValidateEnum(p.LocationType, validLocationTypes, "location_type"); err != nil { + return fmt.Errorf("无效的 location_type: %s", p.LocationType) + } + } + + // 当 location_type 为 regex 类型时,验证 path 是否是有效正则 + if p.LocationType == "regex" || p.LocationType == "regex_caseless" { + // Path 必填且必须能编译为有效正则 + if p.Path == "" { + return errors.New("location_type 为 regex/regex_caseless 时,path 必填") + } + // 使用 regexp.Compile 验证正则语法有效性 + if _, err := regexp.Compile(p.Path); err != nil { + return fmt.Errorf("location_type 为 '%s' 时,path '%s' 不是有效正则: %v", p.LocationType, p.Path, err) + } + } + + // 当 location_type 为 named 时,验证 location_name 必填 + if p.LocationType == "named" { + if p.LocationName == "" { + return errors.New("location_type 为 named 时,location_name 必填") + } + } + return nil }