mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 05:11:06 +02:00
better private determination
This commit is contained in:
parent
289f228915
commit
a013b00625
1 changed files with 24 additions and 18 deletions
|
@ -262,32 +262,38 @@ func FindOpenPorts(host string, portNumStart, numPorts int) (openPorts []int) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var PrivateIPNetworks = []net.IPNet{
|
var privateIPBlocks []*net.IPNet
|
||||||
net.IPNet{
|
|
||||||
IP: net.ParseIP("10.0.0.0"),
|
func init() {
|
||||||
Mask: net.CIDRMask(8, 32),
|
for _, cidr := range []string{
|
||||||
},
|
"127.0.0.0/8", // IPv4 loopback
|
||||||
net.IPNet{
|
"10.0.0.0/8", // RFC1918
|
||||||
IP: net.ParseIP("172.16.0.0"),
|
"172.16.0.0/12", // RFC1918
|
||||||
Mask: net.CIDRMask(12, 32),
|
"192.168.0.0/16", // RFC1918
|
||||||
},
|
"169.254.0.0/16", // RFC3927 link-local
|
||||||
net.IPNet{
|
"::1/128", // IPv6 loopback
|
||||||
IP: net.ParseIP("192.168.0.0"),
|
"fe80::/10", // IPv6 link-local
|
||||||
Mask: net.CIDRMask(16, 32),
|
"fc00::/7", // IPv6 unique local addr
|
||||||
},
|
} {
|
||||||
|
_, block, err := net.ParseCIDR(cidr)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("parse error on %q: %v", cidr, err))
|
||||||
|
}
|
||||||
|
privateIPBlocks = append(privateIPBlocks, block)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsLocalIP(ipaddress string) bool {
|
func IsLocalIP(ipaddress string) bool {
|
||||||
if strings.Contains(ipaddress, "localhost") {
|
if strings.Contains(ipaddress, "localhost") {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if strings.Contains(ipaddress, "fe80") {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
host, _, _ := net.SplitHostPort(ipaddress)
|
host, _, _ := net.SplitHostPort(ipaddress)
|
||||||
ip := net.ParseIP(host)
|
ip := net.ParseIP(host)
|
||||||
for _, ipNet := range PrivateIPNetworks {
|
if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
|
||||||
if ipNet.Contains(ip) {
|
return true
|
||||||
|
}
|
||||||
|
for _, block := range privateIPBlocks {
|
||||||
|
if block.Contains(ip) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue