-
Notifications
You must be signed in to change notification settings - Fork 2
/
gotrace.go
183 lines (164 loc) · 5.24 KB
/
gotrace.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"flag"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"github.com/oschwald/geoip2-golang"
"github.com/timoseven/gotrace/mmdb"
"github.com/elazarl/go-bindata-assetfs"
"log"
"net"
"os"
"time"
// "strconv"
)
func findaddr(ipaddr net.Addr) (country string, asdesc string, asn uint) {
dirs := []string{"resource"} // 设置需要释放的目录
for _, dir := range dirs {
// 解压dir目录到当前目录
if err := asset.RestoreAssets("./", dir); err != nil {
isSuccess = false
break
}
}
if !isSuccess {
for _, dir := range dirs {
os.RemoveAll(filepath.Join("./", dir))
}
}
db, err := geoip2.Open("./resource/GeoLite2-Country.mmdb")
asndb, err := geoip2.Open("./resource/GeoLite2-ASN.mmdb")
if err != nil {
log.Panic(err)
}
defer db.Close()
defer asndb.Close()
// If you are using strings that may be invalid, check that ip is not nil
ip := net.ParseIP(ipaddr.String())
record, err := db.Country(ip)
if err != nil {
log.Panic(err)
}
asrecord, err := asndb.ASN(ip)
if err != nil {
log.Panic(err)
}
asn = asrecord.AutonomousSystemNumber
asdesc = asrecord.AutonomousSystemOrganization
country = record.Country.IsoCode
// }
return
// fmt.Printf("Russian country name: %v\n", record.Country.Names["en"])
// fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)
// fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
// fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)
// Output:
// Portuguese (BR) city name: Londres
// English subdivision name: England
// Russian country name: Великобритания
// ISO country code: GB
// Time zone: Europe/London
// Coordinates: 51.5142, -0.0931
}
func main() {
dirs := []string{"resource"} // 设置需要释放的目录
for _, dir := range dirs {
// 解压dir目录到当前目录
if err := asset.RestoreAssets("./", dir); err != nil {
isSuccess = false
break
}
}
if !isSuccess {
for _, dir := range dirs {
os.RemoveAll(filepath.Join("./", dir))
}
}
// Tracing an IP packet route to www.baidu.com.
// const host = "www.baidu.com"
var host string
flag.StringVar(&host, "H", "www.baidu.com", "默认www.baidu.com")
flag.Parse()
ips, err := net.LookupIP(host)
if err != nil {
log.Fatal(err)
}
var dst net.IPAddr
for _, ip := range ips {
if ip.To4() != nil {
dst.IP = ip
fmt.Printf("using %v for tracing an IP packet route to %s\n", dst.IP, host)
break
}
}
if dst.IP == nil {
log.Fatal("no A record found")
}
c, err := net.ListenPacket("ip4:1", "0.0.0.0") // ICMP for IPv4
if err != nil {
log.Fatal(err)
}
defer c.Close()
p := ipv4.NewPacketConn(c)
if err := p.SetControlMessage(ipv4.FlagTTL|ipv4.FlagSrc|ipv4.FlagDst|ipv4.FlagInterface, true); err != nil {
log.Fatal(err)
}
wm := icmp.Message{
Type: ipv4.ICMPTypeEcho, Code: 0,
Body: &icmp.Echo{
ID: os.Getpid() & 0xffff,
Data: []byte("HELLO-R-U-THERE"),
},
}
rb := make([]byte, 1500)
for i := 1; i <= 64; i++ { // up to 64 hops
wm.Body.(*icmp.Echo).Seq = i
wb, err := wm.Marshal(nil)
if err != nil {
log.Fatal(err)
}
if err := p.SetTTL(i); err != nil {
log.Fatal(err)
}
// In the real world usually there are several
// multiple traffic-engineered paths for each hop.
// You may need to probe a few times to each hop.
begin := time.Now()
if _, err := p.WriteTo(wb, nil, &dst); err != nil {
log.Fatal(err)
}
if err := p.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil {
log.Fatal(err)
}
n, _, peer, err := p.ReadFrom(rb)
if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() {
fmt.Printf("%v\t*\tNULL\n", i)
continue
}
log.Fatal(err)
}
rm, err := icmp.ParseMessage(1, rb[:n])
if err != nil {
log.Fatal(err)
}
rtt := time.Since(begin)
// In the real world you need to determine whether the
// received message is yours using ControlMessage.Src,
// ControlMessage.Dst, icmp.Echo.ID and icmp.Echo.Seq.
switch rm.Type {
case ipv4.ICMPTypeTimeExceeded:
names, _ := net.LookupAddr(peer.String())
fcountry, fasdesc, fasn := findaddr(peer)
fmt.Printf("%d\t%v %+v %v\tCountry_code:%v\tAS:%d\t%v\n", i, peer, names, rtt, fcountry, fasn, fasdesc)
case ipv4.ICMPTypeEchoReply:
names, _ := net.LookupAddr(peer.String())
fcountry, fasdesc, fasn := findaddr(peer)
fmt.Printf("%d\t%v %+v %v\tCountry_code:%v\tAS:%d\t%v\n", i, peer, names, rtt, fcountry, fasn, fasdesc)
return
default:
log.Printf("unknown ICMP message: %+v\n", rm)
}
}
}