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

143 lines
3.6 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/rpc/pb"
2021-11-17 19:50:52 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
2021-06-23 13:12:33 +08:00
"github.com/iwind/TeaGo/maps"
2021-11-17 19:50:52 +08:00
timeutil "github.com/iwind/TeaGo/utils/time"
2021-11-17 21:18:33 +08:00
"time"
2021-06-23 13:12:33 +08:00
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "index")
}
func (this *IndexAction) RunGet(params struct {
Ip string
GlobalOnly bool
2021-06-23 13:12:33 +08:00
}) {
2021-11-17 19:50:52 +08:00
this.Data["type"] = ""
this.Data["ip"] = params.Ip
this.Data["globalOnly"] = params.GlobalOnly
2021-06-23 13:12:33 +08:00
countResp, err := this.RPC().IPItemRPC().CountAllEnabledIPItems(this.AdminContext(), &pb.CountAllEnabledIPItemsRequest{
Ip: params.Ip,
GlobalOnly: params.GlobalOnly,
})
2021-06-23 13:12:33 +08:00
if err != nil {
this.ErrorPage(err)
return
}
2021-11-17 19:50:52 +08:00
var count = countResp.Count
var page = this.NewPage(count)
2021-06-23 13:12:33 +08:00
this.Data["page"] = page.AsHTML()
2021-11-17 19:50:52 +08:00
itemsResp, err := this.RPC().IPItemRPC().ListAllEnabledIPItems(this.AdminContext(), &pb.ListAllEnabledIPItemsRequest{
Ip: params.Ip,
GlobalOnly: params.GlobalOnly,
Offset: page.Offset,
Size: page.Size,
2021-06-23 13:12:33 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
2021-11-17 19:50:52 +08:00
var itemMaps = []maps.Map{}
for _, result := range itemsResp.Results {
var item = result.IpItem
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,
}
2021-06-23 13:12:33 +08:00
}
2021-11-17 19:50:52 +08:00
// server
var sourceServerMap = maps.Map{"id": 0}
if item.SourceServer != nil {
sourceServerMap = maps.Map{
"id": item.SourceServer.Id,
"name": item.SourceServer.Name,
}
}
// IP名单
var listMap = maps.Map{"id": 0}
if result.IpList != nil {
listMap = maps.Map{
"id": result.IpList.Id,
"name": result.IpList.Name,
"type": result.IpList.Type,
}
}
// policy
var policyMap = maps.Map{"id": 0}
if result.HttpFirewallPolicy != nil {
policyMap = maps.Map{
"id": result.HttpFirewallPolicy.Id,
"name": result.HttpFirewallPolicy.Name,
}
if result.Server != nil {
policyMap["server"] = maps.Map{"id": result.Server.Id, "name": result.Server.Name}
}
}
itemMaps = append(itemMaps, maps.Map{
"id": item.Id,
"ipFrom": item.IpFrom,
"ipTo": item.IpTo,
"createdTime": timeutil.FormatTime("Y-m-d", item.CreatedAt),
2021-11-17 21:18:33 +08:00
"isExpired": item.ExpiredAt < time.Now().Unix(),
2021-11-17 19:50:52 +08:00
"expiredTime": expiredTime,
"reason": item.Reason,
"type": item.Type,
"eventLevelName": firewallconfigs.FindFirewallEventLevelName(item.EventLevel),
"sourcePolicy": sourcePolicyMap,
"sourceGroup": sourceGroupMap,
"sourceSet": sourceSetMap,
"sourceServer": sourceServerMap,
"list": listMap,
"policy": policyMap,
2021-06-23 13:12:33 +08:00
})
}
2021-11-17 19:50:52 +08:00
this.Data["items"] = itemMaps
2021-06-23 13:12:33 +08:00
this.Show()
}