Compare commits

..

5 Commits

Author SHA1 Message Date
Leo
95f8cef54c small improvement 2023-01-24 14:47:02 +08:00
Leo
99ad5649a0 fix: filter switch-case condition 2023-01-24 08:51:53 +08:00
sjlleo
4bc4f1c176 Merge pull request #64 from tsosunchia/main
improve: 在本地对部分IP做了过滤
2023-01-24 05:37:59 +08:00
tsosunchia
b663e69343 improve: 在本地对部分IP做了过滤
IANA Reserved Address Space
2023-01-24 01:37:29 +08:00
Leo
7619e74152 feat: 添加 report 模式 2023-01-23 21:58:22 +08:00
6 changed files with 113 additions and 9 deletions

2
.gitignore vendored
View File

@@ -161,3 +161,5 @@ Network Trash Folder
Temporary Items
.apdisk
# compile target directory
dist/

View File

@@ -39,7 +39,8 @@ func Excute() {
dataOrigin := parser.Selector("d", "data-provider", []string{"IP.SB", "IPInfo", "IPInsight", "IPAPI.com"}, &argparse.Options{Default: "LeoMoeAPI",
Help: "Choose IP Geograph Data Provider [LeoMoeAPI,IP.SB, IPInfo, IPInsight, IPAPI.com]"})
noRdns := parser.Flag("n", "no-rdns", &argparse.Options{Help: " Do not resolve IP addresses to their domain names"})
routePath := parser.Flag("r", "route-path", &argparse.Options{Help: "Print traceroute hop path by ASN and location"})
routePath := parser.Flag("P", "route-path", &argparse.Options{Help: "Print traceroute hop path by ASN and location"})
report := parser.Flag("r", "report", &argparse.Options{Help: "output using report mode"})
output := parser.Flag("o", "output", &argparse.Options{Help: "Write trace result to file (RealTimePrinter ONLY)"})
tablePrint := parser.Flag("t", "table", &argparse.Options{Help: "Output trace results as table"})
classicPrint := parser.Flag("c", "classic", &argparse.Options{Help: "Classic Output trace results like BestTrace"})
@@ -168,7 +169,9 @@ func Excute() {
}
}
} else {
conf.AsyncPrinter = printer.TracerouteTablePrinter
if !*report {
conf.AsyncPrinter = printer.TracerouteTablePrinter
}
}
res, err := trace.Traceroute(m, conf)

86
ipgeo/ipfilter.go Normal file
View File

@@ -0,0 +1,86 @@
package ipgeo
import (
"net"
)
func cidrRangeContains(cidrRange string, checkIP string) bool {
_, ipNet, err := net.ParseCIDR(cidrRange)
if err != nil {
return false
}
secondIP := net.ParseIP(checkIP)
return ipNet.Contains(secondIP)
}
// 被选到的返回 geodata, true 否则返回 nil, false
func Filter(ip string) (*IPGeoData, bool) {
//geodata := &IPGeoData{}
asn := ""
whois := ""
isFiltered := false
switch {
//rfc1918
case net.ParseIP(ip).IsPrivate():
asn = ""
whois = "RFC1918"
isFiltered = true
//IANA Reserved Address Space
case cidrRangeContains("100.64.0.0/10", ip):
asn = ""
whois = "RFC6598"
isFiltered = true
case cidrRangeContains("198.18.0.0/15", ip):
asn = ""
whois = "RFC2544"
isFiltered = true
case cidrRangeContains("198.51.100.0/24", ip):
fallthrough
case cidrRangeContains("203.0.113.0/24", ip):
asn = ""
whois = "RFC5737"
isFiltered = true
case cidrRangeContains("240.0.0.0/4", ip):
asn = ""
whois = "RFC1112"
isFiltered = true
//Defense Information System Network
case cidrRangeContains("6.0.0.0/8", ip):
fallthrough
case cidrRangeContains("7.0.0.0/8", ip):
fallthrough
case cidrRangeContains("11.0.0.0/8", ip):
fallthrough
case cidrRangeContains("21.0.0.0/8", ip):
fallthrough
case cidrRangeContains("22.0.0.0/8", ip):
fallthrough
case cidrRangeContains("26.0.0.0/8", ip):
fallthrough
case cidrRangeContains("28.0.0.0/8", ip):
fallthrough
case cidrRangeContains("29.0.0.0/8", ip):
fallthrough
case cidrRangeContains("30.0.0.0/8", ip):
fallthrough
case cidrRangeContains("33.0.0.0/8", ip):
fallthrough
case cidrRangeContains("55.0.0.0/8", ip):
fallthrough
case cidrRangeContains("214.0.0.0/8", ip):
fallthrough
case cidrRangeContains("215.0.0.0/8", ip):
asn = ""
whois = "DOD"
isFiltered = true
default:
}
if !isFiltered {
return nil, false
} else {
return &IPGeoData{
Asnumber: asn,
Whois: whois,
}, true
}
}

View File

@@ -74,12 +74,13 @@ func formatIpGeoData(ip string, data *ipgeo.IPGeoData) string {
// TODO: 判断阿里云和腾讯云内网,数据不足,有待进一步完善
// TODO: 移动IDC判断到Hop.fetchIPData函数减少API调用
if strings.HasPrefix(ip, "9.") {
res = append(res, "LAN Address")
} else if strings.HasPrefix(ip, "11.") {
res = append(res, "LAN Address")
} else if data.Country == "" {
res = append(res, "LAN Address")
//if strings.HasPrefix(ip, "9.") {
// res = append(res, "LAN Address")
//} else if strings.HasPrefix(ip, "11.") {
// res = append(res, "LAN Address")
//} else if data.Country == "" {
// res = append(res, "LAN Address")
if false {
} else {
// 有些IP的归属信息为空这个时候将ISP的信息填入
if data.Owner == "" {

View File

@@ -72,6 +72,10 @@ func RealtimePrinter(res *trace.Result, ttl int) {
fallthrough
case res.Hops[ttl][i].Geo.Asnumber == "9929":
fallthrough
case res.Hops[ttl][i].Geo.Asnumber == "23764":
fallthrough
case res.Hops[ttl][i].Geo.Whois == "CMIN2-NET":
fallthrough
case strings.HasPrefix(res.Hops[ttl][i].Address.String(), "59.43."):
fmt.Fprintf(color.Output, " %s", color.New(color.FgHiYellow, color.Bold).Sprintf("AS%-6s", res.Hops[ttl][i].Geo.Asnumber))
default:
@@ -102,10 +106,14 @@ func RealtimePrinter(res *trace.Result, ttl int) {
fallthrough
case res.Hops[ttl][i].Geo.Asnumber == "9929":
fallthrough
case res.Hops[ttl][i].Geo.Asnumber == "23764":
fallthrough
case whoisFormat[0] == "[CNC-BACKBONE]":
fallthrough
case whoisFormat[0] == "[CUG-BACKBONE]":
fallthrough
case whoisFormat[0] == "[CMIN2-NET]":
fallthrough
case strings.HasPrefix(res.Hops[ttl][i].Address.String(), "59.43."):
fmt.Fprintf(color.Output, " %s", color.New(color.FgHiYellow, color.Bold).Sprintf("%-16s", whoisFormat[0]))
default:

View File

@@ -140,7 +140,11 @@ func (h *Hop) fetchIPData(c Config) (err error) {
}
}
if c.IPGeoSource != nil && h.Geo == nil {
h.Geo, err = c.IPGeoSource(h.Address.String())
res := false
h.Geo, res = ipgeo.Filter(h.Address.String())
if !res {
h.Geo, err = c.IPGeoSource(h.Address.String())
}
}
return
}