feat: enhance logging and message encryption features
- Introduced new logging services: operation logs, login logs, and data change logs. - Added support for message content encryption in the Message model, including methods for encrypting and decrypting message segments. - Updated router to include admin log handling and integrated log service for access logging. - Modified configuration to support encryption settings.
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"carrot_bbs/internal/pkg/crypto"
|
||||
"carrot_bbs/internal/pkg/utils"
|
||||
)
|
||||
|
||||
@@ -138,30 +139,35 @@ func (s *MessageSegments) Scan(value interface{}) error {
|
||||
|
||||
// Message 消息实体
|
||||
// 使用雪花算法ID(string类型)和seq机制实现消息排序和增量同步
|
||||
// 支持消息内容加密存储
|
||||
type Message struct {
|
||||
ID string `gorm:"primaryKey;size:20" json:"id"` // 雪花算法ID(string类型)
|
||||
ConversationID string `gorm:"not null;size:20;index:idx_msg_conversation_seq,priority:1" json:"conversation_id"` // 会话ID(string类型)
|
||||
SenderID string `gorm:"column:sender_id;type:varchar(50);index;not null" json:"sender_id"` // 发送者ID (UUID格式)
|
||||
Seq int64 `gorm:"not null;index:idx_msg_conversation_seq,priority:2" json:"seq"` // 会话内序号,用于排序和增量同步
|
||||
Segments MessageSegments `gorm:"type:json" json:"segments"` // 消息链(结构体数组)
|
||||
ReplyToID *string `json:"reply_to_id,omitempty"` // 回复的消息ID(string类型)
|
||||
Status MessageStatus `gorm:"type:varchar(20);default:'normal'" json:"status"` // 消息状态
|
||||
ID string `gorm:"primaryKey;size:20" json:"id"` // 雪花算法ID(string类型)
|
||||
ConversationID string `gorm:"not null;size:20;index:idx_msg_conversation_seq,priority:1" json:"conversation_id"` // 会话ID(string类型)
|
||||
SenderID string `gorm:"column:sender_id;type:varchar(50);index;not null" json:"sender_id"` // 发送者ID (UUID格式)
|
||||
Seq int64 `gorm:"not null;index:idx_msg_conversation_seq,priority:2" json:"seq"` // 会话内序号,用于排序和增量同步
|
||||
|
||||
// 消息内容字段
|
||||
Segments MessageSegments `gorm:"-" json:"segments"` // 消息链(内存中使用,不映射到数据库)
|
||||
SegmentsEncrypted string `gorm:"column:segments;type:text" json:"-"` // 加密后的消息链(存储在数据库)
|
||||
SegmentsKeyVersion int `gorm:"column:segments_key_version;default:0" json:"segments_key_version"` // 密钥版本,用于密钥轮换
|
||||
|
||||
ReplyToID *string `json:"reply_to_id,omitempty"` // 回复的消息ID(string类型)
|
||||
Status MessageStatus `gorm:"type:varchar(20);default:'normal'" json:"status"` // 消息状态
|
||||
|
||||
// 新增字段:消息分类和系统消息类型
|
||||
Category MessageCategory `gorm:"type:varchar(20);default:'chat'" json:"category"` // 消息分类
|
||||
SystemType SystemMessageType `gorm:"type:varchar(30)" json:"system_type,omitempty"` // 系统消息类型
|
||||
ExtraData *ExtraData `gorm:"type:json" json:"extra_data,omitempty"` // 额外数据(JSON格式)
|
||||
|
||||
// @相关字段
|
||||
MentionUsers string `gorm:"type:text" json:"mention_users"` // @的用户ID列表,JSON数组
|
||||
MentionAll bool `gorm:"default:false" json:"mention_all"` // 是否@所有人
|
||||
|
||||
// 软删除
|
||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
||||
|
||||
// 时间戳
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||||
|
||||
// 内部标记,表示是否已解密
|
||||
decrypted bool `gorm:"-"`
|
||||
}
|
||||
|
||||
// SenderIDStr 返回发送者ID字符串(保持兼容性)
|
||||
@@ -169,7 +175,7 @@ func (m *Message) SenderIDStr() string {
|
||||
return m.SenderID
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前生成雪花算法ID
|
||||
// BeforeCreate 创建前生成雪花算法ID并加密消息内容
|
||||
func (m *Message) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.ID == "" {
|
||||
id, err := utils.GetSnowflake().GenerateID()
|
||||
@@ -178,6 +184,107 @@ func (m *Message) BeforeCreate(tx *gorm.DB) error {
|
||||
}
|
||||
m.ID = strconv.FormatInt(id, 10)
|
||||
}
|
||||
|
||||
// 加密消息内容
|
||||
return m.encryptSegments()
|
||||
}
|
||||
|
||||
// BeforeUpdate 更新前加密消息内容
|
||||
func (m *Message) BeforeUpdate(tx *gorm.DB) error {
|
||||
// 只有当 Segments 被修改时才重新加密
|
||||
if tx.Statement.Changed("Segments") || len(m.Segments) > 0 {
|
||||
return m.encryptSegments()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AfterFind 查询后自动解密消息内容
|
||||
func (m *Message) AfterFind(tx *gorm.DB) error {
|
||||
return m.decryptSegments()
|
||||
}
|
||||
|
||||
// AfterCreate 创建后解密消息内容(保持内存中的数据可读)
|
||||
func (m *Message) AfterCreate(tx *gorm.DB) error {
|
||||
m.decrypted = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// encryptSegments 加密消息段
|
||||
func (m *Message) encryptSegments() error {
|
||||
// 如果没有消息内容或加密器未初始化,跳过加密
|
||||
if len(m.Segments) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
encryptor := crypto.GetMessageEncryptor()
|
||||
if encryptor == nil {
|
||||
// 加密器未初始化时,直接存储JSON(兼容模式)
|
||||
data, err := json.Marshal(m.Segments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.SegmentsEncrypted = string(data)
|
||||
m.SegmentsKeyVersion = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
// 序列化消息段
|
||||
plaintext, err := json.Marshal(m.Segments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 加密
|
||||
ciphertext, err := encryptor.Encrypt(plaintext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.SegmentsEncrypted = ciphertext
|
||||
m.SegmentsKeyVersion = encryptor.GetKeyVersion()
|
||||
return nil
|
||||
}
|
||||
|
||||
// decryptSegments 解密消息段
|
||||
func (m *Message) decryptSegments() error {
|
||||
// 如果已经解密过,跳过
|
||||
if m.decrypted {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果没有加密内容,跳过
|
||||
if m.SegmentsEncrypted == "" {
|
||||
m.decrypted = true
|
||||
return nil
|
||||
}
|
||||
|
||||
encryptor := crypto.GetMessageEncryptor()
|
||||
if encryptor == nil {
|
||||
// 加密器未初始化,尝试直接解析JSON(兼容旧数据)
|
||||
if err := json.Unmarshal([]byte(m.SegmentsEncrypted), &m.Segments); err != nil {
|
||||
return err
|
||||
}
|
||||
m.decrypted = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// 尝试解密
|
||||
plaintext, err := encryptor.Decrypt(m.SegmentsEncrypted)
|
||||
if err != nil {
|
||||
// 解密失败,可能是未加密的旧数据,尝试直接解析
|
||||
if parseErr := json.Unmarshal([]byte(m.SegmentsEncrypted), &m.Segments); parseErr != nil {
|
||||
return err // 返回解密错误
|
||||
}
|
||||
m.decrypted = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// 解析解密后的数据
|
||||
if err := json.Unmarshal(plaintext, &m.Segments); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
m.decrypted = true
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user