mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
优化规则选择逻辑,优先检查扩展名规则,简化302跳转处理,确保在无匹配规则时不强制使用扩展名规则,提升代码可读性和逻辑清晰度。
This commit is contained in:
parent
5790b41a03
commit
ef03d71375
@ -107,34 +107,9 @@ func (rs *RuleService) SelectRuleForRedirect(client *http.Client, pathConfig con
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果默认目标配置了302跳转,优先使用
|
// 优先检查扩展名规则,即使根级别配置了302跳转
|
||||||
if pathConfig.RedirectMode {
|
|
||||||
result.Found = true
|
|
||||||
result.ShouldRedirect = true
|
|
||||||
result.TargetURL = pathConfig.DefaultTarget
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查扩展名规则
|
|
||||||
if len(pathConfig.ExtRules) > 0 {
|
if len(pathConfig.ExtRules) > 0 {
|
||||||
ext := extractExtension(path)
|
// 尝试选择最佳规则(包括文件大小检测)
|
||||||
|
|
||||||
var matcher *utils.ExtensionMatcher
|
|
||||||
|
|
||||||
// 尝试使用缓存管理器
|
|
||||||
if rs.cacheManager != nil {
|
|
||||||
pathKey := fmt.Sprintf("redirect_%p", &pathConfig)
|
|
||||||
matcher = rs.cacheManager.GetExtensionMatcher(pathKey, pathConfig.ExtRules)
|
|
||||||
} else {
|
|
||||||
matcher = utils.NewExtensionMatcher(pathConfig.ExtRules)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 快速检查:如果没有任何302跳转规则,跳过复杂逻辑
|
|
||||||
if !matcher.HasRedirectRule() {
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// 尝试选择最佳规则
|
|
||||||
if rule, found, usedAlt := rs.SelectBestRule(client, pathConfig, path); found && rule != nil && rule.RedirectMode {
|
if rule, found, usedAlt := rs.SelectBestRule(client, pathConfig, path); found && rule != nil && rule.RedirectMode {
|
||||||
result.Rule = rule
|
result.Rule = rule
|
||||||
result.Found = found
|
result.Found = found
|
||||||
@ -144,26 +119,22 @@ func (rs *RuleService) SelectRuleForRedirect(client *http.Client, pathConfig con
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// 回退到简单的扩展名匹配
|
// 注意:这里不再进行"忽略大小"的回退匹配
|
||||||
if rule, found := pathConfig.GetProcessedExtRule(ext); found && rule.RedirectMode {
|
// 如果SelectBestRule没有找到合适的规则,说明:
|
||||||
result.Rule = rule
|
// 1. 扩展名不匹配,或者
|
||||||
result.Found = found
|
// 2. 扩展名匹配但文件大小不在配置范围内,或者
|
||||||
result.UsedAltTarget = true
|
// 3. 无法获取文件大小,或者
|
||||||
result.ShouldRedirect = true
|
// 4. 目标服务器不可访问
|
||||||
result.TargetURL = rule.Target
|
// 在这些情况下,我们不应该强制使用扩展名规则
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查通配符规则
|
// 如果没有匹配的扩展名规则,且默认目标配置了302跳转,使用默认目标
|
||||||
if rule, found := pathConfig.GetProcessedExtRule("*"); found && rule.RedirectMode {
|
if pathConfig.RedirectMode {
|
||||||
result.Rule = rule
|
result.Found = true
|
||||||
result.Found = found
|
|
||||||
result.UsedAltTarget = true
|
|
||||||
result.ShouldRedirect = true
|
result.ShouldRedirect = true
|
||||||
result.TargetURL = rule.Target
|
result.TargetURL = pathConfig.DefaultTarget
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user