forked from HemmeligOrg/Hemmelig.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
141 lines (120 loc) · 3.5 KB
/
cli.js
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
#!/usr/bin/env node
import meow from 'meow';
import fetch from 'node-fetch';
import { generateKey, encrypt } from './src/shared/helpers/crypto.js';
const cli = meow(
`
Usage
$ hemmelig <secret>
Options
--title, -t The secret title
--password, -p The password to protect the secret
--lifetime, -l The lifetime of the secret
--maxViews, -m The max views of the secret
--cidr, -c Provide the IP or CIDR range
--expire, -e Burn the secret only after the expire time
--url, -u If you have your own instance of the Hemmelig.app
Examples
$ hemmelig "my super secret" --password=1337
[*] Hemmelig.app URL: https://hemmelig.app/secret/myencryptionkey/thesecretid
# Pipe data to the hemmelig cli
$ cat mysecret.txt | hemmelig
[*] Hemmelig.app URL: https://hemmelig.app/secret/myencryptionkey2/thesecretid2
`,
{
importMeta: import.meta,
flags: {
title: {
type: 'string',
alias: 't',
default: '',
},
password: {
type: 'string',
alias: 'p',
default: '',
},
lifetime: {
type: 'number',
alias: 'l',
default: 3600,
},
maxViews: {
type: 'number',
alias: 'm',
default: 1,
},
cidr: {
type: 'string',
alias: 'c',
default: '',
},
expire: {
type: 'boolean',
alias: 'e',
default: false,
},
url: {
type: 'string',
alias: 'u',
default: 'https://hemmelig.app',
},
},
}
);
const createSecret = async (data = {}) => {
const options = {
method: 'POST',
cache: 'no-cache',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
};
try {
const data = await fetch(`${cli.flags.url}/api/secret`, options);
const json = await data.json();
return { ...json, statusCode: data.status };
} catch (error) {
console.error(error);
return { error: 'Failed to create your secret' };
}
};
const getSecretURL = (encryptionKey, secretId) =>
`${cli.flags.url}/secret/${encryptionKey}/${secretId}`;
const submit = async (secret, values) => {
if (!secret) {
console.error('No secret set');
return;
}
const userEncryptionKey = generateKey();
const body = {
text: encrypt(secret, userEncryptionKey),
files: [],
title: encrypt(values.title, userEncryptionKey),
password: values.password,
ttl: values.lifetime,
allowedIp: values.cidr,
preventBurn: values.expire,
maxViews: values.maxViews,
};
const json = await createSecret(body);
console.log(`[*] Hemmelig.app URL: ${getSecretURL(userEncryptionKey, json.id)}`);
};
async function getSecretText() {
const [secret] = cli.input;
if (secret) {
return secret;
}
const data = await new Promise((res) => {
process.stdin.once('data', function (data) {
res(data.toString().trim());
});
});
return data;
}
// Execute the CLI
(async function execute() {
const secret = await getSecretText();
submit(secret, cli.flags);
})();