no message
This commit is contained in:
@ -33,15 +33,11 @@ type Checker struct {
|
|||||||
log *logger.Logger
|
log *logger.Logger
|
||||||
}
|
}
|
||||||
type CheckerItem struct {
|
type CheckerItem struct {
|
||||||
IsHTTPS bool
|
|
||||||
Method string
|
|
||||||
URL string
|
|
||||||
Domain string
|
Domain string
|
||||||
Host string
|
Address string
|
||||||
Data []byte
|
|
||||||
SuccessCount uint
|
SuccessCount uint
|
||||||
FailCount uint
|
FailCount uint
|
||||||
Key string
|
Lasttime int64
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewChecker args:
|
//NewChecker args:
|
||||||
@ -101,7 +97,7 @@ func (c *Checker) start() {
|
|||||||
//log.Printf("check %s", item.Host)
|
//log.Printf("check %s", item.Host)
|
||||||
var conn net.Conn
|
var conn net.Conn
|
||||||
var err error
|
var err error
|
||||||
conn, err = ConnectHost(item.Host, c.timeout)
|
conn, err = ConnectHost(item.Address, c.timeout)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
conn.SetDeadline(time.Now().Add(time.Millisecond))
|
conn.SetDeadline(time.Now().Add(time.Millisecond))
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@ -111,7 +107,8 @@ func (c *Checker) start() {
|
|||||||
} else {
|
} else {
|
||||||
item.SuccessCount = item.SuccessCount + 1
|
item.SuccessCount = item.SuccessCount + 1
|
||||||
}
|
}
|
||||||
c.data.Set(item.Host, item)
|
item.Lasttime = time.Now().Unix()
|
||||||
|
c.data.Set(item.Domain, item)
|
||||||
}
|
}
|
||||||
}(v.(CheckerItem))
|
}(v.(CheckerItem))
|
||||||
}
|
}
|
||||||
@ -126,23 +123,28 @@ func (c *Checker) isNeedCheck(item CheckerItem) bool {
|
|||||||
var minCount uint = 5
|
var minCount uint = 5
|
||||||
if (item.SuccessCount >= minCount && item.SuccessCount > item.FailCount) ||
|
if (item.SuccessCount >= minCount && item.SuccessCount > item.FailCount) ||
|
||||||
(item.FailCount >= minCount && item.SuccessCount > item.FailCount) ||
|
(item.FailCount >= minCount && item.SuccessCount > item.FailCount) ||
|
||||||
c.domainIsInMap(item.Host, false) ||
|
c.domainIsInMap(item.Domain, false) ||
|
||||||
c.domainIsInMap(item.Host, true) {
|
c.domainIsInMap(item.Domain, true) ||
|
||||||
|
time.Now().Unix()-item.Lasttime > 1800 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
func (c *Checker) IsBlocked(address string) (blocked, isInMap bool, failN, successN uint) {
|
func (c *Checker) IsBlocked(domain string) (blocked, isInMap bool, failN, successN uint) {
|
||||||
if c.domainIsInMap(address, true) {
|
h, _, _ := net.SplitHostPort(domain)
|
||||||
|
if h != "" {
|
||||||
|
domain = h
|
||||||
|
}
|
||||||
|
if c.domainIsInMap(domain, true) {
|
||||||
//log.Printf("%s in blocked ? true", address)
|
//log.Printf("%s in blocked ? true", address)
|
||||||
return true, true, 0, 0
|
return true, true, 0, 0
|
||||||
}
|
}
|
||||||
if c.domainIsInMap(address, false) {
|
if c.domainIsInMap(domain, false) {
|
||||||
//log.Printf("%s in direct ? true", address)
|
//log.Printf("%s in direct ? true", address)
|
||||||
return false, true, 0, 0
|
return false, true, 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_item, ok := c.data.Get(address)
|
_item, ok := c.data.Get(domain)
|
||||||
if !ok {
|
if !ok {
|
||||||
//log.Printf("%s not in map, blocked true", address)
|
//log.Printf("%s not in map, blocked true", address)
|
||||||
return true, false, 0, 0
|
return true, false, 0, 0
|
||||||
@ -174,16 +176,20 @@ func (c *Checker) domainIsInMap(address string, blockedMap bool) bool {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
func (c *Checker) Add(key, address string) {
|
func (c *Checker) Add(domain, address string) {
|
||||||
if c.domainIsInMap(key, false) || c.domainIsInMap(key, true) {
|
h, _, _ := net.SplitHostPort(domain)
|
||||||
|
if h != "" {
|
||||||
|
domain = h
|
||||||
|
}
|
||||||
|
if c.domainIsInMap(domain, false) || c.domainIsInMap(domain, true) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var item CheckerItem
|
var item CheckerItem
|
||||||
item = CheckerItem{
|
item = CheckerItem{
|
||||||
Host: address,
|
Domain: domain,
|
||||||
Key: key,
|
Address: address,
|
||||||
}
|
}
|
||||||
c.data.SetIfAbsent(item.Host, item)
|
c.data.SetIfAbsent(item.Domain, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BasicAuth struct {
|
type BasicAuth struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user