Skip to content

Commit

Permalink
Merge pull request #321 from ag2s20150909/Alpha
Browse files Browse the repository at this point in the history
proxy-provider and proxy-groups support exclude node by node type
  • Loading branch information
stitchrs committed Jan 6, 2023
2 parents 5fa6777 + 908d0b0 commit cd7134e
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 6 deletions.
1 change: 1 addition & 0 deletions adapter/outboundgroup/fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func NewFallback(option *GroupCommonOption, providers []provider.ProxyProvider)
},
option.Filter,
option.ExcludeFilter,
option.ExcludeType,
providers,
}),
disableUDP: option.DisableUDP,
Expand Down
25 changes: 25 additions & 0 deletions adapter/outboundgroup/groupbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type GroupBase struct {
*outbound.Base
filterRegs []*regexp2.Regexp
excludeFilterReg *regexp2.Regexp
excludeTypeArray []string
providers []provider.ProxyProvider
failedTestMux sync.Mutex
failedTimes int
Expand All @@ -33,6 +34,7 @@ type GroupBaseOption struct {
outbound.BaseOption
filter string
excludeFilter string
excludeType string
providers []provider.ProxyProvider
}

Expand All @@ -41,6 +43,10 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
if opt.excludeFilter != "" {
excludeFilterReg = regexp2.MustCompile(opt.excludeFilter, 0)
}
var excludeTypeArray []string
if opt.excludeType!="" {
excludeTypeArray=strings.Split(opt.excludeType,"|")
}

var filterRegs []*regexp2.Regexp
if opt.filter != "" {
Expand All @@ -54,6 +60,7 @@ func NewGroupBase(opt GroupBaseOption) *GroupBase {
Base: outbound.NewBase(opt.BaseOption),
filterRegs: filterRegs,
excludeFilterReg: excludeFilterReg,
excludeTypeArray: excludeTypeArray,
providers: opt.providers,
failedTesting: atomic.NewBool(false),
}
Expand Down Expand Up @@ -148,6 +155,24 @@ func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
}
proxies = newProxies
}
if gb.excludeTypeArray !=nil{
var newProxies []C.Proxy
for _, p := range proxies {
mType := p.Type().String()
flag:=false
for i := range gb.excludeTypeArray {
if(strings.EqualFold(mType,gb.excludeTypeArray[i])){
flag=true
}

}
if(flag){
continue
}
newProxies = append(newProxies, p)
}
proxies = newProxies
}

if gb.excludeFilterReg != nil {
var newProxies []C.Proxy
Expand Down
1 change: 1 addition & 0 deletions adapter/outboundgroup/loadbalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func NewLoadBalance(option *GroupCommonOption, providers []provider.ProxyProvide
},
option.Filter,
option.ExcludeFilter,
option.ExcludeType,
providers,
}),
strategyFn: strategyFn,
Expand Down
1 change: 1 addition & 0 deletions adapter/outboundgroup/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type GroupCommonOption struct {
DisableUDP bool `group:"disable-udp,omitempty"`
Filter string `group:"filter,omitempty"`
ExcludeFilter string `group:"exclude-filter,omitempty"`
ExcludeType string `group:"exclude-type,omitempty"`
}

func ParseProxyGroup(config map[string]any, proxyMap map[string]C.Proxy, providersMap map[string]types.ProxyProvider) (C.ProxyAdapter, error) {
Expand Down
1 change: 1 addition & 0 deletions adapter/outboundgroup/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func NewRelay(option *GroupCommonOption, providers []provider.ProxyProvider) *Re
},
"",
"",
"",
providers,
}),
}
Expand Down
1 change: 1 addition & 0 deletions adapter/outboundgroup/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func NewSelector(option *GroupCommonOption, providers []provider.ProxyProvider)
},
option.Filter,
option.ExcludeFilter,
option.ExcludeType,
providers,
}),
selected: "COMPATIBLE",
Expand Down
1 change: 1 addition & 0 deletions adapter/outboundgroup/urltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func NewURLTest(option *GroupCommonOption, providers []provider.ProxyProvider, o

option.Filter,
option.ExcludeFilter,
option.ExcludeType,
providers,
}),
fastSingle: singledo.NewSingle[C.Proxy](time.Second * 10),
Expand Down
5 changes: 4 additions & 1 deletion adapter/provider/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type proxyProviderSchema struct {
Interval int `provider:"interval,omitempty"`
Filter string `provider:"filter,omitempty"`
ExcludeFilter string `provider:"exclude-filter,omitempty"`
ExcludeType string `provider:"exclude-type,omitempty"`
HealthCheck healthCheckSchema `provider:"health-check,omitempty"`
}

Expand Down Expand Up @@ -63,5 +64,7 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
interval := time.Duration(uint(schema.Interval)) * time.Second
filter := schema.Filter
excludeFilter := schema.ExcludeFilter
return NewProxySetProvider(name, interval, filter, excludeFilter, vehicle, hc)
excludeType:=schema.ExcludeType

return NewProxySetProvider(name, interval, filter, excludeFilter,excludeType,vehicle, hc)
}
32 changes: 29 additions & 3 deletions adapter/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,16 @@ func stopProxyProvider(pd *ProxySetProvider) {
_ = pd.Fetcher.Destroy()
}

func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
func NewProxySetProvider(name string, interval time.Duration, filter string, excludeFilter string,excludeType string, vehicle types.Vehicle, hc *HealthCheck) (*ProxySetProvider, error) {
excludeFilterReg, err := regexp2.Compile(excludeFilter, 0)
if err != nil {
return nil, fmt.Errorf("invalid excludeFilter regex: %w", err)
}
var excludeTypeArray []string
if excludeType !=""{
excludeTypeArray=strings.Split(excludeType,"|")
}

var filterRegs []*regexp2.Regexp
for _, filter := range strings.Split(filter, "`") {
filterReg, err := regexp2.Compile(filter, 0)
Expand All @@ -164,7 +169,7 @@ func NewProxySetProvider(name string, interval time.Duration, filter string, exc
healthCheck: hc,
}

fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, filterRegs, excludeFilterReg), proxiesOnUpdate(pd))
fetcher := resource.NewFetcher[[]C.Proxy](name, interval, vehicle, proxiesParseAndFilter(filter, excludeFilter, excludeTypeArray,filterRegs, excludeFilterReg), proxiesOnUpdate(pd))
pd.Fetcher = fetcher

pd.getSubscriptionInfo()
Expand Down Expand Up @@ -262,7 +267,7 @@ func proxiesOnUpdate(pd *proxySetProvider) func([]C.Proxy) {
}
}

func proxiesParseAndFilter(filter string, excludeFilter string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp) resource.Parser[[]C.Proxy] {
func proxiesParseAndFilter(filter string, excludeFilter string,excludeTypeArray []string, filterRegs []*regexp2.Regexp, excludeFilterReg *regexp2.Regexp) resource.Parser[[]C.Proxy] {
return func(buf []byte) ([]C.Proxy, error) {
schema := &ProxySchema{}

Expand All @@ -282,6 +287,27 @@ func proxiesParseAndFilter(filter string, excludeFilter string, filterRegs []*re
proxiesSet := map[string]struct{}{}
for _, filterReg := range filterRegs {
for idx, mapping := range schema.Proxies {
if nil !=excludeTypeArray && len(excludeTypeArray)>0{
mType,ok:=mapping["type"]
if !ok {
continue
}
pType,ok:=mType.(string)
if !ok {
continue
}
flag:=false
for i := range excludeTypeArray {
if(strings.EqualFold(pType,excludeTypeArray[i])){
flag=true
}

}
if(flag){
continue
}

}
mName, ok := mapping["name"]
if !ok {
continue
Expand Down
7 changes: 5 additions & 2 deletions common/convert/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,11 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
if jsonDc.Decode(&values) != nil {
continue
}

name := uniqueName(names, values["ps"].(string))
tempName,ok:=values["ps"].(string)
if !ok{
continue
}
name := uniqueName(names, tempName)
vmess := make(map[string]any, 20)

vmess["name"] = name
Expand Down

0 comments on commit cd7134e

Please sign in to comment.