mirror of
https://github.com/MetaCubeX/Clash.Meta.git
synced 2025-04-18 08:20:53 +00:00
feat: add proxy name replacement functionality for override
This commit is contained in:
parent
f6164ac195
commit
35ae51daad
3 changed files with 34 additions and 0 deletions
|
@ -27,6 +27,13 @@ type healthCheckSchema struct {
|
|||
ExpectedStatus string `provider:"expected-status,omitempty"`
|
||||
}
|
||||
|
||||
type OverrideProxyNameSchema struct {
|
||||
// matching expression for regex replacement
|
||||
Pattern string `provider:"pattern,omitempty"`
|
||||
// the new content after regex matching
|
||||
Target string `provider:"target,omitempty"`
|
||||
}
|
||||
|
||||
type OverrideSchema struct {
|
||||
TFO *bool `provider:"tfo,omitempty"`
|
||||
MPTcp *bool `provider:"mptcp,omitempty"`
|
||||
|
@ -41,6 +48,8 @@ type OverrideSchema struct {
|
|||
IPVersion *string `provider:"ip-version,omitempty"`
|
||||
AdditionalPrefix *string `provider:"additional-prefix,omitempty"`
|
||||
AdditionalSuffix *string `provider:"additional-suffix,omitempty"`
|
||||
|
||||
ProxyName []*OverrideProxyNameSchema `provider:"proxy-name,omitempty"`
|
||||
}
|
||||
|
||||
type proxyProviderSchema struct {
|
||||
|
|
|
@ -399,6 +399,24 @@ func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray
|
|||
case "additional-suffix":
|
||||
name := mapping["name"].(string)
|
||||
mapping["name"] = name + *field.Interface().(*string)
|
||||
case "proxy-name":
|
||||
exprList, ok := field.Interface().([]*OverrideProxyNameSchema)
|
||||
if !ok {
|
||||
return nil, errors.New("file must have a `proxy-name` field")
|
||||
}
|
||||
// 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, errors.New("proxy name replace error")
|
||||
}
|
||||
mapping["name"] = newName
|
||||
}
|
||||
default:
|
||||
mapping[fieldName] = field.Elem().Interface()
|
||||
}
|
||||
|
|
|
@ -929,6 +929,13 @@ proxy-providers:
|
|||
# ip-version: ipv4-prefer
|
||||
# additional-prefix: "[provider1]"
|
||||
# additional-suffix: "test"
|
||||
# # 名字替换,支持正则表达式
|
||||
# proxy-name:
|
||||
# - pattern: "test"
|
||||
# target: "TEST"
|
||||
# - pattern: "IPLC-(.*?)倍"
|
||||
# target: "iplc x $1"
|
||||
|
||||
test:
|
||||
type: file
|
||||
path: /test.yaml
|
||||
|
|
Loading…
Add table
Reference in a new issue