mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-12-24 08:12:55 +08:00
调整ip工具
This commit is contained in:
@@ -117,27 +117,7 @@ const formRules = {
|
||||
trigger: ['blur']
|
||||
}
|
||||
],
|
||||
Os: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入系统',
|
||||
trigger: ['blur']
|
||||
}
|
||||
],
|
||||
Browser: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入浏览器',
|
||||
trigger: ['blur']
|
||||
}
|
||||
],
|
||||
City: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入城市',
|
||||
trigger: ['blur']
|
||||
}
|
||||
],
|
||||
|
||||
Width: [
|
||||
{
|
||||
required: true,
|
||||
@@ -152,13 +132,7 @@ const formRules = {
|
||||
trigger: ['blur']
|
||||
}
|
||||
],
|
||||
Ua: [
|
||||
{
|
||||
required: true,
|
||||
message: '请输入ua记录',
|
||||
trigger: ['blur']
|
||||
}
|
||||
],
|
||||
|
||||
ClientTime: [
|
||||
{
|
||||
required: true,
|
||||
|
||||
@@ -123,7 +123,7 @@
|
||||
<el-table-column label="城市" prop="City" min-width="130" />
|
||||
<el-table-column label="屏幕" prop="Width" min-width="130" />
|
||||
<el-table-column label="屏幕高度" prop="Height" min-width="130" />
|
||||
<el-table-column label="ua记录" prop="Ua" min-width="130" />
|
||||
<el-table-column label="ua记录" prop="Ua" min-width="380" />
|
||||
<el-table-column label="创建时间" prop="CreateTime" min-width="130" />
|
||||
|
||||
<el-table-column label="操作" width="80" fixed="right">
|
||||
|
||||
@@ -135,9 +135,10 @@ func (hd *MonitorClientHandler) Add(c *gin.Context) {
|
||||
addReq.Os = &ua.Os.Family
|
||||
addReq.Browser = &ua.UserAgent.Family
|
||||
// addReq.Ip = *c.ClientIP()
|
||||
core.IpUtils.Parse(c.ClientIP())
|
||||
core.IpUtils.Parse("124.223.171.170")
|
||||
core.IpUtils.Parse("118.24.157.190")
|
||||
regionInfo := util.IpUtil.Parse(c.ClientIP())
|
||||
// regionInfo := util.IpUtil.Parse("118.24.157.190")
|
||||
|
||||
addReq.City = ®ionInfo.City
|
||||
|
||||
createId, e := MonitorClientService.Add(addReq)
|
||||
response.CheckAndRespWithData(c, createId, e)
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
||||
)
|
||||
|
||||
var IpUtils = new(ipUtils)
|
||||
|
||||
type ipUtils struct {
|
||||
Searcher *xdb.Searcher
|
||||
}
|
||||
|
||||
func init() {
|
||||
var dbPath = "resources/ip/ip2region.xdb"
|
||||
// 创建完全基于内存的查询对象。
|
||||
cBuff, err := xdb.LoadContentFromFile(dbPath)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load content from `%s`: %s\n", dbPath, err)
|
||||
return
|
||||
}
|
||||
// 并发使用,用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
|
||||
searcher, err := xdb.NewWithBuffer(cBuff)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create searcher: %s\n", err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Printf("创建完全基于内存的查询对象。")
|
||||
IpUtils.Searcher = searcher
|
||||
// defer searcher.Close()
|
||||
|
||||
// region, err := searcher.SearchByStr(ip)
|
||||
// if err != nil {
|
||||
// fmt.Printf("failed to SearchIP(%s): %s\n", ip, err)
|
||||
// return
|
||||
// }
|
||||
// fmt.Printf("{region: %+v}\n", region)
|
||||
}
|
||||
|
||||
type Region struct {
|
||||
Country string
|
||||
Province string
|
||||
City string
|
||||
Operator string
|
||||
}
|
||||
|
||||
func (ipUtils *ipUtils) Parse(ip string) Region {
|
||||
// var dbPath = "resources/ip/ip2region.xdb"
|
||||
// // 创建完全基于内存的查询对象。
|
||||
// cBuff, err := xdb.LoadContentFromFile(dbPath)
|
||||
// searcher, err := xdb.NewWithBuffer(cBuff)
|
||||
|
||||
// if err != nil {
|
||||
// fmt.Printf("failed to create searcher: %s\n", err.Error())
|
||||
// return
|
||||
// }
|
||||
// defer searcher.Close()
|
||||
if ip == "" {
|
||||
fmt.Println("输入ip为空")
|
||||
return Region{}
|
||||
}
|
||||
region, err := ipUtils.Searcher.SearchByStr(ip)
|
||||
if err != nil {
|
||||
fmt.Printf("解析ip(%s)错误: %s\n", ip, err)
|
||||
return Region{}
|
||||
}
|
||||
// 中国|0|四川省|成都市|电信
|
||||
parts := strings.Split(region, "|")
|
||||
if len(parts) != 5 {
|
||||
fmt.Println("解析ip返回错误")
|
||||
return Region{}
|
||||
}
|
||||
country := parts[0]
|
||||
province := parts[2]
|
||||
city := parts[3]
|
||||
operator := parts[4]
|
||||
regionInfo := Region{Country: country, Province: province, City: city, Operator: operator}
|
||||
return regionInfo
|
||||
}
|
||||
@@ -1,14 +1,20 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"x_admin/core"
|
||||
|
||||
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
||||
)
|
||||
|
||||
var IpUtil = ipUtil{}
|
||||
var IpUtil = initIpUtil()
|
||||
|
||||
// serverUtil IP工具类
|
||||
type ipUtil struct{}
|
||||
type ipUtil struct {
|
||||
Searcher *xdb.Searcher
|
||||
}
|
||||
|
||||
// GetHostIp 获取本地主机名
|
||||
func (su ipUtil) GetHostIp() (ip string) {
|
||||
@@ -21,3 +27,57 @@ func (su ipUtil) GetHostIp() (ip string) {
|
||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||
return localAddr.IP.String()
|
||||
}
|
||||
|
||||
type Region struct {
|
||||
Country string
|
||||
Province string
|
||||
City string
|
||||
Operator string
|
||||
}
|
||||
|
||||
func (ipUtils *ipUtil) Parse(ip string) Region {
|
||||
|
||||
if ip == "" {
|
||||
fmt.Println("输入ip为空")
|
||||
return Region{}
|
||||
}
|
||||
region, err := ipUtils.Searcher.SearchByStr(ip)
|
||||
if err != nil {
|
||||
fmt.Printf("解析ip(%s)错误: %s\n", ip, err)
|
||||
return Region{}
|
||||
}
|
||||
// 中国|0|四川省|成都市|电信
|
||||
parts := strings.Split(region, "|")
|
||||
if len(parts) != 5 {
|
||||
fmt.Println("解析ip返回错误")
|
||||
return Region{}
|
||||
}
|
||||
country := parts[0]
|
||||
province := parts[2]
|
||||
city := parts[3]
|
||||
operator := parts[4]
|
||||
regionInfo := Region{Country: country, Province: province, City: city, Operator: operator}
|
||||
fmt.Println(regionInfo)
|
||||
return regionInfo
|
||||
}
|
||||
func initIpUtil() *ipUtil {
|
||||
ip_util := ipUtil{}
|
||||
|
||||
var dbPath = "resources/ip/ip2region.xdb"
|
||||
// 创建完全基于内存的查询对象。
|
||||
cBuff, err := xdb.LoadContentFromFile(dbPath)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load content from `%s`: %s\n", dbPath, err)
|
||||
return &ip_util
|
||||
}
|
||||
// 并发使用,用整个 xdb 缓存创建的 searcher 对象可以安全用于并发。
|
||||
searcher, err := xdb.NewWithBuffer(cBuff)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create searcher: %s\n", err.Error())
|
||||
return &ip_util
|
||||
}
|
||||
fmt.Printf("创建完全基于内存的查询对象。")
|
||||
ip_util.Searcher = searcher
|
||||
return &ip_util
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user