```
feat(common): 添加IP私有地址和环回地址判断函数 在 qqwry.go 中新增 isPrivateIP 和 isLoopbackIP 函数,用于判断 IPv4/IPv6 是否为私有地址或环回地址,并在查询 IP 时优先返回局域网和私有地址标识。 fix(base): 修正系统日志查询字段 将 base_sys_log.go 中的查询字段从 "user".name 更正为 "user".username, 确保关联查询正确显示用户名。 ```
This commit is contained in:
@@ -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])
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user