mirror of
https://github.com/chaos-zhu/easynode.git
synced 2026-06-21 18:52:46 +08:00
test: add mobile ssh payload shaping
This commit is contained in:
47
server/app/controller/mobile.js
Normal file
47
server/app/controller/mobile.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const { RSADecryptAsync } = require('../utils/encrypt')
|
||||
const { encryptJsonForMobile } = require('../utils/mobile-crypto')
|
||||
|
||||
function toMobileSshPayload(hostId, name, authInfo) {
|
||||
const { host, port, username, authType } = authInfo
|
||||
if (!['password', 'privateKey'].includes(authType)) {
|
||||
throw new Error(`unsupported mobile ssh auth type: ${ authType || 'empty' }`)
|
||||
}
|
||||
|
||||
return {
|
||||
hostId,
|
||||
name,
|
||||
host,
|
||||
port: Number(port || 22),
|
||||
username,
|
||||
authType,
|
||||
password: authType === 'password' ? authInfo.password || '' : '',
|
||||
privateKey: authType === 'privateKey' ? authInfo.privateKey || '' : '',
|
||||
passphrase: authType === 'privateKey' ? authInfo.passphrase || '' : ''
|
||||
}
|
||||
}
|
||||
|
||||
async function getMobileSshConnection({ request, res }) {
|
||||
try {
|
||||
const { hostId, encryptedKey } = request.body || {}
|
||||
if (!hostId || !encryptedKey) {
|
||||
return res.fail({ msg: 'missing params: hostId or encryptedKey' })
|
||||
}
|
||||
|
||||
const tempKeyText = await RSADecryptAsync(encryptedKey)
|
||||
const tempKey = Buffer.from(tempKeyText, 'base64')
|
||||
const { getConnectionOptions } = require('../socket/terminal')
|
||||
const { authInfo, name } = await getConnectionOptions(hostId)
|
||||
const payload = toMobileSshPayload(hostId, name, authInfo)
|
||||
const data = encryptJsonForMobile(payload, tempKey)
|
||||
|
||||
return res.success({ data, msg: 'success' })
|
||||
} catch (error) {
|
||||
logger.error('getMobileSshConnection error:', error.message)
|
||||
return res.fail({ msg: error.message || 'mobile ssh connection failed' })
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getMobileSshConnection,
|
||||
toMobileSshPayload
|
||||
}
|
||||
54
server/test/test-mobile-ssh-payload.js
Normal file
54
server/test/test-mobile-ssh-payload.js
Normal file
@@ -0,0 +1,54 @@
|
||||
const assert = require('assert')
|
||||
const { toMobileSshPayload } = require('../app/controller/mobile')
|
||||
|
||||
function testPasswordPayload() {
|
||||
const payload = toMobileSshPayload('h1', 'prod', {
|
||||
host: '10.0.0.2',
|
||||
port: 22,
|
||||
username: 'root',
|
||||
authType: 'password',
|
||||
password: 'p@ss'
|
||||
})
|
||||
|
||||
assert.deepStrictEqual(payload, {
|
||||
hostId: 'h1',
|
||||
name: 'prod',
|
||||
host: '10.0.0.2',
|
||||
port: 22,
|
||||
username: 'root',
|
||||
authType: 'password',
|
||||
password: 'p@ss',
|
||||
privateKey: '',
|
||||
passphrase: ''
|
||||
})
|
||||
}
|
||||
|
||||
function testPrivateKeyPayload() {
|
||||
const payload = toMobileSshPayload('h2', 'keyhost', {
|
||||
host: '10.0.0.3',
|
||||
port: 2222,
|
||||
username: 'ubuntu',
|
||||
authType: 'privateKey',
|
||||
privateKey: 'KEY',
|
||||
passphrase: 'phrase'
|
||||
})
|
||||
|
||||
assert.strictEqual(payload.authType, 'privateKey')
|
||||
assert.strictEqual(payload.privateKey, 'KEY')
|
||||
assert.strictEqual(payload.password, '')
|
||||
assert.strictEqual(payload.passphrase, 'phrase')
|
||||
}
|
||||
|
||||
function testRejectsUnsupportedAuth() {
|
||||
assert.throws(() => toMobileSshPayload('h3', 'unsupported', {
|
||||
host: '10.0.0.4',
|
||||
port: 22,
|
||||
username: 'root',
|
||||
authType: 'keyboard'
|
||||
}), /unsupported mobile ssh auth type/)
|
||||
}
|
||||
|
||||
testPasswordPayload()
|
||||
testPrivateKeyPayload()
|
||||
testRejectsUnsupportedAuth()
|
||||
console.log('test-mobile-ssh-payload passed')
|
||||
Reference in New Issue
Block a user