forked from zhanghanyun/backtrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasn.go
66 lines (57 loc) · 1.94 KB
/
asn.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
package main
import (
"fmt"
"github.com/fatih/color"
"net"
"strings"
)
type Result struct {
i int
s string
}
var (
rIp = []string{"219.141.136.12", "202.106.50.1", "221.179.155.161", "202.96.209.133", "210.22.97.1", "211.136.112.200", "58.60.188.222", "210.21.196.6", "120.196.165.24"}
rName = []string{"北京电信", "北京联通", "北京移动", "上海电信", "上海联通", "上海移动", "广州电信", "广州联通", "广州移动"}
ca = []color.Attribute{color.FgHiBlue, color.FgHiMagenta, color.FgHiYellow, color.FgHiGreen, color.FgHiCyan, color.FgHiRed, color.FgHiMagenta, color.FgHiYellow, color.FgHiBlue}
m = map[string]string{"AS4134": "电信163 [普通线路]", "AS4809": "电信CN2 [优质线路]", "AS4837": "联通4837[普通线路]", "AS9929": "联通9929[优质线路]", "AS9808": "移动CMI [普通线路]", "AS58453": "移动CMI [普通线路]"}
)
func trace(ch chan Result, i int) {
hops, err := Trace(net.ParseIP(rIp[i]))
if err != nil {
s := fmt.Sprintf("%v %-15s %v", rName[i], rIp[i], err)
ch <- Result{i, s}
return
}
for _, h := range hops {
for _, n := range h.Nodes {
asn := ipAsn(n.IP.String())
if asn == "" {
continue
} else {
as := m[asn]
c := color.New(ca[i]).Add(color.Bold).SprintFunc()
s := fmt.Sprintf("%v %-15s %-23s", rName[i], rIp[i], c(as))
ch <- Result{i, s}
return
}
}
}
s := fmt.Sprintf("%v %-15s %v", rName[i], rIp[i], "测试超时")
ch <- Result{i, s}
}
func ipAsn(ip string) string {
switch {
case strings.HasPrefix(ip, "59.43"):
return "AS4809"
case strings.HasPrefix(ip, "202.97"):
return "AS4134"
case strings.HasPrefix(ip, "218.105") || strings.HasPrefix(ip, "210.51"):
return "AS9929"
case strings.HasPrefix(ip, "219.158"):
return "AS4837"
case strings.HasPrefix(ip, "223.118") || strings.HasPrefix(ip, "223.119") || strings.HasPrefix(ip, "223.120") || strings.HasPrefix(ip, "223.121"):
return "AS58453"
default:
return ""
}
}