feat(core): 添加IP地址检测服务核心功能
- 实现配置文件加载和解析功能,支持服务器、MMDB数据库和日志配置 - 集成GeoLite2城市数据库查询,提供IP地理位置信息查询服务 - 添加私有IP地址检测逻辑,过滤本地网络地址段 - 构建HTTP路由处理器,返回JSON格式的IP位置信息 - 配置默认启动参数和错误处理机制 - 集成日志系统,记录请求处理过程和错误信息
This commit is contained in:
52
internal/network/private.go
Normal file
52
internal/network/private.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package network
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type IPInformation struct {
|
||||
IP string
|
||||
IsPrivate bool
|
||||
PrivateCIDR string
|
||||
}
|
||||
|
||||
func GetIPInformation(ip string) IPInformation {
|
||||
ipInfo := IPInformation{
|
||||
IP: ip,
|
||||
IsPrivate: false,
|
||||
PrivateCIDR: "",
|
||||
}
|
||||
|
||||
cidrs := []*net.IPNet{
|
||||
parseCIDR("10.0.0.0/8"),
|
||||
parseCIDR("172.16.0.0/12"),
|
||||
parseCIDR("192.168.0.0/16"),
|
||||
parseCIDR("127.0.0.1/16"),
|
||||
}
|
||||
|
||||
ipAddr := net.ParseIP(ip)
|
||||
if ipAddr == nil {
|
||||
logrus.Warn("invalid ip address")
|
||||
return ipInfo
|
||||
}
|
||||
|
||||
for _, cidr := range cidrs {
|
||||
if cidr.Contains(ipAddr) {
|
||||
ipInfo.IsPrivate = true
|
||||
ipInfo.PrivateCIDR = cidr.String()
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ipInfo
|
||||
}
|
||||
|
||||
func parseCIDR(cidr string) *net.IPNet {
|
||||
_, ipNet, err := net.ParseCIDR(cidr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return ipNet
|
||||
}
|
||||
Reference in New Issue
Block a user