mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
general: use builtin dns when it enabled. (#184)
This commit is contained in:
1 parent
89114e678b
commit
84b00d6db6
10 files changed
+88
-74
No files matched your search
+1
-3
@@ -7,8 +7,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/nadoo/conflag"
|
||||
|
||||
"github.com/nadoo/glider/strategy"
|
||||
)
|
||||
|
||||
// Config of rule dialer.
|
||||
@@ -16,7 +14,7 @@ type Config struct {
|
||||
Name string
|
||||
|
||||
Forward []string
|
||||
StrategyConfig strategy.Config
|
||||
StrategyConfig StrategyConfig
|
||||
|
||||
DNSServers []string
|
||||
IPSet string
|
||||
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
// StatusHandler function will be called when the forwarder's status changed
|
||||
type StatusHandler func(*Forwarder)
|
||||
|
||||
// Forwarder is a forwarder
|
||||
type Forwarder struct {
|
||||
proxy.Dialer
|
||||
addr string
|
||||
priority uint32
|
||||
maxFailures uint32 // maxfailures to set to Disabled
|
||||
disabled uint32
|
||||
failures uint32
|
||||
latency int64
|
||||
intface string // local interface or ip address
|
||||
handlers []StatusHandler
|
||||
}
|
||||
|
||||
// ForwarderFromURL parses `forward=` command value and returns a new forwarder.
|
||||
func ForwarderFromURL(s, intface string, dialTimeout, relayTimeout time.Duration) (f *Forwarder, err error) {
|
||||
f = &Forwarder{}
|
||||
|
||||
ss := strings.Split(s, "#")
|
||||
if len(ss) > 1 {
|
||||
err = f.parseOption(ss[1])
|
||||
}
|
||||
|
||||
iface := intface
|
||||
if f.intface != "" && f.intface != intface {
|
||||
iface = f.intface
|
||||
}
|
||||
|
||||
var d proxy.Dialer
|
||||
d, err = proxy.NewDirect(iface, dialTimeout, relayTimeout)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, url := range strings.Split(ss[0], ",") {
|
||||
d, err = proxy.DialerFromURL(url, d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
f.Dialer = d
|
||||
f.addr = d.Addr()
|
||||
|
||||
// set forwarder to disabled by default
|
||||
f.Disable()
|
||||
|
||||
return f, err
|
||||
}
|
||||
|
||||
// DirectForwarder returns a direct forwarder.
|
||||
func DirectForwarder(intface string, dialTimeout, relayTimeout time.Duration) *Forwarder {
|
||||
d, err := proxy.NewDirect(intface, dialTimeout, relayTimeout)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &Forwarder{Dialer: d, addr: d.Addr()}
|
||||
}
|
||||
|
||||
func (f *Forwarder) parseOption(option string) error {
|
||||
query, err := url.ParseQuery(option)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var priority uint64
|
||||
p := query.Get("priority")
|
||||
if p != "" {
|
||||
priority, err = strconv.ParseUint(p, 10, 32)
|
||||
}
|
||||
f.SetPriority(uint32(priority))
|
||||
|
||||
f.intface = query.Get("interface")
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Addr returns the forwarder's addr.
|
||||
func (f *Forwarder) Addr() string {
|
||||
return f.addr
|
||||
}
|
||||
|
||||
// Dial dials to addr and returns conn.
|
||||
func (f *Forwarder) Dial(network, addr string) (c net.Conn, err error) {
|
||||
c, err = f.Dialer.Dial(network, addr)
|
||||
if err != nil {
|
||||
f.IncFailures()
|
||||
}
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
// Failures returns the failuer count of forwarder.
|
||||
func (f *Forwarder) Failures() uint32 {
|
||||
return atomic.LoadUint32(&f.failures)
|
||||
}
|
||||
|
||||
// IncFailures increase the failuer count by 1.
|
||||
func (f *Forwarder) IncFailures() {
|
||||
failures := atomic.AddUint32(&f.failures, 1)
|
||||
log.F("[forwarder] %s recorded %d failures, maxfailures: %d", f.addr, failures, f.MaxFailures())
|
||||
|
||||
if f.MaxFailures() != 0 && failures >= f.MaxFailures() && f.Enabled() {
|
||||
log.F("[forwarder] %s reaches maxfailures %d", f.addr, f.MaxFailures())
|
||||
f.Disable()
|
||||
}
|
||||
}
|
||||
|
||||
// AddHandler adds a custom handler to handle the status change event.
|
||||
func (f *Forwarder) AddHandler(h StatusHandler) {
|
||||
f.handlers = append(f.handlers, h)
|
||||
}
|
||||
|
||||
// Enable the forwarder.
|
||||
func (f *Forwarder) Enable() {
|
||||
if atomic.CompareAndSwapUint32(&f.disabled, 1, 0) {
|
||||
for _, h := range f.handlers {
|
||||
h(f)
|
||||
}
|
||||
}
|
||||
atomic.StoreUint32(&f.failures, 0)
|
||||
}
|
||||
|
||||
// Disable the forwarder.
|
||||
func (f *Forwarder) Disable() {
|
||||
if atomic.CompareAndSwapUint32(&f.disabled, 0, 1) {
|
||||
for _, h := range f.handlers {
|
||||
h(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Enabled returns the status of forwarder.
|
||||
func (f *Forwarder) Enabled() bool {
|
||||
return !isTrue(atomic.LoadUint32(&f.disabled))
|
||||
}
|
||||
|
||||
func isTrue(n uint32) bool {
|
||||
return n&1 == 1
|
||||
}
|
||||
|
||||
// Priority returns the priority of forwarder.
|
||||
func (f *Forwarder) Priority() uint32 {
|
||||
return atomic.LoadUint32(&f.priority)
|
||||
}
|
||||
|
||||
// SetPriority sets the priority of forwarder.
|
||||
func (f *Forwarder) SetPriority(l uint32) {
|
||||
atomic.StoreUint32(&f.priority, l)
|
||||
}
|
||||
|
||||
// MaxFailures returns the maxFailures of forwarder.
|
||||
func (f *Forwarder) MaxFailures() uint32 {
|
||||
return atomic.LoadUint32(&f.maxFailures)
|
||||
}
|
||||
|
||||
// SetMaxFailures sets the maxFailures of forwarder.
|
||||
func (f *Forwarder) SetMaxFailures(l uint32) {
|
||||
atomic.StoreUint32(&f.maxFailures, l)
|
||||
}
|
||||
|
||||
// Latency returns the latency of forwarder.
|
||||
func (f *Forwarder) Latency() int64 {
|
||||
return atomic.LoadInt64(&f.latency)
|
||||
}
|
||||
|
||||
// SetLatency sets the latency of forwarder.
|
||||
func (f *Forwarder) SetLatency(l int64) {
|
||||
atomic.StoreInt64(&f.latency, l)
|
||||
}
|
||||
+33
-27
@@ -7,77 +7,83 @@ import (
|
||||
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
"github.com/nadoo/glider/strategy"
|
||||
)
|
||||
|
||||
// Proxy struct.
|
||||
type Proxy struct {
|
||||
proxy *strategy.Proxy
|
||||
proxies []*strategy.Proxy
|
||||
|
||||
main *FwdrGroup
|
||||
all []*FwdrGroup
|
||||
domainMap sync.Map
|
||||
ipMap sync.Map
|
||||
cidrMap sync.Map
|
||||
}
|
||||
|
||||
// NewProxy returns a new rule proxy.
|
||||
func NewProxy(rules []*Config, proxy *strategy.Proxy) *Proxy {
|
||||
rd := &Proxy{proxy: proxy}
|
||||
func NewProxy(mainForwarders []string, mainStrategy *StrategyConfig, rules []*Config) *Proxy {
|
||||
rd := &Proxy{main: NewFwdrGroup("main", mainForwarders, mainStrategy)}
|
||||
|
||||
for _, r := range rules {
|
||||
sd := strategy.NewProxy(r.Name, r.Forward, &r.StrategyConfig)
|
||||
rd.proxies = append(rd.proxies, sd)
|
||||
group := NewFwdrGroup(r.Name, r.Forward, &r.StrategyConfig)
|
||||
rd.all = append(rd.all, group)
|
||||
|
||||
for _, domain := range r.Domain {
|
||||
rd.domainMap.Store(strings.ToLower(domain), sd)
|
||||
rd.domainMap.Store(strings.ToLower(domain), group)
|
||||
}
|
||||
|
||||
for _, ip := range r.IP {
|
||||
rd.ipMap.Store(ip, sd)
|
||||
rd.ipMap.Store(ip, group)
|
||||
}
|
||||
|
||||
for _, s := range r.CIDR {
|
||||
if _, cidr, err := net.ParseCIDR(s); err == nil {
|
||||
rd.cidrMap.Store(cidr, sd)
|
||||
rd.cidrMap.Store(cidr, group)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(mainForwarders) > 0 {
|
||||
direct := NewFwdrGroup("backup", nil, mainStrategy)
|
||||
for _, f := range rd.main.fwdrs {
|
||||
// Note: the addr maybe ip address, but no matter here.
|
||||
rd.domainMap.Store(strings.ToLower(strings.Split(f.addr, ":")[0]), direct)
|
||||
}
|
||||
}
|
||||
|
||||
return rd
|
||||
}
|
||||
|
||||
// Dial dials to targer addr and return a conn.
|
||||
func (p *Proxy) Dial(network, addr string) (net.Conn, proxy.Dialer, error) {
|
||||
return p.nextProxy(addr).Dial(network, addr)
|
||||
return p.chooseProxy(addr).Dial(network, addr)
|
||||
}
|
||||
|
||||
// DialUDP connects to the given address via the proxy.
|
||||
func (p *Proxy) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||
return p.nextProxy(addr).DialUDP(network, addr)
|
||||
return p.chooseProxy(addr).DialUDP(network, addr)
|
||||
}
|
||||
|
||||
// nextProxy return next proxy according to rule.
|
||||
func (p *Proxy) nextProxy(dstAddr string) *strategy.Proxy {
|
||||
// chooseProxy returns a proxy according to rule.
|
||||
func (p *Proxy) chooseProxy(dstAddr string) *FwdrGroup {
|
||||
host, _, err := net.SplitHostPort(dstAddr)
|
||||
if err != nil {
|
||||
// TODO: check here
|
||||
// logf("[rule] SplitHostPort ERROR: %s", err)
|
||||
return p.proxy
|
||||
return p.main
|
||||
}
|
||||
|
||||
// find ip
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
// check ip
|
||||
if proxy, ok := p.ipMap.Load(ip.String()); ok {
|
||||
return proxy.(*strategy.Proxy)
|
||||
return proxy.(*FwdrGroup)
|
||||
}
|
||||
|
||||
var ret *strategy.Proxy
|
||||
var ret *FwdrGroup
|
||||
// check cidr
|
||||
p.cidrMap.Range(func(key, value interface{}) bool {
|
||||
cidr := key.(*net.IPNet)
|
||||
if cidr.Contains(ip) {
|
||||
ret = value.(*strategy.Proxy)
|
||||
ret = value.(*FwdrGroup)
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -94,21 +100,21 @@ func (p *Proxy) nextProxy(dstAddr string) *strategy.Proxy {
|
||||
for i := len(host); i != -1; {
|
||||
i = strings.LastIndexByte(host[:i], '.')
|
||||
if proxy, ok := p.domainMap.Load(host[i+1:]); ok {
|
||||
return proxy.(*strategy.Proxy)
|
||||
return proxy.(*FwdrGroup)
|
||||
}
|
||||
}
|
||||
|
||||
return p.proxy
|
||||
return p.main
|
||||
}
|
||||
|
||||
// NextDialer return next dialer according to rule.
|
||||
// NextDialer returns next dialer according to rule.
|
||||
func (p *Proxy) NextDialer(dstAddr string) proxy.Dialer {
|
||||
return p.nextProxy(dstAddr).NextDialer(dstAddr)
|
||||
return p.chooseProxy(dstAddr).NextDialer(dstAddr)
|
||||
}
|
||||
|
||||
// Record records result while using the dialer from proxy.
|
||||
func (p *Proxy) Record(dialer proxy.Dialer, success bool) {
|
||||
strategy.OnRecord(dialer, success)
|
||||
OnRecord(dialer, success)
|
||||
}
|
||||
|
||||
// AddDomainIP used to update ipMap rules according to domainMap rule.
|
||||
@@ -128,9 +134,9 @@ func (p *Proxy) AddDomainIP(domain, ip string) error {
|
||||
|
||||
// Check .
|
||||
func (p *Proxy) Check() {
|
||||
p.proxy.Check()
|
||||
p.main.Check()
|
||||
|
||||
for _, d := range p.proxies {
|
||||
d.Check()
|
||||
for _, fwdr := range p.all {
|
||||
fwdr.Check()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,331 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"hash/fnv"
|
||||
"io"
|
||||
"net"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
// StrategyConfig is strategy config struct.
|
||||
type StrategyConfig struct {
|
||||
Strategy string
|
||||
CheckWebSite string
|
||||
CheckInterval int
|
||||
CheckTimeout int
|
||||
CheckDisabledOnly bool
|
||||
MaxFailures int
|
||||
DialTimeout int
|
||||
RelayTimeout int
|
||||
IntFace string
|
||||
}
|
||||
|
||||
// forwarder slice orderd by priority
|
||||
type priSlice []*Forwarder
|
||||
|
||||
func (p priSlice) Len() int { return len(p) }
|
||||
func (p priSlice) Less(i, j int) bool { return p[i].Priority() > p[j].Priority() }
|
||||
func (p priSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||
|
||||
// FwdrGroup is a forwarder group.
|
||||
type FwdrGroup struct {
|
||||
config *StrategyConfig
|
||||
fwdrs priSlice
|
||||
avail []*Forwarder // available forwarders
|
||||
mu sync.RWMutex
|
||||
index uint32
|
||||
priority uint32
|
||||
next func(addr string) *Forwarder
|
||||
}
|
||||
|
||||
// NewFwdrGroup returns a new forward group.
|
||||
func NewFwdrGroup(name string, s []string, c *StrategyConfig) *FwdrGroup {
|
||||
var fwdrs []*Forwarder
|
||||
for _, chain := range s {
|
||||
fwdr, err := ForwarderFromURL(chain, c.IntFace,
|
||||
time.Duration(c.DialTimeout)*time.Second, time.Duration(c.RelayTimeout)*time.Second)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fwdr.SetMaxFailures(uint32(c.MaxFailures))
|
||||
fwdrs = append(fwdrs, fwdr)
|
||||
}
|
||||
|
||||
if len(fwdrs) == 0 {
|
||||
// direct forwarder
|
||||
fwdrs = append(fwdrs, DirectForwarder(c.IntFace,
|
||||
time.Duration(c.DialTimeout)*time.Second, time.Duration(c.RelayTimeout)*time.Second))
|
||||
c.Strategy = "rr"
|
||||
}
|
||||
|
||||
return newFwdrGroup(name, fwdrs, c)
|
||||
}
|
||||
|
||||
// newFwdrGroup returns a new Proxy.
|
||||
func newFwdrGroup(name string, fwdrs []*Forwarder, c *StrategyConfig) *FwdrGroup {
|
||||
p := &FwdrGroup{fwdrs: fwdrs, config: c}
|
||||
sort.Sort(p.fwdrs)
|
||||
|
||||
p.init()
|
||||
|
||||
if strings.IndexByte(p.config.CheckWebSite, ':') == -1 {
|
||||
p.config.CheckWebSite += ":80"
|
||||
}
|
||||
|
||||
switch c.Strategy {
|
||||
case "rr":
|
||||
p.next = p.scheduleRR
|
||||
log.F("[strategy] %s: forward in round robin mode.", name)
|
||||
case "ha":
|
||||
p.next = p.scheduleHA
|
||||
log.F("[strategy] %s: forward in high availability mode.", name)
|
||||
case "lha":
|
||||
p.next = p.scheduleLHA
|
||||
log.F("[strategy] %s: forward in latency based high availability mode.", name)
|
||||
case "dh":
|
||||
p.next = p.scheduleDH
|
||||
log.F("[strategy] %s: forward in destination hashing mode.", name)
|
||||
default:
|
||||
p.next = p.scheduleRR
|
||||
log.F("[strategy] %s: not supported forward mode '%s', use round robin mode.", name, c.Strategy)
|
||||
}
|
||||
|
||||
for _, f := range fwdrs {
|
||||
f.AddHandler(p.onStatusChanged)
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// Dial connects to the address addr on the network net.
|
||||
func (p *FwdrGroup) Dial(network, addr string) (net.Conn, proxy.Dialer, error) {
|
||||
nd := p.NextDialer(addr)
|
||||
c, err := nd.Dial(network, addr)
|
||||
return c, nd, err
|
||||
}
|
||||
|
||||
// DialUDP connects to the given address.
|
||||
func (p *FwdrGroup) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||
return p.NextDialer(addr).DialUDP(network, addr)
|
||||
}
|
||||
|
||||
// NextDialer returns the next dialer.
|
||||
func (p *FwdrGroup) NextDialer(dstAddr string) proxy.Dialer {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
|
||||
if len(p.avail) == 0 {
|
||||
return p.fwdrs[atomic.AddUint32(&p.index, 1)%uint32(len(p.fwdrs))]
|
||||
}
|
||||
|
||||
return p.next(dstAddr)
|
||||
}
|
||||
|
||||
// Record records result while using the dialer from proxy.
|
||||
func (p *FwdrGroup) Record(dialer proxy.Dialer, success bool) {
|
||||
OnRecord(dialer, success)
|
||||
}
|
||||
|
||||
// OnRecord records result while using the dialer from proxy.
|
||||
func OnRecord(dialer proxy.Dialer, success bool) {
|
||||
if fwdr, ok := dialer.(*Forwarder); ok {
|
||||
if !success {
|
||||
fwdr.IncFailures()
|
||||
} else {
|
||||
fwdr.Enable()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Priority returns the active priority of dialer.
|
||||
func (p *FwdrGroup) Priority() uint32 { return atomic.LoadUint32(&p.priority) }
|
||||
|
||||
// SetPriority sets the active priority of daler.
|
||||
func (p *FwdrGroup) SetPriority(pri uint32) { atomic.StoreUint32(&p.priority, pri) }
|
||||
|
||||
// init traverse d.fwdrs and init the available forwarder slice.
|
||||
func (p *FwdrGroup) init() {
|
||||
for _, f := range p.fwdrs {
|
||||
if f.Enabled() {
|
||||
p.SetPriority(f.Priority())
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
p.avail = nil
|
||||
for _, f := range p.fwdrs {
|
||||
if f.Enabled() && f.Priority() >= p.Priority() {
|
||||
p.avail = append(p.avail, f)
|
||||
}
|
||||
}
|
||||
|
||||
if len(p.avail) == 0 {
|
||||
// no available forwarders, set priority to 0 to check all forwarders in check func
|
||||
p.SetPriority(0)
|
||||
// log.F("[strategy] no available forwarders, please check your config file or network settings")
|
||||
}
|
||||
}
|
||||
|
||||
// onStatusChanged will be called when fwdr's status changed.
|
||||
func (p *FwdrGroup) onStatusChanged(fwdr *Forwarder) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
if fwdr.Enabled() {
|
||||
log.F("[strategy] %s changed status from Disabled to Enabled ", fwdr.Addr())
|
||||
if fwdr.Priority() == p.Priority() {
|
||||
p.avail = append(p.avail, fwdr)
|
||||
} else if fwdr.Priority() > p.Priority() {
|
||||
p.init()
|
||||
}
|
||||
} else {
|
||||
log.F("[strategy] %s changed status from Enabled to Disabled", fwdr.Addr())
|
||||
for i, f := range p.avail {
|
||||
if f == fwdr {
|
||||
p.avail[i], p.avail = p.avail[len(p.avail)-1], p.avail[:len(p.avail)-1]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(p.avail) == 0 {
|
||||
p.init()
|
||||
}
|
||||
}
|
||||
|
||||
// Check implements the Checker interface.
|
||||
func (p *FwdrGroup) Check() {
|
||||
// no need to check when there's only 1 forwarder
|
||||
if len(p.fwdrs) > 1 {
|
||||
for i := 0; i < len(p.fwdrs); i++ {
|
||||
go p.check(p.fwdrs[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *FwdrGroup) check(f *Forwarder) {
|
||||
wait := uint8(0)
|
||||
buf := make([]byte, 4)
|
||||
intval := time.Duration(p.config.CheckInterval) * time.Second
|
||||
|
||||
for {
|
||||
time.Sleep(intval * time.Duration(wait))
|
||||
|
||||
// check all forwarders at least one time
|
||||
if wait > 0 && (f.Priority() < p.Priority()) {
|
||||
continue
|
||||
}
|
||||
|
||||
if f.Enabled() && p.config.CheckDisabledOnly {
|
||||
continue
|
||||
}
|
||||
|
||||
if checkWebSite(f, p.config.CheckWebSite, time.Duration(p.config.CheckTimeout)*time.Second, buf) {
|
||||
wait = 1
|
||||
continue
|
||||
}
|
||||
|
||||
if wait == 0 {
|
||||
wait = 1
|
||||
}
|
||||
|
||||
wait *= 2
|
||||
if wait > 16 {
|
||||
wait = 16
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func checkWebSite(fwdr *Forwarder, website string, timeout time.Duration, buf []byte) bool {
|
||||
startTime := time.Now()
|
||||
|
||||
rc, err := fwdr.Dial("tcp", website)
|
||||
if err != nil {
|
||||
fwdr.Disable()
|
||||
log.F("[check] %s(%d) -> %s, DISABLED. error in dial: %s", fwdr.Addr(), fwdr.Priority(),
|
||||
website, err)
|
||||
return false
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
if timeout > 0 {
|
||||
rc.SetDeadline(time.Now().Add(timeout))
|
||||
}
|
||||
|
||||
_, err = io.WriteString(rc, "GET / HTTP/1.0\r\n\r\n")
|
||||
if err != nil {
|
||||
fwdr.Disable()
|
||||
log.F("[check] %s(%d) -> %s, DISABLED. error in write: %s", fwdr.Addr(), fwdr.Priority(),
|
||||
website, err)
|
||||
return false
|
||||
}
|
||||
|
||||
_, err = io.ReadFull(rc, buf)
|
||||
if err != nil {
|
||||
fwdr.Disable()
|
||||
log.F("[check] %s(%d) -> %s, DISABLED. error in read: %s", fwdr.Addr(), fwdr.Priority(),
|
||||
website, err)
|
||||
return false
|
||||
}
|
||||
|
||||
if !bytes.Equal([]byte("HTTP"), buf) {
|
||||
fwdr.Disable()
|
||||
log.F("[check] %s(%d) -> %s, DISABLED. server response: %s", fwdr.Addr(), fwdr.Priority(),
|
||||
website, buf)
|
||||
return false
|
||||
}
|
||||
|
||||
readTime := time.Since(startTime)
|
||||
fwdr.SetLatency(int64(readTime))
|
||||
|
||||
if readTime > timeout {
|
||||
fwdr.Disable()
|
||||
log.F("[check] %s(%d) -> %s, DISABLED. check timeout: %s", fwdr.Addr(), fwdr.Priority(),
|
||||
website, readTime)
|
||||
return false
|
||||
}
|
||||
|
||||
fwdr.Enable()
|
||||
log.F("[check] %s(%d) -> %s, ENABLED. connect time: %s", fwdr.Addr(), fwdr.Priority(),
|
||||
website, readTime)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// Round Robin
|
||||
func (p *FwdrGroup) scheduleRR(dstAddr string) *Forwarder {
|
||||
return p.avail[atomic.AddUint32(&p.index, 1)%uint32(len(p.avail))]
|
||||
}
|
||||
|
||||
// High Availability
|
||||
func (p *FwdrGroup) scheduleHA(dstAddr string) *Forwarder {
|
||||
return p.avail[0]
|
||||
}
|
||||
|
||||
// Latency based High Availability
|
||||
func (p *FwdrGroup) scheduleLHA(dstAddr string) *Forwarder {
|
||||
fwdr := p.avail[0]
|
||||
lowest := fwdr.Latency()
|
||||
for _, f := range p.avail {
|
||||
if f.Latency() < lowest {
|
||||
lowest = f.Latency()
|
||||
fwdr = f
|
||||
}
|
||||
}
|
||||
return fwdr
|
||||
}
|
||||
|
||||
// Destination Hashing
|
||||
func (p *FwdrGroup) scheduleDH(dstAddr string) *Forwarder {
|
||||
fnv1a := fnv.New32a()
|
||||
fnv1a.Write([]byte(dstAddr))
|
||||
return p.avail[fnv1a.Sum32()%uint32(len(p.avail))]
|
||||
}
|
||||
Reference in new issue
Block a user