增加ip获取
This commit is contained in:
104
internal/logic/ip2region/ip2region.go
Normal file
104
internal/logic/ip2region/ip2region.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package ip2region
|
||||
|
||||
import (
|
||||
"github.com/ayflying/utility_go/service"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
ctx = gctx.New()
|
||||
)
|
||||
|
||||
type sIp2region struct {
|
||||
searcher *xdb.Searcher
|
||||
}
|
||||
|
||||
func New() *sIp2region {
|
||||
s := &sIp2region{}
|
||||
s.Load()
|
||||
return s
|
||||
}
|
||||
|
||||
func init() {
|
||||
service.RegisterIp2Region(New())
|
||||
}
|
||||
|
||||
// Load 加载到内存中
|
||||
//
|
||||
// @Description: 加载ip2region数据库到内存中。
|
||||
// @receiver s *sIp2region: sIp2region的实例。
|
||||
func (s *sIp2region) Load() {
|
||||
var err error
|
||||
//var dbPath = "/mnt/s3/ip2region.xdb"
|
||||
var dbPath = "lib/ip2region.xdb"
|
||||
|
||||
if gfile.IsEmpty(dbPath) {
|
||||
//下载文件
|
||||
putData, err2 := g.Client().Discovery(nil).
|
||||
Get(ctx, "https://resource.luoe.cn/pgk/ip2region.xdb")
|
||||
if err2 != nil {
|
||||
return
|
||||
}
|
||||
err = gfile.PutBytes(dbPath, putData.ReadAll())
|
||||
}
|
||||
cBuff := gfile.GetBytes(dbPath)
|
||||
/*
|
||||
var cBuff []byte
|
||||
if gres.Contains(dbPath) {
|
||||
cBuff = gres.GetContent(dbPath)
|
||||
} else {
|
||||
cBuff = gfile.GetBytes(dbPath)
|
||||
}
|
||||
*/
|
||||
|
||||
// 基于读取的内容,创建查询对象
|
||||
s.searcher, err = xdb.NewWithBuffer(cBuff)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "无法创建内容为的搜索器: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *sIp2region) GetIp(ip string) (res []string) {
|
||||
res = make([]string, 5)
|
||||
if s.searcher == nil {
|
||||
return
|
||||
}
|
||||
|
||||
//如果是ipv6直接跳过
|
||||
if s.isIPv6(ip) {
|
||||
return
|
||||
}
|
||||
|
||||
region, err := s.searcher.SearchByStr(ip)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = strings.Split(region, "|")
|
||||
return
|
||||
}
|
||||
|
||||
// isIPv6 判断输入字符串是否为IPv6地址
|
||||
//
|
||||
// @Description: 通过解析输入的IP字符串判断其是否为IPv6地址。
|
||||
// @receiver s *sIp2region: 代表`sIp2region`类型的实例,本函数中未使用,可忽略。
|
||||
// @param ipStr string: 待判断的IP地址字符串。
|
||||
// @return bool: 返回true表示是IPv6地址,返回false表示不是IPv6地址。
|
||||
func (s *sIp2region) isIPv6(ipStr string) bool {
|
||||
// 尝试将输入字符串解析为IP地址
|
||||
ip := net.ParseIP(ipStr)
|
||||
// 尝试将IP地址转换为IPv4格式
|
||||
ipv4 := ip.To4()
|
||||
// 如果转换为IPv4格式不为nil,则说明是IPv4地址,返回false
|
||||
if ipv4 != nil {
|
||||
return false
|
||||
}
|
||||
// 如果无法转换为IPv4格式,则说明是IPv6地址,返回true
|
||||
return true
|
||||
}
|
||||
@@ -6,6 +6,7 @@ package logic
|
||||
|
||||
import (
|
||||
_ "github.com/ayflying/utility_go/internal/logic/gameAct"
|
||||
_ "github.com/ayflying/utility_go/internal/logic/ip2region"
|
||||
_ "github.com/ayflying/utility_go/internal/logic/logData"
|
||||
_ "github.com/ayflying/utility_go/internal/logic/systemCron"
|
||||
_ "github.com/ayflying/utility_go/internal/logic/systemLog"
|
||||
|
||||
Reference in New Issue
Block a user