feat: error correction and new features to init, decrypt and encrypt

This commit is contained in:
2024-09-30 19:42:56 +05:30
parent 009f16796c
commit 3a90ed0d61
4 changed files with 171 additions and 99 deletions

View File

@@ -5,13 +5,17 @@ import { $ } from "bun";
import chalk from "chalk-template";
import { Command } from "commander";
import { decrypt_vault, encrypt_vault, initialize, recovery } from "./src/main";
import { existsSync } from "fs";
import { existsSync, readdirSync } from "fs";
import checkForFiles, {
Files,
getDecryptedName,
getDirectoryNames,
getRandomPassword,
getVaultName,
log,
panic,
} from "./src/utils";
import { randomBytes } from "crypto";
const program = new Command();
const { exit } = process;
@@ -22,16 +26,29 @@ program.name("confidant").description("Creates a very secure file vault.");
program
.command("init")
.description("initialize a confidant vault")
.action(async () => {
const dirname = (await getDirectoryNames()) as string;
.argument("[directory]", "Directory to use to create a vault")
.action(async (dirname) => {
if (!dirname) {
dirname = (await getDirectoryNames()) as string;
}
const selectedDir = readdirSync(".").filter((x) => x.match(dirname))[0];
const pass = await password({
if (!selectedDir) {
console.log(
chalk`{red Directory "${dirname}" not found in current location.}`,
);
exit(1);
}
log`{blue Using "{green ${dirname}}" to create a vault...}`;
const genPass = getRandomPassword(14);
const pass = await input({
message: chalk`{reset {yellow Enter a password to use:}}`,
mask: "•",
default: genPass,
});
const confpass = await password({
const confpass = await input({
message: chalk`{reset {yellow Enter the password again:}}`,
mask: "•",
default: genPass,
});
if (pass !== confpass) {
panic`Passwords don't match. Exiting...`;
@@ -44,24 +61,35 @@ program
program
.command("decrypt")
.description("decrypt the vault")
.argument("[vault]", "Name of the vault to decrypt")
.option("-l, --live", "decrypt in live mode")
.option("-v, --vault <name>", "name of the vault to decrypt")
.action(async (args) => {
if (!args.vault) {
args.vault = await getVaultName();
.action(async (args, opts) => {
if (!args) {
args = await getVaultName();
} else {
const vaults = new Files(/(.*).vault/g).intersection(
new Files(/(.*).key/g),
).data as string[];
if (!vaults.includes(args)) {
console.log(
chalk`{red Vault "${args}" not found in current directory.}`,
);
exit(1);
}
}
log`{blue Decrypting "{green ${args}}"...}`;
const pass = await password({
message: chalk`{reset {yellow Enter the password:}}`,
mask: "•",
});
await decrypt_vault(pass, args.vault);
if (args.live) {
await decrypt_vault(pass, args);
if (opts.live) {
await input({
message: chalk`{yellow Live mode started. Press ENTER to encrypt}`,
message: chalk`{yellow Live mode started. Press ENTER to encrypt:}`,
});
await encrypt_vault(args.vault);
console.log(chalk`{green Successfully encrypted!}`);
await encrypt_vault(args);
console.log(chalk`{green Encrypted successfully!}`);
} else {
console.log(chalk`{green Decrypted sucessfully!}`);
}
@@ -70,13 +98,23 @@ program
program
.command("encrypt")
.description("encrypt the vault")
.option("-v, --vault <name>", "name of the vault to decrypt")
.action(async (args) => {
if (!args.vault) {
args.vault = await getDecryptedName();
.argument("[vault]", "Name of the vault to decrypt")
.action(async (vault) => {
if (!vault) {
vault = await getDecryptedName();
} else {
const vaults = new Files(/(.*).vault/g).intersection(
new Files(/\.(.*)\.confidant/g),
).data as string[];
if (!vaults.includes(vault)) {
console.log(
chalk`{red Vault "${vault}" not found in current directory.}`,
);
exit(1);
}
}
await encrypt_vault(args.vault);
await encrypt_vault(vault);
console.log(chalk`{green Successfully encrypted!}`);
});