2022-04-28 19:01:13 +08:00
|
|
|
package outboundgroup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Dreamacro/clash/adapter/outbound"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/constant/provider"
|
|
|
|
types "github.com/Dreamacro/clash/constant/provider"
|
|
|
|
"github.com/Dreamacro/clash/tunnel"
|
|
|
|
"github.com/dlclark/regexp2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GroupBase struct {
|
|
|
|
*outbound.Base
|
|
|
|
filter *regexp2.Regexp
|
|
|
|
providers []provider.ProxyProvider
|
2022-04-28 23:06:35 +08:00
|
|
|
versions map[string]uint
|
2022-04-28 19:01:13 +08:00
|
|
|
proxies map[string][]C.Proxy
|
|
|
|
}
|
|
|
|
|
|
|
|
type GroupBaseOption struct {
|
|
|
|
outbound.BaseOption
|
|
|
|
filter string
|
|
|
|
providers []provider.ProxyProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGroupBase(opt GroupBaseOption) *GroupBase {
|
|
|
|
var filter *regexp2.Regexp = nil
|
|
|
|
if opt.filter != "" {
|
|
|
|
filter = regexp2.MustCompile(opt.filter, 0)
|
|
|
|
}
|
|
|
|
return &GroupBase{
|
|
|
|
Base: outbound.NewBase(opt.BaseOption),
|
|
|
|
filter: filter,
|
|
|
|
providers: opt.providers,
|
2022-04-28 23:06:35 +08:00
|
|
|
versions: map[string]uint{},
|
2022-04-28 19:01:13 +08:00
|
|
|
proxies: map[string][]C.Proxy{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gb *GroupBase) GetProxies(touch bool) []C.Proxy {
|
|
|
|
if gb.filter == nil {
|
|
|
|
var proxies []C.Proxy
|
|
|
|
for _, pd := range gb.providers {
|
|
|
|
if touch {
|
|
|
|
proxies = append(proxies, pd.ProxiesWithTouch()...)
|
|
|
|
} else {
|
|
|
|
proxies = append(proxies, pd.Proxies()...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(proxies) == 0 {
|
|
|
|
return append(proxies, tunnel.Proxies()["COMPATIBLE"])
|
|
|
|
}
|
|
|
|
return proxies
|
|
|
|
}
|
2022-04-28 23:06:35 +08:00
|
|
|
//TODO("Touch Version 没变的")
|
2022-04-28 19:01:13 +08:00
|
|
|
for _, pd := range gb.providers {
|
2022-04-28 23:06:35 +08:00
|
|
|
if pd.VehicleType() == types.Compatible {
|
2022-04-28 19:01:13 +08:00
|
|
|
if touch {
|
|
|
|
gb.proxies[pd.Name()] = pd.ProxiesWithTouch()
|
|
|
|
} else {
|
|
|
|
gb.proxies[pd.Name()] = pd.Proxies()
|
|
|
|
}
|
|
|
|
|
2022-04-28 23:06:35 +08:00
|
|
|
gb.versions[pd.Name()] = pd.Version()
|
2022-04-28 19:01:13 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-28 23:06:35 +08:00
|
|
|
if version, ok := gb.versions[pd.Name()]; !ok || version != pd.Version() {
|
2022-04-28 19:01:13 +08:00
|
|
|
var (
|
|
|
|
proxies []C.Proxy
|
|
|
|
newProxies []C.Proxy
|
|
|
|
)
|
|
|
|
|
|
|
|
if touch {
|
|
|
|
proxies = pd.ProxiesWithTouch()
|
|
|
|
} else {
|
|
|
|
proxies = pd.Proxies()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range proxies {
|
|
|
|
if mat, _ := gb.filter.FindStringMatch(p.Name()); mat != nil {
|
|
|
|
newProxies = append(newProxies, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gb.proxies[pd.Name()] = newProxies
|
2022-04-28 23:06:35 +08:00
|
|
|
gb.versions[pd.Name()] = pd.Version()
|
2022-04-28 19:01:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
var proxies []C.Proxy
|
|
|
|
for _, v := range gb.proxies {
|
|
|
|
proxies = append(proxies, v...)
|
|
|
|
}
|
|
|
|
if len(proxies) == 0 {
|
|
|
|
return append(proxies, tunnel.Proxies()["COMPATIBLE"])
|
|
|
|
}
|
|
|
|
return proxies
|
|
|
|
}
|