mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 01:15: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"
|
||||
|
||||
"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
|
||||
|
12
main.go
12
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 {
|
||||
|
@ -12,6 +12,7 @@ type Forwarder struct {
|
||||
failures int
|
||||
priority int
|
||||
weight int
|
||||
latency int
|
||||
}
|
||||
|
||||
// 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/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)
|
||||
|
@ -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 {
|
Loading…
Reference in New Issue
Block a user