* 优化图标和LOGO * 修改登录页面动画的速度为3 * 增加对websocket的异常处理 * 修复了用户组和用户名唯一判断错误的问题 * 提示版本号 * 修复readme错别字 * 修复单词拼写错误的问题 * 修复代码格式 * 修改Windows资产属性名称 * Docker 打包流程增加 upx 压缩 * 升级依赖文件,修改sqlite驱动为 github.com/glebarez/sqlite * 修复第一次查询「授权令牌」的错误 * 移除无关代码 * 修改docker打包脚本 * 增加打包脚本 * 增加微信群 * 修复单词拼写错误的问题 * 修复代码格式 * 修改Windows资产属性名称 * Docker 打包流程增加 upx 压缩 * 修改docker打包脚本 * - 替换 sqlite 驱动为 github.com/glebarez/sqlite - 修复数据库锁定的问题 - 修复部分代码不完善的问题 - 修复策略显示不完整的问题 - 修复编辑文件换行符的问题 - 优化guacd连接
120 lines
3.1 KiB
Go
120 lines
3.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
|
|
"next-terminal/server/model"
|
|
"next-terminal/server/utils"
|
|
|
|
"next-terminal/server/config"
|
|
"next-terminal/server/repository"
|
|
)
|
|
|
|
type credentialService struct {
|
|
}
|
|
|
|
func (s credentialService) EncryptAll() error {
|
|
items, err := repository.CredentialRepository.FindAll(context.TODO())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i := range items {
|
|
item := items[i]
|
|
if item.Encrypted {
|
|
continue
|
|
}
|
|
if err := s.Encrypt(&item, config.GlobalCfg.EncryptionPassword); err != nil {
|
|
return err
|
|
}
|
|
if err := repository.CredentialRepository.UpdateById(context.TODO(), &item, item.ID); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s credentialService) Encrypt(item *model.Credential, password []byte) error {
|
|
if item.Password != "-" {
|
|
encryptedCBC, err := utils.AesEncryptCBC([]byte(item.Password), password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
item.Password = base64.StdEncoding.EncodeToString(encryptedCBC)
|
|
}
|
|
if item.PrivateKey != "-" {
|
|
encryptedCBC, err := utils.AesEncryptCBC([]byte(item.PrivateKey), password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
item.PrivateKey = base64.StdEncoding.EncodeToString(encryptedCBC)
|
|
}
|
|
if item.Passphrase != "-" {
|
|
encryptedCBC, err := utils.AesEncryptCBC([]byte(item.Passphrase), password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
item.Passphrase = base64.StdEncoding.EncodeToString(encryptedCBC)
|
|
}
|
|
item.Encrypted = true
|
|
return nil
|
|
}
|
|
|
|
func (s credentialService) Decrypt(item *model.Credential, password []byte) error {
|
|
if item.Encrypted {
|
|
if item.Password != "" && item.Password != "-" {
|
|
origData, err := base64.StdEncoding.DecodeString(item.Password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
decryptedCBC, err := utils.AesDecryptCBC(origData, password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
item.Password = string(decryptedCBC)
|
|
}
|
|
if item.PrivateKey != "" && item.PrivateKey != "-" {
|
|
origData, err := base64.StdEncoding.DecodeString(item.PrivateKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
decryptedCBC, err := utils.AesDecryptCBC(origData, password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
item.PrivateKey = string(decryptedCBC)
|
|
}
|
|
if item.Passphrase != "" && item.Passphrase != "-" {
|
|
origData, err := base64.StdEncoding.DecodeString(item.Passphrase)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
decryptedCBC, err := utils.AesDecryptCBC(origData, password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
item.Passphrase = string(decryptedCBC)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s credentialService) FindByIdAndDecrypt(ctx context.Context, id string) (o model.Credential, err error) {
|
|
credential, err := repository.CredentialRepository.FindById(ctx, id)
|
|
if err != nil {
|
|
return o, err
|
|
}
|
|
if err := s.Decrypt(&credential, config.GlobalCfg.EncryptionPassword); err != nil {
|
|
return o, err
|
|
}
|
|
return credential, nil
|
|
}
|
|
|
|
func (s credentialService) Create(ctx context.Context, item *model.Credential) error {
|
|
// 加密密码之后进行存储
|
|
if err := s.Encrypt(item, config.GlobalCfg.EncryptionPassword); err != nil {
|
|
return err
|
|
}
|
|
return repository.CredentialRepository.Create(ctx, item)
|
|
}
|