-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfidant.ts
More file actions
executable file
·144 lines (130 loc) · 3.92 KB
/
Copy pathconfidant.ts
File metadata and controls
executable file
·144 lines (130 loc) · 3.92 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/env node
import { input, password } from "@inquirer/prompts";
import chalk from "chalk-template";
import { Command } from "commander";
import { decrypt_vault, encrypt_vault, initialize, reset } from "./src/main";
import { readdirSync } from "fs";
import {
Files,
getDecryptedName,
getDirectoryNames,
getRandomPassword,
getVaultName,
log,
panic,
} from "./src/utils";
const program = new Command();
const { exit } = process;
program.name("confidant").description("Creates a very secure file vault.");
program
.command("init")
.description("initialize a confidant vault")
.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];
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:}}`,
default: genPass,
});
const confpass = await input({
message: chalk`{reset {yellow Enter the password again:}}`,
default: genPass,
});
if (pass !== confpass) {
panic`Passwords don't match. Exiting...`;
}
await initialize(pass, dirname);
console.log(chalk`{green Initialized a new Confidant vault sucessfully!}`);
});
program
.command("decrypt")
.description("decrypt the vault")
.argument("[vault]", "name of the vault to decrypt")
.option("-l, --live", "decrypt in live mode")
.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);
if (opts.live) {
await input({
message: chalk`{yellow Live mode started. Press ENTER to encrypt:}`,
});
await encrypt_vault(args);
console.log(chalk`{green Encrypted successfully!}`);
} else {
console.log(chalk`{green Decrypted sucessfully!}`);
}
});
program
.command("encrypt")
.description("encrypt the vault")
.argument("[vault]", "name of the vault to encrypt")
.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(vault);
console.log(chalk`{green Successfully encrypted!}`);
});
program
.command("reset")
.description("reset a vault's password")
.argument("[vault]", "name of the vault to decrypt")
.action(async (vault) => {
if (!vault) {
vault = await getVaultName();
} else {
const vaults = new Files(/(.*).vault/g).intersection(
new Files(/(.*).key/g),
).data as string[];
if (!vaults.includes(vault)) {
console.log(
chalk`{red Vault "${vault}" not found in current directory.}`,
);
exit(1);
}
}
log`{blue Resetting password of "{green ${vault}}"...}`;
const recphrase = await input({
message: chalk`{reset {yellow Enter the recovery phrase:}}`,
});
reset(vault, recphrase);
});
await program.parseAsync();