mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2025-02-23 03:03:14 +08:00
feat: add proxy name replacement functionality for override (#1481)
* feat: add proxy name replacement functionality for override * style: modify `override schema` info and provider parse error message --------- Co-authored-by: chun <pujichun@outlook.com>
This commit is contained in:
parent
58c973ee2b
commit
3676d1b79f
@ -26,6 +26,13 @@ type healthCheckSchema struct {
|
|||||||
ExpectedStatus string `provider:"expected-status,omitempty"`
|
ExpectedStatus string `provider:"expected-status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type OverrideProxyNameSchema struct {
|
||||||
|
// matching expression for regex replacement
|
||||||
|
Pattern string `provider:"pattern"`
|
||||||
|
// the new content after regex matching
|
||||||
|
Target string `provider:"target"`
|
||||||
|
}
|
||||||
|
|
||||||
type OverrideSchema struct {
|
type OverrideSchema struct {
|
||||||
TFO *bool `provider:"tfo,omitempty"`
|
TFO *bool `provider:"tfo,omitempty"`
|
||||||
MPTcp *bool `provider:"mptcp,omitempty"`
|
MPTcp *bool `provider:"mptcp,omitempty"`
|
||||||
@ -40,6 +47,8 @@ type OverrideSchema struct {
|
|||||||
IPVersion *string `provider:"ip-version,omitempty"`
|
IPVersion *string `provider:"ip-version,omitempty"`
|
||||||
AdditionalPrefix *string `provider:"additional-prefix,omitempty"`
|
AdditionalPrefix *string `provider:"additional-prefix,omitempty"`
|
||||||
AdditionalSuffix *string `provider:"additional-suffix,omitempty"`
|
AdditionalSuffix *string `provider:"additional-suffix,omitempty"`
|
||||||
|
|
||||||
|
ProxyName []OverrideProxyNameSchema `provider:"proxy-name,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type proxyProviderSchema struct {
|
type proxyProviderSchema struct {
|
||||||
|
@ -403,6 +403,24 @@ func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray
|
|||||||
case "additional-suffix":
|
case "additional-suffix":
|
||||||
name := mapping["name"].(string)
|
name := mapping["name"].(string)
|
||||||
mapping["name"] = name + *field.Interface().(*string)
|
mapping["name"] = name + *field.Interface().(*string)
|
||||||
|
case "proxy-name":
|
||||||
|
exprList, ok := field.Interface().([]OverrideProxyNameSchema)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("can't parse proxy-provider override proxy-name, please see the docs example config")
|
||||||
|
}
|
||||||
|
// Iterate through all naming replacement rules and perform the replacements.
|
||||||
|
for _, expr := range exprList {
|
||||||
|
name := mapping["name"].(string)
|
||||||
|
nameReg, err := regexp2.Compile(expr.Pattern, regexp2.None)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("parse proxy name regular expression %q error: %w", expr.Pattern, err)
|
||||||
|
}
|
||||||
|
newName, err := nameReg.Replace(name, expr.Target, 0, -1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("proxy name replace error: %w", err)
|
||||||
|
}
|
||||||
|
mapping["name"] = newName
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
mapping[fieldName] = field.Elem().Interface()
|
mapping[fieldName] = field.Elem().Interface()
|
||||||
}
|
}
|
||||||
|
@ -929,6 +929,13 @@ proxy-providers:
|
|||||||
# ip-version: ipv4-prefer
|
# ip-version: ipv4-prefer
|
||||||
# additional-prefix: "[provider1]"
|
# additional-prefix: "[provider1]"
|
||||||
# additional-suffix: "test"
|
# additional-suffix: "test"
|
||||||
|
# # 名字替换,支持正则表达式
|
||||||
|
# proxy-name:
|
||||||
|
# - pattern: "test"
|
||||||
|
# target: "TEST"
|
||||||
|
# - pattern: "IPLC-(.*?)倍"
|
||||||
|
# target: "iplc x $1"
|
||||||
|
|
||||||
test:
|
test:
|
||||||
type: file
|
type: file
|
||||||
path: /test.yaml
|
path: /test.yaml
|
||||||
|
Loading…
Reference in New Issue
Block a user