1
0
Fork 0
forked from svrjs/svrjs

Add utilities to ESLint check list, and lint out the utilities.

This commit is contained in:
Dorian Niemiec 2024-08-26 07:34:07 +02:00
parent 26fc7f3b08
commit 343dce37ec
5 changed files with 593 additions and 316 deletions

View file

@ -5,7 +5,7 @@
"scripts": { "scripts": {
"build": "node esbuild.config.js", "build": "node esbuild.config.js",
"dev": "npm run build && npm run start", "dev": "npm run build && npm run start",
"lint": "eslint --no-error-on-unmatched-pattern src/**/*.js src/*.js tests/**/*.test.js tests/**/*.js", "lint": "eslint --no-error-on-unmatched-pattern src/**/*.js src/*.js tests/**/*.test.js tests/**/*.js utilities/**/*.js",
"lint:fix": "npm run lint -- --fix", "lint:fix": "npm run lint -- --fix",
"start": "node dist/svr.js", "start": "node dist/svr.js",
"test": "jest" "test": "jest"

View file

@ -1,52 +1,91 @@
//SVR.JS LOG HIGHLIGHTER //SVR.JS LOG HIGHLIGHTER
var readline = require("readline"); const readline = require("readline");
var process = require("process");
var args = process.argv; const args = process.argv;
for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1 ? 2 : 1); i < args.length; i++) { for (
if (args[i] == "-h" || args[i] == "--help" || args[i] == "-?" || args[i] == "/h" || args[i] == "/?") { let i =
process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1
? 2
: 1;
i < args.length;
i++
) {
if (
args[i] == "-h" ||
args[i] == "--help" ||
args[i] == "-?" ||
args[i] == "/h" ||
args[i] == "/?"
) {
console.log("SVR.JS log highlighter usage:"); console.log("SVR.JS log highlighter usage:");
console.log("<some process> | node loghighlight.js [-h] [--help] [-?] [/h] [/?]"); console.log(
"<some process> | node loghighlight.js [-h] [--help] [-?] [/h] [/?]",
);
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
process.exit(0); process.exit(0);
} else { } else {
console.log("Unrecognized argument: " + args[i]); console.log("Unrecognized argument: " + args[i]);
console.log("SVR.JS log highlighter usage:"); console.log("SVR.JS log highlighter usage:");
console.log("<some process> | node loghighlight.js [-h] [--help] [-?] [/h] [/?]"); console.log(
"<some process> | node loghighlight.js [-h] [--help] [-?] [/h] [/?]",
);
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
process.exit(1); process.exit(1);
} }
} }
var rl = readline.createInterface({ const rl = readline.createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout, output: process.stdout,
terminal: false, terminal: false,
prompt: '' prompt: "",
}); });
rl.prompt(); rl.prompt();
rl.on('line', (line) => { rl.on("line", (line) => {
viewLog([line]); viewLog([line]);
}); });
function viewLog(log) { function viewLog(log) {
if(log[log.length-1] == "") log.pop(); if (log[log.length - 1] == "") log.pop();
if(log[0] == "") log.shift(); if (log[0] == "") log.shift();
for(var i=0;i<log.length;i++) { for (var i = 0; i < log.length; i++) {
if(log[i].indexOf("SERVER REQUEST MESSAGE") != -1) { if (log[i].indexOf("SERVER REQUEST MESSAGE") != -1) {
log[i] = log[i].replace("SERVER REQUEST MESSAGE","\x1b[34m\x1b[1mSERVER REQUEST MESSAGE\x1b[22m") + "\x1b[37m\x1b[0m"; log[i] =
} else if(log[i].indexOf("SERVER RESPONSE MESSAGE") != -1) { log[i].replace(
log[i] = log[i].replace("SERVER RESPONSE MESSAGE","\x1b[32m\x1b[1mSERVER RESPONSE MESSAGE\x1b[22m") + "\x1b[37m\x1b[0m"; "SERVER REQUEST MESSAGE",
} else if(log[i].indexOf("SERVER RESPONSE ERROR MESSAGE") != -1) { "\x1b[34m\x1b[1mSERVER REQUEST MESSAGE\x1b[22m",
log[i] = log[i].replace("SERVER RESPONSE ERROR MESSAGE","\x1b[31m\x1b[1mSERVER RESPONSE ERROR MESSAGE\x1b[22m") + "\x1b[37m\x1b[0m"; ) + "\x1b[37m\x1b[0m";
} else if(log[i].indexOf("SERVER ERROR MESSAGE") != -1) { } else if (log[i].indexOf("SERVER RESPONSE MESSAGE") != -1) {
log[i] = log[i].replace("SERVER ERROR MESSAGE","\x1b[41m\x1b[1mSERVER ERROR MESSAGE\x1b[22m") + "\x1b[40m\x1b[0m"; log[i] =
} else if(log[i].indexOf("SERVER WARNING MESSAGE") != -1) { log[i].replace(
log[i] = log[i].replace("SERVER WARNING MESSAGE","\x1b[43m\x1b[1mSERVER WARNING MESSAGE\x1b[22m") + "\x1b[40m\x1b[0m"; "SERVER RESPONSE MESSAGE",
} else if(log[i].indexOf("SERVER MESSAGE") != -1) { "\x1b[32m\x1b[1mSERVER RESPONSE MESSAGE\x1b[22m",
log[i] = log[i].replace("SERVER MESSAGE","\x1b[1mSERVER MESSAGE\x1b[22m"); ) + "\x1b[37m\x1b[0m";
} } else if (log[i].indexOf("SERVER RESPONSE ERROR MESSAGE") != -1) {
console.log(log[i]); log[i] =
log[i].replace(
"SERVER RESPONSE ERROR MESSAGE",
"\x1b[31m\x1b[1mSERVER RESPONSE ERROR MESSAGE\x1b[22m",
) + "\x1b[37m\x1b[0m";
} else if (log[i].indexOf("SERVER ERROR MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER ERROR MESSAGE",
"\x1b[41m\x1b[1mSERVER ERROR MESSAGE\x1b[22m",
) + "\x1b[40m\x1b[0m";
} else if (log[i].indexOf("SERVER WARNING MESSAGE") != -1) {
log[i] =
log[i].replace(
"SERVER WARNING MESSAGE",
"\x1b[43m\x1b[1mSERVER WARNING MESSAGE\x1b[22m",
) + "\x1b[40m\x1b[0m";
} else if (log[i].indexOf("SERVER MESSAGE") != -1) {
log[i] = log[i].replace(
"SERVER MESSAGE",
"\x1b[1mSERVER MESSAGE\x1b[22m",
);
} }
console.log(log[i]);
}
} }

File diff suppressed because one or more lines are too long

View file

@ -1 +1 @@
require("./svr.js"); require("./svr.js");

View file

@ -1,73 +1,147 @@
//SVR.JS USER TOOL //SVR.JS USER TOOL
var readline = require("readline"); const readline = require("readline");
var process = require("process"); const fs = require("fs");
var fs = require("fs"); let crypto = {};
try { try {
var crypto = require('crypto'); crypto = require("crypto");
} catch (ex) { } catch (ex) {
var crypto = {}; crypto = {};
crypto.__disabled__ = null; crypto.__disabled__ = null;
crypto.createHash = function (type) { crypto.createHash = (type) => {
if (type != "SHA256") throw new Error("Hash type not supported!"); if (type != "SHA256") throw new Error("Hash type not supported!");
return { return {
msg: "", msg: "",
update: function (a) { update: (a) => {
this.msg = a; this.msg = a;
return this; return this;
}, },
digest: function (ty) { digest: (ty) => {
var chrsz = 8; const chrsz = 8;
var hexcase = 0; const hexcase = 0;
function safe_add(x, y) { const safeAdd = (x, y) => {
var lsw = (x & 0xFFFF) + (y & 0xFFFF); const lsw = (x & 0xffff) + (y & 0xffff);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16); const msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF); return (msw << 16) | (lsw & 0xffff);
} };
function S(X, n) { const S = (X, n) => {
return (X >>> n) | (X << (32 - n)); return (X >>> n) | (X << (32 - n));
} };
function R(X, n) { const R = (X, n) => {
return (X >>> n); return X >>> n;
} };
function Ch(x, y, z) { const Ch = (x, y, z) => {
return ((x & y) ^ ((~x) & z)); return (x & y) ^ (~x & z);
} };
function Maj(x, y, z) { const Maj = (x, y, z) => {
return ((x & y) ^ (x & z) ^ (y & z)); return (x & y) ^ (x & z) ^ (y & z);
} };
function Sigma0256(x) { const Sigma0256 = (x) => {
return (S(x, 2) ^ S(x, 13) ^ S(x, 22)); return S(x, 2) ^ S(x, 13) ^ S(x, 22);
} };
function Sigma1256(x) { const Sigma1256 = (x) => {
return (S(x, 6) ^ S(x, 11) ^ S(x, 25)); return S(x, 6) ^ S(x, 11) ^ S(x, 25);
} };
function Gamma0256(x) { const Gamma0256 = (x) => {
return (S(x, 7) ^ S(x, 18) ^ R(x, 3)); return S(x, 7) ^ S(x, 18) ^ R(x, 3);
} };
function Gamma1256(x) { const Gamma1256 = (x) => {
return (S(x, 17) ^ S(x, 19) ^ R(x, 10)); return S(x, 17) ^ S(x, 19) ^ R(x, 10);
} };
function core_sha256(m, l) { function coreSha256(m, l) {
var K = new Array(0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2); const K = new Array(
var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19); 0x428a2f98,
var W = new Array(64); 0x71374491,
var a, b, c, d, e, f, g, h, i, j; 0xb5c0fbcf,
var T1, T2; 0xe9b5dba5,
0x3956c25b,
0x59f111f1,
0x923f82a4,
0xab1c5ed5,
0xd807aa98,
0x12835b01,
0x243185be,
0x550c7dc3,
0x72be5d74,
0x80deb1fe,
0x9bdc06a7,
0xc19bf174,
0xe49b69c1,
0xefbe4786,
0xfc19dc6,
0x240ca1cc,
0x2de92c6f,
0x4a7484aa,
0x5cb0a9dc,
0x76f988da,
0x983e5152,
0xa831c66d,
0xb00327c8,
0xbf597fc7,
0xc6e00bf3,
0xd5a79147,
0x6ca6351,
0x14292967,
0x27b70a85,
0x2e1b2138,
0x4d2c6dfc,
0x53380d13,
0x650a7354,
0x766a0abb,
0x81c2c92e,
0x92722c85,
0xa2bfe8a1,
0xa81a664b,
0xc24b8b70,
0xc76c51a3,
0xd192e819,
0xd6990624,
0xf40e3585,
0x106aa070,
0x19a4c116,
0x1e376c08,
0x2748774c,
0x34b0bcb5,
0x391c0cb3,
0x4ed8aa4a,
0x5b9cca4f,
0x682e6ff3,
0x748f82ee,
0x78a5636f,
0x84c87814,
0x8cc70208,
0x90befffa,
0xa4506ceb,
0xbef9a3f7,
0xc67178f2,
);
let HASH = new Array(
0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19,
);
let W = new Array(64);
let a, b, c, d, e, f, g, h;
let T1, T2;
m[l >> 5] |= 0x80 << (24 - l % 32); m[l >> 5] |= 0x80 << (24 - (l % 32));
m[((l + 64 >> 9) << 4) + 15] = l; m[(((l + 64) >> 9) << 4) + 15] = l;
for (var i = 0; i < m.length; i += 16) { for (let i = 0; i < m.length; i += 16) {
a = HASH[0]; a = HASH[0];
b = HASH[1]; b = HASH[1];
c = HASH[2]; c = HASH[2];
@ -77,55 +151,65 @@ try {
g = HASH[6]; g = HASH[6];
h = HASH[7]; h = HASH[7];
for (var j = 0; j < 64; j++) { for (let j = 0; j < 64; j++) {
if (j < 16) W[j] = m[j + i]; if (j < 16) W[j] = m[j + i];
else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]); else
W[j] = safeAdd(
safeAdd(
safeAdd(Gamma1256(W[j - 2]), W[j - 7]),
Gamma0256(W[j - 15]),
),
W[j - 16],
);
T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]); T1 = safeAdd(
T2 = safe_add(Sigma0256(a), Maj(a, b, c)); safeAdd(safeAdd(safeAdd(h, Sigma1256(e)), Ch(e, f, g)), K[j]),
W[j],
);
T2 = safeAdd(Sigma0256(a), Maj(a, b, c));
h = g; h = g;
g = f; g = f;
f = e; f = e;
e = safe_add(d, T1); e = safeAdd(d, T1);
d = c; d = c;
c = b; c = b;
b = a; b = a;
a = safe_add(T1, T2); a = safeAdd(T1, T2);
} }
HASH[0] = safe_add(a, HASH[0]); HASH[0] = safeAdd(a, HASH[0]);
HASH[1] = safe_add(b, HASH[1]); HASH[1] = safeAdd(b, HASH[1]);
HASH[2] = safe_add(c, HASH[2]); HASH[2] = safeAdd(c, HASH[2]);
HASH[3] = safe_add(d, HASH[3]); HASH[3] = safeAdd(d, HASH[3]);
HASH[4] = safe_add(e, HASH[4]); HASH[4] = safeAdd(e, HASH[4]);
HASH[5] = safe_add(f, HASH[5]); HASH[5] = safeAdd(f, HASH[5]);
HASH[6] = safe_add(g, HASH[6]); HASH[6] = safeAdd(g, HASH[6]);
HASH[7] = safe_add(h, HASH[7]); HASH[7] = safeAdd(h, HASH[7]);
} }
return HASH; return HASH;
} }
function str2binb(str) { const str2binb = (str) => {
var bin = Array(); let bin = Array();
var mask = (1 << chrsz) - 1; const mask = (1 << chrsz) - 1;
for (var i = 0; i < str.length * chrsz; i += chrsz) { for (let i = 0; i < str.length * chrsz; i += chrsz) {
bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i % 32); bin[i >> 5] |=
(str.charCodeAt(i / chrsz) & mask) << (24 - (i % 32));
} }
return bin; return bin;
} };
function Utf8Encode(string) { const Utf8Encode = (string) => {
string = string.replace(/\r\n/g, '\n'); string = string.replace(/\r\n/g, "\n");
var utftext = ''; let utftext = "";
for (var n = 0; n < string.length; n++) { for (let n = 0; n < string.length; n++) {
let c = string.charCodeAt(n);
var c = string.charCodeAt(n);
if (c < 128) { if (c < 128) {
utftext += String.fromCharCode(c); utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) { } else if (c > 127 && c < 2048) {
utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128); utftext += String.fromCharCode((c & 63) | 128);
} else { } else {
@ -133,43 +217,46 @@ try {
utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128); utftext += String.fromCharCode((c & 63) | 128);
} }
} }
return utftext; return utftext;
} };
function binb2hex(binarray) { const binb2hex = (binarray) => {
var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef'; const hexTab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = ''; let str = "";
for (var i = 0; i < binarray.length * 4; i++) { for (let i = 0; i < binarray.length * 4; i++) {
str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) + str +=
hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF); hexTab.charAt(
(binarray[i >> 2] >> ((3 - (i % 4)) * 8 + 4)) & 0xf,
) +
hexTab.charAt((binarray[i >> 2] >> ((3 - (i % 4)) * 8)) & 0xf);
} }
return str; return str;
} };
s = Utf8Encode(this.msg); let s = Utf8Encode(this.msg);
var str = binb2hex(core_sha256(str2binb(s), s.length * chrsz)); let str = binb2hex(coreSha256(str2binb(s), s.length * chrsz));
if (ty == "hex") return str; if (ty == "hex") return str;
var hx = []; let hx = [];
for (var i = 0; i < str.length; i += 2) { for (var i = 0; i < str.length; i += 2) {
hx.push(parseInt(str[i] + str[i + 1], 16)); hx.push(parseInt(str[i] + str[i + 1], 16));
} }
return new Buffer(hx); return Buffer.from(hx);
} },
}; };
} };
} }
if (!crypto.randomInt) { if (!crypto.randomInt) {
crypto.randomInt = function (min, max) { crypto.randomInt = (min, max) => {
return Math.round(Math.random() * (max - min)) + min; return Math.round(Math.random() * (max - min)) + min;
} };
} }
var configJSON = {};
let configJSON = {};
if (fs.existsSync("config.json")) { if (fs.existsSync("config.json")) {
var configJSONf = ""; let configJSONf = "";
try { try {
configJSONf = fs.readFileSync("config.json"); //Read JSON File configJSONf = fs.readFileSync("config.json"); //Read JSON File
} catch (ex) { } catch (ex) {
@ -182,26 +269,48 @@ if (fs.existsSync("config.json")) {
} }
} }
var users = []; let users = [];
if (configJSON.users != undefined) users = configJSON.users; if (configJSON.users != undefined) users = configJSON.users;
function saveConfig() { function saveConfig() {
var configJSONobj = {}; let configJSONobj = {};
if (fs.existsSync("./config.json")) configJSONobj = JSON.parse(fs.readFileSync("./config.json").toString()); if (fs.existsSync("./config.json"))
configJSONobj = JSON.parse(fs.readFileSync("./config.json").toString());
configJSONobj.users = users; configJSONobj.users = users;
var configString = JSON.stringify(configJSONobj, null, 2); const configString = JSON.stringify(configJSONobj, null, 2);
fs.writeFileSync("config.json", configString); fs.writeFileSync("config.json", configString);
} }
var args = process.argv; const args = process.argv;
var user = ""; let user = "";
var action = "change"; let action = "change";
var forcechange = false; let forcechange = false;
if (process.argv.length <= (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1 ? 2 : 1)) args.push("-h"); if (
for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1 ? 2 : 1); i < args.length; i++) { process.argv.length <=
if (args[i] == "-h" || args[i] == "--help" || args[i] == "-?" || args[i] == "/h" || args[i] == "/?") { (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1
? 2
: 1)
)
args.push("-h");
for (
let i =
process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("bun") > -1
? 2
: 1;
i < args.length;
i++
) {
if (
args[i] == "-h" ||
args[i] == "--help" ||
args[i] == "-?" ||
args[i] == "/h" ||
args[i] == "/?"
) {
console.log("SVR.JS user tool usage:"); console.log("SVR.JS user tool usage:");
console.log("node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>"); console.log(
"node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>",
);
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("-a --add -- Add an user"); console.log("-a --add -- Add an user");
console.log("-d --delete -- Deletes an user"); console.log("-d --delete -- Deletes an user");
@ -210,7 +319,9 @@ for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("b
} else if (args[i] == "-a" || args[i] == "--add") { } else if (args[i] == "-a" || args[i] == "--add") {
if (action != "change") { if (action != "change") {
console.log("Multiple actions specified."); console.log("Multiple actions specified.");
console.log("node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>"); console.log(
"node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>",
);
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("-a --add -- Add an user"); console.log("-a --add -- Add an user");
console.log("-d --delete -- Deletes an user"); console.log("-d --delete -- Deletes an user");
@ -221,7 +332,9 @@ for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("b
} else if (args[i] == "-d" || args[i] == "--delete") { } else if (args[i] == "-d" || args[i] == "--delete") {
if (action != "change") { if (action != "change") {
console.log("Multiple actions specified."); console.log("Multiple actions specified.");
console.log("node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>"); console.log(
"node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>",
);
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("-a --add -- Add an user"); console.log("-a --add -- Add an user");
console.log("-d --delete -- Deletes an user"); console.log("-d --delete -- Deletes an user");
@ -232,7 +345,9 @@ for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("b
} else if (args[i] == "-x") { } else if (args[i] == "-x") {
if (forcechange) { if (forcechange) {
console.log("Multiple -x options specified."); console.log("Multiple -x options specified.");
console.log("node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>"); console.log(
"node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>",
);
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("-a --add -- Add an user"); console.log("-a --add -- Add an user");
console.log("-d --delete -- Deletes an user"); console.log("-d --delete -- Deletes an user");
@ -243,7 +358,9 @@ for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("b
} else { } else {
if (user != "") { if (user != "") {
console.log("Multiple users specified."); console.log("Multiple users specified.");
console.log("node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>"); console.log(
"node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>",
);
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("-a --add -- Add an user"); console.log("-a --add -- Add an user");
console.log("-d --delete -- Deletes an user"); console.log("-d --delete -- Deletes an user");
@ -256,7 +373,9 @@ for (var i = (process.argv[0].indexOf("node") > -1 || process.argv[0].indexOf("b
if (user == "") { if (user == "") {
console.log("No user specified."); console.log("No user specified.");
console.log("node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>"); console.log(
"node svrpasswd.js [-h] [--help] [-?] [/h] [/?] [-x] [-a|--add|-d|--delete] <username>",
);
console.log("-h -? /h /? --help -- Displays help"); console.log("-h -? /h /? --help -- Displays help");
console.log("-a --add -- Add an user"); console.log("-a --add -- Add an user");
console.log("-d --delete -- Deletes an user"); console.log("-d --delete -- Deletes an user");
@ -265,8 +384,8 @@ if (user == "") {
} }
function getUserIndex(username) { function getUserIndex(username) {
var ind = -1 let ind = -1;
for (var i = 0; i < users.length; i++) { for (let i = 0; i < users.length; i++) {
if (users[i].name == username) { if (users[i].name == username) {
ind = i; ind = i;
break; break;
@ -276,44 +395,45 @@ function getUserIndex(username) {
} }
function sha256(msg) { function sha256(msg) {
var hash = crypto.createHash("SHA256"); let hash = crypto.createHash("SHA256");
hash.update(msg); hash.update(msg);
return hash.digest('hex'); return hash.digest("hex");
} }
function generateSalt() { function generateSalt() {
var token = ""; let token = "";
var strlist = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const strlist =
for (var i = 0; i < 63; i++) { "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i = 0; i < 63; i++) {
token += strlist[crypto.randomInt(0, strlist.length)]; token += strlist[crypto.randomInt(0, strlist.length)];
} }
return token; return token;
} }
function password(callback) { function password(callback) {
var rl = readline.createInterface({ const rl = readline.createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout, output: process.stdout,
prompt: 'Password: ' prompt: "Password: ",
}); });
rl.prompt(); rl.prompt();
process.stdout.writeold = process.stdout.write; process.stdout.writeold = process.stdout.write;
process.stdout.write = function (s) { process.stdout.write = (s) => {
process.stdout.writeold(s.replace(/[^\r\n]/g, "")); process.stdout.writeold(s.replace(/[^\r\n]/g, ""));
}; };
rl.once('line', function (line) { rl.once("line", (line) => {
process.stdout.write = process.stdout.writeold; process.stdout.write = process.stdout.writeold;
var rl = readline.createInterface({ var rl = readline.createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout, output: process.stdout,
prompt: 'Confirm password: ', prompt: "Confirm password: ",
}); });
rl.prompt(); rl.prompt();
process.stdout.writeold = process.stdout.write; process.stdout.writeold = process.stdout.write;
process.stdout.write = function (s) { process.stdout.write = (s) => {
process.stdout.writeold(s.replace(/[^\r\n]/g, "")); process.stdout.writeold(s.replace(/[^\r\n]/g, ""));
}; };
rl.on('line', function (line2) { rl.on("line", (line2) => {
process.stdout.write = process.stdout.writeold; process.stdout.write = process.stdout.writeold;
rl.close(); rl.close();
if (line != line2) callback(false); if (line != line2) callback(false);
@ -326,32 +446,45 @@ function promptAlgorithms(callback, bypass, pbkdf2, scrypt) {
if (bypass) { if (bypass) {
if (scrypt) { if (scrypt) {
callback("scrypt"); callback("scrypt");
} else if(pbkdf2) { } else if (pbkdf2) {
callback("pbkdf2"); callback("pbkdf2");
} else { } else {
callback("sha256"); callback("sha256");
} }
return; return;
} }
var algorithms = { let algorithms = {
sha256: "Salted SHA256 (1 iteration) - fastest and uses least memory, but less secure", sha256:
pbkdf2: "PBKDF2 (PBKDF2-HMAC-SHA512, 36250 iterations) - more secure and uses less memory, but slower", "Salted SHA256 (1 iteration) - fastest and uses least memory, but less secure",
scrypt: "scrypt (N=2^14, r=8, p=1) - faster and more secure, but uses more memory" pbkdf2:
} "PBKDF2 (PBKDF2-HMAC-SHA512, 36250 iterations) - more secure and uses less memory, but slower",
if (!crypto.pbkdf2 || (process.isBun && !(process.versions.bun && !process.versions.bun.match(/^(?:0\.|1\.0\.|1\.1\.[0-9](?![0-9])|1\.1\.1[0-2](?![0-9]))/)))) delete algorithms.pbkdf2; scrypt:
var algorithmNames = Object.keys(algorithms); "scrypt (N=2^14, r=8, p=1) - faster and more secure, but uses more memory",
};
if (
!crypto.pbkdf2 ||
(process.isBun &&
!(
process.versions.bun &&
!process.versions.bun.match(
/^(?:0\.|1\.0\.|1\.1\.[0-9](?![0-9])|1\.1\.1[0-2](?![0-9]))/,
)
))
)
delete algorithms.pbkdf2;
const algorithmNames = Object.keys(algorithms);
if (algorithmNames.length < 2) callback(algorithmNames[0]); if (algorithmNames.length < 2) callback(algorithmNames[0]);
console.log("Select password hashing algorithm. Available algorithms:"); console.log("Select password hashing algorithm. Available algorithms:");
for (var i = 0; i < algorithmNames.length; i++) { for (var i = 0; i < algorithmNames.length; i++) {
console.log(algorithmNames[i] + " - " + algorithms[algorithmNames[i]]); console.log(algorithmNames[i] + " - " + algorithms[algorithmNames[i]]);
} }
var rl = readline.createInterface({ const rl = readline.createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout, output: process.stdout,
prompt: 'Algorithm: ' prompt: "Algorithm: ",
}); });
rl.prompt(); rl.prompt();
rl.on('line', function (line) { rl.on("line", (line) => {
rl.close(); rl.close();
line = line.trim(); line = line.trim();
if (!algorithms[line]) callback(false); if (!algorithms[line]) callback(false);
@ -359,7 +492,7 @@ function promptAlgorithms(callback, bypass, pbkdf2, scrypt) {
}); });
} }
var userindex = getUserIndex(user); const userindex = getUserIndex(user);
if (action == "add" && userindex != -1) { if (action == "add" && userindex != -1) {
console.log("User alerady exists."); console.log("User alerady exists.");
process.exit(1); process.exit(1);
@ -372,22 +505,24 @@ if (action == "delete") {
saveConfig(); saveConfig();
console.log("User deleted successfully"); console.log("User deleted successfully");
} else if (action == "add") { } else if (action == "add") {
promptAlgorithms(function (algorithm) { promptAlgorithms((algorithm) => {
if (!algorithm) { if (!algorithm) {
console.log("Invalid algorithm!"); console.log("Invalid algorithm!");
process.exit(1); process.exit(1);
} else { } else {
password(function (password) { password((password) => {
if (!password) { if (!password) {
console.log("Passwords don't match!"); console.log("Passwords don't match!");
process.exit(1); process.exit(1);
} else { } else {
var salt = generateSalt(); const salt = generateSalt();
var hash = ""; let hash = "";
if (algorithm == "scrypt") { if (algorithm == "scrypt") {
hash = crypto.scryptSync(password, salt, 64).toString("hex"); hash = crypto.scryptSync(password, salt, 64).toString("hex");
} else if (algorithm == "pbkdf2") { } else if (algorithm == "pbkdf2") {
hash = crypto.pbkdf2Sync(password, salt, 36250, 64, "sha512").toString("hex"); hash = crypto
.pbkdf2Sync(password, salt, 36250, 64, "sha512")
.toString("hex");
} else { } else {
hash = sha256(password + salt); hash = sha256(password + salt);
} }
@ -395,9 +530,9 @@ if (action == "delete") {
name: user, name: user,
pass: hash, pass: hash,
salt: salt, salt: salt,
pbkdf2: (algorithm == "pbkdf2" ? true : undefined), pbkdf2: algorithm == "pbkdf2" ? true : undefined,
scrypt: (algorithm == "scrypt" ? true : undefined), scrypt: algorithm == "scrypt" ? true : undefined,
__svrpasswd_l2: true __svrpasswd_l2: true,
}); });
saveConfig(); saveConfig();
console.log("User added successfully"); console.log("User added successfully");
@ -406,37 +541,44 @@ if (action == "delete") {
} }
}); });
} else { } else {
promptAlgorithms(function (algorithm) { promptAlgorithms(
if (!algorithm) { (algorithm) => {
console.log("Invalid algorithm!"); if (!algorithm) {
process.exit(1); console.log("Invalid algorithm!");
} else { process.exit(1);
password(function (password) { } else {
if (!password) { password((password) => {
console.log("Passwords don't match!"); if (!password) {
process.exit(1); console.log("Passwords don't match!");
} else { process.exit(1);
var salt = generateSalt();
var hash = "";
if (algorithm == "scrypt") {
hash = crypto.scryptSync(password, salt, 64).toString("hex");
} else if (algorithm == "pbkdf2") {
hash = crypto.pbkdf2Sync(password, salt, 36250, 64, "sha512").toString("hex");
} else { } else {
hash = sha256(password + salt); var salt = generateSalt();
var hash = "";
if (algorithm == "scrypt") {
hash = crypto.scryptSync(password, salt, 64).toString("hex");
} else if (algorithm == "pbkdf2") {
hash = crypto
.pbkdf2Sync(password, salt, 36250, 64, "sha512")
.toString("hex");
} else {
hash = sha256(password + salt);
}
users[userindex] = {
name: user,
pass: hash,
salt: salt,
pbkdf2: algorithm == "pbkdf2" ? true : undefined,
scrypt: algorithm == "scrypt" ? true : undefined,
__svrpasswd_l2: true,
};
saveConfig();
console.log("Password changed successfully");
} }
users[userindex] = { });
name: user, }
pass: hash, },
salt: salt, users[userindex].__svrpasswd_l2 && !forcechange,
pbkdf2: (algorithm == "pbkdf2" ? true : undefined), users[userindex].pbkdf2,
scrypt: (algorithm == "scrypt" ? true : undefined), users[userindex].scrypt,
__svrpasswd_l2: true );
};
saveConfig();
console.log("Password changed successfully");
}
});
}
}, (users[userindex].__svrpasswd_l2 && !forcechange), users[userindex].pbkdf2, users[userindex].scrypt);
} }