-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsDB.js
202 lines (141 loc) · 5.96 KB
/
dnsDB.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
module.exports = dnsDB;
var dns = require('dns');
var aes256 = require('aes256');
var splitRetain = require('split-retain');
var sqlite3 = require('sqlite3').verbose();
var fs = require('fs');
splitChar = "###";
var output = [];
function dnsDB (entry, key, writeMode, dnsServer, verbose, callback) {
// start the function
// set up class properties, and register getters and setters
this.entry = entry;
this.key = key;
this.writeMode = writeMode;
//set up debug array
var debug = [ ];
this.setEntry = function(s) {
entry = s;
}
this.getEntry = function() {
return entry;
}
this.setKey = function(s) {
key = s;
}
this.getKey = function() {
return key;
}
//actually set the DNS server
dns.setServers([dnsServer]);
var example = dns.resolveTxt(entry, function (err, entries, family) {
//we are inside our first callback, waiting for TXT records to come back!
//once that's done.....
var encryptedRecordsInOrder = Array();
if(entries){
entries.forEach(function(entry) {
var rawString = entry[0];
var parts = rawString.split(splitChar);
var sortOrder = parts[0];
encryptedRecordsInOrder[sortOrder] = parts[1];
});
} else{
console.log('\x1b[31m%s\x1b[0m', "Entry does not exist");
return;
}
var encrypted = encryptedRecordsInOrder.join('');
if(encrypted){
var decrypted = aes256.decrypt(key, encrypted);
//debug
debug['txt'] = entries;
debug['entries'] = encryptedRecordsInOrder;
debug['raw'] = encrypted;
debug['key'] = key;
debug['decrypted'] = decrypted;
} else {
console.log('\x1b[31m%s\x1b[0m', "Malformed record");
return;
}
servers = dns.getServers();
debug['decrypted'] = decrypted;
debug['record'] = entry;
debug['dnsserver'] = servers[0];
debug['writeMode'] = writeMode;
if( writeMode == 'true' ){
try {
if (fs.existsSync("./storage.db")) {
fs.unlinkSync("./storage.db");// clear old DBs
}
} catch(err) {
console.error(err);
}
//if write mode is on, use the slower method but we need this to do a sqlite dump
var db = new sqlite3.Database('storage.db', (err) => {
if (err) {
console.error(err.message);
return;
}
});
} else {
//we will use sqlite3 memory mode if read only, to make this faster
var db = new sqlite3.Database(':memory:', (err) => {
if (err) {
console.error(err.message);
return;
}
});
}
db.serialize(function() { //actually work with the DB
var statements = splitRetain(decrypted, ';') //split statements
//instantiate the db by executing decrypted statements from TXT records
var n = 0;
debug['init'] = [];
debug['statements'] = [];
statements.forEach( statement => {
db.exec(statement);
debug['init'][n] = statement;
debug['statements'][n] = statement.replace(/(\r\n|\n|\r)/gm,"");
n++;
});
//nest another serialized set of instructions to ensure API user's instructions happen after the DB is initialized. This makes dnsDB seamless
db.serialize(function() {
//next another serial function with an innocous exec command to execute after
db.serialize(function() {
db.exec("", function ( ){
//this ONLY happens once everything is done
if(writeMode == 'true'){
sqlFileToStatementArray('storage.db', function ( output ) {
//finally we are done
//close db and delete storage file, and save write array
db.close;
});
} else {
//no write mode, therefore go on what you would do with memory
db.close;
}
});
});
//finally do the callback with debug statements
callback(db, debug);
});
});
});
}
function sqlFileToStatementArray( filename, callback ) {
//write db to plaintext for temp storage
const { exec } = require('child_process');
exec('sqlite3 ' + filename + ' .dump > lastPlainText.sql', (error, stdout, stderr) => { //ti
fs.readFile('./lastPlainText.sql', 'utf8', function(err, data) {
if (err) throw err;
var encrypted_init = aes256.encrypt(key, data);
var encrypted_storage = encrypted_init.match(/.{1,200}/g);
var sort = 0;
encrypted_storage.forEach( blob => {
//read out to console
console.log(sort + splitChar + blob);
sort++;
});
callback();
});
});
}