golang如何判断ip和内网ip,代码已准备好,需要的自取~
package main
import (
"fmt"
"net"
"strconv"
"strings"
)
func checkIp(ipStr string) bool {
address := net.ParseIP(ipStr)
if address == nil {
fmt.Println("ip地址格式不正确")
return false
} else {
fmt.Println("正确的ip地址", address.String())
return true
}
}
// ip to int64
func inetAton(ipStr string) int64 {
bits := strings.Split(ipStr, ".")
b0, _ := strconv.Atoi(bits[0])
b1, _ := strconv.Atoi(bits[1])
b2, _ := strconv.Atoi(bits[2])
b3, _ := strconv.Atoi(bits[3])
var sum int64
sum += int64(b0) << 24
sum += int64(b1) << 16
sum += int64(b2) << 8
sum += int64(b3)
return sum
}
//int64 to IP
func inetNtoa(ipnr int64) net.IP {
var bytes [4]byte
bytes[0] = byte(ipnr & 0xFF)
bytes[1] = byte((ipnr >> 8) & 0xFF)
bytes[2] = byte((ipnr >> 16) & 0xFF)
bytes[3] = byte((ipnr >> 24) & 0xFF)
return net.IPv4(bytes[3], bytes[2], bytes[1], bytes[0])
}
func isInnerIp(ipStr string) bool {
if !checkIp(ipStr) {
return false
}
inputIpNum := inetAton(ipStr)
innerIpA := inetAton("10.255.255.255")
innerIpB := inetAton("172.16.255.255")
innerIpC := inetAton("192.168.255.255")
innerIpD := inetAton("100.64.255.255")
innerIpF := inetAton("127.255.255.255")
return inputIpNum>>24 == innerIpA>>24 || inputIpNum>>20 == innerIpB>>20 ||
inputIpNum>>16 == innerIpC>>16 || inputIpNum>>22 == innerIpD>>22 ||
inputIpNum>>24 == innerIpF>>24
}
IP地址查询,通过IP数据云 - 免费IP地址查询。
IP数据云利用大数据挖掘和网络空间地图测绘技术与人工智能(AI)算法相结合,利用动态密度聚类算法和基于多层神经网络的IP地址定位算法,完成IP地址地理位置定位。IP数据云接口响应快,多台服务器构建 API 接口负载均衡。IP数据云利用全球超700个网络监测点辅助测量,IP地址查询定位准确度超过99.8%。