64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"474420502.top/eson/requests"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
ses := requests.NewSession()
|
|
ses.SetConfig(requests.CRequestTimeout, 5)
|
|
|
|
_, err := ses.Get("http://14.17.96.148:3333/ippool/switch/imactive").Execute()
|
|
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
|
|
}
|
|
|
|
ses := requests.NewSession()
|
|
ses.SetConfig(requests.CRequestTimeout, 5)
|
|
|
|
_, err := ses.Get("http://10.10.0.1:3333/ippool/switch/imactive").Execute()
|
|
if err != nil {
|
|
log.Println("PingVPN err http://10.10.0.1:3333/ippool/switch/imactive check Network")
|
|
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
|
|
}
|