Skip to content

Commit febe50f

Browse files
committed
refactor: Ping 由原来的tcping 改为 ICMP
1 parent ac28403 commit febe50f

5 files changed

Lines changed: 84 additions & 48 deletions

File tree

client/client.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/shirou/gopsutil/load"
88
"log"
99
"net"
10+
"strconv"
1011
"strings"
1112
"sync"
1213
"time"
@@ -22,10 +23,9 @@ const DefaultProtocol = "ip4"
2223
const PingPacketHistoryLen = 100
2324
const TimeOut = time.Second * 3
2425

25-
const ProbePort = 80
26-
const PingCu = "cu.tz.cloudcpp.com"
27-
const PingCt = "ct.tz.cloudcpp.com"
28-
const PingCm = "cm.tz.cloudcpp.com"
26+
const PingCu = "cu.tz.vizan.cc"
27+
const PingCt = "ct.tz.vizan.cc"
28+
const PingCm = "cm.tz.vizan.cc"
2929

3030
type Client struct {
3131
Server string
@@ -156,13 +156,13 @@ func (c *Client) getUpdateInfo() update {
156156
c.waitGroup.Add(1)
157157
go c.GetNetRate(ret)
158158

159-
ret.Ping10086 = c.getLostPacket("10086")
160-
ret.Ping10010 = c.getLostPacket("10010")
161-
ret.Ping189 = c.getLostPacket("189")
159+
ret.PingCM = c.getLostPacket("cm")
160+
ret.PingCU = c.getLostPacket("cu")
161+
ret.PingCT = c.getLostPacket("ct")
162162

163-
ret.Time10086 = c.getPingTime("10086")
164-
ret.Time10010 = c.getPingTime("10010")
165-
ret.Time189 = c.getPingTime("189")
163+
ret.TimeCM = c.getPingTime("cm")
164+
ret.TimeCU = c.getPingTime("cu")
165+
ret.TimeCT = c.getPingTime("ct")
166166

167167
c.waitGroup.Add(1)
168168
go c.getDiskIo(ret)

client/ping.go

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package client
22

33
import (
4-
"fmt"
5-
"net"
4+
"github.com/go-ping/ping"
5+
"log"
66
"sync"
77
"time"
88
)
99

10-
var lostPacket10086 = newLostPacket(PingPacketHistoryLen)
11-
var lostPacket10010 = newLostPacket(PingPacketHistoryLen)
12-
var lostPacket189 = newLostPacket(PingPacketHistoryLen)
10+
var CmLostPacket = newLostPacket(PingPacketHistoryLen) // 移动
11+
var CuLostPacket = newLostPacket(PingPacketHistoryLen) // 联通
12+
var CtLostPacket = newLostPacket(PingPacketHistoryLen) // 电信
1313
var pingHost = sync.Map{}
1414

1515
func init() {
16-
pingHost.Store("10010", PingCu)
17-
pingHost.Store("10086", PingCm)
18-
pingHost.Store("189", PingCt)
16+
pingHost.Store("cu", PingCu)
17+
pingHost.Store("cm", PingCm)
18+
pingHost.Store("ct", PingCt)
1919
}
2020

2121
func (c *Client) getPingTime(host string) uint {
@@ -32,54 +32,68 @@ func (c *Client) startPing() {
3232

3333
for range time.Tick(timeout) {
3434
pingHost.Range(func(k, v interface{}) bool {
35-
var ip *net.IPAddr
3635
var lost *lostPacket
3736

3837
host := k.(string)
3938
domain := v.(string)
4039

41-
if c.Protocol == DefaultProtocol {
42-
ip, _ = net.ResolveIPAddr(DefaultProtocol, domain)
40+
if host == "cm" {
41+
lost = &CmLostPacket
42+
} else if host == "cu" {
43+
lost = &CuLostPacket
44+
} else if host == "ct" {
45+
lost = &CtLostPacket
4346
} else {
44-
ip, _ = net.ResolveIPAddr("ip6", domain)
47+
return false
4548
}
4649

47-
if host == "10086" {
48-
lost = &lostPacket10086
49-
} else if host == "10010" {
50-
lost = &lostPacket10010
51-
} else {
52-
lost = &lostPacket189
50+
pinger, err := ping.NewPinger(domain)
51+
if err != nil {
52+
53+
log.Println(err.Error())
54+
return false
5355
}
5456

55-
var start = time.Now()
56-
dial, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, ProbePort), timeout)
57-
if err == nil {
58-
_ = dial.Close()
57+
pinger.Timeout = timeout
58+
pinger.Count = 1
59+
pinger.SetPrivileged(true)
5960

60-
lost.Push(false)
61-
c.pingTime.Store(host, uint(time.Now().Sub(start).Milliseconds()))
61+
if c.Protocol == DefaultProtocol {
62+
pinger.SetNetwork("ipv4")
6263
} else {
64+
pinger.SetNetwork("ipv6")
65+
}
66+
67+
err = pinger.Run()
68+
if err != nil {
6369
lost.Push(true)
70+
71+
log.Println(err.Error())
72+
return false
6473
}
6574

75+
stat := pinger.Statistics()
76+
77+
lost.Push(false)
78+
c.pingTime.Store(host, uint(stat.AvgRtt/time.Millisecond))
79+
6680
return true
6781
})
6882
}
6983
}
7084

71-
func (c *Client) getLostPacket(host string) float64 {
72-
if host == "10086" {
85+
func (c *Client) getLostPacket(isp string) float64 {
86+
if isp == "cm" {
7387

74-
return lostPacket10086.Get()
88+
return CmLostPacket.Get()
7589
}
76-
if host == "10010" {
90+
if isp == "cu" {
7791

78-
return lostPacket10010.Get()
92+
return CuLostPacket.Get()
7993
}
80-
if host == "189" {
94+
if isp == "ct" {
8195

82-
return lostPacket189.Get()
96+
return CtLostPacket.Get()
8397
}
8498

8599
return 0

client/struct.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ type update struct {
3737
IpStatus bool `json:"ip_status"`
3838
NetWorkRx uint64 `json:"network_rx"`
3939
NetWorkTx uint64 `json:"network_tx"`
40-
Ping10010 float64 `json:"ping_10010"`
41-
Ping10086 float64 `json:"ping_10086"`
42-
Ping189 float64 `json:"ping_189"`
43-
Time10010 uint `json:"time_10010"`
44-
Time10086 uint `json:"time_10086"`
45-
Time189 uint `json:"time_189"`
40+
PingCU float64 `json:"ping_10010"`
41+
PingCM float64 `json:"ping_10086"`
42+
PingCT float64 `json:"ping_189"`
43+
TimeCU uint `json:"time_10010"`
44+
TimeCM uint `json:"time_10086"`
45+
TimeCT uint `json:"time_189"`
4646
Tcp uint `json:"tcp"`
4747
Udp uint `json:"udp"`
4848
Process uint `json:"process"`

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ require github.com/shirou/gopsutil v2.21.11+incompatible
66

77
require (
88
github.com/go-ole/go-ole v1.2.6 // indirect
9+
github.com/go-ping/ping v1.1.0 // indirect
10+
github.com/google/uuid v1.3.0 // indirect
911
github.com/stretchr/testify v1.7.1 // indirect
1012
github.com/tklauser/go-sysconf v0.3.10 // indirect
1113
github.com/tklauser/numcpus v0.4.0 // indirect
1214
github.com/yusufpapurcu/wmi v1.2.2 // indirect
13-
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect
15+
golang.org/x/net v0.2.0 // indirect
16+
golang.org/x/sync v0.1.0 // indirect
17+
golang.org/x/sys v0.2.0 // indirect
1418
)

go.sum

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
44
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
5+
github.com/go-ping/ping v1.1.0 h1:3MCGhVX4fyEUuhsfwPrsEdQw6xspHkv5zHsiSoDFZYw=
6+
github.com/go-ping/ping v1.1.0/go.mod h1:xIFjORFzTxqIV/tDVGO4eDy/bLuSyawEeojSm3GfRGk=
7+
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
8+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
9+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
510
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
611
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
712
github.com/shirou/gopsutil v2.21.11+incompatible h1:lOGOyCG67a5dv2hq5Z1BLDUqqKp3HkbjPcz5j6XMS0U=
@@ -15,10 +20,23 @@ github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//
1520
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
1621
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
1722
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
23+
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc=
24+
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
25+
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
26+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
27+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
28+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
1829
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
30+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
31+
golang.org/x/sys v0.0.0-20210315160823-c6e025ad8005/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1932
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2033
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a h1:N2T1jUrTQE9Re6TFF5PhvEHXHCguynGhKjWVsIUt5cY=
2134
golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35+
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
36+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
37+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
38+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
39+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
2240
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2341
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
2442
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)