使用查询本地IP库代替API查询IP信息

This commit is contained in:
GoEdgeLab
2023-08-24 12:22:16 +08:00
parent 802b42d7f3
commit 7e6190e6e1
12 changed files with 45 additions and 168 deletions

View File

@@ -3,11 +3,11 @@ package log
import (
"bytes"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
timeutil "github.com/iwind/TeaGo/utils/time"
"github.com/tealeg/xlsx/v3"
"strconv"
"strings"
)
type ExportExcelAction struct {
@@ -65,23 +65,11 @@ func (this *ExportExcelAction) RunGet(params struct {
for _, log := range logsResp.Logs {
var regionName = ""
var ispName = ""
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: log.Ip})
if err != nil {
this.ErrorPage(err)
return
}
if regionResp.IpRegion != nil {
regionName = regionResp.IpRegion.Summary
// remove isp from regionName
var index = strings.LastIndex(regionName, "|")
if index > 0 {
regionName = regionName[:index]
}
if len(regionResp.IpRegion.Isp) > 0 {
ispName = regionResp.IpRegion.Isp
}
var ipRegion = iplibrary.LookupIP(log.Ip)
if ipRegion != nil && ipRegion.IsOk() {
regionName = ipRegion.RegionSummary()
ispName = ipRegion.ProviderName()
}
var row = sheet.AddRow()

View File

@@ -3,6 +3,7 @@ package log
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
@@ -84,14 +85,10 @@ func (this *IndexAction) RunGet(params struct {
}
var logMaps = []maps.Map{}
for _, log := range logsResp.Logs {
regionName := ""
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: log.Ip})
if err != nil {
this.ErrorPage(err)
return
}
if regionResp.IpRegion != nil {
regionName = regionResp.IpRegion.Summary
var regionName = ""
var ipRegion = iplibrary.LookupIP(log.Ip)
if ipRegion != nil && ipRegion.IsOk() {
regionName = ipRegion.Summary()
}
logMaps = append(logMaps, maps.Map{

View File

@@ -3,6 +3,7 @@ package waf
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/iwind/TeaGo/lists"
@@ -141,20 +142,7 @@ func (this *LogAction) RunGet(params struct {
this.Data["groups"] = groupMaps
// 根据IP查询区域
regionMap := map[string]string{} // ip => region
if len(ipList) > 0 {
resp, err := this.RPC().IPLibraryRPC().LookupIPRegions(this.AdminContext(), &pb.LookupIPRegionsRequest{IpList: ipList})
if err != nil {
this.ErrorPage(err)
return
}
if resp.IpRegionMap != nil {
for ip, region := range resp.IpRegionMap {
regionMap[ip] = region.Summary
}
}
}
this.Data["regions"] = regionMap
this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
// WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

@@ -4,10 +4,10 @@ package ipbox
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"strings"
"time"
)
@@ -25,24 +25,15 @@ func (this *IndexAction) RunGet(params struct {
this.Data["ip"] = params.Ip
// IP信息
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: params.Ip})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["regions"] = ""
this.Data["isp"] = ""
if regionResp.IpRegion != nil {
var regionName = regionResp.IpRegion.Summary
// remove isp from regionName
var index = strings.LastIndex(regionName, "|")
if index > 0 {
regionName = regionName[:index]
}
this.Data["regions"] = regionName
this.Data["isp"] = regionResp.IpRegion.Isp
var ipRegion = iplibrary.LookupIP(params.Ip)
if ipRegion != nil && ipRegion.IsOk() {
this.Data["regions"] = ipRegion.RegionSummary()
this.Data["isp"] = ipRegion.ProviderName()
}
// IP列表

View File

@@ -4,11 +4,11 @@ package iplists
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"strings"
"time"
)
@@ -162,22 +162,10 @@ func (this *IndexAction) RunGet(params struct {
var region = ""
var isp = ""
if len(item.IpFrom) > 0 && len(item.IpTo) == 0 {
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: item.IpFrom})
if err != nil {
this.ErrorPage(err)
return
}
var ipRegion = regionResp.IpRegion
if ipRegion != nil {
region = ipRegion.Summary
// remove isp from regionName
var index = strings.LastIndex(region, "|")
if index > 0 {
region = region[:index]
}
isp = ipRegion.Isp
var ipRegion = iplibrary.LookupIP(item.IpFrom)
if ipRegion != nil && ipRegion.IsOk() {
region = ipRegion.RegionSummary()
isp = ipRegion.ProviderName()
}
}

View File

@@ -4,11 +4,11 @@ package iplists
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"strings"
"time"
)
@@ -109,22 +109,10 @@ func (this *ItemsAction) RunGet(params struct {
var region = ""
var isp = ""
if len(item.IpFrom) > 0 && len(item.IpTo) == 0 {
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: item.IpFrom})
if err != nil {
this.ErrorPage(err)
return
}
var ipRegion = regionResp.IpRegion
if ipRegion != nil {
region = ipRegion.Summary
// remove isp from regionName
var index = strings.LastIndex(region, "|")
if index > 0 {
region = region[:index]
}
isp = ipRegion.Isp
var ipRegion = iplibrary.LookupIP(item.IpFrom)
if ipRegion != nil && ipRegion.IsOk() {
region = ipRegion.RegionSummary()
isp = ipRegion.ProviderName()
}
}

View File

@@ -5,6 +5,7 @@ package logs
import (
"fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
@@ -169,20 +170,7 @@ func (this *IndexAction) RunGet(params struct {
}
// 根据IP查询区域
var regionMap = map[string]string{} // ip => region
if len(ipList) > 0 {
resp, err := this.RPC().IPLibraryRPC().LookupIPRegions(this.AdminContext(), &pb.LookupIPRegionsRequest{IpList: ipList})
if err != nil {
this.ErrorPage(err)
return
}
if resp.IpRegionMap != nil {
for ip, region := range resp.IpRegionMap {
regionMap[ip] = region.Summary
}
}
}
this.Data["regions"] = regionMap
this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
// WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

@@ -1,6 +1,7 @@
package log
import (
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
@@ -160,20 +161,7 @@ func (this *HistoryAction) RunGet(params struct {
}
// 根据IP查询区域
var regionMap = map[string]string{} // ip => region
if len(ipList) > 0 {
resp, err := this.RPC().IPLibraryRPC().LookupIPRegions(this.AdminContext(), &pb.LookupIPRegionsRequest{IpList: ipList})
if err != nil {
this.ErrorPage(err)
return
}
if resp.IpRegionMap != nil {
for ip, region := range resp.IpRegionMap {
regionMap[ip] = region.Summary
}
}
}
this.Data["regions"] = regionMap
this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
// WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

@@ -1,6 +1,7 @@
package log
import (
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
@@ -129,20 +130,7 @@ func (this *IndexAction) RunPost(params struct {
this.Data["hasMore"] = accessLogsResp.HasMore
// 根据IP查询区域
var regionMap = map[string]string{} // ip => region
if len(ipList) > 0 {
resp, err := this.RPC().IPLibraryRPC().LookupIPRegions(this.AdminContext(), &pb.LookupIPRegionsRequest{IpList: ipList})
if err != nil {
this.ErrorPage(err)
return
}
if resp.IpRegionMap != nil {
for ip, region := range resp.IpRegionMap {
regionMap[ip] = region.Summary
}
}
}
this.Data["regions"] = regionMap
this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
// WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

@@ -1,6 +1,7 @@
package log
import (
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
@@ -141,20 +142,7 @@ func (this *TodayAction) RunGet(params struct {
}
// 根据IP查询区域
var regionMap = map[string]string{} // ip => region
if len(ipList) > 0 {
resp, err := this.RPC().IPLibraryRPC().LookupIPRegions(this.AdminContext(), &pb.LookupIPRegionsRequest{IpList: ipList})
if err != nil {
this.ErrorPage(err)
return
}
if resp.IpRegionMap != nil {
for ip, region := range resp.IpRegionMap {
regionMap[ip] = region.Summary
}
}
}
this.Data["regions"] = regionMap
this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
// WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

@@ -2,6 +2,7 @@ package log
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
"net/http"
@@ -86,24 +87,11 @@ func (this *ViewPopupAction) RunGet(params struct {
// 地域相关
var regionMap maps.Map = nil
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: accessLog.RemoteAddr})
if err != nil {
this.ErrorPage(err)
return
}
region := regionResp.IpRegion
if region != nil {
var regionName = region.Summary
// remove isp from regionName
var index = strings.LastIndex(regionName, "|")
if index > 0 {
regionName = regionName[:index]
}
var ipRegion = iplibrary.LookupIP(accessLog.RemoteAddr)
if ipRegion != nil && ipRegion.IsOk() {
regionMap = maps.Map{
"full": regionName,
"isp": region.Isp,
"full": ipRegion.RegionSummary(),
"isp": ipRegion.ProviderName(),
}
}
this.Data["region"] = regionMap

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/users/userutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
)
@@ -60,13 +61,9 @@ func (this *UserAction) RunGet(params struct {
// IP地址
var registeredRegion = ""
if len(user.RegisteredIP) > 0 {
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: user.RegisteredIP})
if err != nil {
this.ErrorPage(err)
return
}
if regionResp.IpRegion != nil {
registeredRegion = regionResp.IpRegion.Summary
var ipRegion = iplibrary.LookupIP(user.RegisteredIP)
if ipRegion != nil && ipRegion.IsOk() {
registeredRegion = ipRegion.Summary()
}
}