mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 09:25:41 +08:00
strategy: moved to a separate package
This commit is contained in:
parent
0da05ecedd
commit
e3888a6bd3
10
conf.go
10
conf.go
@ -9,6 +9,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/nadoo/conflag"
|
"github.com/nadoo/conflag"
|
||||||
|
|
||||||
|
"github.com/nadoo/glider/strategy"
|
||||||
)
|
)
|
||||||
|
|
||||||
var flag = conflag.New()
|
var flag = conflag.New()
|
||||||
@ -18,8 +20,8 @@ var conf struct {
|
|||||||
|
|
||||||
Listen []string
|
Listen []string
|
||||||
|
|
||||||
Forward []string
|
Forward []string
|
||||||
StrategyConfig
|
StrategyConfig strategy.Config
|
||||||
|
|
||||||
RuleFile []string
|
RuleFile []string
|
||||||
RulesDir string
|
RulesDir string
|
||||||
@ -122,8 +124,8 @@ func listDir(dirPth string, suffix string) (files []string, err error) {
|
|||||||
type RuleConf struct {
|
type RuleConf struct {
|
||||||
name string
|
name string
|
||||||
|
|
||||||
Forward []string
|
Forward []string
|
||||||
StrategyConfig
|
StrategyConfig strategy.Config
|
||||||
|
|
||||||
DNSServer []string
|
DNSServer []string
|
||||||
IPSet string
|
IPSet string
|
||||||
|
12
main.go
12
main.go
@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/nadoo/glider/common/log"
|
"github.com/nadoo/glider/common/log"
|
||||||
"github.com/nadoo/glider/dns"
|
"github.com/nadoo/glider/dns"
|
||||||
"github.com/nadoo/glider/proxy"
|
"github.com/nadoo/glider/proxy"
|
||||||
|
"github.com/nadoo/glider/strategy"
|
||||||
|
|
||||||
_ "github.com/nadoo/glider/proxy/http"
|
_ "github.com/nadoo/glider/proxy/http"
|
||||||
_ "github.com/nadoo/glider/proxy/mixed"
|
_ "github.com/nadoo/glider/proxy/mixed"
|
||||||
@ -27,21 +28,14 @@ import (
|
|||||||
const VERSION = "0.6.7"
|
const VERSION = "0.6.7"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Config
|
|
||||||
confInit()
|
confInit()
|
||||||
|
|
||||||
// Log
|
|
||||||
log.F = func(f string, v ...interface{}) {
|
log.F = func(f string, v ...interface{}) {
|
||||||
if conf.Verbose {
|
if conf.Verbose {
|
||||||
stdlog.Printf(f, v...)
|
stdlog.Printf(f, v...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forwarder
|
dialer := NewRuleDialer(conf.rules, strategy.NewDialer(conf.Forward, &conf.StrategyConfig))
|
||||||
dialer := NewRuleDialer(conf.rules, StrategyDialer(conf.Forward, &conf.StrategyConfig))
|
|
||||||
|
|
||||||
// IPSet manager
|
|
||||||
ipsetM, _ := NewIPSetManager(conf.IPSet, conf.rules)
|
ipsetM, _ := NewIPSetManager(conf.IPSet, conf.rules)
|
||||||
|
|
||||||
// DNS Server
|
// DNS Server
|
||||||
@ -79,7 +73,7 @@ func main() {
|
|||||||
go d.ListenAndServe()
|
go d.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Servers
|
// Proxy Servers
|
||||||
for _, listen := range conf.Listen {
|
for _, listen := range conf.Listen {
|
||||||
local, err := proxy.ServerFromURL(listen, proxy.NewForwarder(dialer))
|
local, err := proxy.ServerFromURL(listen, proxy.NewForwarder(dialer))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -12,6 +12,7 @@ type Forwarder struct {
|
|||||||
failures int
|
failures int
|
||||||
priority int
|
priority int
|
||||||
weight int
|
weight int
|
||||||
|
latency int
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForwarderFromURL returns a new forwarder
|
// ForwarderFromURL returns a new forwarder
|
||||||
|
3
rule.go
3
rule.go
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/nadoo/glider/common/log"
|
"github.com/nadoo/glider/common/log"
|
||||||
"github.com/nadoo/glider/proxy"
|
"github.com/nadoo/glider/proxy"
|
||||||
|
"github.com/nadoo/glider/strategy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RuleDialer struct
|
// RuleDialer struct
|
||||||
@ -23,7 +24,7 @@ func NewRuleDialer(rules []*RuleConf, gDialer proxy.Dialer) *RuleDialer {
|
|||||||
rd := &RuleDialer{gDialer: gDialer}
|
rd := &RuleDialer{gDialer: gDialer}
|
||||||
|
|
||||||
for _, r := range rules {
|
for _, r := range rules {
|
||||||
sDialer := StrategyDialer(r.Forward, &r.StrategyConfig)
|
sDialer := strategy.NewDialer(r.Forward, &r.StrategyConfig)
|
||||||
|
|
||||||
for _, domain := range r.Domain {
|
for _, domain := range r.Domain {
|
||||||
rd.domainMap.Store(strings.ToLower(domain), sDialer)
|
rd.domainMap.Store(strings.ToLower(domain), sDialer)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package strategy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -12,15 +12,15 @@ import (
|
|||||||
"github.com/nadoo/glider/proxy"
|
"github.com/nadoo/glider/proxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StrategyConfig .
|
// Config of strategy
|
||||||
type StrategyConfig struct {
|
type Config struct {
|
||||||
Strategy string
|
Strategy string
|
||||||
CheckWebSite string
|
CheckWebSite string
|
||||||
CheckInterval int
|
CheckInterval int
|
||||||
}
|
}
|
||||||
|
|
||||||
// StrategyDialer .
|
// NewDialer returns a new strategy dialer
|
||||||
func StrategyDialer(s []string, c *StrategyConfig) proxy.Dialer {
|
func NewDialer(s []string, c *Config) proxy.Dialer {
|
||||||
// global forwarders in xx.conf
|
// global forwarders in xx.conf
|
||||||
var fwdrs []*proxy.Forwarder
|
var fwdrs []*proxy.Forwarder
|
||||||
for _, chain := range s {
|
for _, chain := range s {
|
Loading…
Reference in New Issue
Block a user