-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfileCookieStore.js
73 lines (60 loc) · 2.13 KB
/
fileCookieStore.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
var util = require('util'),
fs = require('fs'),
crypto = require('crypto'),
tough = require('tough-cookie'),
Store = tough.MemoryCookieStore;
function FileCookieStore(filePath, option) {
Store.call(this);
this.idx = {}; // idx is memory cache
this.filePath = filePath;
this.option = option || {};
this.option.encrypt = !(option.encrypt === false);
if (this.option.encrypt) {
this.option.algorithm = this.option.algorithm || 'aes-256-ctr';
this.option.password = this.option.password || 'cookie-store';
}
var self = this;
loadFromFile(this.filePath, this.option, function (dataJson) {
if (dataJson)
self.idx = dataJson;
})
}
util.inherits(FileCookieStore, Store);
module.exports = FileCookieStore;
FileCookieStore.prototype.flush = function () {
saveToFile(this.filePath, this.idx, this.option);
};
function encrypt(text, option){
var cipher = crypto.createCipher(option.algorithm, option.password);
var crypted = cipher.update(text, 'utf8', 'hex');
crypted += cipher.final('hex');
return crypted;
}
function saveToFile(filePath, data, option, cb) {
var dataJson = JSON.stringify(data);
if (option.encrypt) {
dataJson = encrypt(dataJson, option);
}
fs.writeFileSync(filePath, dataJson);
}
function decrypt(text, option){
var decipher = crypto.createDecipher(option.algorithm, option.password)
var dec = decipher.update(text, 'hex', 'utf8')
dec += decipher.final('utf8');
return dec;
}
function loadFromFile(filePath, option, cb) {
var fileData = fs.readFileSync(filePath, {encoding: 'utf8', flag: 'a+'});
if (option.encrypt && fileData) {
var decrypted = decrypt(fileData, option)
}
var dataJson = decrypted ? JSON.parse(decrypted) : null;
for (var domainName in dataJson) {
for (var pathName in dataJson[domainName]) {
for (var cookieName in dataJson[domainName][pathName]) {
dataJson[domainName][pathName][cookieName] = tough.fromJSON(JSON.stringify(dataJson[domainName][pathName][cookieName]));
}
}
}
cb(dataJson);
}