-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwned
executable file
·60 lines (58 loc) · 1.74 KB
/
pwned
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
#!/usr/bin/env node
const { userAgent } = require("./configs");
const puppeteer = require("puppeteer");
const fs = require('fs');
async function isPwned(email) {
let browser;
try {
browser = await puppeteer.launch();
const url = `https://haveibeenpwned.com/unifiedsearch/${email}`;
const [page] = await browser.pages();
await page.setUserAgent(userAgent);
const response = await page.goto(url);
console.log(await response.text());
const { Breaches } = await response.json();
console.log(Breaches)
return Breaches;
} catch(error) {
console.log(error);
return [];
} finally {
if (browser) {
await browser.close();
}
}
}
const args = process.argv;
if (process.stdin.isTTY) {
const { program } = require('commander');
program
.version('0.0.1', '-v, --version', 'Output the current version')
.description('Check if an email has been pwned')
.option("-o --out <output_filename>", "Output redirections into a file")
.arguments('<email>')
.action(async (email, args) => {
const result = await isPwned(email);
if (args.out) {
const outputFile = /\.json$/.test(args.out) ? args.out : `${args.out}.json`;
fs.writeFileSync(`./${outputFile}`, JSON.stringify(result, null, 2));
} else {
console.log(result);
}
})
.parse(args);
} else {
const { createInterface } = require('readline');
const { 2: out, 3: outFilename } = args;
if (out && !outFilename) {
console.log("Missing required <output_filename> for '-o, --out <output_filename>'");
}
const rl = createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', url => {
isPwned(url, { out: out && outFilename });
});
}