外网测试问题仍在处理,修正一些小问题

pull/92/head
CHN-STUDENT 2020-12-23 11:08:15 +08:00
parent 2441534a93
commit 4aeb26f915
4 changed files with 110 additions and 90 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 ### TODO
- IPV4 和 IPV6 状态
- 外网连接状态
- 三网丢包计算
- 三网延迟计算
- 其他算法优化 - 其他算法优化
### 鸣谢 ### 鸣谢

View File

@ -10,6 +10,9 @@ package main
import ( import (
"fmt" "fmt"
"os/signal"
"syscall"
//下面这是已经封装好的轮子 //下面这是已经封装好的轮子
"github.com/bitcav/nitr-core/cpu" "github.com/bitcav/nitr-core/cpu"
"github.com/bitcav/nitr-core/host" "github.com/bitcav/nitr-core/host"
@ -35,9 +38,9 @@ var (
PASSWORD string = "123456" PASSWORD string = "123456"
INTERVAL int = 1 INTERVAL int = 1
PORBEPORT int = 80 PORBEPORT int = 80
CU string = "cu.tz.cloudcpp.com" CU string = "cu.tz.cloudcpp.com" //120.52.99.224 河北联通
CT string = "ct.tz.cloudcpp.com" CT string = "ct.tz.cloudcpp.com" //183.78.182.66 北京电信
CM string = "cm.tz.cloudcpp.com" CM string = "cm.tz.cloudcpp.com" //211.139.145.129 广州移动
) )
type ClientInfo struct { type ClientInfo struct {
@ -147,13 +150,14 @@ func spaceCount() {
strings.Index(fsType, "fat32") < 0 && strings.Index(fsType, "fat32") < 0 &&
strings.Index(fsType, "exfat") < 0 && strings.Index(fsType, "exfat") < 0 &&
strings.Index(fsType, "xfs") < 0 { strings.Index(fsType, "xfs") < 0 {
//if(d.Device == "A") { //特殊盘符自己写处理
continue
//}
} else { } else {
diskUsageOf, _ := disk.Usage(d.Mountpoint) if strings.Index(d.Device, "Z:") > -1 { //特殊盘符自己写处理
used += diskUsageOf.Used continue
total += diskUsageOf.Total } else {
diskUsageOf, _ := disk.Usage(d.Mountpoint)
used += diskUsageOf.Used
total += diskUsageOf.Total
}
} }
} }
clientInfo.HddUsed = used / 1024.0 / 1024.0 clientInfo.HddUsed = used / 1024.0 / 1024.0
@ -186,32 +190,26 @@ func getNetworkStatus() {
defaulttimeout := 1 * time.Second defaulttimeout := 1 * time.Second
count := 0 count := 0
conn1 , err1 := net.DialTimeout("tcp",CU_ADDR,defaulttimeout) conn1 , err1 := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
defer conn1.Close()
if err1 != nil { if err1 != nil {
fmt.Println("Error try to connect China unicom :", err1) fmt.Println("Error try to connect China unicom :", err1)
count += 1 count += 1
conn1.Close() return
} else {
conn1.Close()
} }
defer conn1.Close()
conn2 , err2 := net.DialTimeout("tcp", CT_ADDR,defaulttimeout) conn2 , err2 := net.DialTimeout("tcp", CT_ADDR,defaulttimeout)
defer conn2.Close()
if err2 != nil { if err2 != nil {
fmt.Println("Error try to connect China telecom :", err2) fmt.Println("Error try to connect China telecom :", err2)
count += 1 count += 1
conn2.Close() return
} else {
conn2.Close()
} }
defer conn2.Close()
conn3 , err3 := net.DialTimeout("tcp", CM_ADDR,defaulttimeout) conn3 , err3 := net.DialTimeout("tcp", CM_ADDR,defaulttimeout)
defer conn3.Close()
if err3 != nil { if err3 != nil {
fmt.Println("Error try to connect China mobile :", err3) fmt.Println("Error try to connect China mobile :", err3)
count += 1 count += 1
conn3.Close() return
} else {
conn3.Close()
} }
defer conn3.Close()
if count >= 2 { if count >= 2 {
clientInfo.IpStatus = false clientInfo.IpStatus = false
} else { } else {
@ -232,8 +230,33 @@ func bytes2str(b []byte) string {
var clientInfo ClientInfo 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() { func main() {
// Setup our Ctrl+C handler
SetupCloseHandler()
for _, args := range os.Args { for _, args := range os.Args {
if strings.Index(args,"SERVER") > -1 { if strings.Index(args,"SERVER") > -1 {
strArr := strings.Split(args,"SERVER=") strArr := strings.Split(args,"SERVER=")
@ -253,25 +276,37 @@ func main() {
} }
} }
defaulttimeout := 30 * time.Second defaulttimeout := 30 * time.Second
clientInfo = NewDefaultClientInfo()
netSpeed = NewNetSpeed()
pingValueCU = NewPingValue()
pingValueCT = NewPingValue()
pingValueCM = NewPingValue()
//pingValueCU.RunCU()
//pingValueCT.RunCT()
//pingValueCM.RunCM()
netSpeed.Run()
for { 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 { if err != nil {
fmt.Println("Error listening:", err) fmt.Println("Error listening:", err)
} }
defer conn.Close() defer mainConnect.Close()
buff := make([]byte, 1024) buff := make([]byte, 1024)
conn.Read(buff) mainConnect.Read(buff)
str := bytes2str(buff) str := bytes2str(buff)
if strings.Index(str,"Authentication required") > -1 { if strings.Index(str,"Authentication required") > -1 {
auth := str2bytes(USER + ":" + PASSWORD + "\n") auth := str2bytes(USER + ":" + PASSWORD + "\n")
_ , err = conn.Write(auth) _ , err = mainConnect.Write(auth)
if err != nil { if err != nil {
fmt.Println("Error Sending auth info:", err) fmt.Println("Error Sending auth info:", err)
return
} }
buff = make([]byte, 1024) buff = make([]byte, 1024)
_ , err = conn.Read(buff) _ , err = mainConnect.Read(buff)
if err != nil { if err != nil {
fmt.Println("Error Getting Server Data:", err) fmt.Println("Error Getting Server Data:", err)
return
} }
str = bytes2str(buff) str = bytes2str(buff)
if strings.Index(str,"Authentication required") < 0 { if strings.Index(str,"Authentication required") < 0 {
@ -283,9 +318,10 @@ func main() {
fmt.Println(str) fmt.Println(str)
if strings.Index(str,"You are connecting via") < 0 { if strings.Index(str,"You are connecting via") < 0 {
buff = make([]byte, 1024) buff = make([]byte, 1024)
_ , err = conn.Read(buff) _ , err = mainConnect.Read(buff)
if err != nil { if err != nil {
fmt.Println("Error Getting Server Data:", err) fmt.Println("Error Getting Server Data:", err)
return
} }
str = bytes2str(buff) str = bytes2str(buff)
fmt.Println(str) fmt.Println(str)
@ -299,21 +335,11 @@ func main() {
fmt.Println(str) fmt.Println(str)
} }
fmt.Println(checkIP) fmt.Println(checkIP)
clientInfo = NewDefaultClientInfo()
netSpeed := NewNetSpeed()
pingValueCU := NewPingValue()
pingValueCT := NewPingValue()
pingValueCM := NewPingValue()
pingValueCU.RunCU()
pingValueCT.RunCT()
pingValueCM.RunCM()
netSpeed.Run()
for { for {
clientInfo.MemoryTotal = ram.Info().Total / 1024 // 需要转单位 clientInfo.MemoryTotal = ram.Info().Total / 1024 // 需要转单位
clientInfo.MemoryUsed = ram.Info().Usage / 1024 // 需要转单位 clientInfo.MemoryUsed = ram.Info().Usage / 1024 // 需要转单位
clientInfo.CPU = cpu.Info().Usage clientInfo.CPU = cpu.Info().Usage
clientInfo.Uptime = host.Info().Uptime clientInfo.Uptime = host.Info().Uptime
//swap 没有造好的轮子,自己加的 //swap 没有造好的轮子,自己加的
swapMemory, _ := mem.SwapMemory() swapMemory, _ := mem.SwapMemory()
clientInfo.SwapTotal = swapMemory.Total / 1024 // 需要转单位 clientInfo.SwapTotal = swapMemory.Total / 1024 // 需要转单位
@ -322,7 +348,7 @@ func main() {
tupd() tupd()
trafficCount() trafficCount()
spaceCount() spaceCount()
getNetworkStatus() //getNetworkStatus()
netSpeed.Get() netSpeed.Get()
clientInfo.Ping10086, clientInfo.Time10086 = pingValueCM.Get() clientInfo.Ping10086, clientInfo.Time10086 = pingValueCM.Get()
clientInfo.Ping189, clientInfo.Time189 = pingValueCT.Get() clientInfo.Ping189, clientInfo.Time189 = pingValueCT.Get()
@ -333,12 +359,14 @@ func main() {
//fmt.Println(data) //fmt.Println(data)
if err != nil { if err != nil {
fmt.Println("Transformation Error: ", err) fmt.Println("Transformation Error: ", err)
return
} }
info := "update " + data + "\n" info := "update " + data + "\n"
//fmt.Println(info) //fmt.Println(info)
_ , err = conn.Write(str2bytes(info)) _ , err = mainConnect.Write(str2bytes(info))
if err != nil { if err != nil {
fmt.Println("Error Sending Data Info:", err) fmt.Println("Error Sending Data Info:", err)
return
} }
} }
} }

View File

@ -37,40 +37,41 @@ func (netSpeed *NetSpeed) Run() {
t := time.NewTicker(t1) t := time.NewTicker(t1)
for { for {
select { select {
case <- netSpeed.stop:
t.Stop() case <- netSpeed.stop:
return t.Stop()
case <-t.C: return
netSpeed.mtx.Lock() case <-t.C:
var bytesSent uint64 = 0 netSpeed.mtx.Lock()
var bytesRecv uint64 = 0 var bytesSent uint64 = 0
netInfo, err := nnet.IOCounters(true) var bytesRecv uint64 = 0
if err != nil { netInfo, err := nnet.IOCounters(true)
fmt.Println("Get network speed error:",err) if err != nil {
} fmt.Println("Get network speed error:",err)
for _, v := range netInfo {
if strings.Index(v.Name,"lo") > -1 ||
strings.Index(v.Name,"tun") > -1 ||
strings.Index(v.Name,"docker") > -1 ||
strings.Index(v.Name,"veth") > -1 ||
strings.Index(v.Name,"br-") > -1 ||
strings.Index(v.Name,"vmbr") > -1 ||
strings.Index(v.Name,"vnet") > -1 ||
strings.Index(v.Name,"kube") > -1 {
continue
} }
bytesSent += v.BytesSent for _, v := range netInfo {
bytesRecv += v.BytesRecv if strings.Index(v.Name,"lo") > -1 ||
strings.Index(v.Name,"tun") > -1 ||
strings.Index(v.Name,"docker") > -1 ||
strings.Index(v.Name,"veth") > -1 ||
strings.Index(v.Name,"br-") > -1 ||
strings.Index(v.Name,"vmbr") > -1 ||
strings.Index(v.Name,"vnet") > -1 ||
strings.Index(v.Name,"kube") > -1 {
continue
}
bytesSent += v.BytesSent
bytesRecv += v.BytesRecv
}
timeUnix:= float64(time.Now().Unix())
netSpeed.diff = timeUnix - netSpeed.clock
netSpeed.clock = timeUnix
netSpeed.netrx = float64(bytesRecv - netSpeed.avgrx)/netSpeed.diff
netSpeed.nettx = float64(bytesSent - netSpeed.avgtx)/netSpeed.diff
netSpeed.avgtx = bytesSent
netSpeed.avgrx = bytesRecv
netSpeed.mtx.Unlock()
} }
timeUnix:= float64(time.Now().Unix())
netSpeed.diff = timeUnix - netSpeed.clock
netSpeed.clock = timeUnix
netSpeed.netrx = float64(bytesRecv - netSpeed.avgrx)/netSpeed.diff
netSpeed.nettx = float64(bytesSent - netSpeed.avgtx)/netSpeed.diff
netSpeed.avgtx = bytesSent
netSpeed.avgrx = bytesRecv
netSpeed.mtx.Unlock()
}
} }
}() }()
} }

View File

@ -39,14 +39,12 @@ func (pingValue *PingValue) RunCU() {
pingValue.mtx.Lock() pingValue.mtx.Lock()
t := time.Now() t := time.Now()
conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout) conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
//defer conn.Close()
if err != nil { if err != nil {
fmt.Println("Error try to connect China unicom :", err) fmt.Println("Error try to connect China unicom :", err)
_ = conn.Close()
lostPacket += 1 lostPacket += 1
} else { return
_ = conn.Close()
} }
defer conn.Close()
diffTime := time.Since(t) diffTime := time.Since(t)
//TODO:三网延迟和丢包率算法存在问题 //TODO:三网延迟和丢包率算法存在问题
//fmt.Println(diffTime) //fmt.Println(diffTime)
@ -84,14 +82,12 @@ func (pingValue *PingValue) RunCT() {
pingValue.mtx.Lock() pingValue.mtx.Lock()
t := time.Now() t := time.Now()
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout) conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
//defer conn.Close()
if err != nil { if err != nil {
fmt.Println("Error try to connect China Telecom :", err) fmt.Println("Error try to connect China Telecom :", err)
_ = conn.Close()
lostPacket += 1 lostPacket += 1
} else { return
_ = conn.Close()
} }
defer conn.Close()
diffTime := time.Since(t) diffTime := time.Since(t)
allPacket += 1 allPacket += 1
if allPacket > 100 { if allPacket > 100 {
@ -127,14 +123,12 @@ func (pingValue *PingValue) RunCM() {
pingValue.mtx.Lock() pingValue.mtx.Lock()
t := time.Now() t := time.Now()
conn , err := net.DialTimeout("tcp",CM_ADDR,defaulttimeout) conn , err := net.DialTimeout("tcp",CM_ADDR,defaulttimeout)
//defer conn.Close()
if err != nil { if err != nil {
fmt.Println("Error try to connect China mobile :", err) fmt.Println("Error try to connect China mobile :", err)
_ = conn.Close()
lostPacket += 1 lostPacket += 1
} else { return
_ = conn.Close()
} }
defer conn.Close()
diffTime := time.Since(t) diffTime := time.Since(t)
allPacket += 1 allPacket += 1
if allPacket > 100 { if allPacket > 100 {