2020-10-10 11:49:21 +08:00
|
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2021-07-29 16:50:59 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/accesslogs"
|
2020-10-10 11:49:21 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
|
|
|
|
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-06-02 11:53:24 +08:00
|
|
|
|
// HTTPAccessLogService 访问日志相关服务
|
2020-10-10 11:49:21 +08:00
|
|
|
|
type HTTPAccessLogService struct {
|
2021-01-01 20:49:09 +08:00
|
|
|
|
BaseService
|
2020-10-10 11:49:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-02 11:53:24 +08:00
|
|
|
|
// CreateHTTPAccessLogs 创建访问日志
|
2020-10-10 11:49:21 +08:00
|
|
|
|
func (this *HTTPAccessLogService) CreateHTTPAccessLogs(ctx context.Context, req *pb.CreateHTTPAccessLogsRequest) (*pb.CreateHTTPAccessLogsResponse, error) {
|
2020-10-10 19:21:32 +08:00
|
|
|
|
// 校验请求
|
2021-07-11 18:05:57 +08:00
|
|
|
|
_, _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode)
|
2020-10-10 11:49:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-02 11:53:24 +08:00
|
|
|
|
if len(req.HttpAccessLogs) == 0 {
|
2020-10-10 11:49:21 +08:00
|
|
|
|
return &pb.CreateHTTPAccessLogsResponse{}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
2021-06-02 11:53:24 +08:00
|
|
|
|
err = models.SharedHTTPAccessLogDAO.CreateHTTPAccessLogs(tx, req.HttpAccessLogs)
|
2020-10-10 11:49:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-29 16:50:59 +08:00
|
|
|
|
// 发送到访问日志策略
|
|
|
|
|
|
policyId, err := models.SharedHTTPAccessLogPolicyDAO.FindCurrentPublicPolicyId(tx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if policyId > 0 {
|
|
|
|
|
|
err = accesslogs.SharedStorageManager.Write(policyId, req.HttpAccessLogs)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-10 11:49:21 +08:00
|
|
|
|
return &pb.CreateHTTPAccessLogsResponse{}, nil
|
|
|
|
|
|
}
|
2020-10-10 19:21:32 +08:00
|
|
|
|
|
2021-06-02 11:53:24 +08:00
|
|
|
|
// ListHTTPAccessLogs 列出单页访问日志
|
2020-10-10 19:21:32 +08:00
|
|
|
|
func (this *HTTPAccessLogService) ListHTTPAccessLogs(ctx context.Context, req *pb.ListHTTPAccessLogsRequest) (*pb.ListHTTPAccessLogsResponse, error) {
|
|
|
|
|
|
// 校验请求
|
2021-01-01 20:49:09 +08:00
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-10-10 19:21:32 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
2021-01-01 20:49:09 +08:00
|
|
|
|
// 检查服务ID
|
|
|
|
|
|
if userId > 0 {
|
2021-01-20 16:46:19 +08:00
|
|
|
|
if req.UserId > 0 && userId != req.UserId {
|
|
|
|
|
|
return nil, this.PermissionError()
|
2021-01-01 20:49:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-20 16:46:19 +08:00
|
|
|
|
// 这里不用担心serverId <= 0 的情况,因为如果userId>0,则只会查询当前用户下的服务,不会产生安全问题
|
|
|
|
|
|
if req.ServerId > 0 {
|
|
|
|
|
|
err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2021-01-01 20:49:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-09 11:00:54 +08:00
|
|
|
|
accessLogs, requestId, hasMore, err := models.SharedHTTPAccessLogDAO.ListAccessLogs(tx, req.RequestId, req.Size, req.Day, req.HourFrom, req.HourTo, req.NodeClusterId, req.NodeId, req.ServerId, req.Reverse, req.HasError, req.FirewallPolicyId, req.FirewallRuleGroupId, req.FirewallRuleSetId, req.HasFirewallPolicy, req.UserId, req.Keyword, req.Ip, req.Domain)
|
2020-10-10 19:21:32 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result := []*pb.HTTPAccessLog{}
|
2021-08-10 11:15:15 +08:00
|
|
|
|
var pbNodeMap = map[int64]*pb.Node{}
|
|
|
|
|
|
var pbClusterMap = map[int64]*pb.NodeCluster{}
|
2020-10-10 19:21:32 +08:00
|
|
|
|
for _, accessLog := range accessLogs {
|
|
|
|
|
|
a, err := accessLog.ToPB()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2021-08-10 11:15:15 +08:00
|
|
|
|
|
|
|
|
|
|
// 节点 & 集群
|
|
|
|
|
|
pbNode, ok := pbNodeMap[a.NodeId]
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
a.Node = pbNode
|
|
|
|
|
|
} else {
|
|
|
|
|
|
node, err := models.SharedNodeDAO.FindEnabledNode(tx, a.NodeId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if node != nil {
|
|
|
|
|
|
pbNode = &pb.Node{Id: int64(node.Id), Name: node.Name}
|
|
|
|
|
|
|
|
|
|
|
|
var clusterId = int64(node.ClusterId)
|
|
|
|
|
|
pbCluster, ok := pbClusterMap[clusterId]
|
|
|
|
|
|
if ok {
|
|
|
|
|
|
pbNode.NodeCluster = pbCluster
|
|
|
|
|
|
} else {
|
|
|
|
|
|
cluster, err := models.SharedNodeClusterDAO.FindEnabledNodeCluster(tx, clusterId)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if cluster != nil {
|
|
|
|
|
|
pbCluster = &pb.NodeCluster{
|
|
|
|
|
|
Id: int64(cluster.Id),
|
|
|
|
|
|
Name: cluster.Name,
|
|
|
|
|
|
}
|
|
|
|
|
|
pbNode.NodeCluster = pbCluster
|
|
|
|
|
|
pbClusterMap[clusterId] = pbCluster
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pbNodeMap[a.NodeId] = pbNode
|
|
|
|
|
|
a.Node = pbNode
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-10 19:21:32 +08:00
|
|
|
|
result = append(result, a)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return &pb.ListHTTPAccessLogsResponse{
|
2021-06-02 11:53:24 +08:00
|
|
|
|
HttpAccessLogs: result,
|
2021-06-04 11:41:50 +08:00
|
|
|
|
AccessLogs: result, // TODO 仅仅为了兼容,当用户节点版本大于0.0.8时可以删除
|
2021-06-02 11:53:24 +08:00
|
|
|
|
HasMore: hasMore,
|
|
|
|
|
|
RequestId: requestId,
|
2020-10-10 19:21:32 +08:00
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
2020-11-02 21:15:31 +08:00
|
|
|
|
|
2021-06-02 11:53:24 +08:00
|
|
|
|
// FindHTTPAccessLog 查找单个日志
|
2020-11-02 21:15:31 +08:00
|
|
|
|
func (this *HTTPAccessLogService) FindHTTPAccessLog(ctx context.Context, req *pb.FindHTTPAccessLogRequest) (*pb.FindHTTPAccessLogResponse, error) {
|
|
|
|
|
|
// 校验请求
|
2021-01-14 18:01:00 +08:00
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-11-02 21:15:31 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
|
|
accessLog, err := models.SharedHTTPAccessLogDAO.FindAccessLogWithRequestId(tx, req.RequestId)
|
2020-11-02 21:15:31 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if accessLog == nil {
|
2021-06-02 11:53:24 +08:00
|
|
|
|
return &pb.FindHTTPAccessLogResponse{HttpAccessLog: nil}, nil
|
2020-11-02 21:15:31 +08:00
|
|
|
|
}
|
2021-01-14 18:01:00 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查权限
|
|
|
|
|
|
if userId > 0 {
|
2021-01-17 16:48:00 +08:00
|
|
|
|
err = models.SharedServerDAO.CheckUserServer(tx, userId, int64(accessLog.ServerId))
|
2021-01-14 18:01:00 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-02 21:15:31 +08:00
|
|
|
|
a, err := accessLog.ToPB()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2021-06-02 11:53:24 +08:00
|
|
|
|
return &pb.FindHTTPAccessLogResponse{HttpAccessLog: a}, nil
|
2020-11-02 21:15:31 +08:00
|
|
|
|
}
|