improve: 在本地对部分IP做了过滤

IANA Reserved Address Space
This commit is contained in:
tsosunchia
2023-01-24 01:37:29 +08:00
parent 7619e74152
commit b663e69343
4 changed files with 95 additions and 7 deletions

2
.gitignore vendored
View File

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

81
ipgeo/ipfilter.go Normal file
View File

@@ -0,0 +1,81 @@
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
break
//IANA Reserved Address Space
case cidrRangeContains("100.64.0.0/10", ip):
asn = ""
whois = "RFC6598"
isFiltered = true
break
case cidrRangeContains("198.18.0.0/15", ip):
asn = ""
whois = "RFC2544"
isFiltered = true
break
case cidrRangeContains("198.51.100.0/24", ip):
case cidrRangeContains("203.0.113.0/24", ip):
asn = ""
whois = "RFC5737"
isFiltered = true
break
case cidrRangeContains("240.0.0.0/4", ip):
asn = ""
whois = "RFC1112"
isFiltered = true
break
//Defense Information System Network
case cidrRangeContains("6.0.0.0/8", ip):
case cidrRangeContains("7.0.0.0/8", ip):
case cidrRangeContains("11.0.0.0/8", ip):
case cidrRangeContains("21.0.0.0/8", ip):
case cidrRangeContains("22.0.0.0/8", ip):
case cidrRangeContains("26.0.0.0/8", ip):
case cidrRangeContains("28.0.0.0/8", ip):
case cidrRangeContains("29.0.0.0/8", ip):
case cidrRangeContains("30.0.0.0/8", ip):
case cidrRangeContains("33.0.0.0/8", ip):
case cidrRangeContains("55.0.0.0/8", ip):
case cidrRangeContains("214.0.0.0/8", ip):
case cidrRangeContains("215.0.0.0/8", ip):
asn = ""
whois = "DOD"
isFiltered = true
break
default:
}
if isFiltered == false {
return nil, false
} else {
return &IPGeoData{
Asnumber: asn,
//Isp: isp,
//Owner: isp,
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

@@ -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
}