|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -import * as fs from 'fs' |
4 | | -import { Argument, program } from '@commander-js/extra-typings' |
| 3 | +import { Argument, Option, program } from '@commander-js/extra-typings' |
| 4 | +import { createReadStream, createWriteStream } from 'node:fs' |
5 | 5 | import { SSHAgentClient } from './lib/ssh_agent_client.js' |
6 | | -import { buffer, text } from 'stream/consumers' |
7 | 6 |
|
8 | 7 | program |
9 | 8 | .name('ssh-crypt') |
10 | 9 | .description('Encrypt/Decrypt a file with your ssh-agent private key') |
11 | 10 | .addArgument(new Argument('<command>', 'action').choices(['encrypt', 'decrypt'])) |
12 | 11 | .option('-i, --input <path>', 'input path (default to stdin)') |
| 12 | + .addOption(new Option('--encryptEncoding <encoding>', 'encrypt output encoding').choices(['hex', 'base64'])) |
13 | 13 | .option('-o, --output <path>', 'output path (default to stdout)') |
| 14 | + .addOption(new Option('--decryptEncoding <encoding>', 'decrypt input encoding').choices(['hex', 'base64'])) |
14 | 15 | .requiredOption('-k, --key <string>', 'select the first matching pubkey in the ssh-agent') |
15 | 16 | .requiredOption('-s, --seed <string>', 'is used to generate the secret') |
16 | 17 | .action(async (action, options) => { |
17 | 18 | try { |
18 | | - const agent = new SSHAgentClient() |
| 19 | + const agent = new SSHAgentClient({ timeout: 10000 }) |
19 | 20 | const key = await agent.getIdentity(options.key) |
20 | 21 | if (!key) { |
21 | 22 | program.error(`Error: no SSH key found for "${options.key}"`) |
22 | 23 | } |
23 | | - const readable = options.input ? fs.createReadStream(options.input) : process.stdin |
24 | | - const writable = options.output ? fs.createWriteStream(options.output) : process.stdout |
25 | | - switch (action) { |
26 | | - case 'encrypt': { |
27 | | - await buffer(readable) |
28 | | - .then(data => agent.encrypt(key, options.seed, data)) |
29 | | - .then(encrypted => writable.write(encrypted)) |
30 | | - |
31 | | - break |
32 | | - } |
33 | | - case 'decrypt': { |
34 | | - await text(readable) |
35 | | - .then(data => agent.decrypt(key, options.seed, data.trim())) |
36 | | - .then(decrypted => writable.write(decrypted)) |
37 | | - |
38 | | - break |
39 | | - } |
40 | | - default: |
41 | | - throw new Error('unknwon action') |
42 | | - } |
| 24 | + const readable = options.input ? createReadStream(options.input) : process.stdin |
| 25 | + const writable = options.output ? createWriteStream(options.output) : process.stdout |
| 26 | + const transform = |
| 27 | + action === 'decrypt' |
| 28 | + ? await agent.getDecryptTransform(key, options.seed, options.decryptEncoding) |
| 29 | + : await agent.getEncryptTransform(key, options.seed, options.encryptEncoding) |
| 30 | + readable.pipe(transform).pipe(writable) |
43 | 31 | } catch (err) { |
44 | 32 | program.error(`Error: ${(err as Error).message}`) |
45 | 33 | } |
|
0 commit comments