|
@ -4,6 +4,7 @@ |
|
|
import bcrypt from 'bcryptjs'; |
|
|
import bcrypt from 'bcryptjs'; |
|
|
import { Writable } from 'stream'; |
|
|
import { Writable } from 'stream'; |
|
|
import readline from 'readline'; |
|
|
import readline from 'readline'; |
|
|
|
|
|
import WireGuardService from './services/WireGuard.js'; |
|
|
|
|
|
|
|
|
// Function to generate hash
|
|
|
// Function to generate hash
|
|
|
const generateHash = async (password) => { |
|
|
const generateHash = async (password) => { |
|
@ -22,10 +23,10 @@ const comparePassword = async (password, hash) => { |
|
|
try { |
|
|
try { |
|
|
const match = await bcrypt.compare(password, hash); |
|
|
const match = await bcrypt.compare(password, hash); |
|
|
if (match) { |
|
|
if (match) { |
|
|
// eslint-disable-next-line no-console
|
|
|
// eslint-disable-next-line no-console
|
|
|
console.log('Password matches the hash !'); |
|
|
console.log('Password matches the hash !'); |
|
|
} else { |
|
|
} else { |
|
|
// eslint-disable-next-line no-console
|
|
|
// eslint-disable-next-line no-console
|
|
|
console.log('Password does not match the hash.'); |
|
|
console.log('Password does not match the hash.'); |
|
|
} |
|
|
} |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
@ -56,22 +57,98 @@ const readStdinPassword = () => { |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const handleClientCommand = async (action, args) => { |
|
|
|
|
|
try { |
|
|
|
|
|
switch (action) { |
|
|
|
|
|
case 'create': { |
|
|
|
|
|
const name = args[0] || 'default_client'; |
|
|
|
|
|
const expiredDate = args[1] || null; |
|
|
|
|
|
const client = await WireGuardService.createClient({ name, expiredDate }); |
|
|
|
|
|
console.log(JSON.stringify(client)); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
case 'get': { |
|
|
|
|
|
const clientId = args[0]; |
|
|
|
|
|
if (!clientId) { |
|
|
|
|
|
throw new Error('Usage: wgcli client get [CLIENT_ID]'); |
|
|
|
|
|
} |
|
|
|
|
|
const client = await WireGuardService.getClient({ clientId }); |
|
|
|
|
|
console.log(JSON.stringify(client)); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
case 'delete': { |
|
|
|
|
|
const clientId = args[0]; |
|
|
|
|
|
if (!clientId) { |
|
|
|
|
|
throw new Error('Usage: wgcli client delete [CLIENT_ID]'); |
|
|
|
|
|
} |
|
|
|
|
|
await WireGuardService.deleteClient({ clientId }); |
|
|
|
|
|
console.log(`Client deleted: ${clientId}`); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
case 'enable': { |
|
|
|
|
|
const clientId = args[0]; |
|
|
|
|
|
if (!clientId) { |
|
|
|
|
|
throw new Error('Usage: wgcli client enable [CLIENT_ID]'); |
|
|
|
|
|
} |
|
|
|
|
|
await WireGuardService.enableClient({ clientId }); |
|
|
|
|
|
console.log(`Client enabled: ${clientId}`); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
case 'disable': { |
|
|
|
|
|
const clientId = args[0]; |
|
|
|
|
|
if (!clientId) { |
|
|
|
|
|
throw new Error('Usage: wgcli client disable [CLIENT_ID]'); |
|
|
|
|
|
} |
|
|
|
|
|
await WireGuardService.disableClient({ clientId }); |
|
|
|
|
|
console.log(`Client disabled: ${clientId}`); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
default: |
|
|
|
|
|
throw new Error('Invalid client command. Usage: client [create|get|delete|enable|disable]'); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
throw new Error(`Failed to execute client command: ${error.message}`); |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
(async () => { |
|
|
(async () => { |
|
|
try { |
|
|
try { |
|
|
// Retrieve command line arguments
|
|
|
// Retrieve command line arguments
|
|
|
const args = process.argv.slice(2); // Ignore the first two arguments
|
|
|
const args = process.argv.slice(2); // Ignore the first two arguments
|
|
|
if (args.length > 2) { |
|
|
if (args.length < 1) { |
|
|
throw new Error('Usage : wgcli [YOUR_PASSWORD] [HASH]'); |
|
|
throw new Error('Usage: wgcli <command> [options]'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const [password, hash] = args; |
|
|
const [command, subCommand, ...restArgs] = args; |
|
|
if (password && hash) { |
|
|
|
|
|
await comparePassword(password, hash); |
|
|
switch (command) { |
|
|
} else if (password) { |
|
|
case 'pw': { |
|
|
await generateHash(password); |
|
|
if (subCommand === 'hash') { |
|
|
} else { |
|
|
if (restArgs.length > 0) { |
|
|
const password = await readStdinPassword(); |
|
|
await generateHash(restArgs[0]); |
|
|
await generateHash(password); |
|
|
} else { |
|
|
|
|
|
const password = await readStdinPassword(); |
|
|
|
|
|
await generateHash(password); |
|
|
|
|
|
} |
|
|
|
|
|
} else if (subCommand === 'compare') { |
|
|
|
|
|
if (restArgs.length !== 2) { |
|
|
|
|
|
throw new Error('Usage: wgcli pw compare [YOUR_PASSWORD] [HASH]'); |
|
|
|
|
|
} |
|
|
|
|
|
await comparePassword(restArgs[0], restArgs[1]); |
|
|
|
|
|
} else { |
|
|
|
|
|
throw new Error('Invalid pw command. Usage: pw [hash|compare]'); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
case 'client': { |
|
|
|
|
|
if (!subCommand) { |
|
|
|
|
|
throw new Error('Usage: wgcli client [create|delete|enable|disable]'); |
|
|
|
|
|
} |
|
|
|
|
|
await handleClientCommand(subCommand, restArgs); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
default: |
|
|
|
|
|
throw new Error('Invalid command. Usage: wgcli <pw|client> [options]'); |
|
|
} |
|
|
} |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
// eslint-disable-next-line no-console
|
|
|
// eslint-disable-next-line no-console
|
|
|