-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
303 lines (273 loc) · 12.3 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const path = require("path");
const fs = require("fs");
const Database = require("sqlite-async");
const uuidV4 = require("uuid").v4;
const browsers = require("./browsers");
const { tmpdir } = require("os");
/**
* Get the path to the temp directory of
* the current platform.
*/
function getTempDir() {
return process.env.TMP || process.env.TMPDIR || tmpdir();
}
/**
* Runs the the proper function for the given browser. Some browsers follow the same standards as
* chrome and firefox others have their own syntax.
* Returns an empty array or an array of browser record objects
* @param paths
* @param browserName
* @param historyTimeLength
* @returns {Promise<array>}
*/
async function getBrowserHistory(paths = [], browserName, historyTimeLength) {
switch (browserName) {
case browsers.FIREFOX:
case browsers.SEAMONKEY:
return getMozillaBasedBrowserRecords(paths, browserName, historyTimeLength);
case browsers.CHROME:
case browsers.OPERA:
case browsers.TORCH:
case browsers.VIVALDI:
case browsers.BRAVE:
case browsers.EDGE:
case browsers.AVAST:
return await getChromeBasedBrowserRecords(paths, browserName, historyTimeLength);
case browsers.MAXTHON:
return await getMaxthonBasedBrowserRecords(paths, browserName, historyTimeLength);
default:
return [];
}
}
async function getHistoryFromDb(dbPath, sql, browserName) {
const db = await Database.open(dbPath);
const rows = await db.all(sql);
let browserHistory = rows.map(row => {
return {
title: row.title,
utc_time: row.last_visit_time,
url: row.url,
browser: browserName,
};
});
await db.close();
return browserHistory;
}
function copyDbAndWalFile(dbPath, fileExtension = 'sqlite') {
const newDbPath = path.join(getTempDir(), uuidV4() + `.${fileExtension}`);
const filePaths = {};
filePaths.db = newDbPath;
filePaths.dbWal = `${newDbPath}-wal`;
fs.copyFileSync(dbPath, filePaths.db);
fs.copyFileSync(dbPath, filePaths.dbWal);
return filePaths;
}
async function forceWalFileDump(tmpDbPath) {
const db = await Database.open(tmpDbPath);
// If the browser uses a wal file we need to create a wal file with the same filename as our temp database.
await db.run("PRAGMA wal_checkpoint(FULL)");
await db.close();
}
function deleteTempFiles(paths) {
paths.forEach(path => {
fs.unlinkSync(path);
});
}
async function getChromeBasedBrowserRecords(paths, browserName, historyTimeLength) {
if (!paths || paths.length === 0) {
return [];
}
let newDbPaths = [];
let browserHistory = [];
for (let i = 0; i < paths.length; i++) {
let newDbPath = path.join(getTempDir(), uuidV4() + ".sqlite");
newDbPaths.push(newDbPath);
let sql = `SELECT title, datetime(last_visit_time/1000000 + (strftime('%s', '1601-01-01')),'unixepoch') last_visit_time, url from urls WHERE DATETIME (last_visit_time/1000000 + (strftime('%s', '1601-01-01')), 'unixepoch') >= DATETIME('now', '-${historyTimeLength} minutes') group by title, last_visit_time order by last_visit_time`;
//Assuming the sqlite file is locked so lets make a copy of it
fs.copyFileSync(paths[i], newDbPath);
browserHistory.push(await getHistoryFromDb(newDbPath, sql, browserName));
}
deleteTempFiles(newDbPaths);
return browserHistory;
}
async function getMozillaBasedBrowserRecords(paths, browserName, historyTimeLength) {
if (!paths || paths.length === 0) {
return [];
}
let newDbPaths = [];
let browserHistory = [];
for (let i = 0; i < paths.length; i++) {
const tmpFilePaths = copyDbAndWalFile(paths[i]);
console.log(tmpFilePaths)
newDbPaths.push(tmpFilePaths.db);
let sql = `SELECT title, datetime(last_visit_date/1000000,'unixepoch') last_visit_time, url from moz_places WHERE DATETIME (last_visit_date/1000000, 'unixepoch') >= DATETIME('now', '-${historyTimeLength} minutes') group by title, last_visit_time order by last_visit_time`;
await forceWalFileDump(tmpFilePaths.db);
browserHistory.push(await getHistoryFromDb(tmpFilePaths.db, sql, browserName));
}
deleteTempFiles(newDbPaths);
return browserHistory;
}
async function getMaxthonBasedBrowserRecords(paths, browserName, historyTimeLength) {
let browserHistory = [];
for (let i = 0; i < paths.length; i++) {
let sql = `SELECT zlastvisittime last_visit_time, zhost host, ztitle title, zurl url FROM zmxhistoryentry WHERE Datetime (zlastvisittime + 978307200, 'unixepoch') >= Datetime('now', '-${historyTimeLength} minutes')`;
browserHistory.push(await getHistoryFromDb(paths[i], sql, browserName));
}
return browserHistory;
}
/**
* Gets Firefox history
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getFirefoxHistory(historyTimeLength = 5) {
browsers.browserDbLocations.firefox = browsers.findPaths(browsers.defaultPaths.firefox, browsers.FIREFOX);
return getBrowserHistory(browsers.browserDbLocations.firefox, browsers.FIREFOX, historyTimeLength).then(records => {
return records;
});
}
/**
* Gets Seamonkey History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
function getSeaMonkeyHistory(historyTimeLength = 5) {
browsers.browserDbLocations.seamonkey = browsers.findPaths(browsers.defaultPaths.seamonkey, browsers.SEAMONKEY);
return getBrowserHistory(browsers.browserDbLocations.seamonkey, browsers.SEAMONKEY, historyTimeLength).then(records => {
return records;
});
}
/**
* Gets Chrome History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getChromeHistory(historyTimeLength = 5) {
browsers.browserDbLocations.chrome = browsers.findPaths(browsers.defaultPaths.chrome, browsers.CHROME);
return getBrowserHistory(browsers.browserDbLocations.chrome, browsers.CHROME, historyTimeLength).then(records => {
return records;
});
}
/**
* Get Opera History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getOperaHistory(historyTimeLength = 5) {
browsers.browserDbLocations.opera = browsers.findPaths(browsers.defaultPaths.opera, browsers.OPERA);
return getBrowserHistory(browsers.browserDbLocations.opera, browsers.OPERA, historyTimeLength).then(records => {
return records;
});
}
/**
* Get Torch History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getTorchHistory(historyTimeLength = 5) {
browsers.browserDbLocations.torch = browsers.findPaths(browsers.defaultPaths.torch, browsers.TORCH);
return getBrowserHistory(browsers.browserDbLocations.torch, browsers.TORCH, historyTimeLength).then(records => {
return records;
});
}
/**
* Get Brave History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getBraveHistory(historyTimeLength = 5) {
browsers.browserDbLocations.brave = browsers.findPaths(browsers.defaultPaths.brave, browsers.BRAVE);
return getBrowserHistory(browsers.browserDbLocations.brave, browsers.BRAVE, historyTimeLength).then(records => {
return records;
});
}
/**
* Get Maxthon History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getMaxthonHistory(historyTimeLength = 5) {
browsers.browserDbLocations.maxthon = browsers.findPaths(browsers.defaultPaths.maxthon, browsers.MAXTHON);
return getBrowserHistory(browsers.browserDbLocations.maxthon, browsers.MAXTHON, historyTimeLength).then(records => {
return records;
});
}
/**
* Get Vivaldi History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getVivaldiHistory(historyTimeLength = 5) {
browsers.browserDbLocations.vivaldi = browsers.findPaths(browsers.defaultPaths.vivaldi, browsers.VIVALDI);
return getBrowserHistory(browsers.browserDbLocations.vivaldi, browsers.VIVALDI, historyTimeLength).then(records => {
return records;
});
}
/**
* Get AVAST Browser History
* @param historyTimeLength
* @return {Promise<Array>}
*/
async function getAvastHistory(historyTimeLength = 5) {
browsers.browserDbLocations.avast = browsers.findPaths(browsers.defaultPaths.avast, browsers.AVAST);
return getBrowserHistory(browsers.browserDbLocations.avast, browsers.AVAST, historyTimeLength).then(records => {
return records;
});
}
/**
* Get Microsoft Edge History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getMicrosoftEdge(historyTimeLength = 5) {
browsers.browserDbLocations.edge = browsers.findPaths(browsers.defaultPaths.edge, browsers.EDGE);
return getBrowserHistory(browsers.browserDbLocations.edge, browsers.EDGE, historyTimeLength).then(records => {
return records;
});
}
/**
* Gets the history for the Specified browsers and time in minutes.
* Returns an array of browser records.
* @param historyTimeLength | Integer
* @returns {Promise<array>}
*/
async function getAllHistory(historyTimeLength = 5) {
let allBrowserRecords = [];
browsers.browserDbLocations.firefox = browsers.findPaths(browsers.defaultPaths.firefox, browsers.FIREFOX);
browsers.browserDbLocations.chrome = browsers.findPaths(browsers.defaultPaths.chrome, browsers.CHROME);
browsers.browserDbLocations.seamonkey = browsers.findPaths(browsers.defaultPaths.seamonkey, browsers.SEAMONKEY);
browsers.browserDbLocations.opera = browsers.findPaths(browsers.defaultPaths.opera, browsers.OPERA);
browsers.browserDbLocations.torch = browsers.findPaths(browsers.defaultPaths.torch, browsers.TORCH);
browsers.browserDbLocations.brave = browsers.findPaths(browsers.defaultPaths.brave, browsers.BRAVE);
browsers.browserDbLocations.seamonkey = browsers.findPaths(browsers.defaultPaths.seamonkey, browsers.SEAMONKEY);
browsers.browserDbLocations.maxthon = browsers.findPaths(browsers.defaultPaths.maxthon, browsers.MAXTHON);
browsers.browserDbLocations.vivaldi = browsers.findPaths(browsers.defaultPaths.vivaldi, browsers.VIVALDI);
browsers.browserDbLocations.edge = browsers.findPaths(browsers.defaultPaths.edge, browsers.EDGE);
browsers.browserDbLocations.avast = browsers.findPaths(browsers.defaultPaths.avast, browsers.AVAST);
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.firefox, browsers.FIREFOX, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.seamonkey, browsers.SEAMONKEY, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.chrome, browsers.CHROME, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.opera, browsers.OPERA, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.torch, browsers.TORCH, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.brave, browsers.BRAVE, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.vivaldi, browsers.VIVALDI, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.seamonkey, browsers.SEAMONKEY, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.maxthon, browsers.MAXTHON, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.edge, browsers.EDGE, historyTimeLength));
allBrowserRecords = allBrowserRecords.concat(await getBrowserHistory(browsers.browserDbLocations.avast, browsers.EDGE, historyTimeLength));
//No Path because this is handled by the dll
return allBrowserRecords;
}
module.exports = {
getAllHistory,
getFirefoxHistory,
getSeaMonkeyHistory,
getChromeHistory,
getOperaHistory,
getTorchHistory,
getBraveHistory,
getMaxthonHistory,
getVivaldiHistory,
getMicrosoftEdge,
getAvastHistory
};