diff --git a/common/utils/qqwry/qqwry.go b/common/utils/qqwry/qqwry.go index fff50265b..78db6fac6 100644 --- a/common/utils/qqwry/qqwry.go +++ b/common/utils/qqwry/qqwry.go @@ -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]) diff --git a/modules/base/service/base_sys_log.go b/modules/base/service/base_sys_log.go index ab29cbc11..aad6db939 100644 --- a/modules/base/service/base_sys_log.go +++ b/modules/base/service/base_sys_log.go @@ -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(),