-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathandroid.go
92 lines (86 loc) · 1.93 KB
/
android.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
*@Author: krisjczhang
*@Description:
*@Date: 2023/03/23 09:21
*/
package ztrace
import (
"fmt"
"os/exec"
"strings"
"time"
)
func (t *TraceRoute) ExecCmd() error {
key := GetHash(t.NetSrcAddr.To4(), t.NetDstAddr.To4(), 65535, 65535, 1)
db := NewStatsDB(key)
t.DB.Store(key, db)
go db.Cache.Run()
t.StartTime = time.Now()
lastHop := 30
for c := 1; c <= t.Count; c++ {
for i := 1; i <= lastHop; i++ {
ttl := fmt.Sprintf("-t %d", i)
cmd := NewExecute()
m := &SendMetric{
FlowKey: key,
ID: uint32(i),
TTL: uint8(i),
TimeStamp: time.Now(),
}
t.RecordSend(m)
timeout := time.Millisecond * 200
stdOut, _, err := cmd.RunWithTimeout(timeout, "/system/bin/ping", "-c 1", ttl, "-W 200", t.Dest)
if _, ok := err.(*exec.ExitError); ok {
}
hopIp := t.parseHopIp(stdOut, i)
if hopIp == t.NetDstAddr.String() {
// 减少循环次数
lastHop = i
break
// 设置最后一跳
//t.LastHop = i
}
}
time.Sleep(t.Interval)
if t.IsFinish() {
break
}
}
t.Statistics()
return nil
}
func (t *TraceRoute) parseHopIp(text string, ttl int) string {
key := GetHash(t.NetSrcAddr.To4(), t.NetDstAddr.To4(), 65535, 65535, 1)
var hopIp string
arr := strings.Split(text, "\n")
if len(arr) < 2 {
return "*"
}
/*
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: icmp_seq=0 ttl=107 time=24.922 ms
--- 8.8.8.8 ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 24.922/24.922/24.922/0.000 ms
*/
// 为了取第2行
row := arr[1]
if len(row) == 0 {
return "*"
}
word := strings.Fields(row)
if strings.Contains(row, "ttl") {
// 8.8.8.8:
hopIp = strings.ReplaceAll(word[3], ":", "")
} else {
hopIp = strings.ReplaceAll(word[1], ":", "")
}
recv := &RecvMetric{
FlowKey: key,
ID: uint32(ttl),
RespAddr: hopIp,
TimeStamp: time.Now(),
}
t.RecordRecv(recv)
return hopIp
}