Ported the ChangeIPFormat operation.
This commit is contained in:
parent
161f89c038
commit
792218df9c
121
src/core/operations/ChangeIPFormat.mjs
Normal file
121
src/core/operations/ChangeIPFormat.mjs
Normal file
@ -0,0 +1,121 @@
|
||||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Utils from "../Utils";
|
||||
import {fromHex} from "../lib/Hex";
|
||||
|
||||
/**
|
||||
* Change IP format operation
|
||||
*/
|
||||
class ChangeIPFormat extends Operation {
|
||||
|
||||
/**
|
||||
* ChangeIPFormat constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Change IP format";
|
||||
this.module = "JSBN";
|
||||
this.description = "Convert an IP address from one format to another, e.g. <code>172.20.23.54</code> to <code>ac141736</code>";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Input format",
|
||||
"type": "option",
|
||||
"value": ["Hex", "Raw"]
|
||||
},
|
||||
{
|
||||
"name": "Output format",
|
||||
"type": "option",
|
||||
"value": ["Hex", "Raw"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const inFormat = args[0],
|
||||
outFormat = args[1],
|
||||
lines = input.split("\n");
|
||||
let output = "",
|
||||
j = 0;
|
||||
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
if (lines[i] === "") continue;
|
||||
let baIp = [];
|
||||
let octets;
|
||||
let decimal;
|
||||
|
||||
if (inFormat === outFormat) {
|
||||
output += lines[i] + "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
// Convert to byte array IP from input format
|
||||
switch (inFormat) {
|
||||
case "Dotted Decimal":
|
||||
octets = lines[i].split(".");
|
||||
for (j = 0; j < octets.length; j++) {
|
||||
baIp.push(parseInt(octets[j], 10));
|
||||
}
|
||||
break;
|
||||
case "Decimal":
|
||||
decimal = lines[i].toString();
|
||||
baIp.push(decimal >> 24 & 255);
|
||||
baIp.push(decimal >> 16 & 255);
|
||||
baIp.push(decimal >> 8 & 255);
|
||||
baIp.push(decimal & 255);
|
||||
break;
|
||||
case "Hex":
|
||||
baIp = fromHex(lines[i]);
|
||||
break;
|
||||
default:
|
||||
throw "Unsupported input IP format";
|
||||
}
|
||||
|
||||
let ddIp;
|
||||
let decIp;
|
||||
let hexIp;
|
||||
|
||||
// Convert byte array IP to output format
|
||||
switch (outFormat) {
|
||||
case "Dotted Decimal":
|
||||
ddIp = "";
|
||||
for (j = 0; j < baIp.length; j++) {
|
||||
ddIp += baIp[j] + ".";
|
||||
}
|
||||
output += ddIp.slice(0, ddIp.length-1) + "\n";
|
||||
break;
|
||||
case "Decimal":
|
||||
decIp = ((baIp[0] << 24) | (baIp[1] << 16) | (baIp[2] << 8) | baIp[3]) >>> 0;
|
||||
output += decIp.toString() + "\n";
|
||||
break;
|
||||
case "Hex":
|
||||
hexIp = "";
|
||||
for (j = 0; j < baIp.length; j++) {
|
||||
hexIp += Utils.hex(baIp[j]);
|
||||
}
|
||||
output += hexIp + "\n";
|
||||
break;
|
||||
default:
|
||||
throw "Unsupported output IP format";
|
||||
}
|
||||
}
|
||||
|
||||
return output.slice(0, output.length-1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default ChangeIPFormat;
|
Loading…
x
Reference in New Issue
Block a user