forked from HemmeligOrg/Hemmelig.app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
180 lines (153 loc) · 4.82 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
import meow from 'meow';
import fetch from 'node-fetch';
import YAML from 'yaml';
import { encrypt, generateKey } from './shared/helpers/crypto.js';
// Adding this hack to make it work for pkg
// https://github.com/vercel/pkg/issues/1291#issuecomment-1360586986
let url = import.meta.url;
// Allow rewriting `import.meta.url` to `__dirname` when bundling with esbuild
if (!url.startsWith('file://')) url = new URL(`file://${import.meta.url}`).toString();
// end hack
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
--output, -o Present the result as json|yaml. Example: --output=json
Examples
$ hemmelig "my super secret" --password=1337
[*] Hemmelig.app URL: https://hemmelig.app/secret/thesecretid#encryption_key=myencryptionkey
# Pipe data to the hemmelig cli
$ cat mysecret.txt | hemmelig
[*] Hemmelig.app URL: https://hemmelig.app/secret/thesecretid2#encryption_key=myencryptionkey2
# Different output
$ hemmelig "I am secret" -o=json
{"encryptionKey":"9LiWq3iMAF0IkQs1tecOxbYKFesEnTN9","secretId":"manageable_CEsgWtxEaNNbwld6PjwyF1bQaiy4jQl9","url":"https://hemmelig.app/secret/manageable_CEsgWtxEaNNbwld6PjwyF1bQaiy4jQl9#encryption_key=9LiWq3iMAF0IkQs1tecOxbYKFesEnTN9"}
`,
{
importMeta: { url },
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',
},
output: {
type: 'string',
alias: 'o',
default: 'text',
},
},
}
);
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/${secretId}#encryption_key=${encryptionKey}`;
const createOutput = (encryptionKey, secretId) => {
const url = getSecretURL(encryptionKey, secretId);
if (cli.flags.output === 'json') {
return JSON.stringify({
encryptionKey,
secretId,
url,
});
} else if (cli.flags.output === 'yaml') {
return YAML.stringify({
encryptionKey,
secretId,
url,
});
}
return `[*] Hemmelig.app URL: ${url}`;
};
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(createOutput(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);
})();