check: support full url check and expect resp setting(#195)

This commit is contained in:
nadoo committed 2020-11-20 18:11:25 +08:00
1 parent 38f84a625d
commit 65d606d29c
8 files changed
+199 -146

No files matched your search

+117
View File
@@ -0,0 +1,117 @@
package rule
import (
"bytes"
"io"
"time"
"github.com/nadoo/glider/log"
"github.com/nadoo/glider/pool"
)
// Checker is a forwarder health checker.
type Checker interface {
Check(fwdr *Forwarder) (healthy bool)
}
type tcpChecker struct {
addr string
timeout time.Duration
}
func newTcpChecker(addr string, timeout time.Duration) *tcpChecker {
return &tcpChecker{addr, timeout}
}
func (c *tcpChecker) Check(fwdr *Forwarder) bool {
startTime := time.Now()
rc, err := fwdr.Dial("tcp", c.addr)
if err != nil {
log.F("[check] tcp:%s(%d), FAILED. error in dial: %s", fwdr.Addr(), fwdr.Priority(), err)
fwdr.Disable()
return false
}
defer rc.Close()
if c.timeout > 0 {
rc.SetDeadline(time.Now().Add(c.timeout))
}
elapsed := time.Since(startTime)
fwdr.SetLatency(int64(elapsed))
if elapsed > c.timeout {
log.F("[check] tcp:%s(%d), FAILED. check timeout: %s", fwdr.Addr(), fwdr.Priority(), elapsed)
fwdr.Disable()
return false
}
log.F("[check] tcp:%s(%d), SUCCESS. elapsed: %s", fwdr.Addr(), fwdr.Priority(), elapsed)
fwdr.Enable()
return true
}
type httpChecker struct {
addr string
uri string
expect string
timeout time.Duration
}
func newHttpChecker(addr, uri, expect string, timeout time.Duration) *httpChecker {
return &httpChecker{addr, uri, expect, timeout}
}
func (c *httpChecker) Check(fwdr *Forwarder) bool {
startTime := time.Now()
rc, err := fwdr.Dial("tcp", c.addr)
if err != nil {
log.F("[check] %s(%d) -> http://%s, FAILED. error in dial: %s", fwdr.Addr(), fwdr.Priority(), c.addr, err)
fwdr.Disable()
return false
}
defer rc.Close()
if c.timeout > 0 {
rc.SetDeadline(time.Now().Add(c.timeout))
}
_, err = io.WriteString(rc, "GET "+c.uri+" HTTP/1.1\r\nHost:"+c.addr+"\r\nConnection: close"+"\r\n\r\n")
if err != nil {
log.F("[check] %s(%d) -> http://%s, FAILED. error in write: %s", fwdr.Addr(), fwdr.Priority(), c.addr, err)
fwdr.Disable()
return false
}
r := pool.GetBufReader(rc)
defer pool.PutBufReader(r)
line, _, err := r.ReadLine()
if err != nil {
log.F("[check] %s(%d) -> http://%s, FAILED. error in read: %s", fwdr.Addr(), fwdr.Priority(), c.addr, err)
fwdr.Disable()
return false
}
if !bytes.Contains(line, []byte(c.expect)) {
log.F("[check] %s(%d) -> http://%s, FAILED. expect: %s, server response: %s", fwdr.Addr(), fwdr.Priority(), c.addr, c.expect, line)
fwdr.Disable()
return false
}
elapsed := time.Since(startTime)
fwdr.SetLatency(int64(elapsed))
if elapsed > c.timeout {
log.F("[check] %s(%d) -> http://%s, FAILED. check timeout: %s", fwdr.Addr(), fwdr.Priority(), c.addr, elapsed)
fwdr.Disable()
return false
}
log.F("[check] %s(%d) -> http://%s, SUCCESS. elapsed: %s", fwdr.Addr(), fwdr.Priority(), c.addr, elapsed)
fwdr.Enable()
return true
}
+15 -17
View File
@@ -12,8 +12,8 @@ import (
type Config struct {
Name string
Forward []string
StrategyConfig StrategyConfig
Forward []string
Strategy Strategy
DNSServers []string
IPSet string
@@ -23,11 +23,10 @@ type Config struct {
CIDR []string
}
// StrategyConfig is config of strategy.
type StrategyConfig struct {
// Strategy configurations.
type Strategy struct {
Strategy string
CheckType string
CheckAddr string
Check string
CheckInterval int
CheckTimeout int
CheckTolerance int
@@ -44,17 +43,16 @@ func NewConfFromFile(ruleFile string) (*Config, error) {
f := conflag.NewFromFile("rule", ruleFile)
f.StringSliceUniqVar(&p.Forward, "forward", nil, "forward url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS[,SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS]")
f.StringVar(&p.StrategyConfig.Strategy, "strategy", "rr", "forward strategy, default: rr")
f.StringVar(&p.StrategyConfig.CheckType, "checktype", "http", "fowarder check type, http/tcp")
f.StringVar(&p.StrategyConfig.CheckAddr, "checkaddr", "www.apple.com:80", "fowarder check addr, format: HOST[:PORT], default port: 80,")
f.IntVar(&p.StrategyConfig.CheckInterval, "checkinterval", 30, "fowarder check interval(seconds)")
f.IntVar(&p.StrategyConfig.CheckTimeout, "checktimeout", 10, "fowarder check timeout(seconds)")
f.IntVar(&p.StrategyConfig.CheckTolerance, "checktolerance", 0, "fowarder check tolerance(ms), switch only when new_latency < old_latency - tolerance, only used in lha mode")
f.BoolVar(&p.StrategyConfig.CheckDisabledOnly, "checkdisabledonly", false, "check disabled fowarders only")
f.IntVar(&p.StrategyConfig.MaxFailures, "maxfailures", 3, "max failures to change forwarder status to disabled")
f.IntVar(&p.StrategyConfig.DialTimeout, "dialtimeout", 3, "dial timeout(seconds)")
f.IntVar(&p.StrategyConfig.RelayTimeout, "relaytimeout", 0, "relay timeout(seconds)")
f.StringVar(&p.StrategyConfig.IntFace, "interface", "", "source ip or source interface")
f.StringVar(&p.Strategy.Strategy, "strategy", "rr", "forward strategy, default: rr")
f.StringVar(&p.Strategy.Check, "check", "http://www.msftconnecttest.com/connecttest.txt#expect=200", "check=disable: disable health check\ncheck=tcp[://HOST:PORT]: tcp port connect check\ncheck=http://HOST[:PORT][/URI][#expect=STRING_IN_RESP_LINE]")
f.IntVar(&p.Strategy.CheckInterval, "checkinterval", 30, "fowarder check interval(seconds)")
f.IntVar(&p.Strategy.CheckTimeout, "checktimeout", 10, "fowarder check timeout(seconds)")
f.IntVar(&p.Strategy.CheckTolerance, "checktolerance", 0, "fowarder check tolerance(ms), switch only when new_latency < old_latency - tolerance, only used in lha mode")
f.BoolVar(&p.Strategy.CheckDisabledOnly, "checkdisabledonly", false, "check disabled fowarders only")
f.IntVar(&p.Strategy.MaxFailures, "maxfailures", 3, "max failures to change forwarder status to disabled")
f.IntVar(&p.Strategy.DialTimeout, "dialtimeout", 3, "dial timeout(seconds)")
f.IntVar(&p.Strategy.RelayTimeout, "relaytimeout", 0, "relay timeout(seconds)")
f.StringVar(&p.Strategy.IntFace, "interface", "", "source ip or source interface")
f.StringSliceUniqVar(&p.DNSServers, "dnsserver", nil, "remote dns server")
f.StringVar(&p.IPSet, "ipset", "", "ipset name")
+46 -107
View File
@@ -1,10 +1,9 @@
package rule
import (
"bytes"
"hash/fnv"
"io"
"net"
"net/url"
"sort"
"strings"
"sync"
@@ -24,7 +23,7 @@ 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
config *Strategy
fwdrs priSlice
avail []*Forwarder // available forwarders
mu sync.RWMutex
@@ -34,7 +33,7 @@ type FwdrGroup struct {
}
// NewFwdrGroup returns a new forward group.
func NewFwdrGroup(name string, s []string, c *StrategyConfig) *FwdrGroup {
func NewFwdrGroup(name string, s []string, c *Strategy) *FwdrGroup {
var fwdrs []*Forwarder
for _, chain := range s {
fwdr, err := ForwarderFromURL(chain, c.IntFace,
@@ -57,16 +56,12 @@ func NewFwdrGroup(name string, s []string, c *StrategyConfig) *FwdrGroup {
}
// newFwdrGroup returns a new FwdrGroup.
func newFwdrGroup(name string, fwdrs []*Forwarder, c *StrategyConfig) *FwdrGroup {
func newFwdrGroup(name string, fwdrs []*Forwarder, c *Strategy) *FwdrGroup {
p := &FwdrGroup{fwdrs: fwdrs, config: c}
sort.Sort(p.fwdrs)
p.init()
if strings.IndexByte(p.config.CheckAddr, ':') == -1 {
p.config.CheckAddr += ":80"
}
// default scheduler
p.next = p.scheduleRR
@@ -178,24 +173,55 @@ func (p *FwdrGroup) onStatusChanged(fwdr *Forwarder) {
}
}
// Check implements the Checker interface.
// Check runs the forwarder checks.
func (p *FwdrGroup) Check() {
if p.config.CheckType != "http" && p.config.CheckType != "tcp" {
if len(p.fwdrs) == 1 {
p.config.MaxFailures = 0
log.F("[group] only 1 forwarder found, disable health checking")
return
}
// 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], p.config.CheckType == "http")
if !strings.Contains(p.config.Check, "://") {
p.config.Check += "://"
}
u, err := url.Parse(p.config.Check)
if err != nil {
log.F("[group] parse check config error: %s", err)
return
}
addr := u.Host
if strings.IndexByte(addr, ':') == -1 {
addr += ":80"
}
timeout := time.Duration(p.config.CheckTimeout) * time.Second
var checker Checker
switch u.Scheme {
case "tcp":
checker = newTcpChecker(addr, timeout)
case "http":
expect := "HTTP" // default: check the first 4 chars in response
params, _ := url.ParseQuery(u.Fragment)
if ex := params.Get("expect"); ex != "" {
expect = ex
}
checker = newHttpChecker(addr, u.RequestURI(), expect, timeout)
default:
p.config.MaxFailures = 0
log.F("[group] invalid check config `%s`, disable health checking", p.config.Check)
return
}
for i := 0; i < len(p.fwdrs); i++ {
go p.check(p.fwdrs[i], checker)
}
}
func (p *FwdrGroup) check(f *Forwarder, http bool) {
func (p *FwdrGroup) check(f *Forwarder, checker Checker) {
wait := uint8(0)
buf := make([]byte, 4)
intval := time.Duration(p.config.CheckInterval) * time.Second
for {
@@ -210,16 +236,9 @@ func (p *FwdrGroup) check(f *Forwarder, http bool) {
continue
}
if http {
if checkHttp(f, p.config.CheckAddr, time.Duration(p.config.CheckTimeout)*time.Second, buf) {
wait = 1
continue
}
} else {
if checkTcp(f, p.config.CheckAddr, time.Duration(p.config.CheckTimeout)*time.Second) {
wait = 1
continue
}
if checker.Check(f) {
wait = 1
continue
}
if wait == 0 {
@@ -233,86 +252,6 @@ func (p *FwdrGroup) check(f *Forwarder, http bool) {
}
}
func checkTcp(fwdr *Forwarder, addr string, timeout time.Duration) bool {
startTime := time.Now()
rc, err := fwdr.Dial("tcp", addr)
if err != nil {
log.F("[check] tcp://%s(%d), FAILED. error in dial: %s", fwdr.Addr(), fwdr.Priority(), err)
fwdr.Disable()
return false
}
defer rc.Close()
if timeout > 0 {
rc.SetDeadline(time.Now().Add(timeout))
}
elapsed := time.Since(startTime)
fwdr.SetLatency(int64(elapsed))
if elapsed > timeout {
log.F("[check] tcp://%s(%d), FAILED. check timeout: %s", fwdr.Addr(), fwdr.Priority(), elapsed)
fwdr.Disable()
return false
}
log.F("[check] tcp://%s(%d), SUCCESS. elapsed: %s", fwdr.Addr(), fwdr.Priority(), elapsed)
fwdr.Enable()
return true
}
func checkHttp(fwdr *Forwarder, addr string, timeout time.Duration, buf []byte) bool {
startTime := time.Now()
rc, err := fwdr.Dial("tcp", addr)
if err != nil {
log.F("[check] %s(%d) -> http://%s, FAILED. error in dial: %s", fwdr.Addr(), fwdr.Priority(), addr, err)
fwdr.Disable()
return false
}
defer rc.Close()
if timeout > 0 {
rc.SetDeadline(time.Now().Add(timeout))
}
_, err = io.WriteString(rc, "GET / HTTP/1.1\r\nHost:"+addr+"\r\nConnection: close"+"\r\n\r\n")
if err != nil {
log.F("[check] %s(%d) -> http://%s, FAILED. error in write: %s", fwdr.Addr(), fwdr.Priority(), addr, err)
fwdr.Disable()
return false
}
_, err = io.ReadFull(rc, buf)
if err != nil {
log.F("[check] %s(%d) -> http://%s, FAILED. error in read: %s", fwdr.Addr(), fwdr.Priority(), addr, err)
fwdr.Disable()
return false
}
if !bytes.Equal([]byte("HTTP"), buf) {
log.F("[check] %s(%d) -> http://%s, FAILED. server response: %s", fwdr.Addr(), fwdr.Priority(), addr, buf)
fwdr.Disable()
return false
}
elapsed := time.Since(startTime)
fwdr.SetLatency(int64(elapsed))
if elapsed > timeout {
log.F("[check] %s(%d) -> http://%s, FAILED. check timeout: %s", fwdr.Addr(), fwdr.Priority(), addr, elapsed)
fwdr.Disable()
return false
}
log.F("[check] %s(%d) -> http://%s, SUCCESS. elapsed: %s", fwdr.Addr(), fwdr.Priority(), addr, elapsed)
fwdr.Enable()
return true
}
// Round Robin.
func (p *FwdrGroup) scheduleRR(dstAddr string) *Forwarder {
return p.avail[atomic.AddUint32(&p.index, 1)%uint32(len(p.avail))]
+2 -2
View File
@@ -19,11 +19,11 @@ type Proxy struct {
}
// NewProxy returns a new rule proxy.
func NewProxy(mainForwarders []string, mainStrategy *StrategyConfig, rules []*Config) *Proxy {
func NewProxy(mainForwarders []string, mainStrategy *Strategy, rules []*Config) *Proxy {
rd := &Proxy{main: NewFwdrGroup("main", mainForwarders, mainStrategy)}
for _, r := range rules {
group := NewFwdrGroup(r.Name, r.Forward, &r.StrategyConfig)
group := NewFwdrGroup(r.Name, r.Forward, &r.Strategy)
rd.all = append(rd.all, group)
for _, domain := range r.Domain {