This repository has been archived by the owner on Nov 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·108 lines (98 loc) · 3.49 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env node
const fs = require("fs"), path = require("path");
const generate = require("./generate");
global.getWords = async function(lang) {
const p = path.join(__dirname, "./words/" + lang + ".txt")
const s = fs.statSync(p);
const start = generate.randrange(0, s.size - 1024);
const r = fs.createReadStream(p, {
flags: 'r',
encoding: 'utf-8',
fd: null,
start: start,
end: start + 1023
});
let words = "";
let resolve, reject;
let pr = new Promise((res, rej) => { resolve = res; reject = rej })
r.on("data", function (chunk) {
words += chunk;
});
r.on("end", function() {
resolve(words.split("\n").slice(start == 0 ? 0 : 1, -1).filter(x => x.length));
});
r.on("error", function(err) {
reject(err);
});
return pr;
}
function help() {
console.log("pag 1.0.0 - The probably user-friendliest password generator out there.")
console.log("Usage: " + process.argv0 + " [type][strength] [-c]");
console.log();
console.log(" [type]");
console.log(" A alphanum");
console.log(" R random");
console.log(" G gibberish (default)");
console.log(" D passphrase-de");
console.log(" E passphrase-en");
console.log();
console.log(" [strength]");
console.log(" 1|2|3|4 complexity of the password (default: 2)");
console.log();
console.log(" [-c]");
console.log(" Copy to clipboard. Only works on Linux with xclip installed.");
console.log();
console.log("Example: " + process.argv0 + " b2 -c");
}
(async function() {
if (process.argv.slice(2).indexOf("--help") > -1) return help();
let type = "gibberish";
let strength = 2;
let args = process.argv.slice(2)
let clipboard = false;
if (args.indexOf("-c") > -1) {
clipboard = true;
args.splice(args.indexOf("-c"));
}
args = args.join(" ").toLowerCase();
let replacements = [
["alphanum", () => type = "alphanum"],
["random", () => type = "random"],
["gibberish", () => type = "gibberish"],
["passphrase-en", () => type = "passphrase-en"],
["passphrase-de", () => type = "passphrase-de"],
["a", () => type = "alphanum"],
["b", () => type = "random"],
["r", () => type = "random"],
["c", () => type = "gibberish"],
["g", () => type = "gibberish"],
["d", () => type = "passphrase-de"],
["e", () => type = "passphrase-en"],
]
for (let i = 0; i < replacements.length; i++) {
if (args != (args = args.replace(replacements[i][0], ""))) {
replacements[i][1](); break;
}
}
args = args.replace(/[1234]/, x => { strength = parseInt(x); return ""; });
// Too many arguments!
if (args.replace(/\s+/g, "").length) return help();
const pw = await generate(type, strength);
console.log(pw);
if (clipboard) {
// Copy to clipboard using xclip
const child = require("child_process");
const p = child.spawn("xclip", ["-se", "c"], { stdio: ["pipe", 1, 1]});
p.stdin.write(pw);
p.stdin.end();
p.on("error", function(error) {
process.stderr.write("Couldn't copy to clipboard: " + error.message + "\n");
});
p.on("exit", function(code, signal) {
if (code !== 0) {
process.stderr.write("Couldn't copy to clipboard: exit code " + result.status + " (signal: " + signal + ")\n");
}
});
}
})();