From e3888a6bd3287d142d05c63ea71b2b56ce030d15 Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Sat, 11 Aug 2018 11:46:10 +0800 Subject: [PATCH] strategy: moved to a separate package --- conf.go | 10 ++++++---- main.go | 12 +++--------- proxy/forwarder.go | 1 + rule.go | 3 ++- strategy.go => strategy/strategy.go | 10 +++++----- 5 files changed, 17 insertions(+), 19 deletions(-) rename strategy.go => strategy/strategy.go (96%) diff --git a/conf.go b/conf.go index 7042228..13abcc5 100644 --- a/conf.go +++ b/conf.go @@ -9,6 +9,8 @@ import ( "strings" "github.com/nadoo/conflag" + + "github.com/nadoo/glider/strategy" ) var flag = conflag.New() @@ -18,8 +20,8 @@ var conf struct { Listen []string - Forward []string - StrategyConfig + Forward []string + StrategyConfig strategy.Config RuleFile []string RulesDir string @@ -122,8 +124,8 @@ func listDir(dirPth string, suffix string) (files []string, err error) { type RuleConf struct { name string - Forward []string - StrategyConfig + Forward []string + StrategyConfig strategy.Config DNSServer []string IPSet string diff --git a/main.go b/main.go index 48a3a05..528ec37 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/nadoo/glider/common/log" "github.com/nadoo/glider/dns" "github.com/nadoo/glider/proxy" + "github.com/nadoo/glider/strategy" _ "github.com/nadoo/glider/proxy/http" _ "github.com/nadoo/glider/proxy/mixed" @@ -27,21 +28,14 @@ import ( const VERSION = "0.6.7" func main() { - - // Config confInit() - - // Log log.F = func(f string, v ...interface{}) { if conf.Verbose { stdlog.Printf(f, v...) } } - // Forwarder - dialer := NewRuleDialer(conf.rules, StrategyDialer(conf.Forward, &conf.StrategyConfig)) - - // IPSet manager + dialer := NewRuleDialer(conf.rules, strategy.NewDialer(conf.Forward, &conf.StrategyConfig)) ipsetM, _ := NewIPSetManager(conf.IPSet, conf.rules) // DNS Server @@ -79,7 +73,7 @@ func main() { go d.ListenAndServe() } - // Servers + // Proxy Servers for _, listen := range conf.Listen { local, err := proxy.ServerFromURL(listen, proxy.NewForwarder(dialer)) if err != nil { diff --git a/proxy/forwarder.go b/proxy/forwarder.go index f25f5e6..0f640b7 100644 --- a/proxy/forwarder.go +++ b/proxy/forwarder.go @@ -12,6 +12,7 @@ type Forwarder struct { failures int priority int weight int + latency int } // ForwarderFromURL returns a new forwarder diff --git a/rule.go b/rule.go index 4ae1150..a6b990e 100644 --- a/rule.go +++ b/rule.go @@ -7,6 +7,7 @@ import ( "github.com/nadoo/glider/common/log" "github.com/nadoo/glider/proxy" + "github.com/nadoo/glider/strategy" ) // RuleDialer struct @@ -23,7 +24,7 @@ func NewRuleDialer(rules []*RuleConf, gDialer proxy.Dialer) *RuleDialer { rd := &RuleDialer{gDialer: gDialer} for _, r := range rules { - sDialer := StrategyDialer(r.Forward, &r.StrategyConfig) + sDialer := strategy.NewDialer(r.Forward, &r.StrategyConfig) for _, domain := range r.Domain { rd.domainMap.Store(strings.ToLower(domain), sDialer) diff --git a/strategy.go b/strategy/strategy.go similarity index 96% rename from strategy.go rename to strategy/strategy.go index b9670a1..34608d7 100644 --- a/strategy.go +++ b/strategy/strategy.go @@ -1,4 +1,4 @@ -package main +package strategy import ( "bytes" @@ -12,15 +12,15 @@ import ( "github.com/nadoo/glider/proxy" ) -// StrategyConfig . -type StrategyConfig struct { +// Config of strategy +type Config struct { Strategy string CheckWebSite string CheckInterval int } -// StrategyDialer . -func StrategyDialer(s []string, c *StrategyConfig) proxy.Dialer { +// NewDialer returns a new strategy dialer +func NewDialer(s []string, c *Config) proxy.Dialer { // global forwarders in xx.conf var fwdrs []*proxy.Forwarder for _, chain := range s {