mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-10 00:05:11 -04:00
Fix 109 grunt lint
errors
This commit is contained in:
parent
d11850beb9
commit
6805f90e05
1 changed files with 111 additions and 100 deletions
125
cli.js
125
cli.js
|
@ -4,19 +4,23 @@
|
||||||
* @copyright Crown Copyright 2020
|
* @copyright Crown Copyright 2020
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
'use strict';
|
"use strict";
|
||||||
|
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|
||||||
///////// Helper Functions /////////
|
/* * * * * Helper Functions * * * * */
|
||||||
|
|
||||||
let slurpStream = (istream) => { // {{{1
|
/**
|
||||||
// Slurp the contents of a stream up into a Buffer to pass to CyberChef
|
* Slurp the contents of a stream up into a Buffer to pass to CyberChef
|
||||||
var ret = [];
|
*
|
||||||
var len = 0;
|
* @param {Stream} istream
|
||||||
|
*/
|
||||||
|
const slurpStream = (istream) => { // {{{1
|
||||||
|
const ret = [];
|
||||||
|
let len = 0;
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
istream.on('readable', () => {
|
istream.on("readable", () => {
|
||||||
var chunk;
|
let chunk;
|
||||||
while ((chunk = istream.read()) !== null) {
|
while ((chunk = istream.read()) !== null) {
|
||||||
ret.push(chunk);
|
ret.push(chunk);
|
||||||
len += chunk.length;
|
len += chunk.length;
|
||||||
|
@ -26,25 +30,35 @@ let slurpStream = (istream) => { // {{{1
|
||||||
});
|
});
|
||||||
}; // }}}1
|
}; // }}}1
|
||||||
|
|
||||||
let slurp = (fname) => { // {{{1
|
/**
|
||||||
// Slurp the contents of a file (or stdin) into a Buffer
|
* Slurp the contents of a file (or stdin) into a Buffer
|
||||||
|
*
|
||||||
|
* @param {String} fname
|
||||||
|
*/
|
||||||
|
const slurp = (fname) => { // {{{1
|
||||||
let istream;
|
let istream;
|
||||||
|
|
||||||
if (fname === undefined || fname == '-') {
|
if (fname === undefined || fname === "-") {
|
||||||
istream = process.stdin;
|
istream = process.stdin;
|
||||||
if (istream.isTTY) {
|
if (istream.isTTY) {
|
||||||
return Promise.reject(new Error("TTY input not supported"));
|
return Promise.reject(new Error("TTY input not supported"));
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
istream = fs.createReadStream(fname, { flags: "r" });
|
||||||
istream = fs.createReadStream(fname, { flags: 'r' });
|
|
||||||
}
|
}
|
||||||
return slurpStream(istream);
|
return slurpStream(istream);
|
||||||
}; // }}}1
|
}; // }}}1
|
||||||
|
|
||||||
let getPort = (value, dummyPrevious) => { // {{{1
|
/**
|
||||||
// Get a valid port number from the command line
|
* Get a valid port number from the command line
|
||||||
let ret = parseInt(value, 10);
|
*
|
||||||
|
* Used by commander
|
||||||
|
*
|
||||||
|
* @param {String} value
|
||||||
|
* @param {String} dummyPrevious
|
||||||
|
*/
|
||||||
|
const getPort = (value, dummyPrevious) => { // {{{1
|
||||||
|
const ret = parseInt(value, 10);
|
||||||
if (ret < 1 || ret > 65535) {
|
if (ret < 1 || ret > 65535) {
|
||||||
throw new Error("invalid port number");
|
throw new Error("invalid port number");
|
||||||
}
|
}
|
||||||
|
@ -52,29 +66,28 @@ let getPort = (value, dummyPrevious) => { // {{{1
|
||||||
};
|
};
|
||||||
// }}}1
|
// }}}1
|
||||||
|
|
||||||
///////// MAIN ///////// {{{1
|
/* * * * * MAIN * * * * */ // {{{1
|
||||||
|
|
||||||
const chef = require("cyberchef");
|
const chef = require("cyberchef");
|
||||||
const program = require("commander");
|
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 '
|
.description("Bake data from files and/or TCP clients " +
|
||||||
+ 'using a CyberChef recipe.')
|
"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 (random if not given)', 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>",
|
||||||
'where to write result (file input only; default:stdout)');
|
"where to write result (file input only; default:stdout)");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
program.exitOverride().parse(process.argv);
|
program.exitOverride().parse(process.argv);
|
||||||
}
|
} catch (e) {
|
||||||
catch (e) {
|
if (e.code !== "commander.helpDisplayed") {
|
||||||
if (e.code != 'commander.helpDisplayed') {
|
console.error("Run with \"--help\" for usage");
|
||||||
console.error("Run with '--help' for usage");
|
|
||||||
}
|
}
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
@ -82,8 +95,8 @@ catch (e) {
|
||||||
// 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
|
||||||
let inputs = program.args;
|
let inputs = program.args;
|
||||||
if (inputs.length == 0 && !program.listen) {
|
if (inputs.length === 0 && !program.listen) {
|
||||||
inputs = [ '-' ];
|
inputs = ["-"];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Likewise stdout for our output
|
// Likewise stdout for our output
|
||||||
|
@ -92,17 +105,15 @@ 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;
|
||||||
}
|
} else if (inputs.length > 0) {
|
||||||
else if (inputs.length > 0) {
|
|
||||||
// See if our output is a directory
|
// See if our output is a directory
|
||||||
let st;
|
let st;
|
||||||
try {
|
try {
|
||||||
st = fs.statSync(program.output);
|
st = fs.statSync(program.output);
|
||||||
outputIsDir = st.isDirectory();
|
outputIsDir = st.isDirectory();
|
||||||
}
|
} catch (err) {
|
||||||
catch(err) {
|
// We"re fine if the output doesn"t exist yet
|
||||||
// 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);
|
||||||
|
@ -114,18 +125,18 @@ 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.message}`);
|
console.error(`Error parsing recipe: ${err.message}`);
|
||||||
process.exit(2);
|
process.exit(2);
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// First, deal with any files we want to read
|
// First, deal with any files we want to read
|
||||||
for(let i of inputs) {
|
for (const i of inputs) {
|
||||||
slurp(i).then((data) => {
|
slurp(i).then((data) => {
|
||||||
let output = chef.bake(data, recipe);
|
const output = chef.bake(data, recipe);
|
||||||
if (outputIsDir) {
|
if (outputIsDir) {
|
||||||
let outFileName = path.basename(i);
|
let outFileName = path.basename(i);
|
||||||
if (outFileName == '-') outFileName = 'from-stdin';
|
if (outFileName === "-") outFileName = "from-stdin";
|
||||||
ostream = fs.createWriteStream(
|
ostream = fs.createWriteStream(
|
||||||
path.join(program.output, outFileName));
|
path.join(program.output, outFileName));
|
||||||
}
|
}
|
||||||
|
@ -146,10 +157,10 @@ slurp(program.recipeFile).then((data) => {
|
||||||
// This is intentionally hardcoded to localhost to discourage
|
// This is intentionally hardcoded to localhost to discourage
|
||||||
// the use of this script as a production system.
|
// the use of this script as a production system.
|
||||||
if (program.listen) {
|
if (program.listen) {
|
||||||
const net = require('net');
|
const net = require("net");
|
||||||
const server = net.createServer((socket) => {
|
const server = net.createServer((socket) => {
|
||||||
slurpStream(socket).then((data) => {
|
slurpStream(socket).then((data) => {
|
||||||
let output = chef.bake(data, recipe);
|
const output = chef.bake(data, recipe);
|
||||||
socket.write(output.presentAs("string", true));
|
socket.write(output.presentAs("string", true));
|
||||||
socket.end();
|
socket.end();
|
||||||
})
|
})
|
||||||
|
@ -160,21 +171,21 @@ slurp(program.recipeFile).then((data) => {
|
||||||
|
|
||||||
// If no port given by user, 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", () => {
|
||||||
console.log('Now listening on '
|
console.log("Now listening on " +
|
||||||
+ server.address().address
|
server.address().address +
|
||||||
+ ":" + server.address().port);
|
":" + server.address().port);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Exit gracefully
|
// Exit gracefully
|
||||||
process.on('SIGINT', () => {
|
process.on("SIGINT", () => {
|
||||||
console.log("Exiting");
|
console.log("Exiting");
|
||||||
server.close();
|
server.close();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
process.exit(3);
|
process.exit(3);
|
||||||
})
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue