From 35ae51daad9e8c79c1acf9aee24985de389643f9 Mon Sep 17 00:00:00 2001 From: chun Date: Sat, 31 Aug 2024 17:23:00 +0800 Subject: [PATCH 1/2] feat: add proxy name replacement functionality for override --- adapter/provider/parser.go | 9 +++++++++ adapter/provider/provider.go | 18 ++++++++++++++++++ docs/config.yaml | 7 +++++++ 3 files changed, 34 insertions(+) diff --git a/adapter/provider/parser.go b/adapter/provider/parser.go index edb6b9110a..4cf53f8766 100644 --- a/adapter/provider/parser.go +++ b/adapter/provider/parser.go @@ -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 { diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index a99c1d9680..8f934f1617 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -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() } diff --git a/docs/config.yaml b/docs/config.yaml index 1da37841cb..da3e8dd640 100644 --- a/docs/config.yaml +++ b/docs/config.yaml @@ -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 From 061ac5efde1ccad76e8a1a24232e6e126ad4fab9 Mon Sep 17 00:00:00 2001 From: chun Date: Sat, 14 Sep 2024 17:24:45 +0800 Subject: [PATCH 2/2] style: modify `override schema` info and provider parse error message --- adapter/provider/parser.go | 6 +++--- adapter/provider/provider.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/adapter/provider/parser.go b/adapter/provider/parser.go index 4cf53f8766..e15e9f29a9 100644 --- a/adapter/provider/parser.go +++ b/adapter/provider/parser.go @@ -29,9 +29,9 @@ type healthCheckSchema struct { type OverrideProxyNameSchema struct { // matching expression for regex replacement - Pattern string `provider:"pattern,omitempty"` + Pattern string `provider:"pattern"` // the new content after regex matching - Target string `provider:"target,omitempty"` + Target string `provider:"target"` } type OverrideSchema struct { @@ -49,7 +49,7 @@ type OverrideSchema struct { AdditionalPrefix *string `provider:"additional-prefix,omitempty"` AdditionalSuffix *string `provider:"additional-suffix,omitempty"` - ProxyName []*OverrideProxyNameSchema `provider:"proxy-name,omitempty"` + ProxyName []OverrideProxyNameSchema `provider:"proxy-name,omitempty"` } type proxyProviderSchema struct { diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 8f934f1617..6f89552250 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -400,9 +400,9 @@ func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray name := mapping["name"].(string) mapping["name"] = name + *field.Interface().(*string) case "proxy-name": - exprList, ok := field.Interface().([]*OverrideProxyNameSchema) + exprList, ok := field.Interface().([]OverrideProxyNameSchema) if !ok { - return nil, errors.New("file must have a `proxy-name` field") + 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 { @@ -413,7 +413,7 @@ func proxiesParseAndFilter(filter string, excludeFilter string, excludeTypeArray } newName, err := nameReg.Replace(name, expr.Target, 0, -1) if err != nil { - return nil, errors.New("proxy name replace error") + return nil, fmt.Errorf("proxy name replace error: %w", err) } mapping["name"] = newName }