forked from MyOutDeskLLC/node-browser-history
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
408 lines (375 loc) · 16.5 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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.ARC:
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);
case browsers.SAFARI:
return await getSafariBasedBrowserRecords(paths, browserName, historyTimeLength);
default:
return [];
}
}
async function getHistoryFromDb(dbPath, sql, browserName) {
let db;
try {
db = await Database.open(dbPath);
const rows = await db.all(sql);
let uniqueUrls = new Set();
let browserHistory = rows.reduce((acc, row) => {
if (!uniqueUrls.has(row.url)) {
uniqueUrls.add(row.url);
acc.push({
title: row.title,
utc_time: row.last_visit_time,
url: row.url,
browser: browserName,
});
}
return acc;
}, []);
return browserHistory;
} catch (error) {
console.error(`Error fetching history from database: ${error.message}`);
return [];
} finally {
if (db) {
await db.close();
}
}
}
function copyDbAndWalFile(dbPath, fileExtension = 'sqlite') {
const newDbPath = path.join(getTempDir(), uuidV4() + `.${fileExtension}`);
const filePaths = {};
filePaths.db = newDbPath;
filePaths.dbWal = `${newDbPath}-wal`;
try {
// Check if source files exist
if (!fs.existsSync(dbPath)) {
throw new Error(`Source database file does not exist: ${dbPath}`);
}
if (!fs.existsSync(dbPath + '-wal')) {
throw new Error(`Source WAL file does not exist: ${dbPath}-wal`);
}
// Attempt to copy files
fs.copyFileSync(dbPath, filePaths.db);
fs.copyFileSync(dbPath + '-wal', filePaths.dbWal);
console.log('Files copied successfully.');
} catch (error) {
console.error(`Error during file copy: ${error.message}`);
// Optionally, you can handle cleanup or additional logging here
}
return filePaths;
}
async function forceWalFileDump(tmpDbPath) {
let db;
try {
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)");
} catch (error) {
console.error(`Error forcing WAL file dump: ${error.message}`);
} finally {
if (db) {
await db.close();
}
}
}
function deleteTempFiles(paths) {
paths.forEach(filePath => {
try {
fs.unlinkSync(filePath);
} catch (error) {
console.error(`Error deleting temporary file ${filePath}: ${error.message}`);
}
});
}
async function getChromeBasedBrowserRecords(paths, browserName, historyTimeLength) {
if (!paths || paths.length === 0) {
return [];
}
let newDbPaths = [];
let browserHistory = [];
for (let i = 0; i < paths.length; i++) {
try {
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));
} catch (error) {
console.error(`Error processing Chrome-based browser record: ${error.message}`);
}
}
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++) {
try {
const tmpFilePaths = copyDbAndWalFile(paths[i]);
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));
} catch (error) {
console.error(`Error processing Mozilla-based browser record: ${error.message}`);
}
}
deleteTempFiles(newDbPaths);
return browserHistory;
}
async function getSafariBasedBrowserRecords(paths, browserName, historyTimeLength) {
if (!paths || paths.length === 0) {
return [];
}
let newDbPaths = [];
let browserHistory = [];
for (let i = 0; i < paths.length; i++) {
try {
const tmpFilePaths = copyDbAndWalFile(paths[i]);
newDbPaths.push(tmpFilePaths.db);
let sql = `SELECT i.id, i.url, v.title, DATETIME(v.visit_time + 978307200, 'unixepoch') as last_visit_time FROM history_items i INNER JOIN history_visits v ON i.id = v.history_item WHERE DATETIME(v.visit_time + 978307200, 'unixepoch') >= DATETIME('now', '-${historyTimeLength} minutes')`;
await forceWalFileDump(tmpFilePaths.db);
browserHistory.push(await getHistoryFromDb(tmpFilePaths.db, sql, browserName));
} catch (error) {
console.error(`Error processing Safari-based browser record: ${error.message}`);
}
}
deleteTempFiles(newDbPaths);
return browserHistory;
}
async function getMaxthonBasedBrowserRecords(paths, browserName, historyTimeLength) {
let browserHistory = [];
for (let i = 0; i < paths.length; i++) {
try {
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));
} catch (error) {
console.error(`Error processing Maxthon-based browser record: ${error.message}`);
}
}
return browserHistory;
}
/**
* Gets Arc history
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getArcHistory(historyTimeLength = 5) {
browsers.browserDbLocations.arc = browsers.findPaths(browsers.defaultPaths.arc, browsers.ARC);
return getBrowserHistory(browsers.browserDbLocations.arc, browsers.ARC, historyTimeLength).then(records => {
return records;
});
}
/**
* 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 Safari History
* @param historyTimeLength time is in minutes
* @returns {Promise<array>}
*/
async function getSafariHistory(historyTimeLength = 5) {
browsers.browserDbLocations.safari = browsers.findPaths(browsers.defaultPaths.safari, browsers.SAFARI);
return getBrowserHistory(browsers.browserDbLocations.safari, browsers.SAFARI, 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.arc = browsers.findPaths(browsers.defaultPaths.arc, browsers.ARC);
browsers.browserDbLocations.torch = browsers.findPaths(browsers.defaultPaths.torch, browsers.TORCH);
browsers.browserDbLocations.brave = browsers.findPaths(browsers.defaultPaths.brave, browsers.BRAVE);
browsers.browserDbLocations.safari = browsers.findPaths(browsers.defaultPaths.safari, browsers.SAFARI);
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.arc, browsers.ARC, 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.safari, browsers.SAFARI, 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,
getArcHistory,
getTorchHistory,
getBraveHistory,
getSafariHistory,
getMaxthonHistory,
getVivaldiHistory,
getMicrosoftEdge,
getAvastHistory
};