forked from mirrors/amnezia-wg-easy
Allow wgpw to prompt for a password through stdin (#1348)
* Allow wgpw to prompt for a password through stdin If the user does not pass the password as a parameter, they are prompted for it through stdin. The password is not echoed back, just like any other command-line log-in prompt (ie. sudo). * Fix lint errors in wgpw
This commit is contained in:
parent
4758c0dddc
commit
11872de321
2 changed files with 35 additions and 1 deletions
30
src/wgpw.mjs
30
src/wgpw.mjs
|
@ -2,6 +2,8 @@
|
|||
|
||||
// Import needed libraries
|
||||
import bcrypt from 'bcryptjs';
|
||||
import { Writable } from 'stream';
|
||||
import readline from 'readline';
|
||||
|
||||
// Function to generate hash
|
||||
const generateHash = async (password) => {
|
||||
|
@ -31,12 +33,35 @@ const comparePassword = async (password, hash) => {
|
|||
}
|
||||
};
|
||||
|
||||
const readStdinPassword = () => {
|
||||
return new Promise((resolve) => {
|
||||
process.stdout.write('Enter your password: ');
|
||||
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: new Writable({
|
||||
write(_chunk, _encoding, callback) {
|
||||
callback();
|
||||
},
|
||||
}),
|
||||
terminal: true,
|
||||
});
|
||||
|
||||
rl.question('', (answer) => {
|
||||
rl.close();
|
||||
// Print a new line after password prompt
|
||||
process.stdout.write('\n');
|
||||
resolve(answer);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
// Retrieve command line arguments
|
||||
const args = process.argv.slice(2); // Ignore the first two arguments
|
||||
if (args.length > 2) {
|
||||
throw new Error('Usage : wgpw YOUR_PASSWORD [HASH]');
|
||||
throw new Error('Usage : wgpw [YOUR_PASSWORD] [HASH]');
|
||||
}
|
||||
|
||||
const [password, hash] = args;
|
||||
|
@ -44,6 +69,9 @@ const comparePassword = async (password, hash) => {
|
|||
await comparePassword(password, hash);
|
||||
} else if (password) {
|
||||
await generateHash(password);
|
||||
} else {
|
||||
const password = await readStdinPassword();
|
||||
await generateHash(password);
|
||||
}
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue