Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 499c48f

Browse files
committed
[cookie.chrome] add windows support.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent af2caa2 commit 499c48f

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

plugins/cookie.chrome.js

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var path = require('path');
2+
13
var log = require('../log');
24
var Plugin = require('../plugin');
35
var session = require('../session');
@@ -8,11 +10,17 @@ var session = require('../session');
810
//
911
var plugin = new Plugin(13, 'cookie.chrome', '2017.12.23',
1012
'Plugin to reuse Chrome\'s leetcode cookie.',
11-
['keytar:darwin', 'sqlite3']);
13+
['ffi:win32', 'keytar:darwin', 'ref:win32', 'ref-struct:win32', 'sqlite3']);
1214

1315
plugin.help = function() {
14-
if (process.platform === 'linux') {
15-
log.info('To complete the install: sudo apt install libsecret-tools');
16+
switch (process.platform) {
17+
case 'darwin':
18+
break;
19+
case 'linux':
20+
log.info('To complete the install: sudo apt install libsecret-tools');
21+
break;
22+
case 'win32':
23+
break;
1624
}
1725
};
1826

@@ -39,7 +47,8 @@ var ChromeLinux = {
3947
};
4048

4149
var ChromeWindows = {
42-
// TODO
50+
db: path.resolve(process.env.APPDATA || '', '../Local/Google/Chrome/User Data/Default/Cookies'),
51+
getPassword: function(cb) { cb(); }
4352
};
4453

4554
Object.setPrototypeOf(ChromeMAC, Chrome);
@@ -55,6 +64,32 @@ Chrome.getInstance = function() {
5564
};
5665
var my = Chrome.getInstance();
5766

67+
ChromeWindows.decodeCookie = function(cookie, cb) {
68+
var ref = require('ref');
69+
var ffi = require('ffi');
70+
var Struct = require('ref-struct');
71+
72+
var DATA_BLOB = Struct({
73+
cbData: ref.types.uint32,
74+
pbData: ref.refType(ref.types.byte)
75+
});
76+
var PDATA_BLOB = new ref.refType(DATA_BLOB);
77+
var Crypto = new ffi.Library('Crypt32', {
78+
'CryptUnprotectData': ['bool', [PDATA_BLOB, 'string', 'string', 'void *', 'string', 'int', PDATA_BLOB]]
79+
});
80+
81+
var inBlob = new DATA_BLOB();
82+
inBlob.pbData = cookie;
83+
inBlob.cbData = cookie.length;
84+
var outBlob = ref.alloc(DATA_BLOB);
85+
86+
Crypto.CryptUnprotectData(inBlob.ref(), null, null, null, null, 0, outBlob);
87+
var outDeref = outBlob.deref();
88+
var buf = ref.reinterpret(outDeref.pbData, outDeref.cbData, 0);
89+
90+
return cb(null, buf.toString('utf8'));
91+
};
92+
5893
Chrome.decodeCookie = function(cookie, cb) {
5994
var crypto = require('crypto');
6095
crypto.pbkdf2(my.password, 'saltysalt', my.iterations, 16, 'sha1', function(e, key) {
@@ -114,15 +149,15 @@ Chrome.getCookies = function(cb) {
114149
plugin.signin = function(user, cb) {
115150
log.debug('running cookie.chrome.signin');
116151
log.debug('try to copy leetcode cookies from chrome ...');
117-
my.getCookies(function(e, cookie) {
152+
my.getCookies(function(e, cookies) {
118153
if (e) {
119154
log.error('failed to copy cookies: ' + e);
120155
return plugin.next.signin(user, cb);
121156
}
122157

123158
log.debug('Successfully copied leetcode cookies!');
124-
user.sessionId = cookie.LEETCODE_SESSION;
125-
user.sessionCSRF = cookie.csrftoken;
159+
user.sessionId = cookies.LEETCODE_SESSION;
160+
user.sessionCSRF = cookies.csrftoken;
126161
session.saveUser(user);
127162
return cb(null, user);
128163
});

0 commit comments

Comments
 (0)