Initial commit (code only without large binaries)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package log
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
)
|
||||
|
||||
type BaseAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *BaseAction) initClusterAccessLogConfig(serverId int64) bool {
|
||||
this.Data["clusterAccessLogIsOn"] = true
|
||||
var clusterId int64
|
||||
serverResp, err := this.RPC().ServerRPC().FindEnabledUserServerBasic(this.AdminContext(), &pb.FindEnabledUserServerBasicRequest{ServerId: serverId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return false
|
||||
}
|
||||
if serverResp.Server == nil {
|
||||
this.NotFound("Server", serverId)
|
||||
return false
|
||||
}
|
||||
if serverResp.Server.NodeCluster != nil && serverResp.Server.NodeCluster.Id > 0 {
|
||||
clusterId = serverResp.Server.NodeCluster.Id
|
||||
}
|
||||
|
||||
if clusterId > 0 {
|
||||
globalServerConfigResp, err := this.RPC().NodeClusterRPC().FindNodeClusterGlobalServerConfig(this.AdminContext(), &pb.FindNodeClusterGlobalServerConfigRequest{NodeClusterId: clusterId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return false
|
||||
}
|
||||
|
||||
if len(globalServerConfigResp.GlobalServerConfigJSON) > 0 {
|
||||
var globalServerConfig = serverconfigs.NewGlobalServerConfig()
|
||||
err = json.Unmarshal(globalServerConfigResp.GlobalServerConfigJSON, globalServerConfig)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return false
|
||||
}
|
||||
this.Data["clusterAccessLogIsOn"] = globalServerConfig.HTTPAccessLog.IsOn
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HistoryAction struct {
|
||||
BaseAction
|
||||
}
|
||||
|
||||
func (this *HistoryAction) Init() {
|
||||
this.Nav("", "log", "")
|
||||
this.SecondMenu("history")
|
||||
}
|
||||
|
||||
func (this *HistoryAction) RunGet(params struct {
|
||||
ServerId int64
|
||||
Day string
|
||||
Hour string
|
||||
Keyword string
|
||||
Ip string
|
||||
Domain string
|
||||
HasWAF int
|
||||
|
||||
RequestId string
|
||||
HasError int
|
||||
|
||||
ClusterId int64
|
||||
NodeId int64
|
||||
|
||||
Partition int32 `default:"-1"`
|
||||
|
||||
PageSize int
|
||||
}) {
|
||||
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["hour"] = params.Hour
|
||||
this.Data["keyword"] = params.Keyword
|
||||
this.Data["ip"] = params.Ip
|
||||
this.Data["domain"] = params.Domain
|
||||
this.Data["accessLogs"] = []interface{}{}
|
||||
this.Data["hasError"] = params.HasError
|
||||
this.Data["hasWAF"] = params.HasWAF
|
||||
this.Data["pageSize"] = params.PageSize
|
||||
this.Data["clusterId"] = params.ClusterId
|
||||
this.Data["nodeId"] = params.NodeId
|
||||
this.Data["partition"] = params.Partition
|
||||
|
||||
// 检查集群全局设置
|
||||
if !this.initClusterAccessLogConfig(params.ServerId) {
|
||||
return
|
||||
}
|
||||
|
||||
// 检查当前网站有无开启访问日志
|
||||
this.Data["serverAccessLogIsOn"] = true
|
||||
|
||||
groupResp, err := this.RPC().ServerGroupRPC().FindEnabledServerGroupConfigInfo(this.AdminContext(), &pb.FindEnabledServerGroupConfigInfoRequest{
|
||||
ServerId: params.ServerId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if !groupResp.HasAccessLogConfig {
|
||||
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if webConfig != nil && webConfig.AccessLogRef != nil && !webConfig.AccessLogRef.IsOn {
|
||||
this.Data["serverAccessLogIsOn"] = false
|
||||
}
|
||||
}
|
||||
|
||||
var day = params.Day
|
||||
var ipList = []string{}
|
||||
var wafMaps = []maps.Map{}
|
||||
|
||||
if len(day) > 0 && regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(day) {
|
||||
day = strings.ReplaceAll(day, "-", "")
|
||||
size := int64(params.PageSize)
|
||||
if size < 1 {
|
||||
size = 20
|
||||
}
|
||||
|
||||
this.Data["hasError"] = params.HasError
|
||||
|
||||
resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
|
||||
Partition: params.Partition,
|
||||
RequestId: params.RequestId,
|
||||
ServerId: params.ServerId,
|
||||
HasError: params.HasError > 0,
|
||||
HasFirewallPolicy: params.HasWAF > 0,
|
||||
Day: day,
|
||||
HourFrom: params.Hour,
|
||||
HourTo: params.Hour,
|
||||
Keyword: params.Keyword,
|
||||
Ip: params.Ip,
|
||||
Domain: params.Domain,
|
||||
NodeId: params.NodeId,
|
||||
NodeClusterId: params.ClusterId,
|
||||
Size: size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(resp.HttpAccessLogs) == 0 {
|
||||
this.Data["accessLogs"] = []interface{}{}
|
||||
} else {
|
||||
this.Data["accessLogs"] = resp.HttpAccessLogs
|
||||
for _, accessLog := range resp.HttpAccessLogs {
|
||||
// IP
|
||||
if len(accessLog.RemoteAddr) > 0 {
|
||||
if !lists.ContainsString(ipList, accessLog.RemoteAddr) {
|
||||
ipList = append(ipList, accessLog.RemoteAddr)
|
||||
}
|
||||
}
|
||||
|
||||
// WAF信息集合
|
||||
if accessLog.FirewallPolicyId > 0 && accessLog.FirewallRuleGroupId > 0 && accessLog.FirewallRuleSetId > 0 {
|
||||
// 检查Set是否已经存在
|
||||
var existSet = false
|
||||
for _, wafMap := range wafMaps {
|
||||
if wafMap.GetInt64("setId") == accessLog.FirewallRuleSetId {
|
||||
existSet = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !existSet {
|
||||
wafMaps = append(wafMaps, maps.Map{
|
||||
"policyId": accessLog.FirewallPolicyId,
|
||||
"groupId": accessLog.FirewallRuleGroupId,
|
||||
"setId": accessLog.FirewallRuleSetId,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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{
|
||||
Partition: params.Partition,
|
||||
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,
|
||||
NodeId: params.NodeId,
|
||||
NodeClusterId: params.ClusterId,
|
||||
Size: size,
|
||||
Reverse: true,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if int64(len(prevResp.HttpAccessLogs)) == size {
|
||||
this.Data["lastRequestId"] = prevResp.RequestId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据IP查询区域
|
||||
this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
|
||||
|
||||
// WAF相关
|
||||
var wafInfos = map[int64]maps.Map{} // set id => WAF Map
|
||||
var wafPolicyCacheMap = map[int64]*pb.HTTPFirewallPolicy{} // id => *pb.HTTPFirewallPolicy
|
||||
var wafGroupCacheMap = map[int64]*pb.HTTPFirewallRuleGroup{} // id => *pb.HTTPFirewallRuleGroup
|
||||
var wafSetCacheMap = map[int64]*pb.HTTPFirewallRuleSet{} // id => *pb.HTTPFirewallRuleSet
|
||||
for _, wafMap := range wafMaps {
|
||||
var policyId = wafMap.GetInt64("policyId")
|
||||
var groupId = wafMap.GetInt64("groupId")
|
||||
var setId = wafMap.GetInt64("setId")
|
||||
if policyId > 0 {
|
||||
pbPolicy, ok := wafPolicyCacheMap[policyId]
|
||||
if !ok {
|
||||
policyResp, err := this.RPC().HTTPFirewallPolicyRPC().FindEnabledHTTPFirewallPolicy(this.AdminContext(), &pb.FindEnabledHTTPFirewallPolicyRequest{HttpFirewallPolicyId: policyId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbPolicy = policyResp.HttpFirewallPolicy
|
||||
wafPolicyCacheMap[policyId] = pbPolicy
|
||||
}
|
||||
if pbPolicy != nil {
|
||||
wafMap = maps.Map{
|
||||
"policy": maps.Map{
|
||||
"id": pbPolicy.Id,
|
||||
"name": pbPolicy.Name,
|
||||
"serverId": pbPolicy.ServerId,
|
||||
},
|
||||
}
|
||||
if groupId > 0 {
|
||||
pbGroup, ok := wafGroupCacheMap[groupId]
|
||||
if !ok {
|
||||
groupResp, err := this.RPC().HTTPFirewallRuleGroupRPC().FindEnabledHTTPFirewallRuleGroup(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleGroupRequest{FirewallRuleGroupId: groupId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbGroup = groupResp.FirewallRuleGroup
|
||||
wafGroupCacheMap[groupId] = pbGroup
|
||||
}
|
||||
|
||||
if pbGroup != nil {
|
||||
wafMap["group"] = maps.Map{
|
||||
"id": pbGroup.Id,
|
||||
"name": pbGroup.Name,
|
||||
}
|
||||
|
||||
if setId > 0 {
|
||||
pbSet, ok := wafSetCacheMap[setId]
|
||||
if !ok {
|
||||
setResp, err := this.RPC().HTTPFirewallRuleSetRPC().FindEnabledHTTPFirewallRuleSet(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleSetRequest{FirewallRuleSetId: setId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbSet = setResp.FirewallRuleSet
|
||||
wafSetCacheMap[setId] = pbSet
|
||||
}
|
||||
|
||||
if pbSet != nil {
|
||||
wafMap["set"] = maps.Map{
|
||||
"id": pbSet.Id,
|
||||
"name": pbSet.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wafInfos[setId] = wafMap
|
||||
}
|
||||
this.Data["wafInfos"] = wafInfos
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
BaseAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "log", "")
|
||||
this.SecondMenu("index")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
ServerId int64
|
||||
RequestId string
|
||||
Ip string
|
||||
Domain string
|
||||
ClusterId int64
|
||||
NodeId int64
|
||||
Keyword string
|
||||
}) {
|
||||
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
|
||||
this.Data["clusterId"] = params.ClusterId
|
||||
this.Data["nodeId"] = params.NodeId
|
||||
|
||||
// 检查集群全局设置
|
||||
if !this.initClusterAccessLogConfig(params.ServerId) {
|
||||
return
|
||||
}
|
||||
|
||||
// 检查当前网站有无开启访问日志
|
||||
this.Data["serverAccessLogIsOn"] = true
|
||||
|
||||
groupResp, err := this.RPC().ServerGroupRPC().FindEnabledServerGroupConfigInfo(this.AdminContext(), &pb.FindEnabledServerGroupConfigInfoRequest{
|
||||
ServerId: params.ServerId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if !groupResp.HasAccessLogConfig {
|
||||
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if webConfig != nil && webConfig.AccessLogRef != nil && !webConfig.AccessLogRef.IsOn {
|
||||
this.Data["serverAccessLogIsOn"] = false
|
||||
}
|
||||
}
|
||||
|
||||
// 记录最近使用
|
||||
_, err = this.RPC().LatestItemRPC().IncreaseLatestItem(this.AdminContext(), &pb.IncreaseLatestItemRequest{
|
||||
ItemType: "server",
|
||||
ItemId: params.ServerId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunPost(params struct {
|
||||
ServerId int64
|
||||
RequestId string
|
||||
Keyword string
|
||||
Ip string
|
||||
Domain string
|
||||
ClusterId int64
|
||||
NodeId int64
|
||||
|
||||
Partition int32 `default:"-1"`
|
||||
|
||||
Must *actions.Must
|
||||
}) {
|
||||
var isReverse = len(params.RequestId) > 0
|
||||
accessLogsResp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
|
||||
Partition: params.Partition,
|
||||
ServerId: params.ServerId,
|
||||
RequestId: params.RequestId,
|
||||
Size: 20,
|
||||
Day: timeutil.Format("Ymd"),
|
||||
Keyword: params.Keyword,
|
||||
Ip: params.Ip,
|
||||
Domain: params.Domain,
|
||||
NodeId: params.NodeId,
|
||||
NodeClusterId: params.ClusterId,
|
||||
Reverse: isReverse,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var ipList = []string{}
|
||||
var wafMaps = []maps.Map{}
|
||||
|
||||
var accessLogs = accessLogsResp.HttpAccessLogs
|
||||
if len(accessLogs) == 0 {
|
||||
accessLogs = []*pb.HTTPAccessLog{}
|
||||
} else {
|
||||
for _, accessLog := range accessLogs {
|
||||
// IP
|
||||
if len(accessLog.RemoteAddr) > 0 {
|
||||
if !lists.ContainsString(ipList, accessLog.RemoteAddr) {
|
||||
ipList = append(ipList, accessLog.RemoteAddr)
|
||||
}
|
||||
}
|
||||
|
||||
// WAF信息集合
|
||||
if accessLog.FirewallPolicyId > 0 && accessLog.FirewallRuleGroupId > 0 && accessLog.FirewallRuleSetId > 0 {
|
||||
// 检查Set是否已经存在
|
||||
var existSet = false
|
||||
for _, wafMap := range wafMaps {
|
||||
if wafMap.GetInt64("setId") == accessLog.FirewallRuleSetId {
|
||||
existSet = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !existSet {
|
||||
wafMaps = append(wafMaps, maps.Map{
|
||||
"policyId": accessLog.FirewallPolicyId,
|
||||
"groupId": accessLog.FirewallRuleGroupId,
|
||||
"setId": accessLog.FirewallRuleSetId,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
// 根据IP查询区域
|
||||
this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
|
||||
|
||||
// WAF相关
|
||||
var wafInfos = map[int64]maps.Map{} // set id => WAF Map
|
||||
var wafPolicyCacheMap = map[int64]*pb.HTTPFirewallPolicy{} // id => *pb.HTTPFirewallPolicy
|
||||
var wafGroupCacheMap = map[int64]*pb.HTTPFirewallRuleGroup{} // id => *pb.HTTPFirewallRuleGroup
|
||||
var wafSetCacheMap = map[int64]*pb.HTTPFirewallRuleSet{} // id => *pb.HTTPFirewallRuleSet
|
||||
for _, wafMap := range wafMaps {
|
||||
var policyId = wafMap.GetInt64("policyId")
|
||||
var groupId = wafMap.GetInt64("groupId")
|
||||
var setId = wafMap.GetInt64("setId")
|
||||
if policyId > 0 {
|
||||
pbPolicy, ok := wafPolicyCacheMap[policyId]
|
||||
if !ok {
|
||||
policyResp, err := this.RPC().HTTPFirewallPolicyRPC().FindEnabledHTTPFirewallPolicy(this.AdminContext(), &pb.FindEnabledHTTPFirewallPolicyRequest{HttpFirewallPolicyId: policyId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbPolicy = policyResp.HttpFirewallPolicy
|
||||
wafPolicyCacheMap[policyId] = pbPolicy
|
||||
}
|
||||
if pbPolicy != nil {
|
||||
wafMap = maps.Map{
|
||||
"policy": maps.Map{
|
||||
"id": pbPolicy.Id,
|
||||
"name": pbPolicy.Name,
|
||||
"serverId": pbPolicy.ServerId,
|
||||
},
|
||||
}
|
||||
if groupId > 0 {
|
||||
pbGroup, ok := wafGroupCacheMap[groupId]
|
||||
if !ok {
|
||||
groupResp, err := this.RPC().HTTPFirewallRuleGroupRPC().FindEnabledHTTPFirewallRuleGroup(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleGroupRequest{FirewallRuleGroupId: groupId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbGroup = groupResp.FirewallRuleGroup
|
||||
wafGroupCacheMap[groupId] = pbGroup
|
||||
}
|
||||
|
||||
if pbGroup != nil {
|
||||
wafMap["group"] = maps.Map{
|
||||
"id": pbGroup.Id,
|
||||
"name": pbGroup.Name,
|
||||
}
|
||||
|
||||
if setId > 0 {
|
||||
pbSet, ok := wafSetCacheMap[setId]
|
||||
if !ok {
|
||||
setResp, err := this.RPC().HTTPFirewallRuleSetRPC().FindEnabledHTTPFirewallRuleSet(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleSetRequest{FirewallRuleSetId: setId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbSet = setResp.FirewallRuleSet
|
||||
wafSetCacheMap[setId] = pbSet
|
||||
}
|
||||
|
||||
if pbSet != nil {
|
||||
wafMap["set"] = maps.Map{
|
||||
"id": pbSet.Id,
|
||||
"name": pbSet.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wafInfos[setId] = wafMap
|
||||
}
|
||||
this.Data["wafInfos"] = wafInfos
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/serverutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeServer)).
|
||||
Helper(serverutils.NewServerHelper()).
|
||||
Prefix("/servers/server/log").
|
||||
GetPost("", new(IndexAction)).
|
||||
GetPost("/today", new(TodayAction)).
|
||||
GetPost("/history", new(HistoryAction)).
|
||||
Get("/viewPopup", new(ViewPopupAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/dao"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
type TodayAction struct {
|
||||
BaseAction
|
||||
}
|
||||
|
||||
func (this *TodayAction) Init() {
|
||||
this.Nav("", "log", "")
|
||||
this.SecondMenu("today")
|
||||
}
|
||||
|
||||
func (this *TodayAction) RunGet(params struct {
|
||||
RequestId string
|
||||
ServerId int64
|
||||
HasError int
|
||||
HasWAF int
|
||||
Keyword string
|
||||
Ip string
|
||||
Domain string
|
||||
ClusterId int64
|
||||
NodeId int64
|
||||
|
||||
Partition int32 `default:"-1"`
|
||||
|
||||
PageSize int
|
||||
}) {
|
||||
this.Data["pageSize"] = params.PageSize
|
||||
|
||||
var size = int64(params.PageSize)
|
||||
if size < 1 {
|
||||
size = 20
|
||||
}
|
||||
|
||||
this.Data["path"] = this.Request.URL.Path
|
||||
this.Data["hasError"] = params.HasError
|
||||
this.Data["keyword"] = params.Keyword
|
||||
this.Data["ip"] = params.Ip
|
||||
this.Data["domain"] = params.Domain
|
||||
this.Data["hasWAF"] = params.HasWAF
|
||||
this.Data["clusterId"] = params.ClusterId
|
||||
this.Data["nodeId"] = params.NodeId
|
||||
this.Data["partition"] = params.Partition
|
||||
this.Data["day"] = timeutil.Format("Ymd")
|
||||
|
||||
// 检查集群全局设置
|
||||
if !this.initClusterAccessLogConfig(params.ServerId) {
|
||||
return
|
||||
}
|
||||
|
||||
// 检查当前网站有无开启访问日志
|
||||
this.Data["serverAccessLogIsOn"] = true
|
||||
|
||||
groupResp, err := this.RPC().ServerGroupRPC().FindEnabledServerGroupConfigInfo(this.AdminContext(), &pb.FindEnabledServerGroupConfigInfoRequest{
|
||||
ServerId: params.ServerId,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if !groupResp.HasAccessLogConfig {
|
||||
webConfig, err := dao.SharedHTTPWebDAO.FindWebConfigWithServerId(this.AdminContext(), params.ServerId)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if webConfig != nil && webConfig.AccessLogRef != nil && !webConfig.AccessLogRef.IsOn {
|
||||
this.Data["serverAccessLogIsOn"] = false
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
|
||||
Partition: params.Partition,
|
||||
RequestId: params.RequestId,
|
||||
ServerId: params.ServerId,
|
||||
HasError: params.HasError > 0,
|
||||
HasFirewallPolicy: params.HasWAF > 0,
|
||||
Day: timeutil.Format("Ymd"),
|
||||
Keyword: params.Keyword,
|
||||
Ip: params.Ip,
|
||||
Domain: params.Domain,
|
||||
NodeId: params.NodeId,
|
||||
NodeClusterId: params.ClusterId,
|
||||
Size: size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var ipList = []string{}
|
||||
var wafMaps = []maps.Map{}
|
||||
|
||||
if len(resp.HttpAccessLogs) == 0 {
|
||||
this.Data["accessLogs"] = []interface{}{}
|
||||
} else {
|
||||
this.Data["accessLogs"] = resp.HttpAccessLogs
|
||||
for _, accessLog := range resp.HttpAccessLogs {
|
||||
// IP
|
||||
if len(accessLog.RemoteAddr) > 0 {
|
||||
if !lists.ContainsString(ipList, accessLog.RemoteAddr) {
|
||||
ipList = append(ipList, accessLog.RemoteAddr)
|
||||
}
|
||||
}
|
||||
|
||||
// WAF信息集合
|
||||
if accessLog.FirewallPolicyId > 0 && accessLog.FirewallRuleGroupId > 0 && accessLog.FirewallRuleSetId > 0 {
|
||||
// 检查Set是否已经存在
|
||||
var existSet = false
|
||||
for _, wafMap := range wafMaps {
|
||||
if wafMap.GetInt64("setId") == accessLog.FirewallRuleSetId {
|
||||
existSet = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !existSet {
|
||||
wafMaps = append(wafMaps, maps.Map{
|
||||
"policyId": accessLog.FirewallPolicyId,
|
||||
"groupId": accessLog.FirewallRuleGroupId,
|
||||
"setId": accessLog.FirewallRuleSetId,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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{
|
||||
Partition: params.Partition,
|
||||
RequestId: params.RequestId,
|
||||
ServerId: params.ServerId,
|
||||
HasError: params.HasError > 0,
|
||||
HasFirewallPolicy: params.HasWAF > 0,
|
||||
Day: timeutil.Format("Ymd"),
|
||||
Keyword: params.Keyword,
|
||||
Ip: params.Ip,
|
||||
Domain: params.Domain,
|
||||
NodeId: params.NodeId,
|
||||
NodeClusterId: params.ClusterId,
|
||||
Size: size,
|
||||
Reverse: true,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if int64(len(prevResp.HttpAccessLogs)) == size {
|
||||
this.Data["lastRequestId"] = prevResp.RequestId
|
||||
}
|
||||
}
|
||||
|
||||
// 根据IP查询区域
|
||||
this.Data["regions"] = iplibrary.LookupIPSummaries(ipList)
|
||||
|
||||
// WAF相关
|
||||
var wafInfos = map[int64]maps.Map{} // set id => WAF Map
|
||||
var wafPolicyCacheMap = map[int64]*pb.HTTPFirewallPolicy{} // id => *pb.HTTPFirewallPolicy
|
||||
var wafGroupCacheMap = map[int64]*pb.HTTPFirewallRuleGroup{} // id => *pb.HTTPFirewallRuleGroup
|
||||
var wafSetCacheMap = map[int64]*pb.HTTPFirewallRuleSet{} // id => *pb.HTTPFirewallRuleSet
|
||||
for _, wafMap := range wafMaps {
|
||||
var policyId = wafMap.GetInt64("policyId")
|
||||
var groupId = wafMap.GetInt64("groupId")
|
||||
var setId = wafMap.GetInt64("setId")
|
||||
if policyId > 0 {
|
||||
pbPolicy, ok := wafPolicyCacheMap[policyId]
|
||||
if !ok {
|
||||
policyResp, err := this.RPC().HTTPFirewallPolicyRPC().FindEnabledHTTPFirewallPolicy(this.AdminContext(), &pb.FindEnabledHTTPFirewallPolicyRequest{HttpFirewallPolicyId: policyId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbPolicy = policyResp.HttpFirewallPolicy
|
||||
wafPolicyCacheMap[policyId] = pbPolicy
|
||||
}
|
||||
if pbPolicy != nil {
|
||||
wafMap = maps.Map{
|
||||
"policy": maps.Map{
|
||||
"id": pbPolicy.Id,
|
||||
"name": pbPolicy.Name,
|
||||
"serverId": pbPolicy.ServerId,
|
||||
},
|
||||
}
|
||||
if groupId > 0 {
|
||||
pbGroup, ok := wafGroupCacheMap[groupId]
|
||||
if !ok {
|
||||
groupResp, err := this.RPC().HTTPFirewallRuleGroupRPC().FindEnabledHTTPFirewallRuleGroup(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleGroupRequest{FirewallRuleGroupId: groupId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbGroup = groupResp.FirewallRuleGroup
|
||||
wafGroupCacheMap[groupId] = pbGroup
|
||||
}
|
||||
|
||||
if pbGroup != nil {
|
||||
wafMap["group"] = maps.Map{
|
||||
"id": pbGroup.Id,
|
||||
"name": pbGroup.Name,
|
||||
}
|
||||
|
||||
if setId > 0 {
|
||||
pbSet, ok := wafSetCacheMap[setId]
|
||||
if !ok {
|
||||
setResp, err := this.RPC().HTTPFirewallRuleSetRPC().FindEnabledHTTPFirewallRuleSet(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleSetRequest{FirewallRuleSetId: setId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
pbSet = setResp.FirewallRuleSet
|
||||
wafSetCacheMap[setId] = pbSet
|
||||
}
|
||||
|
||||
if pbSet != nil {
|
||||
wafMap["set"] = maps.Map{
|
||||
"id": pbSet.Id,
|
||||
"name": pbSet.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wafInfos[setId] = wafMap
|
||||
}
|
||||
this.Data["wafInfos"] = wafInfos
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ViewPopupAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *ViewPopupAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *ViewPopupAction) RunGet(params struct {
|
||||
RequestId string
|
||||
}) {
|
||||
accessLogResp, err := this.RPC().HTTPAccessLogRPC().FindHTTPAccessLog(this.AdminContext(), &pb.FindHTTPAccessLogRequest{RequestId: params.RequestId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
accessLog := accessLogResp.HttpAccessLog
|
||||
if accessLog == nil {
|
||||
this.WriteString("not found: " + params.RequestId)
|
||||
return
|
||||
}
|
||||
|
||||
// 状态
|
||||
if len(accessLog.StatusMessage) == 0 {
|
||||
accessLog.StatusMessage = http.StatusText(int(accessLog.Status))
|
||||
}
|
||||
|
||||
this.Data["accessLog"] = accessLog
|
||||
|
||||
// WAF相关
|
||||
var wafMap maps.Map = nil
|
||||
if accessLog.FirewallPolicyId > 0 {
|
||||
policyResp, err := this.RPC().HTTPFirewallPolicyRPC().FindEnabledHTTPFirewallPolicy(this.AdminContext(), &pb.FindEnabledHTTPFirewallPolicyRequest{HttpFirewallPolicyId: accessLog.FirewallPolicyId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if policyResp.HttpFirewallPolicy != nil {
|
||||
wafMap = maps.Map{
|
||||
"policy": maps.Map{
|
||||
"id": policyResp.HttpFirewallPolicy.Id,
|
||||
"name": policyResp.HttpFirewallPolicy.Name,
|
||||
"serverId": policyResp.HttpFirewallPolicy.ServerId,
|
||||
},
|
||||
}
|
||||
if accessLog.FirewallRuleGroupId > 0 {
|
||||
groupResp, err := this.RPC().HTTPFirewallRuleGroupRPC().FindEnabledHTTPFirewallRuleGroup(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleGroupRequest{FirewallRuleGroupId: accessLog.FirewallRuleGroupId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if groupResp.FirewallRuleGroup != nil {
|
||||
wafMap["group"] = maps.Map{
|
||||
"id": groupResp.FirewallRuleGroup.Id,
|
||||
"name": groupResp.FirewallRuleGroup.Name,
|
||||
}
|
||||
|
||||
if accessLog.FirewallRuleSetId > 0 {
|
||||
setResp, err := this.RPC().HTTPFirewallRuleSetRPC().FindEnabledHTTPFirewallRuleSet(this.AdminContext(), &pb.FindEnabledHTTPFirewallRuleSetRequest{FirewallRuleSetId: accessLog.FirewallRuleSetId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if setResp.FirewallRuleSet != nil {
|
||||
wafMap["set"] = maps.Map{
|
||||
"id": setResp.FirewallRuleSet.Id,
|
||||
"name": setResp.FirewallRuleSet.Name,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this.Data["wafInfo"] = wafMap
|
||||
|
||||
// 地域相关
|
||||
var regionMap maps.Map = nil
|
||||
var ipRegion = iplibrary.LookupIP(accessLog.RemoteAddr)
|
||||
if ipRegion != nil && ipRegion.IsOk() {
|
||||
regionMap = maps.Map{
|
||||
"full": ipRegion.RegionSummary(),
|
||||
"isp": ipRegion.ProviderName(),
|
||||
}
|
||||
}
|
||||
this.Data["region"] = regionMap
|
||||
|
||||
// 请求内容
|
||||
this.Data["requestBody"] = string(accessLog.RequestBody)
|
||||
this.Data["requestContentType"] = "text/plain"
|
||||
|
||||
requestContentType, ok := accessLog.Header["Content-Type"]
|
||||
if ok {
|
||||
if len(requestContentType.Values) > 0 {
|
||||
var contentType = requestContentType.Values[0]
|
||||
if strings.HasSuffix(contentType, "/json") || strings.Contains(contentType, "/json;") {
|
||||
this.Data["requestContentType"] = "application/json"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
Reference in New Issue
Block a user