Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
spiritLHLS committed Jul 3, 2024
1 parent 86da486 commit ed1878d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ require (
github.com/imroc/req/v3 v3.43.7
github.com/mattn/go-runewidth v0.0.15
github.com/oneclickvirt/defaultset v0.0.2-20240624082446
github.com/prometheus-community/pro-bing v0.4.0
golang.org/x/net v0.22.0
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/klauspost/compress v1.17.7 // indirect
Expand All @@ -29,8 +28,6 @@ require (
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.19.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7 h1:y3N7Bm7Y9/CtpiVkw/ZWj6lSlDF3F74SfKwfTCer72Q=
github.com/google/pprof v0.0.0-20240227163752-401108e1b7e7/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand All @@ -36,8 +34,6 @@ github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus-community/pro-bing v0.4.0 h1:YMbv+i08gQz97OZZBwLyvmmQEEzyfyrrjEaAchdy3R4=
github.com/prometheus-community/pro-bing v0.4.0/go.mod h1:b7wRYZtCcPmt4Sz319BykUU241rWLe1VFXyiyWK/dH4=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/quic-go v0.41.0 h1:aD8MmHfgqTURWNJy48IYFg2OnxwHT3JL7ahGs73lb4k=
Expand Down Expand Up @@ -66,8 +62,6 @@ golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down
84 changes: 73 additions & 11 deletions pt/pt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pt

import (
"fmt"
"net"
"sort"
"strings"
"sync"
Expand All @@ -10,31 +11,92 @@ import (
"github.com/mattn/go-runewidth"
. "github.com/oneclickvirt/defaultset"
"github.com/oneclickvirt/pingtest/model"
probing "github.com/prometheus-community/pro-bing"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
)

const ICMPProtocolICMP = 1

func pingServer(server *model.Server, wg *sync.WaitGroup) {
if model.EnableLoger {
InitLogger()
defer Logger.Sync()
}
defer wg.Done()

conn, err := icmp.ListenPacket("ip4:icmp", "")
if err != nil {
if model.EnableLoger {
Logger.Info("cannot listen for ICMP packets: " + err.Error())
}
return
}
defer conn.Close()

target := server.IP
pinger, err := probing.NewPinger(target)
dst, err := net.ResolveIPAddr("ip4", target)
if err != nil {
if model.EnableLoger {
Logger.Info("cannot create Pinger: " + err.Error())
Logger.Info("cannot resolve IP address: " + err.Error())
}
return
}
pinger.Count = 3
pinger.Size = 24
pinger.Interval = 500 * time.Millisecond
pinger.Timeout = 3 * time.Second
pinger.TTL = 64
pinger.SetPrivileged(true)
stats := pinger.Statistics() // 获取ping统计信息
server.Avg = stats.AvgRtt

var totalRtt time.Duration
pingCount := 3
for i := 0; i < pingCount; i++ {
msg := icmp.Message{
Type: ipv4.ICMPTypeEcho,
Code: 0,
Body: &icmp.Echo{
ID: i,
Seq: i,
Data: []byte("ping"),
},
}
msgBytes, err := msg.Marshal(nil)
if err != nil {
if model.EnableLoger {
Logger.Info("cannot marshal ICMP message: " + err.Error())
}
return
}

start := time.Now()
_, err = conn.WriteTo(msgBytes, dst)
if err != nil {
if model.EnableLoger {
Logger.Info("cannot send ICMP message: " + err.Error())
}
return
}

reply := make([]byte, 1500)
conn.SetReadDeadline(time.Now().Add(3 * time.Second))
n, _, err := conn.ReadFrom(reply)
if err != nil {
if model.EnableLoger {
Logger.Info("cannot receive ICMP reply: " + err.Error())
}
return
}
duration := time.Since(start)

rm, err := icmp.ParseMessage(ICMPProtocolICMP, reply[:n])
if err != nil {
if model.EnableLoger {
Logger.Info("cannot parse ICMP reply: " + err.Error())
}
return
}

switch rm.Type {
case ipv4.ICMPTypeEchoReply:
totalRtt += duration
}
}

server.Avg = totalRtt / time.Duration(pingCount)
}

func PingTest() string {
Expand Down

0 comments on commit ed1878d

Please sign in to comment.