feat: 完善日志审计功能
- 实现文件系统日志(FilesystemLog)记录文件管理器操作 - 实现操作日志(OperationLog)记录用户操作行为 - 实现数据库SQL日志(DatabaseSQLLog)模型和API - 实现SSH会话命令记录(SessionCommand)含命令输出和风险等级 - 添加IP提取服务支持X-Real-IP和X-Forwarded-For - 添加日志自动清理功能 - 修复ProFormSwitch required验证问题 - 修复设置页面默认值问题 - 修复文件上传错误检测逻辑 - 修复资产树key前缀问题 - 添加VNC/RDP设置默认值 - 修复文件管理标题翻译
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var AccessLogRepository = new(accessLogRepository)
|
||||
|
||||
type accessLogRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r accessLogRepository) Create(c context.Context, o *model.AccessLog) error {
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r accessLogRepository) FindById(c context.Context, id string) (o model.AccessLog, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r accessLogRepository) FindByPage(c context.Context, pageIndex, pageSize int, domain, websiteId, accountId string) (o []model.AccessLog, total int64, err error) {
|
||||
db := r.GetDB(c).Model(&model.AccessLog{})
|
||||
if domain != "" {
|
||||
db = db.Where("domain LIKE ?", "%"+domain+"%")
|
||||
}
|
||||
if websiteId != "" {
|
||||
db = db.Where("website_id = ?", websiteId)
|
||||
}
|
||||
if accountId != "" {
|
||||
db = db.Where("account_id = ?", accountId)
|
||||
}
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r accessLogRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.AccessLog{}).Error
|
||||
}
|
||||
|
||||
func (r accessLogRepository) DeleteAll(c context.Context) error {
|
||||
return r.GetDB(c).Where("1 = 1").Delete(&model.AccessLog{}).Error
|
||||
}
|
||||
|
||||
func (r accessLogRepository) FindOutTimeLog(c context.Context, limit int) (o []model.AccessLog, err error) {
|
||||
err = r.GetDB(c).Where("strftime('%s', created) < strftime('%s', 'now', '-' || ? || ' days')", limit).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r accessLogRepository) Count(c context.Context) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.AccessLog{}).Count(&total).Error
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var DatabaseSQLLogRepository = new(databaseSQLLogRepository)
|
||||
|
||||
type databaseSQLLogRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r databaseSQLLogRepository) FindByPage(c context.Context, pageIndex, pageSize int, assetId, userId, status, source string, order, field string) (o []model.DatabaseSQLLog, total int64, err error) {
|
||||
m := model.DatabaseSQLLog{}
|
||||
db := r.GetDB(c).Table(m.TableName())
|
||||
dbCounter := r.GetDB(c).Table(m.TableName())
|
||||
|
||||
if assetId != "" {
|
||||
db = db.Where("asset_id = ?", assetId)
|
||||
dbCounter = dbCounter.Where("asset_id = ?", assetId)
|
||||
}
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("user_id = ?", userId)
|
||||
dbCounter = dbCounter.Where("user_id = ?", userId)
|
||||
}
|
||||
|
||||
if status != "" {
|
||||
db = db.Where("status = ?", status)
|
||||
dbCounter = dbCounter.Where("status = ?", status)
|
||||
}
|
||||
|
||||
if source != "" {
|
||||
db = db.Where("source = ?", source)
|
||||
dbCounter = dbCounter.Where("source = ?", source)
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
orderBy := "created desc"
|
||||
if order != "" && field != "" {
|
||||
orderBy = field + " " + order
|
||||
}
|
||||
|
||||
err = db.Order(orderBy).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
if o == nil {
|
||||
o = make([]model.DatabaseSQLLog, 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r databaseSQLLogRepository) Create(c context.Context, o *model.DatabaseSQLLog) error {
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r databaseSQLLogRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.DatabaseSQLLog{}).Error
|
||||
}
|
||||
|
||||
func (r databaseSQLLogRepository) DeleteAll(c context.Context) error {
|
||||
return r.GetDB(c).Where("1 = 1").Delete(&model.DatabaseSQLLog{}).Error
|
||||
}
|
||||
|
||||
func (r databaseSQLLogRepository) FindOutTimeLog(c context.Context, dayLimit int) (o []model.DatabaseSQLLog, err error) {
|
||||
limitTime := time.Now().Add(time.Duration(-dayLimit*24) * time.Hour)
|
||||
err = r.GetDB(c).Where("created < ?", limitTime).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r databaseSQLLogRepository) Count(c context.Context) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.DatabaseSQLLog{}).Count(&total).Error
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var FilesystemLogRepository = new(filesystemLogRepository)
|
||||
|
||||
type filesystemLogRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r filesystemLogRepository) FindByPage(c context.Context, pageIndex, pageSize int, action string) (o []model.FilesystemLog, total int64, err error) {
|
||||
m := model.FilesystemLog{}
|
||||
db := r.GetDB(c).Table(m.TableName())
|
||||
dbCounter := r.GetDB(c).Table(m.TableName())
|
||||
|
||||
if action != "" {
|
||||
db = db.Where("action = ?", action)
|
||||
dbCounter = dbCounter.Where("action = ?", action)
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
if o == nil {
|
||||
o = make([]model.FilesystemLog, 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r filesystemLogRepository) Create(c context.Context, o *model.FilesystemLog) error {
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r filesystemLogRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.FilesystemLog{}).Error
|
||||
}
|
||||
|
||||
func (r filesystemLogRepository) DeleteAll(c context.Context) error {
|
||||
return r.GetDB(c).Where("1 = 1").Delete(&model.FilesystemLog{}).Error
|
||||
}
|
||||
|
||||
func (r filesystemLogRepository) FindOutTimeLog(c context.Context, dayLimit int) (o []model.FilesystemLog, err error) {
|
||||
limitTime := time.Now().Add(time.Duration(-dayLimit*24) * time.Hour)
|
||||
err = r.GetDB(c).Where("created < ?", limitTime).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r filesystemLogRepository) Count(c context.Context) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.FilesystemLog{}).Count(&total).Error
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var OperationLogRepository = new(operationLogRepository)
|
||||
|
||||
type operationLogRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r operationLogRepository) FindByPage(c context.Context, pageIndex, pageSize int, accountName, action, status string, order, field string) (o []model.OperationLog, total int64, err error) {
|
||||
m := model.OperationLog{}
|
||||
db := r.GetDB(c).Table(m.TableName())
|
||||
dbCounter := r.GetDB(c).Table(m.TableName())
|
||||
|
||||
if accountName != "" {
|
||||
db = db.Where("account_name like ?", "%"+accountName+"%")
|
||||
dbCounter = dbCounter.Where("account_name like ?", "%"+accountName+"%")
|
||||
}
|
||||
|
||||
if action != "" {
|
||||
db = db.Where("action = ?", action)
|
||||
dbCounter = dbCounter.Where("action = ?", action)
|
||||
}
|
||||
|
||||
if status != "" {
|
||||
db = db.Where("status = ?", status)
|
||||
dbCounter = dbCounter.Where("status = ?", status)
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
orderBy := "created desc"
|
||||
if order != "" && field != "" {
|
||||
orderBy = field + " " + order
|
||||
}
|
||||
|
||||
err = db.Order(orderBy).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
if o == nil {
|
||||
o = make([]model.OperationLog, 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r operationLogRepository) Create(c context.Context, o *model.OperationLog) error {
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r operationLogRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.OperationLog{}).Error
|
||||
}
|
||||
|
||||
func (r operationLogRepository) DeleteAll(c context.Context) error {
|
||||
return r.GetDB(c).Where("1 = 1").Delete(&model.OperationLog{}).Error
|
||||
}
|
||||
|
||||
func (r operationLogRepository) FindOutTimeLog(c context.Context, dayLimit int) (o []model.OperationLog, err error) {
|
||||
limitTime := time.Now().Add(time.Duration(-dayLimit*24) * time.Hour)
|
||||
err = r.GetDB(c).Where("created < ?", limitTime).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r operationLogRepository) Count(c context.Context) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.OperationLog{}).Count(&total).Error
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var SessionCommandRepository = new(sessionCommandRepository)
|
||||
|
||||
type sessionCommandRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r sessionCommandRepository) FindBySessionId(c context.Context, sessionId string) (o []model.SessionCommand, err error) {
|
||||
err = r.GetDB(c).Where("session_id = ?", sessionId).Order("created asc").Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r sessionCommandRepository) FindByPage(c context.Context, pageIndex, pageSize int, sessionId string) (o []model.SessionCommand, total int64, err error) {
|
||||
m := model.SessionCommand{}
|
||||
db := r.GetDB(c).Table(m.TableName())
|
||||
dbCounter := r.GetDB(c).Table(m.TableName())
|
||||
|
||||
if sessionId != "" {
|
||||
db = db.Where("session_id = ?", sessionId)
|
||||
dbCounter = dbCounter.Where("session_id = ?", sessionId)
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
if o == nil {
|
||||
o = make([]model.SessionCommand, 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r sessionCommandRepository) Create(c context.Context, o *model.SessionCommand) error {
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r sessionCommandRepository) DeleteBySessionId(c context.Context, sessionId string) error {
|
||||
return r.GetDB(c).Where("session_id = ?", sessionId).Delete(&model.SessionCommand{}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user