Merge pull request #4 from CHN-STUDENT/dev

修复网络延迟测试和状态测试造成的程序崩溃
pull/92/head
CHN-STUDENT 2020-12-24 15:19:15 +08:00 committed by GitHub
commit e808042967
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 174 additions and 103 deletions

View File

@ -2,12 +2,6 @@
懒得改项目名了,需要的话自己改。
### 编译环境
CentOS7: go version go1.15.5 linux/amd64
Windows10: go version go1.15.5 windows/amd64
### 使用方法
可选:更改源码中的连接信息
```
@ -19,7 +13,10 @@ go build .
```
### TODO
- IPV4 和 IPV6 状态
- 外网连接状态
- 三网丢包计算
- 三网延迟计算
- 其他算法优化
### 鸣谢

View File

@ -10,6 +10,9 @@ package main
import (
"fmt"
"os/signal"
"syscall"
//下面这是已经封装好的轮子
"github.com/bitcav/nitr-core/cpu"
"github.com/bitcav/nitr-core/host"
@ -35,9 +38,9 @@ var (
PASSWORD string = "123456"
INTERVAL int = 1
PORBEPORT int = 80
CU string = "cu.tz.cloudcpp.com"
CT string = "ct.tz.cloudcpp.com"
CM string = "cm.tz.cloudcpp.com"
CU string = "cu.tz.cloudcpp.com" //120.52.99.224 河北联通
CT string = "ct.tz.cloudcpp.com" //183.78.182.66 北京电信
CM string = "cm.tz.cloudcpp.com" //211.139.145.129 广州移动
)
type ClientInfo struct {
@ -147,15 +150,16 @@ func spaceCount() {
strings.Index(fsType, "fat32") < 0 &&
strings.Index(fsType, "exfat") < 0 &&
strings.Index(fsType, "xfs") < 0 {
//if(d.Device == "A") { //特殊盘符自己写处理
} else {
if strings.Index(d.Device, "Z:") > -1 { //特殊盘符自己写处理
continue
//}
} else {
diskUsageOf, _ := disk.Usage(d.Mountpoint)
used += diskUsageOf.Used
total += diskUsageOf.Total
}
}
}
clientInfo.HddUsed = used / 1024.0 / 1024.0
clientInfo.HddTotal = total / 1024.0 / 1024.0
}
@ -179,37 +183,46 @@ func getLoad() {
}
var CU_ADDR = CU + ":" + strconv.Itoa(PORBEPORT)
var CT_ADDR = CU + ":" + strconv.Itoa(PORBEPORT)
var CM_ADDR = CU + ":" + strconv.Itoa(PORBEPORT)
var CT_ADDR = CT + ":" + strconv.Itoa(PORBEPORT)
var CM_ADDR = CM + ":" + strconv.Itoa(PORBEPORT)
func getNetworkStatus() {
defaulttimeout := 1 * time.Second
count := 0
conn1 , err1 := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
defer conn1.Close()
if err1 != nil {
fmt.Println("Error try to connect China unicom :", err1)
fmt.Println("[getNetworkStatus]Error try to connect China unicom :", err1)
count += 1
conn1.Close()
} else {
}
tcpconn1, ok := conn1.(*net.TCPConn)
if ok {
tcpconn1.SetLinger(0)
}
if conn1 != nil {
conn1.Close()
}
conn2 , err2 := net.DialTimeout("tcp", CT_ADDR,defaulttimeout)
defer conn2.Close()
if err2 != nil {
fmt.Println("Error try to connect China telecom :", err2)
fmt.Println("[getNetworkStatus]Error try to connect China telecom :", err2)
count += 1
conn2.Close()
} else {
}
tcpconn2, ok := conn2.(*net.TCPConn)
if ok {
tcpconn2.SetLinger(0)
}
if conn2 != nil {
conn2.Close()
}
conn3 , err3 := net.DialTimeout("tcp", CM_ADDR,defaulttimeout)
defer conn3.Close()
if err3 != nil {
fmt.Println("Error try to connect China mobile :", err3)
fmt.Println("[getNetworkStatus]Error try to connect China mobile :", err3)
count += 1
conn3.Close()
} else {
}
tcpconn3, ok := conn2.(*net.TCPConn)
if ok {
tcpconn3.SetLinger(0)
}
if conn3 != nil {
conn3.Close()
}
if count >= 2 {
@ -232,8 +245,33 @@ func bytes2str(b []byte) string {
var clientInfo ClientInfo
func SetupCloseHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("\r- Ctrl+C pressed in Terminal,Stop client program")
if mainConnect != nil {
pingValueCU.Stop()
pingValueCT.Stop()
pingValueCM.Stop()
netSpeed.Stop()
mainConnect.Close()
}
os.Exit(0)
}()
}
var mainConnect net.Conn
var netSpeed *NetSpeed
var pingValueCU *PingValue
var pingValueCT *PingValue
var pingValueCM *PingValue
func main() {
// Setup our Ctrl+C handler
SetupCloseHandler()
for _, args := range os.Args {
if strings.Index(args,"SERVER") > -1 {
strArr := strings.Split(args,"SERVER=")
@ -253,25 +291,37 @@ func main() {
}
}
defaulttimeout := 30 * time.Second
clientInfo = NewDefaultClientInfo()
netSpeed = NewNetSpeed()
pingValueCU = NewPingValue()
pingValueCT = NewPingValue()
pingValueCM = NewPingValue()
pingValueCU.RunCU()
pingValueCT.RunCT()
pingValueCM.RunCM()
netSpeed.Run()
for {
conn , err := net.DialTimeout("tcp", SERVER + ":" + strconv.Itoa(PORT),defaulttimeout)
var err error
mainConnect , err = net.DialTimeout("tcp", SERVER + ":" + strconv.Itoa(PORT),defaulttimeout)
if err != nil {
fmt.Println("Error listening:", err)
}
defer conn.Close()
defer mainConnect.Close()
buff := make([]byte, 1024)
conn.Read(buff)
mainConnect.Read(buff)
str := bytes2str(buff)
if strings.Index(str,"Authentication required") > -1 {
auth := str2bytes(USER + ":" + PASSWORD + "\n")
_ , err = conn.Write(auth)
_ , err = mainConnect.Write(auth)
if err != nil {
fmt.Println("Error Sending auth info:", err)
return
}
buff = make([]byte, 1024)
_ , err = conn.Read(buff)
_ , err = mainConnect.Read(buff)
if err != nil {
fmt.Println("Error Getting Server Data:", err)
return
}
str = bytes2str(buff)
if strings.Index(str,"Authentication required") < 0 {
@ -283,9 +333,10 @@ func main() {
fmt.Println(str)
if strings.Index(str,"You are connecting via") < 0 {
buff = make([]byte, 1024)
_ , err = conn.Read(buff)
_ , err = mainConnect.Read(buff)
if err != nil {
fmt.Println("Error Getting Server Data:", err)
return
}
str = bytes2str(buff)
fmt.Println(str)
@ -299,21 +350,11 @@ func main() {
fmt.Println(str)
}
fmt.Println(checkIP)
clientInfo = NewDefaultClientInfo()
netSpeed := NewNetSpeed()
pingValueCU := NewPingValue()
pingValueCT := NewPingValue()
pingValueCM := NewPingValue()
pingValueCU.RunCU()
pingValueCT.RunCT()
pingValueCM.RunCM()
netSpeed.Run()
for {
clientInfo.MemoryTotal = ram.Info().Total / 1024 // 需要转单位
clientInfo.MemoryUsed = ram.Info().Usage / 1024 // 需要转单位
clientInfo.CPU = cpu.Info().Usage
clientInfo.Uptime = host.Info().Uptime
//swap 没有造好的轮子,自己加的
swapMemory, _ := mem.SwapMemory()
clientInfo.SwapTotal = swapMemory.Total / 1024 // 需要转单位
@ -327,18 +368,18 @@ func main() {
clientInfo.Ping10086, clientInfo.Time10086 = pingValueCM.Get()
clientInfo.Ping189, clientInfo.Time189 = pingValueCT.Get()
clientInfo.Ping10010, clientInfo.Time10010 = pingValueCU.Get()
//fmt.Println(clientInfo.Time10086)
//结构体转json字符串
data, err := jsoniter.MarshalToString(&clientInfo)
//fmt.Println(data)
if err != nil {
fmt.Println("Transformation Error: ", err)
break
}
info := "update " + data + "\n"
//fmt.Println(info)
_ , err = conn.Write(str2bytes(info))
_ , err = mainConnect.Write(str2bytes(info))
if err != nil {
fmt.Println("Error Sending Data Info:", err)
break
}
}
}

View File

@ -37,6 +37,7 @@ func (netSpeed *NetSpeed) Run() {
t := time.NewTicker(t1)
for {
select {
case <- netSpeed.stop:
t.Stop()
return

View File

@ -28,6 +28,7 @@ func (pingValue *PingValue) RunCU() {
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
var lostConnect = false
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
@ -39,12 +40,16 @@ func (pingValue *PingValue) RunCU() {
pingValue.mtx.Lock()
t := time.Now()
conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
defer conn.Close()
if err != nil {
fmt.Println("Error try to connect China unicom :", err)
conn.Close()
fmt.Println("[ping]Error try to connect China unicom :", err)
lostConnect = true
lostPacket += 1
} else {
}
tcpconn, ok := conn.(*net.TCPConn)
if ok {
tcpconn.SetLinger(0)
}
if conn != nil {
conn.Close()
}
diffTime := time.Since(t)
@ -52,10 +57,17 @@ func (pingValue *PingValue) RunCU() {
//fmt.Println(diffTime)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket)
pingValue.lostRate = float64(lostPacket/allPacket) * 100
}
//fmt.Println("ALL LOST RATE")
//fmt.Printf("%10d %10d %10f\n",allPacket,lostPacket,pingValue.lostRate)
if lostConnect {
pingValue.ping = 0
} else {
pingValue.ping = uint64(diffTime/time.Millisecond)
resetTime := uint64(time.Since(startTime) * time.Second)
}
lostConnect = false
resetTime := uint64(time.Since(startTime) / time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0
@ -73,6 +85,7 @@ func (pingValue *PingValue) RunCT() {
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
var lostConnect = false
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
@ -84,21 +97,30 @@ func (pingValue *PingValue) RunCT() {
pingValue.mtx.Lock()
t := time.Now()
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
defer conn.Close()
if err != nil {
fmt.Println("Error try to connect China Telecom :", err)
conn.Close()
fmt.Println("[ping]Error try to connect China Telecom :", err)
lostConnect = true
lostPacket += 1
} else {
}
tcpconn, ok := conn.(*net.TCPConn)
if ok {
tcpconn.SetLinger(0)
}
if conn != nil {
conn.Close()
}
diffTime := time.Since(t)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket)
pingValue.lostRate = float64(lostPacket/allPacket) * 100
}
if lostConnect {
pingValue.ping = 0
} else {
pingValue.ping = uint64(diffTime/time.Millisecond)
resetTime := uint64(time.Since(startTime) * time.Second)
}
lostConnect = false
resetTime := uint64(time.Since(startTime) / time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0
@ -116,6 +138,7 @@ func (pingValue *PingValue) RunCM() {
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
var lostConnect = false
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
@ -126,22 +149,31 @@ func (pingValue *PingValue) RunCM() {
case <-t.C:
pingValue.mtx.Lock()
t := time.Now()
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
defer conn.Close()
conn , err := net.DialTimeout("tcp",CM_ADDR,defaulttimeout)
if err != nil {
fmt.Println("Error try to connect China mobile :", err)
conn.Close()
fmt.Println("[ping]Error try to connect China mobile :", err)
lostConnect = true
lostPacket += 1
} else {
}
tcpconn, ok := conn.(*net.TCPConn)
if ok {
tcpconn.SetLinger(0)
}
if conn != nil {
conn.Close()
}
diffTime := time.Since(t)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket)
pingValue.lostRate = float64(lostPacket/allPacket) * 100
}
if lostConnect {
pingValue.ping = 0
} else {
pingValue.ping = uint64(diffTime/time.Millisecond)
resetTime := uint64(time.Since(startTime) * time.Second)
}
lostConnect = false
resetTime := uint64(time.Since(startTime) / time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0