add selector

This commit is contained in:
ginuerzh
2022-09-02 15:00:07 +08:00
parent 09dbdbb03c
commit c643014e12
9 changed files with 264 additions and 33 deletions

View File

@ -10,6 +10,7 @@ import (
tls_util "github.com/go-gost/x/internal/util/tls"
"github.com/go-gost/x/metadata"
"github.com/go-gost/x/registry"
xs "github.com/go-gost/x/selector"
)
func ParseChain(cfg *config.ChainConfig) (chain.SelectableChainer, error) {
@ -140,6 +141,9 @@ func ParseChain(cfg *config.ChainConfig) (chain.SelectableChainer, error) {
if s := parseNodeSelector(hop.Selector); s != nil {
sel = s
}
if sel == nil {
sel = xs.DefaultNodeSelector
}
group.WithSelector(sel).
WithBypass(bypass.BypassGroup(bypassList(hop.Bypass, hop.Bypasses...)...))

View File

@ -12,6 +12,7 @@ import (
"github.com/go-gost/core/logger"
"github.com/go-gost/core/recorder"
"github.com/go-gost/core/resolver"
"github.com/go-gost/core/selector"
admission_impl "github.com/go-gost/x/admission"
auth_impl "github.com/go-gost/x/auth"
bypass_impl "github.com/go-gost/x/bypass"
@ -21,6 +22,7 @@ import (
recorder_impl "github.com/go-gost/x/recorder"
"github.com/go-gost/x/registry"
resolver_impl "github.com/go-gost/x/resolver"
xs "github.com/go-gost/x/selector"
)
func ParseAuther(cfg *config.AutherConfig) auth.Authenticator {
@ -83,50 +85,50 @@ func parseAuth(cfg *config.AuthConfig) *url.Userinfo {
return url.UserPassword(cfg.Username, cfg.Password)
}
func parseChainSelector(cfg *config.SelectorConfig) chain.Selector[chain.SelectableChainer] {
func parseChainSelector(cfg *config.SelectorConfig) selector.Selector[chain.SelectableChainer] {
if cfg == nil {
return nil
}
var strategy chain.Strategy[chain.SelectableChainer]
var strategy selector.Strategy[chain.SelectableChainer]
switch cfg.Strategy {
case "round", "rr":
strategy = chain.RoundRobinStrategy[chain.SelectableChainer]()
strategy = xs.RoundRobinStrategy[chain.SelectableChainer]()
case "random", "rand":
strategy = chain.RandomStrategy[chain.SelectableChainer]()
strategy = xs.RandomStrategy[chain.SelectableChainer]()
case "fifo", "ha":
strategy = chain.FIFOStrategy[chain.SelectableChainer]()
strategy = xs.FIFOStrategy[chain.SelectableChainer]()
default:
strategy = chain.RoundRobinStrategy[chain.SelectableChainer]()
strategy = xs.RoundRobinStrategy[chain.SelectableChainer]()
}
return chain.NewSelector(
return xs.NewSelector(
strategy,
chain.FailFilter[chain.SelectableChainer](cfg.MaxFails, cfg.FailTimeout),
chain.BackupFilter[chain.SelectableChainer](),
xs.FailFilter[chain.SelectableChainer](cfg.MaxFails, cfg.FailTimeout),
xs.BackupFilter[chain.SelectableChainer](),
)
}
func parseNodeSelector(cfg *config.SelectorConfig) chain.Selector[*chain.Node] {
func parseNodeSelector(cfg *config.SelectorConfig) selector.Selector[*chain.Node] {
if cfg == nil {
return nil
}
var strategy chain.Strategy[*chain.Node]
var strategy selector.Strategy[*chain.Node]
switch cfg.Strategy {
case "round", "rr":
strategy = chain.RoundRobinStrategy[*chain.Node]()
strategy = xs.RoundRobinStrategy[*chain.Node]()
case "random", "rand":
strategy = chain.RandomStrategy[*chain.Node]()
strategy = xs.RandomStrategy[*chain.Node]()
case "fifo", "ha":
strategy = chain.FIFOStrategy[*chain.Node]()
strategy = xs.FIFOStrategy[*chain.Node]()
default:
strategy = chain.RoundRobinStrategy[*chain.Node]()
strategy = xs.RoundRobinStrategy[*chain.Node]()
}
return chain.NewSelector(
return xs.NewSelector(
strategy,
chain.FailFilter[*chain.Node](cfg.MaxFails, cfg.FailTimeout),
chain.BackupFilter[*chain.Node](),
xs.FailFilter[*chain.Node](cfg.MaxFails, cfg.FailTimeout),
xs.BackupFilter[*chain.Node](),
)
}

View File

@ -11,11 +11,13 @@ import (
"github.com/go-gost/core/listener"
"github.com/go-gost/core/logger"
"github.com/go-gost/core/recorder"
"github.com/go-gost/core/selector"
"github.com/go-gost/core/service"
"github.com/go-gost/x/config"
tls_util "github.com/go-gost/x/internal/util/tls"
"github.com/go-gost/x/metadata"
"github.com/go-gost/x/registry"
xs "github.com/go-gost/x/selector"
)
func ParseService(cfg *config.ServiceConfig) (service.Service, error) {
@ -184,7 +186,11 @@ func parseForwarder(cfg *config.ForwarderConfig) *chain.NodeGroup {
}
}
return group.WithSelector(parseNodeSelector(cfg.Selector))
sel := parseNodeSelector(cfg.Selector)
if sel == nil {
sel = xs.DefaultNodeSelector
}
return group.WithSelector(sel)
}
func bypassList(name string, names ...string) []bypass.Bypass {
@ -228,18 +234,25 @@ func admissionList(name string, names ...string) []admission.Admission {
}
func chainGroup(name string, group *config.ChainGroupConfig) chain.Chainer {
cg := &chain.ChainGroup{}
var chains []chain.SelectableChainer
var sel selector.Selector[chain.SelectableChainer]
if c := registry.ChainRegistry().Get(name); c != nil {
cg.Chains = append(cg.Chains, c)
chains = append(chains, c)
}
if group != nil {
for _, s := range group.Chains {
if c := registry.ChainRegistry().Get(s); c != nil {
cg.Chains = append(cg.Chains, c)
chains = append(chains, c)
}
}
cg.Selector = parseChainSelector(group.Selector)
sel = parseChainSelector(group.Selector)
}
return cg
if sel == nil {
sel = xs.DefaultChainSelector
}
return chain.NewChainGroup(chains...).
WithSelector(sel)
}