1
+ var path = require ( 'path' ) ;
2
+
1
3
var log = require ( '../log' ) ;
2
4
var Plugin = require ( '../plugin' ) ;
3
5
var session = require ( '../session' ) ;
@@ -8,11 +10,17 @@ var session = require('../session');
8
10
//
9
11
var plugin = new Plugin ( 13 , 'cookie.chrome' , '2017.12.23' ,
10
12
'Plugin to reuse Chrome\'s leetcode cookie.' ,
11
- [ 'keytar:darwin' , 'sqlite3' ] ) ;
13
+ [ 'ffi:win32' , ' keytar:darwin' , 'ref:win32' , 'ref-struct:win32 ', 'sqlite3' ] ) ;
12
14
13
15
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 ;
16
24
}
17
25
} ;
18
26
@@ -39,7 +47,8 @@ var ChromeLinux = {
39
47
} ;
40
48
41
49
var ChromeWindows = {
42
- // TODO
50
+ db : path . resolve ( process . env . APPDATA || '' , '../Local/Google/Chrome/User Data/Default/Cookies' ) ,
51
+ getPassword : function ( cb ) { cb ( ) ; }
43
52
} ;
44
53
45
54
Object . setPrototypeOf ( ChromeMAC , Chrome ) ;
@@ -55,6 +64,32 @@ Chrome.getInstance = function() {
55
64
} ;
56
65
var my = Chrome . getInstance ( ) ;
57
66
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
+
58
93
Chrome . decodeCookie = function ( cookie , cb ) {
59
94
var crypto = require ( 'crypto' ) ;
60
95
crypto . pbkdf2 ( my . password , 'saltysalt' , my . iterations , 16 , 'sha1' , function ( e , key ) {
@@ -114,15 +149,15 @@ Chrome.getCookies = function(cb) {
114
149
plugin . signin = function ( user , cb ) {
115
150
log . debug ( 'running cookie.chrome.signin' ) ;
116
151
log . debug ( 'try to copy leetcode cookies from chrome ...' ) ;
117
- my . getCookies ( function ( e , cookie ) {
152
+ my . getCookies ( function ( e , cookies ) {
118
153
if ( e ) {
119
154
log . error ( 'failed to copy cookies: ' + e ) ;
120
155
return plugin . next . signin ( user , cb ) ;
121
156
}
122
157
123
158
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 ;
126
161
session . saveUser ( user ) ;
127
162
return cb ( null , user ) ;
128
163
} ) ;
0 commit comments