Skip to content

Commit

Permalink
♻️ 重构本地数据库-credentials模块
Browse files Browse the repository at this point in the history
  • Loading branch information
chaos-zhu committed Oct 22, 2024
1 parent cdd741b commit 70bdaa5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 82 deletions.
44 changes: 19 additions & 25 deletions server/app/controller/ssh.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { readSSHRecord, writeSSHRecord } = require('../utils/storage')
const { RSADecryptAsync, AESEncryptAsync, AESDecryptAsync } = require('../utils/encrypt')
const { HostListDB } = require('../utils/db-class')
const { HostListDB, CredentialsDB } = require('../utils/db-class')
const hostListDB = new HostListDB().getInstance()
const credentialsDB = new CredentialsDB().getInstance()

async function getSSHList({ res }) {
// console.log('get-host-list')
let data = await readSSHRecord()
let data = await credentialsDB.findAsync({})
data = data?.map(item => {
const { name, authType, _id: id, date } = item
return { id, name, authType, privateKey: '', password: '', date }
Expand All @@ -18,18 +18,16 @@ const addSSH = async ({ res, request }) => {
let { body: { name, authType, password, privateKey, tempKey } } = request
let record = { name, authType, password, privateKey }
if (!name || !record[authType]) return res.fail({ data: false, msg: '参数错误' })
let sshRecord = await readSSHRecord()
if (sshRecord.some(item => item.name === name)) return res.fail({ data: false, msg: '已存在同名凭证' })
let count = await credentialsDB.countAsync({ name })
if (count > 0) return res.fail({ data: false, msg: '已存在同名凭证' })

const clearTempKey = await RSADecryptAsync(tempKey)
console.log('clearTempKey:', clearTempKey)
const clearSSHKey = await AESDecryptAsync(record[authType], clearTempKey)
// console.log(`${ authType }原密文: `, clearSSHKey)
record[authType] = await AESEncryptAsync(clearSSHKey)
// console.log(`${ authType }__commonKey加密存储: `, record[authType])

sshRecord.push({ ...record, date: Date.now() })
await writeSSHRecord(sshRecord)
await credentialsDB.insertAsync({ ...record, date: Date.now() })
consola.info('添加凭证:', name)
res.success({ data: '保存成功' })
}
Expand All @@ -38,11 +36,8 @@ const updateSSH = async ({ res, request }) => {
let { body: { id, name, authType, password, privateKey, date, tempKey } } = request
let record = { name, authType, password, privateKey, date }
if (!id || !name) return res.fail({ data: false, msg: '请输入凭据名称' })
let sshRecord = await readSSHRecord()
let idx = sshRecord.findIndex(item => item._id === id)
if (sshRecord.some(item => item.name === name && item.date !== date)) return res.fail({ data: false, msg: '已存在同名凭证' })
if (idx === -1) res.fail({ data: false, msg: '请输入凭据名称' })
const oldRecord = sshRecord[idx]
let oldRecord = await credentialsDB.findOneAsync({ _id: id })
if (!oldRecord) return res.fail({ data: false, msg: '凭证不存在' })
// 判断原记录是否存在当前更新记录的认证方式
if (!oldRecord[authType] && !record[authType]) return res.fail({ data: false, msg: `请输入${ authType === 'password' ? '密码' : '密钥' }` })
if (!record[authType] && oldRecord[authType]) {
Expand All @@ -55,31 +50,30 @@ const updateSSH = async ({ res, request }) => {
record[authType] = await AESEncryptAsync(clearSSHKey)
// console.log(`${ authType }__commonKey加密存储: `, record[authType])
}
record._id = sshRecord[idx]._id
sshRecord.splice(idx, 1, record)
await writeSSHRecord(sshRecord)
await credentialsDB.updateAsync({ _id: id }, record)
consola.info('修改凭证:', name)
res.success({ data: '保存成功' })
}

const removeSSH = async ({ res, request }) => {
let { params: { id } } = request
let sshRecord = await readSSHRecord()
let idx = sshRecord.findIndex(item => item._id === id)
if (idx === -1) return res.fail({ msg: '凭证不存在' })
sshRecord.splice(idx, 1)
let count = await credentialsDB.countAsync({ _id: id })
if (count === 0) return res.fail({ msg: '凭证不存在' })
// 将删除的凭证id从host中删除
let hostList = await hostListDB.findAsync({})
if (Array.isArray(hostList) && hostList.length > 0) {
for (let item of hostList) {
if (item.credential === id) {
item.credential = ''
await hostListDB.updateAsync({ _id: item._id }, item)
for (let host of hostList) {
let { credential } = host
credential = await AESDecryptAsync(credential)
if (credential === id) {
host.credential = ''
await hostListDB.updateAsync({ _id: host._id }, host)
}
}
}
await hostListDB.compactDatafileAsync()
consola.info('移除凭证:', id)
await writeSSHRecord(sshRecord)
await credentialsDB.removeAsync({ _id: id })
res.success({ data: '移除成功' })
}

Expand Down
6 changes: 3 additions & 3 deletions server/app/socket/onekey.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const { Server } = require('socket.io')
const { Client: SSHClient } = require('ssh2')
const { sendNoticeAsync } = require('../utils/notify')
const { readSSHRecord } = require('../utils/storage')
const { verifyAuthSync } = require('../utils/verify-auth')
const { shellThrottle } = require('../utils/tools')
const { AESDecryptAsync } = require('../utils/encrypt')
const { isAllowedIp } = require('../utils/tools')
const { HostListDB, OnekeyDB } = require('../utils/db-class')
const { HostListDB, CredentialsDB, OnekeyDB } = require('../utils/db-class')
const hostListDB = new HostListDB().getInstance()
const credentialsDB = new CredentialsDB().getInstance()
const onekeyDB = new OnekeyDB().getInstance()

const execStatusEnum = {
Expand Down Expand Up @@ -149,7 +149,7 @@ module.exports = (httpServer) => {
try {
if (authType === 'credential') {
let credentialId = await AESDecryptAsync(hostInfo['credential'])
const sshRecordList = await readSSHRecord()
const sshRecordList = await credentialsDB.findAsync({})
const sshRecord = sshRecordList.find(item => item._id === credentialId)
authInfo.authType = sshRecord.authType
authInfo[authInfo.authType] = await AESDecryptAsync(sshRecord[authInfo.authType])
Expand Down
6 changes: 3 additions & 3 deletions server/app/socket/sftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const { Server } = require('socket.io')
const { sftpCacheDir } = require('../config')
const { verifyAuthSync } = require('../utils/verify-auth')
const { AESDecryptAsync } = require('../utils/encrypt')
const { readSSHRecord } = require('../utils/storage')
const { isAllowedIp } = require('../utils/tools')
const { HostListDB } = require('../utils/db-class')
const { HostListDB, CredentialsDB } = require('../utils/db-class')
const hostListDB = new HostListDB().getInstance()
const credentialsDB = new CredentialsDB().getInstance()

// 读取切片
const pipeStream = (path, writeStream) => {
Expand Down Expand Up @@ -241,7 +241,7 @@ module.exports = (httpServer) => {
// 解密放到try里面,防止报错【commonKey必须配对, 否则需要重新添加服务器密钥】
if (authType === 'credential') {
let credentialId = await AESDecryptAsync(targetHostInfo[authType])
const sshRecordList = await readSSHRecord()
const sshRecordList = await credentialsDB.findAsync({})
const sshRecord = sshRecordList.find(item => item._id === credentialId)
authInfo.authType = sshRecord.authType
authInfo[authInfo.authType] = await AESDecryptAsync(sshRecord[authInfo.authType])
Expand Down
7 changes: 3 additions & 4 deletions server/app/socket/terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const { Server } = require('socket.io')
const { Client: SSHClient } = require('ssh2')
const { verifyAuthSync } = require('../utils/verify-auth')
const { AESDecryptAsync } = require('../utils/encrypt')
const { readSSHRecord } = require('../utils/storage')
const { sendNoticeAsync } = require('../utils/notify')
const { isAllowedIp, ping } = require('../utils/tools')
const { HostListDB } = require('../utils/db-class')
const { HostListDB, CredentialsDB } = require('../utils/db-class')
const hostListDB = new HostListDB().getInstance()
const credentialsDB = new CredentialsDB().getInstance()

function createInteractiveShell(socket, sshClient) {
return new Promise((resolve) => {
Expand Down Expand Up @@ -63,8 +63,7 @@ async function createTerminal(hostId, socket, sshClient) {
// 解密放到try里面,防止报错【commonKey必须配对, 否则需要重新添加服务器密钥】
if (authType === 'credential') {
let credentialId = await AESDecryptAsync(targetHostInfo[authType])
const sshRecordList = await readSSHRecord()
const sshRecord = sshRecordList.find(item => item._id === credentialId)
const sshRecord = await credentialsDB.findOneAsync({ _id: credentialId })
authInfo.authType = sshRecord.authType
authInfo[authInfo.authType] = await AESDecryptAsync(sshRecord[authInfo.authType])
} else {
Expand Down
10 changes: 5 additions & 5 deletions server/app/utils/db-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ module.exports.HostListDB = class HostListDB {
}
}

module.exports.SshRecordDB = class SshRecordDB {
module.exports.CredentialsDB = class CredentialsDB {
constructor() {
if (!SshRecordDB.instance) {
SshRecordDB.instance = new Datastore({ filename: credentialsDBPath, autoload: true })
// SshRecordDB.instance.setAutocompactionInterval(5000)
if (!CredentialsDB.instance) {
CredentialsDB.instance = new Datastore({ filename: credentialsDBPath, autoload: true })
// CredentialsDB.instance.setAutocompactionInterval(5000)
}
}
getInstance() {
return SshRecordDB.instance
return CredentialsDB.instance
}
}

Expand Down
42 changes: 0 additions & 42 deletions server/app/utils/storage.js

This file was deleted.

0 comments on commit 70bdaa5

Please sign in to comment.