This repository was archived by the owner on Jul 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (78 loc) · 3.68 KB
/
index.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
const rsa = require('node-rsa');
const mongo = require('mongodb');
const fs = require('node:fs');
const { stdin, stdout } = require('node:process');
const rl = require('node:readline');
const cfg = require('./private/config.json');
const i = rl.createInterface(stdin, stdout);
function query() {
i.question("Select an option: UPLOAD | READ | DUMP | FLUSH | QUIT\n", async a => {
switch (a.toLowerCase()) {
case "upload": {
const r = new rsa()
.importKey(
fs.readFileSync(cfg.public_key_path).toString('utf-8'),
cfg.public_key_format
);
i.question("Enter data.\n", async data => {
var client = new mongo.MongoClient(cfg.mongodb_connection_string);
await client.connect();
const ret = r.encrypt(data, 'buffer');
const collection = client.db(cfg.mongodb_database_name).collection("exception_reports");
await collection.insertOne(
{
"data": ret
}
);
i.write("Uploaded data encrypted with public key.\n");
query();
return;
});
return;
}
case "dump": {
const r = new rsa().importKey(fs.readFileSync(cfg.private_key_path).toString('utf-8'), cfg.private_key_format);
var client = new mongo.MongoClient(cfg.mongodb_connection_string);
await client.connect();
const data = (await client.db(cfg.mongodb_database_name).collection("exception_reports").find({_id: {$exists: true}}).toArray())
.map(x => ` ${x._id}:\n\n${r.decrypt(x.data.buffer, 'buffer').toString('utf-8')}`);
console.log("Data successfully decrypted locally, dumping to file.")
fs.writeFileSync(cfg.dump_file_path, data.join("\n\n------------------------------------------------\n"));
console.log(`Data dumped to ${cfg.dump_file_path}`);
return;
}
case "read": {
const r = new rsa().importKey(fs.readFileSync(cfg.private_key_path).toString('utf-8'), cfg.private_key_format);
var client = new mongo.MongoClient(cfg.mongodb_connection_string);
await client.connect();
const data = (await client.db(cfg.mongodb_database_name).collection("exception_reports").find({ _id: { $exists: true } }).toArray())
.map(x => r.decrypt(x.data.buffer, 'buffer').toString('utf-8'))
.map(x => JSON.parse(x))
.map(x => {
delete x.stack_trace;
return x;
});
console.table(data);
query();
return;
}
case "flush": {
console.log("Flushing reports...");
var client = new mongo.MongoClient(cfg.mongodb_connection_string);
await client.connect();
await client.db(cfg.mongodb_database_name).collection("exception_reports").deleteMany({});
console.log("Flushed all exception reports.");
query();
return;
}
case "quit": {
process.exit(0);
return;
}
default: {
query();
}
}
});
}
query();