-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracert.go
61 lines (54 loc) · 1.24 KB
/
tracert.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
package main
import (
"errors"
"fmt"
"net"
"os"
)
// LookupHostIP 解析 Host 对应的 IP 地址
func LookupHostIP(host string) ([]net.IPAddr, error) {
ip46 := make([]net.IPAddr, 2)
ips, err := net.LookupIP(host)
if err != nil {
return ip46, err
}
// 获取 ipv4 地址
for _, ip := range ips {
if ip.To4() != nil {
ip46[0].IP = ip
break
}
}
// 获取 ipv6 地址
for _, ip := range ips {
if ip.To16() != nil && ip.To4() == nil {
ip46[1].IP = ip
break
}
}
// 不存在 A AAAA 记录
if ip46[0].IP == nil && ip46[1].IP == nil {
return ip46, errors.New("No A & AAAA Records")
}
return ip46, nil
}
// Tracert 路由追踪
func Tracert(host string, maxhoop int, ttype int) {
ips, err := LookupHostIP(host)
if err != nil {
fmt.Printf("无法解析目标系统名称 %s。\n\n", host)
os.Exit(0)
}
// 根据选项进行追踪 优先 IPv6
if ttype == 4 && ips[0].IP != nil {
Tracert4(host, ips[0], maxhoop)
} else if ttype == 6 && ips[1].IP != nil {
Tracert6(host, ips[1], maxhoop)
} else if ttype == 0 && ips[1].IP != nil {
Tracert6(host, ips[1], maxhoop)
} else if ttype == 0 && ips[0].IP != nil {
Tracert4(host, ips[0], maxhoop)
} else {
fmt.Printf("无法解析目标系统名称 %s。\n\n", host)
}
}