Files
EdgeAdmin/internal/web/actions/default/servers/iplists/items.go

144 lines
3.7 KiB
Go
Raw Normal View History

2021-06-23 13:12:33 +08:00
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package iplists
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
2021-06-23 13:12:33 +08:00
"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"
"time"
2021-06-23 13:12:33 +08:00
)
type ItemsAction struct {
actionutils.ParentAction
}
func (this *ItemsAction) Init() {
this.Nav("", "", "item")
}
func (this *ItemsAction) RunGet(params struct {
2022-03-30 09:41:14 +08:00
ListId int64
Keyword string
EventLevel string
2021-06-23 13:12:33 +08:00
}) {
2021-10-22 12:19:02 +08:00
this.Data["keyword"] = params.Keyword
2022-03-30 09:41:14 +08:00
this.Data["eventLevel"] = params.EventLevel
2021-10-22 12:19:02 +08:00
2021-06-23 13:12:33 +08:00
err := InitIPList(this.Parent(), params.ListId)
if err != nil {
this.ErrorPage(err)
return
}
// 数量
var listId = params.ListId
2021-10-22 12:19:02 +08:00
countResp, err := this.RPC().IPItemRPC().CountIPItemsWithListId(this.AdminContext(), &pb.CountIPItemsWithListIdRequest{
2022-03-30 09:41:14 +08:00
IpListId: listId,
Keyword: params.Keyword,
EventLevel: params.EventLevel,
2021-10-22 12:19:02 +08:00
})
2021-06-23 13:12:33 +08:00
if err != nil {
this.ErrorPage(err)
return
}
count := countResp.Count
page := this.NewPage(count)
this.Data["page"] = page.AsHTML()
// 列表
itemsResp, err := this.RPC().IPItemRPC().ListIPItemsWithListId(this.AdminContext(), &pb.ListIPItemsWithListIdRequest{
2022-03-30 09:41:14 +08:00
IpListId: listId,
Keyword: params.Keyword,
EventLevel: params.EventLevel,
Offset: page.Offset,
Size: page.Size,
2021-06-23 13:12:33 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
itemMaps := []maps.Map{}
for _, item := range itemsResp.IpItems {
expiredTime := ""
if item.ExpiredAt > 0 {
expiredTime = timeutil.FormatTime("Y-m-d H:i:s", item.ExpiredAt)
}
// policy
var sourcePolicyMap = maps.Map{"id": 0}
if item.SourceHTTPFirewallPolicy != nil {
sourcePolicyMap = maps.Map{
"id": item.SourceHTTPFirewallPolicy.Id,
"name": item.SourceHTTPFirewallPolicy.Name,
"serverId": item.SourceHTTPFirewallPolicy.ServerId,
}
}
// group
var sourceGroupMap = maps.Map{"id": 0}
if item.SourceHTTPFirewallRuleGroup != nil {
sourceGroupMap = maps.Map{
"id": item.SourceHTTPFirewallRuleGroup.Id,
"name": item.SourceHTTPFirewallRuleGroup.Name,
}
}
// set
var sourceSetMap = maps.Map{"id": 0}
if item.SourceHTTPFirewallRuleSet != nil {
sourceSetMap = maps.Map{
"id": item.SourceHTTPFirewallRuleSet.Id,
"name": item.SourceHTTPFirewallRuleSet.Name,
}
}
// server
var sourceServerMap = maps.Map{"id": 0}
if item.SourceServer != nil {
sourceServerMap = maps.Map{
"id": item.SourceServer.Id,
"name": item.SourceServer.Name,
}
}
2022-04-19 10:57:47 +08:00
// 区域 & ISP
var region = ""
var isp = ""
if len(item.IpFrom) > 0 && len(item.IpTo) == 0 {
var ipRegion = iplibrary.LookupIP(item.IpFrom)
if ipRegion != nil && ipRegion.IsOk() {
region = ipRegion.RegionSummary()
isp = ipRegion.ProviderName()
2022-04-19 10:57:47 +08:00
}
}
2021-06-23 13:12:33 +08:00
itemMaps = append(itemMaps, maps.Map{
"id": item.Id,
"ipFrom": item.IpFrom,
"ipTo": item.IpTo,
"createdTime": timeutil.FormatTime("Y-m-d", item.CreatedAt),
2021-06-23 13:12:33 +08:00
"expiredTime": expiredTime,
"reason": item.Reason,
"type": item.Type,
"eventLevelName": firewallconfigs.FindFirewallEventLevelName(item.EventLevel),
"sourcePolicy": sourcePolicyMap,
"sourceGroup": sourceGroupMap,
"sourceSet": sourceSetMap,
"sourceServer": sourceServerMap,
"lifeSeconds": item.ExpiredAt - time.Now().Unix(),
2022-04-19 10:57:47 +08:00
"region": region,
"isp": isp,
2021-06-23 13:12:33 +08:00
})
}
this.Data["items"] = itemMaps
2022-03-30 09:41:14 +08:00
// 所有级别
this.Data["eventLevels"] = firewallconfigs.FindAllFirewallEventLevels()
2021-06-23 13:12:33 +08:00
this.Show()
}