IP接口
This commit is contained in:
11
api/network/model/ip_info.go
Normal file
11
api/network/model/ip_info.go
Normal file
@ -0,0 +1,11 @@
|
||||
package model
|
||||
|
||||
// IpInfo // IP信息
|
||||
type IpInfo struct {
|
||||
IP string `json:"ip"` // IP地址
|
||||
Country string `json:"country"` // 国家
|
||||
Region string `json:"region"` // 区域
|
||||
Province string `json:"province"` // 省份
|
||||
City string `json:"city"` // 城市
|
||||
ISP string `json:"ISP"` // ISP
|
||||
}
|
6
api/network/model/req/ip.go
Normal file
6
api/network/model/req/ip.go
Normal file
@ -0,0 +1,6 @@
|
||||
package req
|
||||
|
||||
// GetIpInfoRequest 获取IP信息请求
|
||||
type GetIpInfoRequest struct {
|
||||
IP string `json:"ip" validate:"required"` // IP 地址
|
||||
}
|
47
api/network/server/ip_api.go
Normal file
47
api/network/server/ip_api.go
Normal file
@ -0,0 +1,47 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"api/api/network/model"
|
||||
"api/api/network/model/req"
|
||||
"api/utils"
|
||||
"api/utils/r"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// IpApi ip接口
|
||||
type IpApi struct {
|
||||
}
|
||||
|
||||
// GetIpInfo 获取IP信息
|
||||
func (*IpApi) GetIpInfo(c *gin.Context) {
|
||||
data := utils.BindValidJson[req.GetIpInfoRequest](c)
|
||||
ipInfo, err := ipService.GetIpInfo(data.IP)
|
||||
if err != nil {
|
||||
r.Error(c)
|
||||
}
|
||||
r.SuccessData(c, ipInfo)
|
||||
return
|
||||
}
|
||||
|
||||
// GetIpInfoList 获取IP信息
|
||||
func (*IpApi) GetIpInfoList(c *gin.Context) {
|
||||
scanner := bufio.NewScanner(c.Request.Body)
|
||||
var ipInfoList []model.IpInfo
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
ipInfo, err := ipService.GetIpInfo(line)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if ipInfo.ISP == "谷歌" {
|
||||
ipInfoList = append(ipInfoList, ipInfo)
|
||||
fmt.Println(ipInfo.IP)
|
||||
}
|
||||
// 在这里你可以处理每一行的数据,执行你的业务逻辑
|
||||
}
|
||||
r.SuccessData(c, ipInfoList)
|
||||
return
|
||||
}
|
@ -4,4 +4,5 @@ import "api/api/network/service"
|
||||
|
||||
var (
|
||||
networkService = service.NetWorkService{}
|
||||
ipService = service.IpService{}
|
||||
)
|
||||
|
54
api/network/service/ip_service.go
Normal file
54
api/network/service/ip_service.go
Normal file
@ -0,0 +1,54 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"api/api/network/model"
|
||||
"fmt"
|
||||
"github.com/lionsoul2014/ip2region/binding/golang/xdb"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
searcher *xdb.Searcher
|
||||
)
|
||||
|
||||
type IpService struct{}
|
||||
|
||||
func init() {
|
||||
var dbPath = "config/google.xdb"
|
||||
// 1、从 dbPath 加载整个 xdb 到内存
|
||||
cBuff, err := xdb.LoadContentFromFile(dbPath)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to load content from `%s`: %s\n", dbPath, err)
|
||||
if err != nil {
|
||||
log.Panic("dbPath初始化失败: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 2、用全局的 cBuff 创建完全基于内存的查询对象。
|
||||
searcher, err = xdb.NewWithBuffer(cBuff)
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create searcher with content: %s\n", err)
|
||||
if err != nil {
|
||||
log.Panic("searcher初始化失败: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetIpInfo 获取IP信息
|
||||
func (*IpService) GetIpInfo(ip string) (model.IpInfo, error) {
|
||||
region, err := searcher.SearchByStr(ip)
|
||||
if err != nil {
|
||||
return model.IpInfo{}, err
|
||||
}
|
||||
info := strings.Split(region, "|")
|
||||
var ipInfo = model.IpInfo{
|
||||
IP: ip,
|
||||
Country: info[0],
|
||||
Region: info[1],
|
||||
Province: info[2],
|
||||
City: info[3],
|
||||
ISP: info[4],
|
||||
}
|
||||
return ipInfo, nil
|
||||
}
|
Reference in New Issue
Block a user