2020-10-31 17:44:48 +08:00
|
|
|
package log
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2021-01-13 17:00:09 +08:00
|
|
|
"github.com/iwind/TeaGo/lists"
|
2020-10-31 17:44:48 +08:00
|
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type HistoryAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *HistoryAction) Init() {
|
|
|
|
|
this.Nav("", "log", "")
|
|
|
|
|
this.SecondMenu("history")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *HistoryAction) RunGet(params struct {
|
|
|
|
|
ServerId int64
|
|
|
|
|
Day string
|
2021-06-04 10:15:31 +08:00
|
|
|
Keyword string
|
2021-08-07 22:04:30 +08:00
|
|
|
Ip string
|
|
|
|
|
Domain string
|
2021-08-22 16:49:15 +08:00
|
|
|
HasWAF int
|
2020-10-31 17:44:48 +08:00
|
|
|
|
|
|
|
|
RequestId string
|
|
|
|
|
HasError int
|
2021-12-08 19:13:34 +08:00
|
|
|
|
|
|
|
|
PageSize int
|
2020-10-31 17:44:48 +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
|
2021-06-04 10:15:31 +08:00
|
|
|
this.Data["keyword"] = params.Keyword
|
2021-08-07 22:04:30 +08:00
|
|
|
this.Data["ip"] = params.Ip
|
|
|
|
|
this.Data["domain"] = params.Domain
|
2020-10-31 17:44:48 +08:00
|
|
|
this.Data["accessLogs"] = []interface{}{}
|
|
|
|
|
this.Data["hasError"] = params.HasError
|
2021-08-22 16:49:15 +08:00
|
|
|
this.Data["hasWAF"] = params.HasWAF
|
2021-12-08 19:13:34 +08:00
|
|
|
this.Data["pageSize"] = params.PageSize
|
2020-10-31 17:44:48 +08:00
|
|
|
|
|
|
|
|
day := params.Day
|
2021-01-13 17:00:09 +08:00
|
|
|
ipList := []string{}
|
|
|
|
|
|
2020-10-31 17:44:48 +08:00
|
|
|
if len(day) > 0 && regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(day) {
|
|
|
|
|
day = strings.ReplaceAll(day, "-", "")
|
2021-12-08 19:13:34 +08:00
|
|
|
size := int64(params.PageSize)
|
|
|
|
|
if size < 1 {
|
|
|
|
|
size = 20
|
|
|
|
|
}
|
2020-10-31 17:44:48 +08:00
|
|
|
|
|
|
|
|
this.Data["hasError"] = params.HasError
|
|
|
|
|
|
|
|
|
|
resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
|
2021-08-22 16:49:15 +08:00
|
|
|
RequestId: params.RequestId,
|
|
|
|
|
ServerId: params.ServerId,
|
|
|
|
|
HasError: params.HasError > 0,
|
|
|
|
|
HasFirewallPolicy: params.HasWAF > 0,
|
|
|
|
|
Day: day,
|
|
|
|
|
Keyword: params.Keyword,
|
|
|
|
|
Ip: params.Ip,
|
|
|
|
|
Domain: params.Domain,
|
|
|
|
|
Size: size,
|
2020-10-31 17:44:48 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-02 11:53:08 +08:00
|
|
|
if len(resp.HttpAccessLogs) == 0 {
|
2020-10-31 17:44:48 +08:00
|
|
|
this.Data["accessLogs"] = []interface{}{}
|
|
|
|
|
} else {
|
2021-06-02 11:53:08 +08:00
|
|
|
this.Data["accessLogs"] = resp.HttpAccessLogs
|
|
|
|
|
for _, accessLog := range resp.HttpAccessLogs {
|
2021-01-13 17:00:09 +08:00
|
|
|
if len(accessLog.RemoteAddr) > 0 {
|
|
|
|
|
if !lists.ContainsString(ipList, accessLog.RemoteAddr) {
|
|
|
|
|
ipList = append(ipList, accessLog.RemoteAddr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-31 17:44:48 +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{
|
2021-08-22 16:49:15 +08:00
|
|
|
RequestId: params.RequestId,
|
|
|
|
|
ServerId: params.ServerId,
|
|
|
|
|
HasError: params.HasError > 0,
|
|
|
|
|
HasFirewallPolicy: params.HasWAF > 0,
|
|
|
|
|
Day: day,
|
|
|
|
|
Keyword: params.Keyword,
|
|
|
|
|
Ip: params.Ip,
|
|
|
|
|
Domain: params.Domain,
|
|
|
|
|
Size: size,
|
|
|
|
|
Reverse: true,
|
2020-10-31 17:44:48 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-06-02 11:53:08 +08:00
|
|
|
if int64(len(prevResp.HttpAccessLogs)) == size {
|
2020-10-31 17:44:48 +08:00
|
|
|
this.Data["lastRequestId"] = prevResp.RequestId
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 17:00:09 +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-31 17:44:48 +08:00
|
|
|
this.Show()
|
|
|
|
|
}
|