2021-07-28 18:03:19 +08:00
|
|
|
package entity
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-05 08:55:34 +08:00
|
|
|
"errors"
|
2022-08-02 21:44:01 +08:00
|
|
|
"mayfly-go/internal/common/utils"
|
2023-10-27 17:41:45 +08:00
|
|
|
"mayfly-go/internal/redis/rdm"
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/pkg/model"
|
2023-10-27 17:41:45 +08:00
|
|
|
"mayfly-go/pkg/utils/structx"
|
2021-07-28 18:03:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Redis struct {
|
|
|
|
|
model.Model
|
|
|
|
|
|
2023-12-05 23:03:51 +08:00
|
|
|
Code string `orm:"column(code)" json:"code"`
|
2022-10-26 20:49:29 +08:00
|
|
|
Name string `orm:"column(name)" json:"name"`
|
2022-07-20 23:25:52 +08:00
|
|
|
Host string `orm:"column(host)" json:"host"`
|
|
|
|
|
Mode string `json:"mode"`
|
2023-07-20 22:41:13 +08:00
|
|
|
Username string `json:"username"`
|
2022-07-20 23:25:52 +08:00
|
|
|
Password string `orm:"column(password)" json:"-"`
|
2022-09-29 13:14:50 +08:00
|
|
|
Db string `orm:"column(database)" json:"db"`
|
2023-03-06 16:59:57 +08:00
|
|
|
SshTunnelMachineId int `orm:"column(ssh_tunnel_machine_id)" json:"sshTunnelMachineId"` // ssh隧道机器id
|
2022-07-20 23:25:52 +08:00
|
|
|
Remark string
|
2021-07-28 18:03:19 +08:00
|
|
|
}
|
2022-07-10 12:14:06 +08:00
|
|
|
|
2024-01-05 08:55:34 +08:00
|
|
|
func (r *Redis) PwdEncrypt() error {
|
2022-08-02 21:44:01 +08:00
|
|
|
// 密码替换为加密后的密码
|
2024-01-05 08:55:34 +08:00
|
|
|
password, err := utils.PwdAesEncrypt(r.Password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.New("加密 Redis 密码失败")
|
|
|
|
|
}
|
|
|
|
|
r.Password = password
|
|
|
|
|
return nil
|
2022-08-02 21:44:01 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 08:55:34 +08:00
|
|
|
func (r *Redis) PwdDecrypt() error {
|
2022-08-02 21:44:01 +08:00
|
|
|
// 密码替换为解密后的密码
|
2024-01-05 08:55:34 +08:00
|
|
|
password, err := utils.PwdAesDecrypt(r.Password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.New("解密 Redis 密码失败")
|
|
|
|
|
}
|
|
|
|
|
r.Password = password
|
|
|
|
|
return nil
|
2022-08-02 21:44:01 +08:00
|
|
|
}
|
2023-10-27 17:41:45 +08:00
|
|
|
|
2024-01-05 08:55:34 +08:00
|
|
|
// ToRedisInfo 转换为redisInfo进行连接
|
|
|
|
|
func (r *Redis) ToRedisInfo(db int, tagPath ...string) *rdm.RedisInfo {
|
2023-10-27 17:41:45 +08:00
|
|
|
redisInfo := new(rdm.RedisInfo)
|
2024-01-05 08:55:34 +08:00
|
|
|
_ = structx.Copy(redisInfo, r)
|
2023-10-27 17:41:45 +08:00
|
|
|
redisInfo.Db = db
|
2023-12-05 23:03:51 +08:00
|
|
|
redisInfo.TagPath = tagPath
|
2023-10-27 17:41:45 +08:00
|
|
|
return redisInfo
|
|
|
|
|
}
|