This commit is contained in:
1
2025-11-06 13:43:21 +00:00
2 changed files with 53 additions and 4 deletions

View File

@@ -4,14 +4,16 @@ import (
"bytes"
"encoding/binary"
"errors"
"github.com/ipipdotnet/ipdb-go"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"fmt"
"io"
"net"
"os"
"strings"
"sync"
"github.com/ipipdotnet/ipdb-go"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
var (
@@ -71,12 +73,59 @@ func QueryIP(ip string) (location *Location, err error) {
}
}
// 判断IP是否为私有地址包括IPv4和IPv6
func isPrivateIP(ipStr string) (bool, error) {
ip := net.ParseIP(ipStr)
if ip == nil {
return false, fmt.Errorf("无效的IP地址: %s", ipStr)
}
// 定义私有网段
privateNets := []string{
// IPv4私有网段
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
// IPv6私有网段ULA和链路本地
"fc00::/7",
"fe80::/10",
}
// 检查IP是否属于任一私有网段
for _, cidr := range privateNets {
_, netIP, err := net.ParseCIDR(cidr)
if err != nil {
return false, err
}
if netIP.Contains(ip) {
return true, nil
}
}
return false, nil
}
// 判断IP是否为环回地址
func isLoopbackIP(ipStr string) (bool, error) {
ip := net.ParseIP(ipStr)
if ip == nil {
return false, fmt.Errorf("无效的IP地址: %s", ipStr)
}
return ip.IsLoopback(), nil
}
// QueryIPDat 从dat查询IP仅加载dat格式数据库时使用
func QueryIPDat(ipv4 string) (location *Location, err error) {
ip := net.ParseIP(ipv4).To4()
if ip == nil {
return nil, errors.New("ip is not ipv4")
}
if isLoopback, err := isLoopbackIP(ipv4); isLoopback || err != nil {
return &Location{ISP: "局域网", IP: ipv4}, nil
}
if isPrivateIP, err := isPrivateIP(ipv4); isPrivateIP || err != nil {
return &Location{ISP: "私有地址", IP: ipv4}, nil
}
ip32 := binary.BigEndian.Uint32(ip)
posA := binary.LittleEndian.Uint32(data[:4])
posZ := binary.LittleEndian.Uint32(data[4:8])

View File

@@ -22,7 +22,7 @@ func NewBaseSysLogService() *BaseSysLogService {
Model: model.NewBaseSysLog(),
PageQueryOp: &cool.QueryOp{
KeyWordField: []string{"name", "params", "ipAddr"},
Select: `base_sys_log.*,"user".name `,
Select: `base_sys_log.*,"user".username `,
Join: []*cool.JoinOp{
{
Model: model.NewBaseSysUser(),