fix: check local ip for 123pan

This commit is contained in:
Xhofe
2022-03-15 14:48:39 +08:00
parent 5a1b16a601
commit ef19e851e3
2 changed files with 29 additions and 1 deletions

28
utils/check.go Normal file
View File

@ -0,0 +1,28 @@
package utils
import (
"net"
)
func IsLocalIPAddr(ip string) bool {
return IsLocalIP(net.ParseIP(ip))
}
func IsLocalIP(ip net.IP) bool {
if ip == nil {
return false
}
if ip.IsLoopback() {
return true
}
ip4 := ip.To4()
if ip4 == nil {
return false
}
return ip4[0] == 10 || // 10.0.0.0/8
(ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31) || // 172.16.0.0/12
(ip4[0] == 169 && ip4[1] == 254) || // 169.254.0.0/16
(ip4[0] == 192 && ip4[1] == 168) // 192.168.0.0/16
}