58 lines
2.3 KiB
Go
58 lines
2.3 KiB
Go
// Package serversecurity 管理控制器:绑定 admin 受权限保护组(查询/统计)。
|
||
package serversecurity
|
||
|
||
import (
|
||
"context"
|
||
|
||
serversecurityv1 "service.xpcool.com/api/serversecurity/v1"
|
||
"service.xpcool.com/internal/model/dto"
|
||
"service.xpcool.com/internal/service/serversecurity"
|
||
)
|
||
|
||
// ManageController 实现安全日志查询与统计端点。
|
||
type ManageController struct{}
|
||
|
||
// NewManage 创建管理控制器。
|
||
func NewManage() *ManageController { return &ManageController{} }
|
||
|
||
// ListLog 安全日志分页查询(时间倒序)。
|
||
func (c *ManageController) ListLog(ctx context.Context, req *serversecurityv1.SecurityLogListReq) (res *serversecurityv1.SecurityLogListRes, err error) {
|
||
list, total, err := serversecurity.Security().List(ctx, dto.SecurityLogFilter{
|
||
Page: req.Page, Size: req.Size, SrcIp: req.SrcIp, EventType: req.EventType,
|
||
DestPort: req.DestPort, DateFrom: req.DateFrom, DateTo: req.DateTo,
|
||
})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
out := make([]*serversecurityv1.SecurityLogListItem, 0, len(list))
|
||
for i := range list {
|
||
v := &list[i]
|
||
out = append(out, &serversecurityv1.SecurityLogListItem{
|
||
Id: v.Id, LogTime: v.LogTime, SrcIp: v.SrcIp, SrcPort: v.SrcPort,
|
||
DestPort: v.DestPort, EventType: v.EventType, EventName: v.EventName,
|
||
Detail: v.Detail, CreatedAt: v.CreatedAt,
|
||
})
|
||
}
|
||
return &serversecurityv1.SecurityLogListRes{List: out, Total: total}, nil
|
||
}
|
||
|
||
// Stats 安全日志统计(默认最近24小时)。
|
||
func (c *ManageController) Stats(ctx context.Context, req *serversecurityv1.SecurityLogStatsReq) (res *serversecurityv1.SecurityLogStatsRes, err error) {
|
||
st, err := serversecurity.Security().Stats(ctx, req.DateFrom, req.DateTo)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
topIps := make([]serversecurityv1.SecurityTopIp, 0, len(st.TopIps))
|
||
for _, v := range st.TopIps {
|
||
topIps = append(topIps, serversecurityv1.SecurityTopIp{SrcIp: v.SrcIp, Count: v.Count})
|
||
}
|
||
byType := make([]serversecurityv1.SecurityByType, 0, len(st.ByType))
|
||
for _, v := range st.ByType {
|
||
byType = append(byType, serversecurityv1.SecurityByType{EventType: v.EventType, EventName: v.EventName, Count: v.Count})
|
||
}
|
||
return &serversecurityv1.SecurityLogStatsRes{
|
||
Total: st.Total, Failed: st.Failed, Banned: st.Banned, Accepted: st.Accepted,
|
||
TopIps: topIps, ByType: byType,
|
||
}, nil
|
||
}
|