Files
EdgeAdmin/internal/web/actions/default/servers/components/waf/log.go

136 lines
3.5 KiB
Go
Raw Normal View History

2020-10-06 21:02:37 +08:00
package waf
2020-11-02 14:37:28 +08:00
import (
"encoding/json"
2020-11-02 14:37:28 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
2021-01-20 14:19:10 +08:00
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
2020-11-02 14:37:28 +08:00
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"strings"
)
2020-10-06 21:02:37 +08:00
type LogAction struct {
actionutils.ParentAction
}
func (this *LogAction) Init() {
2020-10-07 11:18:07 +08:00
this.Nav("", "", "log")
2020-10-06 21:02:37 +08:00
}
2020-11-02 14:37:28 +08:00
func (this *LogAction) RunGet(params struct {
Day string
RequestId string
FirewallPolicyId int64
GroupId int64
2020-11-02 14:37:28 +08:00
}) {
if len(params.Day) == 0 {
params.Day = timeutil.Format("Y-m-d")
}
this.Data["path"] = this.Request.URL.Path
this.Data["day"] = params.Day
this.Data["groupId"] = params.GroupId
2020-11-02 14:37:28 +08:00
this.Data["accessLogs"] = []interface{}{}
day := params.Day
2021-01-20 14:19:10 +08:00
ipList := []string{}
2020-11-02 14:37:28 +08:00
if len(day) > 0 && regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(day) {
day = strings.ReplaceAll(day, "-", "")
size := int64(10)
resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
RequestId: params.RequestId,
FirewallPolicyId: params.FirewallPolicyId,
FirewallRuleGroupId: params.GroupId,
Day: day,
Size: size,
2020-11-02 14:37:28 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
if len(resp.AccessLogs) == 0 {
this.Data["accessLogs"] = []interface{}{}
} else {
this.Data["accessLogs"] = resp.AccessLogs
2021-01-20 14:19:10 +08:00
for _, accessLog := range resp.AccessLogs {
if len(accessLog.RemoteAddr) > 0 {
if !lists.ContainsString(ipList, accessLog.RemoteAddr) {
ipList = append(ipList, accessLog.RemoteAddr)
}
}
}
2020-11-02 14:37:28 +08:00
}
this.Data["hasMore"] = resp.HasMore
this.Data["nextRequestId"] = resp.RequestId
// 上一个requestId
this.Data["hasPrev"] = false
this.Data["lastRequestId"] = ""
if len(params.RequestId) > 0 {
this.Data["hasPrev"] = true
prevResp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
RequestId: params.RequestId,
FirewallPolicyId: params.FirewallPolicyId,
FirewallRuleGroupId: params.GroupId,
Day: day,
Size: size,
Reverse: true,
2020-11-02 14:37:28 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
if int64(len(prevResp.AccessLogs)) == size {
this.Data["lastRequestId"] = prevResp.RequestId
}
}
}
// 所有分组
policyResp, err := this.RPC().HTTPFirewallPolicyRPC().FindEnabledHTTPFirewallPolicyConfig(this.AdminContext(), &pb.FindEnabledHTTPFirewallPolicyConfigRequest{
HttpFirewallPolicyId: params.FirewallPolicyId,
})
if err != nil {
this.ErrorPage(err)
return
}
policyConfig := &firewallconfigs.HTTPFirewallPolicy{}
err = json.Unmarshal(policyResp.HttpFirewallPolicyJSON, policyConfig)
if err != nil {
this.ErrorPage(err)
return
}
groupMaps := []maps.Map{}
for _, group := range policyConfig.AllRuleGroups() {
groupMaps = append(groupMaps, maps.Map{
"id": group.Id,
"name": group.Name,
})
}
this.Data["groups"] = groupMaps
2021-01-20 14:19:10 +08:00
// 根据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
2020-10-06 21:02:37 +08:00
this.Show()
}