mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-10 08:15:00 -04:00
Improve internal and external documentation
This commit is contained in:
parent
648ecb6e69
commit
d11850beb9
1 changed files with 35 additions and 12 deletions
47
cli.js
47
cli.js
|
@ -7,11 +7,11 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const path = require("path");
|
|
||||||
const chef = require("cyberchef");
|
///////// Helper Functions /////////
|
||||||
const program = require("commander");
|
|
||||||
|
|
||||||
let slurpStream = (istream) => { // {{{1
|
let slurpStream = (istream) => { // {{{1
|
||||||
|
// Slurp the contents of a stream up into a Buffer to pass to CyberChef
|
||||||
var ret = [];
|
var ret = [];
|
||||||
var len = 0;
|
var len = 0;
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
|
@ -27,12 +27,13 @@ let slurpStream = (istream) => { // {{{1
|
||||||
}; // }}}1
|
}; // }}}1
|
||||||
|
|
||||||
let slurp = (fname) => { // {{{1
|
let slurp = (fname) => { // {{{1
|
||||||
|
// Slurp the contents of a file (or stdin) into a Buffer
|
||||||
let istream;
|
let istream;
|
||||||
|
|
||||||
if (fname === undefined || fname == '-') {
|
if (fname === undefined || fname == '-') {
|
||||||
istream = process.stdin;
|
istream = process.stdin;
|
||||||
if (istream.isTTY) {
|
if (istream.isTTY) {
|
||||||
throw new Error("TTY input not supported");
|
return Promise.reject(new Error("TTY input not supported"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -53,16 +54,30 @@ let getPort = (value, dummyPrevious) => { // {{{1
|
||||||
|
|
||||||
///////// MAIN ///////// {{{1
|
///////// MAIN ///////// {{{1
|
||||||
|
|
||||||
|
const chef = require("cyberchef");
|
||||||
|
const program = require("commander");
|
||||||
|
|
||||||
program
|
program
|
||||||
.version(require('./package.json').version)
|
.version(require('./package.json').version)
|
||||||
|
.description('Bake data from files and/or TCP clients '
|
||||||
|
+ 'using a CyberChef recipe.')
|
||||||
.usage('[options] [file [file ...]]')
|
.usage('[options] [file [file ...]]')
|
||||||
.requiredOption('-r, --recipe-file <file>',
|
.requiredOption('-r, --recipe-file <file>',
|
||||||
'recipe JSON file')
|
'recipe JSON file')
|
||||||
.option('-l, --listen [port]',
|
.option('-l, --listen [port]',
|
||||||
'listen on TCP port for data (default:random)', getPort, false)
|
'listen on TCP port for data (random if not given)', getPort, false)
|
||||||
.option('-o, --output <file-or-dir>',
|
.option('-o, --output <file-or-dir>',
|
||||||
'write result here (not for TCP; default:stdout)')
|
'where to write result (file input only; default:stdout)');
|
||||||
.parse(process.argv);
|
|
||||||
|
try {
|
||||||
|
program.exitOverride().parse(process.argv);
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
if (e.code != 'commander.helpDisplayed') {
|
||||||
|
console.error("Run with '--help' for usage");
|
||||||
|
}
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// If we get no inputs and we aren't running a server,
|
// If we get no inputs and we aren't running a server,
|
||||||
// make stdin our single input
|
// make stdin our single input
|
||||||
|
@ -73,6 +88,7 @@ if (inputs.length == 0 && !program.listen) {
|
||||||
|
|
||||||
// Likewise stdout for our output
|
// Likewise stdout for our output
|
||||||
let ostream;
|
let ostream;
|
||||||
|
let path;
|
||||||
let outputIsDir = false;
|
let outputIsDir = false;
|
||||||
if (program.output === undefined && !program.listen) {
|
if (program.output === undefined && !program.listen) {
|
||||||
ostream = process.stdout;
|
ostream = process.stdout;
|
||||||
|
@ -85,20 +101,22 @@ else if (inputs.length > 0) {
|
||||||
outputIsDir = st.isDirectory();
|
outputIsDir = st.isDirectory();
|
||||||
}
|
}
|
||||||
catch(err) {
|
catch(err) {
|
||||||
|
// We're fine if the output doesn't exist yet
|
||||||
if (err.code != 'ENOENT') throw err;
|
if (err.code != 'ENOENT') throw err;
|
||||||
}
|
}
|
||||||
if (!outputIsDir) {
|
if (!outputIsDir) {
|
||||||
ostream = fs.createWriteStream(program.output);
|
ostream = fs.createWriteStream(program.output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (outputIsDir) path = require("path");
|
||||||
|
|
||||||
let recipe;
|
let recipe;
|
||||||
slurp(program.recipeFile).then((data) => {
|
slurp(program.recipeFile).then((data) => {
|
||||||
recipe = JSON.parse(data);
|
recipe = JSON.parse(data);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(`Error parsing recipe: ${err}`);
|
console.error(`Error parsing recipe: ${err.message}`);
|
||||||
process.exit(1);
|
process.exit(2);
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// First, deal with any files we want to read
|
// First, deal with any files we want to read
|
||||||
|
@ -113,9 +131,14 @@ slurp(program.recipeFile).then((data) => {
|
||||||
}
|
}
|
||||||
ostream.write(output.presentAs("string", true));
|
ostream.write(output.presentAs("string", true));
|
||||||
if (outputIsDir) ostream.end();
|
if (outputIsDir) ostream.end();
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
console.error(err.message);
|
||||||
|
process.exitCode = 2;
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err.message);
|
||||||
|
process.exitCode = 2;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,7 +158,7 @@ slurp(program.recipeFile).then((data) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// If no port given, let the OS choose one
|
// If no port given by user, let the OS choose one
|
||||||
if (program.listen === true) program.listen = 0;
|
if (program.listen === true) program.listen = 0;
|
||||||
server.listen(program.listen, '127.0.0.1')
|
server.listen(program.listen, '127.0.0.1')
|
||||||
.on('listening', () => {
|
.on('listening', () => {
|
||||||
|
@ -153,5 +176,5 @@ slurp(program.recipeFile).then((data) => {
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
process.exit(2);
|
process.exit(3);
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue