* Start an SSH server that tracks which auth methods are attempted. * @param {string} publicKey - The public key to accept for publickey auth * @returns {{ port: number, authAttempts: string[], close: () => Promise }}
(publicKey)
| 98 | * @returns {{ port: number, authAttempts: string[], close: () => Promise<void> }} |
| 99 | */ |
| 100 | async function startTrackingServer (publicKey) { |
| 101 | const clients = new Set() |
| 102 | const authAttempts = [] |
| 103 | |
| 104 | const server = new Server({ |
| 105 | hostKeys: [HOST_KEY.private] |
| 106 | }, (client) => { |
| 107 | clients.add(client) |
| 108 | const cleanup = () => clients.delete(client) |
| 109 | |
| 110 | client.on('close', cleanup) |
| 111 | client.on('end', cleanup) |
| 112 | client.on('authentication', (ctx) => { |
| 113 | authAttempts.push(ctx.method) |
| 114 | |
| 115 | if (ctx.method === 'none') { |
| 116 | return ctx.reject(['publickey', 'password']) |
| 117 | } |
| 118 | |
| 119 | // Accept password auth |
| 120 | if (ctx.method === 'password' && ctx.username === USERNAME && ctx.password === PASSWORD) { |
| 121 | return ctx.accept() |
| 122 | } |
| 123 | |
| 124 | // Accept publickey auth |
| 125 | if (ctx.method === 'publickey' && ctx.username === USERNAME && matchesPublicKey(ctx, publicKey)) { |
| 126 | return ctx.accept() |
| 127 | } |
| 128 | |
| 129 | ctx.reject(['publickey', 'password']) |
| 130 | }) |
| 131 | client.on('ready', () => { |
| 132 | client.on('session', (accept) => { |
| 133 | const sshSession = accept() |
| 134 | sshSession.on('env', (accept) => accept()) |
| 135 | sshSession.on('pty', (accept) => accept()) |
| 136 | sshSession.on('shell', (accept) => { |
| 137 | const stream = accept() |
| 138 | stream.write('electerm ready\n') |
| 139 | }) |
| 140 | }) |
| 141 | }) |
| 142 | }) |
| 143 | |
| 144 | server.listen(0, '127.0.0.1') |
| 145 | await once(server, 'listening') |
| 146 | |
| 147 | return { |
| 148 | port: server.address().port, |
| 149 | authAttempts, |
| 150 | async close () { |
| 151 | for (const client of clients) { |
| 152 | client.end() |
| 153 | } |
| 154 | await new Promise((resolve, reject) => { |
| 155 | server.close((err) => { |
| 156 | if (err) { |
| 157 | reject(err) |
no test coverage detected