test: add mobile ssh payload shaping

This commit is contained in:
chaos-zhu
2026-05-16 15:47:38 +08:00
parent 03d960a62c
commit 72d2c8d0ce
2 changed files with 101 additions and 0 deletions

View 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
}

View 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')