feat(config): 添加 location_type 配置验证

- 验证 location_type 枚举值有效性
- regex/regex_caseless 类型时验证 path 为有效正则
- named 类型时验证 location_name 必填

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-20 16:04:26 +08:00
parent 82658359cb
commit 4b6bc056bc

View File

@ -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
}