1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

feat: use tcp handshake to simulate icmp echo

This commit is contained in:
fumiama 2021-12-26 13:32:51 +08:00
parent d72696a0c8
commit b8023f445a
3 changed files with 14 additions and 35 deletions

View File

@ -67,10 +67,10 @@ func (c *QQClient) ConnectionQualityTest() *ConnectionQualityInfo {
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
res := utils.RunICMPPingLoop(c.servers[c.currServerIndex].IP.String(), 10) res := utils.RunICMPPingLoop(c.servers[c.currServerIndex].String(), 10)
r.ChatServerPacketLoss = res.PacketsLoss r.ChatServerPacketLoss = res.PacketsLoss
if c.highwaySession.AddrLength() > 0 { if c.highwaySession.AddrLength() > 0 {
res = utils.RunICMPPingLoop(c.highwaySession.SsoAddr[0].AsNetIP().String(), 10) res = utils.RunICMPPingLoop(c.highwaySession.SsoAddr[0].String(), 10)
r.SrvServerPacketLoss = res.PacketsLoss r.SrvServerPacketLoss = res.PacketsLoss
} }
}() }()

View File

@ -1,10 +1,8 @@
package utils package utils
import ( import (
"errors"
"math/rand"
"net" "net"
"strconv" "strings"
"time" "time"
) )
@ -14,8 +12,8 @@ type ICMPPingResult struct {
AvgTimeMill int64 AvgTimeMill int64
} }
// RunICMPPingLoop unix 下的 ping // RunICMPPingLoop tcp 伪装的 icmp
func RunICMPPingLoop(ip string, count int) (r ICMPPingResult) { func RunICMPPingLoop(ipport string, count int) (r ICMPPingResult) {
r = ICMPPingResult{ r = ICMPPingResult{
PacketsSent: count, PacketsSent: count,
PacketsLoss: count, PacketsLoss: count,
@ -26,7 +24,7 @@ func RunICMPPingLoop(ip string, count int) (r ICMPPingResult) {
} }
durs := make([]int64, 0, count) durs := make([]int64, 0, count)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
d, err := pingudp(ip) d, err := pingtcp(ipport)
if err == nil { if err == nil {
r.PacketsLoss-- r.PacketsLoss--
durs = append(durs, d) durs = append(durs, d)
@ -46,34 +44,15 @@ func RunICMPPingLoop(ip string, count int) (r ICMPPingResult) {
return return
} }
func pingudp(ip string) (int64, error) { func pingtcp(ipport string) (int64, error) {
var buf [256]byte
ch := make(chan error, 1)
port := rand.Intn(10000) + 50000
conn, err := net.Dial("udp", ip+":"+strconv.Itoa(port))
if err != nil {
return 9999, err
}
t := time.Now().UnixMilli() t := time.Now().UnixMilli()
conn, err := net.DialTimeout("tcp", ipport, time.Second*2)
_, err = conn.Write([]byte("fill"))
if err != nil { if err != nil {
return 0, err if strings.Contains(err.Error(), "timeout") {
} return 9999, err
go func() { }
_, err := conn.Read(buf[:]) } else {
ch <- err _ = conn.Close()
}()
select {
case <-time.NewTimer(time.Second * 4).C:
err = errors.New("timeout")
case err = <-ch:
}
if err != nil && err.Error() == "timeout" {
return 9999, err
} }
return time.Now().UnixMilli() - t, nil return time.Now().UnixMilli() - t, nil
} }

View File

@ -5,7 +5,7 @@ import (
) )
func TestPing(t *testing.T) { func TestPing(t *testing.T) {
r := RunICMPPingLoop("127.0.0.1", 4) r := RunICMPPingLoop("127.0.0.1:23333", 4)
if r.PacketsLoss == r.PacketsSent { if r.PacketsLoss == r.PacketsSent {
t.Fatal(r) t.Fatal(r)
} }