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

This commit is contained in:
刘祥超
2023-08-24 12:22:16 +08:00
parent 15f2a2d517
commit 3f664882d5
12 changed files with 45 additions and 168 deletions

View File

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

View File

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

View File

@@ -3,6 +3,7 @@ package waf
import ( import (
"encoding/json" "encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "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/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
@@ -141,20 +142,7 @@ func (this *LogAction) RunGet(params struct {
this.Data["groups"] = groupMaps this.Data["groups"] = groupMaps
// 根据IP查询区域 // 根据IP查询区域
regionMap := map[string]string{} // ip => region this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
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
// WAF相关 // WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

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

View File

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

View File

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

View File

@@ -5,6 +5,7 @@ package logs
import ( import (
"fmt" "fmt"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "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/rpc/pb"
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
@@ -169,20 +170,7 @@ func (this *IndexAction) RunGet(params struct {
} }
// 根据IP查询区域 // 根据IP查询区域
var regionMap = map[string]string{} // ip => region this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
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
// WAF相关 // WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

@@ -1,6 +1,7 @@
package log package log
import ( import (
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
@@ -160,20 +161,7 @@ func (this *HistoryAction) RunGet(params struct {
} }
// 根据IP查询区域 // 根据IP查询区域
var regionMap = map[string]string{} // ip => region this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
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
// WAF相关 // WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

@@ -1,6 +1,7 @@
package log package log
import ( import (
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
@@ -129,20 +130,7 @@ func (this *IndexAction) RunPost(params struct {
this.Data["hasMore"] = accessLogsResp.HasMore this.Data["hasMore"] = accessLogsResp.HasMore
// 根据IP查询区域 // 根据IP查询区域
var regionMap = map[string]string{} // ip => region this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
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
// WAF相关 // WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

@@ -1,6 +1,7 @@
package log package log
import ( import (
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
@@ -141,20 +142,7 @@ func (this *TodayAction) RunGet(params struct {
} }
// 根据IP查询区域 // 根据IP查询区域
var regionMap = map[string]string{} // ip => region this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
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
// WAF相关 // WAF相关
var wafInfos = map[int64]maps.Map{} // set id => WAF Map var wafInfos = map[int64]maps.Map{} // set id => WAF Map

View File

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

View File

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