Files
EdgeAdmin/internal/web/actions/default/servers/server/log/index.go

111 lines
2.6 KiB
Go
Raw Normal View History

2020-08-21 12:32:16 +08:00
package log
2020-10-10 19:22:17 +08:00
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/actions"
2021-01-13 17:00:09 +08:00
"github.com/iwind/TeaGo/lists"
2020-10-10 19:22:17 +08:00
timeutil "github.com/iwind/TeaGo/utils/time"
)
2020-08-21 12:32:16 +08:00
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "log", "")
2020-08-21 21:09:42 +08:00
this.SecondMenu("index")
2020-08-21 12:32:16 +08:00
}
2020-10-10 19:22:17 +08:00
func (this *IndexAction) RunGet(params struct {
ServerId int64
RequestId string
Ip string
Domain string
Keyword string
2020-10-10 19:22:17 +08:00
}) {
this.Data["serverId"] = params.ServerId
this.Data["requestId"] = params.RequestId
this.Data["ip"] = params.Ip
this.Data["domain"] = params.Domain
this.Data["keyword"] = params.Keyword
this.Data["path"] = this.Request.URL.Path
2020-10-10 19:22:17 +08:00
2021-05-03 15:15:31 +08:00
// 记录最近使用
_, err := this.RPC().LatestItemRPC().IncreaseLatestItem(this.AdminContext(), &pb.IncreaseLatestItemRequest{
ItemType: "server",
ItemId: params.ServerId,
})
if err != nil {
this.ErrorPage(err)
return
}
2020-08-21 12:32:16 +08:00
this.Show()
}
2020-10-10 19:22:17 +08:00
func (this *IndexAction) RunPost(params struct {
ServerId int64
RequestId string
Keyword string
Ip string
Domain string
2020-10-10 19:22:17 +08:00
Must *actions.Must
}) {
isReverse := len(params.RequestId) > 0
accessLogsResp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
ServerId: params.ServerId,
RequestId: params.RequestId,
Size: 20,
Day: timeutil.Format("Ymd"),
Keyword: params.Keyword,
Ip: params.Ip,
Domain: params.Domain,
2020-10-10 19:22:17 +08:00
Reverse: isReverse,
})
if err != nil {
this.ErrorPage(err)
return
}
2021-01-13 17:00:09 +08:00
ipList := []string{}
2021-06-02 11:53:08 +08:00
accessLogs := accessLogsResp.HttpAccessLogs
2020-10-10 19:22:17 +08:00
if len(accessLogs) == 0 {
accessLogs = []*pb.HTTPAccessLog{}
2021-01-13 17:00:09 +08:00
} else {
for _, accessLog := range accessLogs {
if len(accessLog.RemoteAddr) > 0 {
if !lists.ContainsString(ipList, accessLog.RemoteAddr) {
ipList = append(ipList, accessLog.RemoteAddr)
}
}
}
2020-10-10 19:22:17 +08:00
}
this.Data["accessLogs"] = accessLogs
if len(accessLogs) > 0 {
this.Data["requestId"] = accessLogs[0].RequestId
} else {
this.Data["requestId"] = params.RequestId
}
this.Data["hasMore"] = accessLogsResp.HasMore
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-10 19:22:17 +08:00
this.Success()
}