@mherod/get-cookie 2.0.0-rc.1 → 2.0.0-rc.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +413 -156
- package/package-lock.json +12295 -0
- package/package.json +12 -8
- package/src/CookieRow.ts +2 -1
- package/src/CookieSpec.ts +4 -2
- package/src/CookieStore.ts +11 -0
- package/src/ExportedCookie.ts +2 -1
- package/src/FetchResponse.ts +2 -0
- package/src/IsCookieRow.ts +1 -1
- package/src/IsExportedCookie.ts +1 -1
- package/src/SpecialCases.ts +1 -1
- package/src/StringToRegex.ts +6 -0
- package/src/argv.ts +4 -0
- package/src/browsers/ChromeCookieQueryStrategy.ts +106 -24
- package/src/browsers/CompositeCookieQueryStrategy.ts +32 -8
- package/src/browsers/CookieQueryStrategy.ts +3 -1
- package/src/browsers/CookieStoreQueryStrategy.ts +94 -0
- package/src/browsers/FirefoxCookieQueryStrategy.ts +5 -3
- package/src/browsers/SafariCookieQueryStrategy.ts +3 -1
- package/src/cli.ts +101 -93
- package/src/comboQueryCookieSpec.ts +21 -0
- package/src/cookieSpecsFromUrl.ts +26 -0
- package/src/doSqliteQuery1.ts +4 -8
- package/src/doSqliteQuery1Params.ts +8 -0
- package/src/fetchWithCookies.ts +79 -34
- package/src/findAllFiles.ts +5 -9
- package/src/getChromeCookie.ts +19 -0
- package/src/getCookie.ts +19 -0
- package/src/getFirefoxCookie.ts +19 -0
- package/src/getGroupedRenderedCookies.ts +12 -25
- package/src/getMergedRenderedCookies.ts +14 -0
- package/src/index.ts +13 -52
- package/src/isValidJwt.ts +2 -2
- package/src/queryCookies.ts +16 -12
- package/src/resultsRendered.ts +7 -4
- package/src/unpackHeaders.ts +7 -4
- package/src/utils.ts +0 -25
- package/dist/cli.js +0 -3
- package/dist/cli.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/module.js +0 -586
- package/dist/module.js.map +0 -1
- package/dist/types.d.ts +0 -35
- package/dist/types.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mherod/get-cookie",
|
|
3
|
-
"version": "2.0.0-rc.
|
|
3
|
+
"version": "2.0.0-rc.11",
|
|
4
4
|
"description": "Node.js module for querying a local user's Chrome cookie",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"bin": "dist/cli.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
|
-
"clean": "git clean -
|
|
14
|
+
"clean": "git clean -fdX && rm -rf node_modules dist && yarn install",
|
|
15
15
|
"build": "parcel build",
|
|
16
16
|
"prepare": "npm run build && npm run prettier",
|
|
17
17
|
"test": "jest",
|
|
@@ -25,16 +25,15 @@
|
|
|
25
25
|
},
|
|
26
26
|
"targets": {
|
|
27
27
|
"main": {},
|
|
28
|
-
"types": {},
|
|
29
28
|
"library": {
|
|
30
|
-
"source":
|
|
31
|
-
"src/index.ts"
|
|
32
|
-
],
|
|
29
|
+
"source": "src/index.ts",
|
|
33
30
|
"distDir": "dist",
|
|
34
31
|
"isLibrary": true,
|
|
35
32
|
"engines": {
|
|
36
33
|
"node": "16"
|
|
37
|
-
}
|
|
34
|
+
},
|
|
35
|
+
"sourceMap": true,
|
|
36
|
+
"includeNodeModules": false
|
|
38
37
|
},
|
|
39
38
|
"cli": {
|
|
40
39
|
"source": "src/cli.ts",
|
|
@@ -43,6 +42,7 @@
|
|
|
43
42
|
"engines": {
|
|
44
43
|
"node": "16"
|
|
45
44
|
},
|
|
45
|
+
"sourceMap": false,
|
|
46
46
|
"includeNodeModules": [
|
|
47
47
|
"colorette"
|
|
48
48
|
]
|
|
@@ -66,7 +66,10 @@
|
|
|
66
66
|
"lodash": "^4.17.21",
|
|
67
67
|
"lru-cache": "^7.14.0",
|
|
68
68
|
"minimist": "^1.2.6",
|
|
69
|
-
"sqlite3": "^5.1.1"
|
|
69
|
+
"sqlite3": "^5.1.1",
|
|
70
|
+
"tough-cookie": "^4.1.2",
|
|
71
|
+
"tough-cookie-file-store": "^2.0.3",
|
|
72
|
+
"user-agents": "^1.0.1165"
|
|
70
73
|
},
|
|
71
74
|
"devDependencies": {
|
|
72
75
|
"@parcel/packager-ts": "^2.7.0",
|
|
@@ -75,6 +78,7 @@
|
|
|
75
78
|
"@types/lodash": "^4.14.186",
|
|
76
79
|
"@types/minimist": "^1.2.2",
|
|
77
80
|
"@types/node": "^18.8.2",
|
|
81
|
+
"@types/tough-cookie": "^4.0.2",
|
|
78
82
|
"@types/user-agents": "^1.0.2",
|
|
79
83
|
"jest": "^29.0.3",
|
|
80
84
|
"parcel": "^2.7.0",
|
package/src/CookieRow.ts
CHANGED
package/src/CookieSpec.ts
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CookieJar } from "tough-cookie";
|
|
2
|
+
import { env } from "./global";
|
|
3
|
+
|
|
4
|
+
const { FileCookieStore } = require("tough-cookie-file-store");
|
|
5
|
+
|
|
6
|
+
export const cookieStore = new FileCookieStore(
|
|
7
|
+
`${env["HOME"]}/cookie-star.json`
|
|
8
|
+
);
|
|
9
|
+
// export const cookieStore = new MemoryCookieStore();
|
|
10
|
+
|
|
11
|
+
export const cookieJar = new CookieJar(cookieStore);
|
package/src/ExportedCookie.ts
CHANGED
package/src/FetchResponse.ts
CHANGED
package/src/IsCookieRow.ts
CHANGED
package/src/IsExportedCookie.ts
CHANGED
package/src/SpecialCases.ts
CHANGED
package/src/argv.ts
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
2
2
|
import { execSimple } from "../utils";
|
|
3
3
|
import { env, HOME } from "../global";
|
|
4
|
-
import { existsSync } from "fs";
|
|
4
|
+
import fs, { existsSync } from "fs";
|
|
5
5
|
import { findAllFiles } from "../findAllFiles";
|
|
6
6
|
import * as crypto from "crypto";
|
|
7
7
|
import * as path from "path";
|
|
8
|
-
import { doSqliteQuery1 } from "../doSqliteQuery1";
|
|
9
8
|
import { merge } from "lodash";
|
|
10
9
|
import { isCookieRow } from "../IsCookieRow";
|
|
11
10
|
import { isExportedCookie } from "../IsExportedCookie";
|
|
12
|
-
import
|
|
13
|
-
import
|
|
11
|
+
import CookieRow from "../CookieRow";
|
|
12
|
+
import ExportedCookie from "../ExportedCookie";
|
|
13
|
+
import { stringToRegex } from "../StringToRegex";
|
|
14
|
+
import { parsedArgs } from "../argv";
|
|
15
|
+
import * as sqlite3 from "sqlite3";
|
|
16
|
+
import { DoSqliteQuery1Params } from "../doSqliteQuery1Params";
|
|
14
17
|
|
|
15
18
|
export default class ChromeCookieQueryStrategy implements CookieQueryStrategy {
|
|
19
|
+
browserName = "Chrome";
|
|
20
|
+
|
|
16
21
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
17
22
|
if (process.platform !== "darwin") {
|
|
18
23
|
throw new Error("This only works on macOS");
|
|
@@ -40,7 +45,7 @@ async function getPromise1(
|
|
|
40
45
|
file: file,
|
|
41
46
|
});
|
|
42
47
|
} catch (e) {
|
|
43
|
-
if (
|
|
48
|
+
if (parsedArgs.verbose) {
|
|
44
49
|
console.log("Error getting encrypted cookie", e);
|
|
45
50
|
}
|
|
46
51
|
return [];
|
|
@@ -56,10 +61,10 @@ async function getPromise(name: string, domain: string): Promise<CookieRow[]> {
|
|
|
56
61
|
const promises: Promise<CookieRow[]>[] = files.map((file) =>
|
|
57
62
|
getPromise1(name, domain, file)
|
|
58
63
|
);
|
|
59
|
-
const results1:
|
|
64
|
+
const results1: CookieRow[][] = await Promise.all(promises);
|
|
60
65
|
return results1.flat().filter(isCookieRow);
|
|
61
66
|
} catch (error) {
|
|
62
|
-
if (
|
|
67
|
+
if (parsedArgs.verbose) {
|
|
63
68
|
console.log("error", error);
|
|
64
69
|
}
|
|
65
70
|
return [];
|
|
@@ -71,7 +76,7 @@ async function decryptValue(password: string, encryptedValue: Buffer) {
|
|
|
71
76
|
try {
|
|
72
77
|
d = await decrypt(password, encryptedValue);
|
|
73
78
|
} catch (e) {
|
|
74
|
-
if (
|
|
79
|
+
if (parsedArgs.verbose) {
|
|
75
80
|
console.log("Error decrypting cookie", e);
|
|
76
81
|
}
|
|
77
82
|
d = null;
|
|
@@ -99,17 +104,28 @@ Promise<ExportedCookie[]> {
|
|
|
99
104
|
const decryptedValue = await decryptValue(password, encryptedValue);
|
|
100
105
|
const meta = {};
|
|
101
106
|
merge(meta, cookieRow.meta ?? {});
|
|
102
|
-
|
|
107
|
+
const exportedCookie: ExportedCookie = {
|
|
103
108
|
domain: cookieRow.domain,
|
|
104
109
|
name: cookieRow.name,
|
|
105
110
|
value: decryptedValue,
|
|
106
111
|
meta: meta,
|
|
107
112
|
};
|
|
113
|
+
const expiry = cookieRow.expiry;
|
|
114
|
+
const mergeExpiry =
|
|
115
|
+
expiry != null && expiry > 0
|
|
116
|
+
? {
|
|
117
|
+
expiry: new Date(expiry),
|
|
118
|
+
}
|
|
119
|
+
: {
|
|
120
|
+
expiry: "Infinity",
|
|
121
|
+
};
|
|
122
|
+
merge(exportedCookie, mergeExpiry);
|
|
123
|
+
return exportedCookie;
|
|
108
124
|
});
|
|
109
125
|
const results: ExportedCookie[] = (await Promise.all(decrypted)).filter(
|
|
110
126
|
isExportedCookie
|
|
111
127
|
);
|
|
112
|
-
if (
|
|
128
|
+
if (parsedArgs.verbose) {
|
|
113
129
|
console.log("results", results);
|
|
114
130
|
}
|
|
115
131
|
return results;
|
|
@@ -136,39 +152,61 @@ async function getEncryptedChromeCookie({
|
|
|
136
152
|
if (!existsSync(file)) {
|
|
137
153
|
throw new Error(`File ${file} does not exist`);
|
|
138
154
|
}
|
|
139
|
-
if (
|
|
155
|
+
if (parsedArgs.verbose) {
|
|
140
156
|
const s = file.split("/").slice(-3).join("/");
|
|
141
157
|
console.log(`Trying Chrome (at ${s}) cookie ${name} for domain ${domain}`);
|
|
142
158
|
}
|
|
143
159
|
let sql;
|
|
144
160
|
//language=SQL
|
|
145
|
-
sql = "SELECT
|
|
161
|
+
sql = "SELECT * FROM cookies";
|
|
162
|
+
// sql = "SELECT encrypted_value, name, host_key FROM cookies";
|
|
163
|
+
|
|
146
164
|
const wildcardRegexp = /^([*%])$/i;
|
|
147
165
|
const specifiedName = name.match(wildcardRegexp) == null;
|
|
148
166
|
const specifiedDomain = domain.match(wildcardRegexp) == null;
|
|
149
|
-
|
|
167
|
+
const wildcardDomain = domain.match(/[%*]/) != null;
|
|
168
|
+
const queryDomain = specifiedDomain && !wildcardDomain;
|
|
169
|
+
// if we have a wildcard domain, we need to use a regexp
|
|
170
|
+
if (specifiedName || queryDomain) {
|
|
150
171
|
sql += ` WHERE `;
|
|
151
172
|
if (specifiedName) {
|
|
152
173
|
sql += `name = '${name}'`;
|
|
153
|
-
if (
|
|
174
|
+
if (queryDomain) {
|
|
154
175
|
sql += ` AND `;
|
|
155
176
|
}
|
|
156
177
|
}
|
|
157
|
-
if (
|
|
158
|
-
|
|
178
|
+
if (queryDomain) {
|
|
179
|
+
// leading dot replaced with % to match subdomains
|
|
180
|
+
const sqlEmbedDomain = domain.replace(/^[.%]?/, "%");
|
|
181
|
+
sql += `host_key LIKE '${sqlEmbedDomain}';`;
|
|
159
182
|
}
|
|
160
183
|
}
|
|
161
|
-
|
|
184
|
+
if (parsedArgs.verbose) {
|
|
185
|
+
console.log("sql", sql);
|
|
186
|
+
}
|
|
187
|
+
const domainRegexp: RegExp = stringToRegex(domain);
|
|
188
|
+
const sqliteQuery1: CookieRow[] = await doChromeSqliteQuery1({
|
|
162
189
|
file: file,
|
|
163
190
|
sql: sql,
|
|
191
|
+
rowFilter: (row) => {
|
|
192
|
+
return row["host_key"].match(domainRegexp) != null;
|
|
193
|
+
},
|
|
164
194
|
rowTransform: (row) => {
|
|
165
|
-
|
|
195
|
+
const cookieRow = {
|
|
196
|
+
expiry: (row["expires_utc"] / 1000000 - 11644473600) * 1000,
|
|
166
197
|
domain: row["host_key"],
|
|
167
198
|
name: row["name"],
|
|
168
199
|
value: row["encrypted_value"],
|
|
169
200
|
};
|
|
201
|
+
if (parsedArgs.verbose) {
|
|
202
|
+
console.log("CookieRow", cookieRow);
|
|
203
|
+
}
|
|
204
|
+
return cookieRow;
|
|
170
205
|
},
|
|
171
206
|
});
|
|
207
|
+
return sqliteQuery1.filter((row) => {
|
|
208
|
+
return row.domain.match(domainRegexp) != null;
|
|
209
|
+
});
|
|
172
210
|
}
|
|
173
211
|
|
|
174
212
|
async function getChromePassword(): Promise<string> {
|
|
@@ -192,7 +230,7 @@ async function decrypt(
|
|
|
192
230
|
if (!(encryptedData1 instanceof Buffer)) {
|
|
193
231
|
if (Array.isArray(encryptedData1) && encryptedData1[0] instanceof Buffer) {
|
|
194
232
|
[encryptedData1] = encryptedData1;
|
|
195
|
-
if (
|
|
233
|
+
if (parsedArgs.verbose) {
|
|
196
234
|
console.log(
|
|
197
235
|
`encryptedData is an array of buffers, selected first: ${encryptedData1}`
|
|
198
236
|
);
|
|
@@ -202,14 +240,14 @@ async function decrypt(
|
|
|
202
240
|
}
|
|
203
241
|
encryptedData1 = Buffer.from(encryptedData1);
|
|
204
242
|
}
|
|
205
|
-
if (
|
|
243
|
+
if (parsedArgs.verbose) {
|
|
206
244
|
console.log(`Trying to decrypt with password ${password}`);
|
|
207
245
|
}
|
|
208
246
|
return new Promise((resolve, reject) => {
|
|
209
247
|
crypto.pbkdf2(password, "saltysalt", 1003, 16, "sha1", (error, buffer) => {
|
|
210
248
|
try {
|
|
211
249
|
if (error) {
|
|
212
|
-
if (
|
|
250
|
+
if (parsedArgs.verbose) {
|
|
213
251
|
console.log("Error doing pbkdf2", error);
|
|
214
252
|
}
|
|
215
253
|
reject(error);
|
|
@@ -217,7 +255,7 @@ async function decrypt(
|
|
|
217
255
|
}
|
|
218
256
|
|
|
219
257
|
if (buffer.length !== 16) {
|
|
220
|
-
if (
|
|
258
|
+
if (parsedArgs.verbose) {
|
|
221
259
|
console.log(
|
|
222
260
|
"Error doing pbkdf2, buffer length is not 16",
|
|
223
261
|
buffer.length
|
|
@@ -237,7 +275,7 @@ async function decrypt(
|
|
|
237
275
|
}
|
|
238
276
|
|
|
239
277
|
if (encryptedData1.length % 16 !== 0) {
|
|
240
|
-
if (
|
|
278
|
+
if (parsedArgs.verbose) {
|
|
241
279
|
console.log(
|
|
242
280
|
"Error doing pbkdf2, encryptedData length is not a multiple of 16",
|
|
243
281
|
encryptedData1.length
|
|
@@ -251,7 +289,7 @@ async function decrypt(
|
|
|
251
289
|
try {
|
|
252
290
|
decipher.final("utf-8");
|
|
253
291
|
} catch (e) {
|
|
254
|
-
if (
|
|
292
|
+
if (parsedArgs.verbose) {
|
|
255
293
|
console.log("Error doing decipher.final()", e);
|
|
256
294
|
}
|
|
257
295
|
reject(e);
|
|
@@ -271,3 +309,47 @@ async function decrypt(
|
|
|
271
309
|
});
|
|
272
310
|
});
|
|
273
311
|
}
|
|
312
|
+
|
|
313
|
+
export async function doChromeSqliteQuery1({
|
|
314
|
+
file,
|
|
315
|
+
sql,
|
|
316
|
+
rowFilter = () => true,
|
|
317
|
+
rowTransform,
|
|
318
|
+
}: DoSqliteQuery1Params): Promise<CookieRow[]> {
|
|
319
|
+
if (!file || (file && !fs.existsSync(file))) {
|
|
320
|
+
throw new Error(`doSqliteQuery1: file ${file} does not exist`);
|
|
321
|
+
}
|
|
322
|
+
const db = new sqlite3.Database(file);
|
|
323
|
+
return new Promise((resolve, reject) => {
|
|
324
|
+
db.all(sql, (err: Error, rows: any[]) => {
|
|
325
|
+
if (err) {
|
|
326
|
+
reject(err);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const rows1: any[] = rows;
|
|
330
|
+
if (rows1 == null || rows1.length === 0) {
|
|
331
|
+
resolve([]);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
if (Array.isArray(rows1)) {
|
|
335
|
+
const cookieRows: CookieRow[] = rows1
|
|
336
|
+
.filter(rowFilter)
|
|
337
|
+
.map((row: any) => {
|
|
338
|
+
const newVar = {
|
|
339
|
+
meta: {
|
|
340
|
+
file: file,
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
const cookieRow: CookieRow = rowTransform(row);
|
|
344
|
+
return merge(newVar, cookieRow);
|
|
345
|
+
});
|
|
346
|
+
resolve(cookieRows);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
if (parsedArgs.verbose) {
|
|
350
|
+
console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
|
|
351
|
+
}
|
|
352
|
+
resolve([rows1]);
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
}
|
|
@@ -2,8 +2,12 @@ import ChromeCookieQueryStrategy from "./ChromeCookieQueryStrategy";
|
|
|
2
2
|
import FirefoxCookieQueryStrategy from "./FirefoxCookieQueryStrategy";
|
|
3
3
|
import SafariCookieQueryStrategy from "./SafariCookieQueryStrategy";
|
|
4
4
|
import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
5
|
-
import
|
|
5
|
+
import ExportedCookie from "../ExportedCookie";
|
|
6
6
|
import LRUCache from "lru-cache";
|
|
7
|
+
import CookieStoreQueryStrategy from "./CookieStoreQueryStrategy";
|
|
8
|
+
import { red } from "colorette";
|
|
9
|
+
import { merge } from "lodash";
|
|
10
|
+
import { parsedArgs } from "../argv";
|
|
7
11
|
|
|
8
12
|
const cache = new LRUCache<string, ExportedCookie[]>({
|
|
9
13
|
ttl: 1000 * 2,
|
|
@@ -13,10 +17,13 @@ const cache = new LRUCache<string, ExportedCookie[]>({
|
|
|
13
17
|
export default class CompositeCookieQueryStrategy
|
|
14
18
|
implements CookieQueryStrategy
|
|
15
19
|
{
|
|
20
|
+
browserName = "all";
|
|
21
|
+
|
|
16
22
|
#strategies;
|
|
17
23
|
|
|
18
24
|
constructor() {
|
|
19
25
|
this.#strategies = [
|
|
26
|
+
CookieStoreQueryStrategy,
|
|
20
27
|
ChromeCookieQueryStrategy,
|
|
21
28
|
FirefoxCookieQueryStrategy,
|
|
22
29
|
SafariCookieQueryStrategy,
|
|
@@ -26,19 +33,36 @@ export default class CompositeCookieQueryStrategy
|
|
|
26
33
|
}
|
|
27
34
|
|
|
28
35
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
36
|
+
// domain = domain.match(/(\w+.+\w+)/gi)?.pop() ?? domain;
|
|
29
37
|
const key = `${name}:${domain}`;
|
|
30
38
|
const cached = cache.get(key);
|
|
31
39
|
if (cached) {
|
|
32
40
|
return cached;
|
|
33
41
|
}
|
|
34
|
-
|
|
35
|
-
|
|
42
|
+
if (parsedArgs.verbose) {
|
|
43
|
+
console.log("Querying cookies:", name, domain);
|
|
44
|
+
}
|
|
45
|
+
const results: ExportedCookie[][] = await Promise.all(
|
|
46
|
+
this.#strategies.map(async (strategy: CookieQueryStrategy) => {
|
|
36
47
|
// @ts-ignore
|
|
37
|
-
|
|
38
|
-
name,
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
48
|
+
return strategy
|
|
49
|
+
.queryCookies(name, domain)
|
|
50
|
+
.then((cookies: ExportedCookie[]) => {
|
|
51
|
+
return cookies.map((cookie: ExportedCookie) => {
|
|
52
|
+
return merge(cookie, {
|
|
53
|
+
meta: {
|
|
54
|
+
browser: strategy.browserName,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
})
|
|
59
|
+
.catch((e) => {
|
|
60
|
+
console.log(
|
|
61
|
+
red(`Error querying ${strategy.browserName} cookies`),
|
|
62
|
+
e
|
|
63
|
+
);
|
|
64
|
+
return [];
|
|
65
|
+
});
|
|
42
66
|
})
|
|
43
67
|
);
|
|
44
68
|
const flat: ExportedCookie[] = results.flat();
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
2
|
+
import CookieSpec from "../CookieSpec";
|
|
3
|
+
import ExportedCookie from "../ExportedCookie";
|
|
4
|
+
import { Cookie } from "tough-cookie";
|
|
5
|
+
import { cookieJar, cookieStore } from "../CookieStore";
|
|
6
|
+
import { stringToRegex } from "../StringToRegex";
|
|
7
|
+
|
|
8
|
+
export default class CookieStoreQueryStrategy implements CookieQueryStrategy {
|
|
9
|
+
browserName = "internal";
|
|
10
|
+
|
|
11
|
+
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
12
|
+
const exportedCookies: ExportedCookie[] = [];
|
|
13
|
+
|
|
14
|
+
// if (name.match(/[%*]/) || domain.match(/[%*]/)) {
|
|
15
|
+
const cookies: Cookie[] = await this.#getAllCookies();
|
|
16
|
+
const allExportedCookies = cookies.map((cookie: Cookie) => {
|
|
17
|
+
return this.#extracted(cookie, { name, domain });
|
|
18
|
+
});
|
|
19
|
+
exportedCookies.push(...allExportedCookies);
|
|
20
|
+
// }
|
|
21
|
+
|
|
22
|
+
const wildcardRegexp = /^([*%])$/i;
|
|
23
|
+
if (name.match(wildcardRegexp) && domain.match(wildcardRegexp)) {
|
|
24
|
+
return exportedCookies;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const path = "/";
|
|
28
|
+
|
|
29
|
+
if (domain != "%") {
|
|
30
|
+
const domain1 = domain.match(/(\w+.+\w+)/gi)?.pop() ?? domain;
|
|
31
|
+
const url = new URL("https://" + domain1);
|
|
32
|
+
url.pathname = path;
|
|
33
|
+
const cookies: Cookie[] = await this.#getCookies(url.href);
|
|
34
|
+
const domainCookies = cookies.map((cookie: Cookie) => {
|
|
35
|
+
return this.#extracted(cookie, {
|
|
36
|
+
domain,
|
|
37
|
+
name,
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
exportedCookies.push(...domainCookies);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (name == "%") {
|
|
44
|
+
return exportedCookies.filter((cookie: ExportedCookie) => {
|
|
45
|
+
return cookie.domain.match(stringToRegex(domain));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return exportedCookies.filter((cookie: ExportedCookie) => {
|
|
50
|
+
return (
|
|
51
|
+
cookie.name.match(stringToRegex(name)) &&
|
|
52
|
+
cookie.domain.match(stringToRegex(domain))
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#extracted(cookie: Cookie, cookieSpec: CookieSpec): ExportedCookie {
|
|
58
|
+
return {
|
|
59
|
+
domain: cookie.domain ?? cookieSpec.domain,
|
|
60
|
+
name: cookie.key ?? cookieSpec.name,
|
|
61
|
+
value: cookie.value,
|
|
62
|
+
expiry: cookie.expires ?? "Infinity",
|
|
63
|
+
meta: {
|
|
64
|
+
file: "tough-cookie",
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#getCookies(url: string): Promise<Cookie[]> {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
return cookieJar.getCookies(url, (err, cookies) => {
|
|
73
|
+
if (err) {
|
|
74
|
+
reject(err);
|
|
75
|
+
} else {
|
|
76
|
+
resolve(cookies ?? []);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#getAllCookies(): Promise<Cookie[]> {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
return cookieStore.getAllCookies((err, cookies) => {
|
|
86
|
+
if (err) {
|
|
87
|
+
reject(err);
|
|
88
|
+
} else {
|
|
89
|
+
resolve(cookies ?? []);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -4,12 +4,14 @@ import { env, HOME } from "../global";
|
|
|
4
4
|
import { existsSync } from "fs";
|
|
5
5
|
import { findAllFiles } from "../findAllFiles";
|
|
6
6
|
import { doSqliteQuery1 } from "../doSqliteQuery1";
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
import
|
|
7
|
+
import ExportedCookie from "../ExportedCookie";
|
|
8
|
+
import CookieRow from "../CookieRow";
|
|
9
|
+
import CookieSpec from "../CookieSpec";
|
|
10
10
|
import { specialCases } from "../SpecialCases";
|
|
11
11
|
|
|
12
12
|
export default class FirefoxCookieQueryStrategy implements CookieQueryStrategy {
|
|
13
|
+
browserName = "Firefox";
|
|
14
|
+
|
|
13
15
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
14
16
|
if (process.platform !== "darwin") {
|
|
15
17
|
throw new Error("This only works on macOS");
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
2
|
-
import
|
|
2
|
+
import ExportedCookie from "../ExportedCookie";
|
|
3
3
|
|
|
4
4
|
export default class SafariCookieQueryStrategy implements CookieQueryStrategy {
|
|
5
|
+
browserName = "Safari";
|
|
6
|
+
|
|
5
7
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
6
8
|
return [];
|
|
7
9
|
}
|