Merge branch 'main' of https://github.com/72wo/blazing
This commit is contained in:
@@ -4,14 +4,16 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/ipipdotnet/ipdb-go"
|
"fmt"
|
||||||
"golang.org/x/text/encoding/simplifiedchinese"
|
|
||||||
"golang.org/x/text/transform"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/ipipdotnet/ipdb-go"
|
||||||
|
"golang.org/x/text/encoding/simplifiedchinese"
|
||||||
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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格式数据库时使用
|
// QueryIPDat 从dat查询IP,仅加载dat格式数据库时使用
|
||||||
func QueryIPDat(ipv4 string) (location *Location, err error) {
|
func QueryIPDat(ipv4 string) (location *Location, err error) {
|
||||||
ip := net.ParseIP(ipv4).To4()
|
ip := net.ParseIP(ipv4).To4()
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
return nil, errors.New("ip is not ipv4")
|
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)
|
ip32 := binary.BigEndian.Uint32(ip)
|
||||||
posA := binary.LittleEndian.Uint32(data[:4])
|
posA := binary.LittleEndian.Uint32(data[:4])
|
||||||
posZ := binary.LittleEndian.Uint32(data[4:8])
|
posZ := binary.LittleEndian.Uint32(data[4:8])
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func NewBaseSysLogService() *BaseSysLogService {
|
|||||||
Model: model.NewBaseSysLog(),
|
Model: model.NewBaseSysLog(),
|
||||||
PageQueryOp: &cool.QueryOp{
|
PageQueryOp: &cool.QueryOp{
|
||||||
KeyWordField: []string{"name", "params", "ipAddr"},
|
KeyWordField: []string{"name", "params", "ipAddr"},
|
||||||
Select: `base_sys_log.*,"user".name `,
|
Select: `base_sys_log.*,"user".username `,
|
||||||
Join: []*cool.JoinOp{
|
Join: []*cool.JoinOp{
|
||||||
{
|
{
|
||||||
Model: model.NewBaseSysUser(),
|
Model: model.NewBaseSysUser(),
|
||||||
|
|||||||
Reference in New Issue
Block a user