2018-08-09 04:51:49 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2018-12-18 02:21:15 +00:00
|
|
|
|
|
|
|
"474420502.top/eson/requests"
|
2018-08-09 04:51:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PingWorker 专为ping服务, 保证不存在多余的Ping的, 让终端服务的请求合理减少.
|
|
|
|
type PingWorker struct {
|
|
|
|
LastPingVPNTime int64
|
|
|
|
LastPingVPN int64 //上次Ping的结果 0 或者 1
|
|
|
|
|
|
|
|
LastPingNetTime int64
|
|
|
|
LastPingNet int64 //上次Ping的结果 0 或者 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// PingNet 确认adsl网络通信
|
|
|
|
func (pw *PingWorker) PingNet() bool {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
if now-atomic.LoadInt64(&pw.LastPingNetTime) <= 2 {
|
|
|
|
return atomic.LoadInt64(&pw.LastPingNet) == 1
|
|
|
|
}
|
|
|
|
|
2018-12-18 02:21:15 +00:00
|
|
|
ses := requests.NewSession()
|
|
|
|
ses.SetConfig(requests.CRequestTimeout, 5)
|
|
|
|
|
|
|
|
_, err := ses.Get("http://14.17.96.148:3333/ippool/switch/imactive").Execute()
|
2018-08-09 04:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("PingNet err http://14.17.96.148:3333/ippool/switch/imactive check Network")
|
|
|
|
atomic.StoreInt64(&pw.LastPingNet, 0)
|
|
|
|
atomic.StoreInt64(&pw.LastPingNetTime, time.Now().Unix())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
atomic.StoreInt64(&pw.LastPingNet, 1)
|
|
|
|
atomic.StoreInt64(&pw.LastPingNetTime, time.Now().Unix())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// PingVPN 确认确认外网网络通信
|
|
|
|
func (pw *PingWorker) PingVPN() bool {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
if now-atomic.LoadInt64(&pw.LastPingVPNTime) <= 2 {
|
|
|
|
return atomic.LoadInt64(&pw.LastPingVPN) == 1
|
|
|
|
}
|
|
|
|
|
2018-12-18 02:21:15 +00:00
|
|
|
ses := requests.NewSession()
|
|
|
|
ses.SetConfig(requests.CRequestTimeout, 5)
|
|
|
|
|
2019-01-02 10:58:28 +00:00
|
|
|
_, err := ses.Get("http://10.10.0.1:3333/ippool/switch/imactive").Execute()
|
2018-08-09 04:51:49 +00:00
|
|
|
if err != nil {
|
2019-01-02 10:58:28 +00:00
|
|
|
log.Println("PingVPN err http://10.10.0.1:3333/ippool/switch/imactive check Network")
|
2018-08-09 04:51:49 +00:00
|
|
|
atomic.StoreInt64(&pw.LastPingVPN, 0)
|
|
|
|
atomic.StoreInt64(&pw.LastPingVPNTime, time.Now().Unix())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.StoreInt64(&pw.LastPingVPN, 1)
|
|
|
|
atomic.StoreInt64(&pw.LastPingVPNTime, time.Now().Unix())
|
|
|
|
return true
|
|
|
|
}
|