@mherod/get-cookie 2.0.0-rc.10 → 2.0.0-rc.12
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/.fleet/settings.json +3 -0
- package/.prettierrc.json +1 -1
- package/dist/index.js +96 -64
- package/package-lock.json +2106 -6865
- package/package.json +5 -7
- package/src/CookieSpec.ts +2 -0
- package/src/CookieStore.ts +9 -5
- package/src/argv.ts +17 -2
- package/src/browsers/ChromeCookieQueryStrategy.ts +30 -18
- package/src/browsers/CookieQueryStrategy.ts +1 -0
- package/src/browsers/CookieStoreQueryStrategy.ts +2 -2
- package/src/browsers/FirefoxCookieQueryStrategy.ts +52 -6
- package/src/browsers/SafariCookieQueryStrategy.ts +1 -0
- package/src/cli.ts +25 -10
- package/src/comboQueryCookieSpec.ts +24 -0
- package/src/cookieSpecsFromUrl.ts +26 -0
- package/src/doSqliteQuery1Params.ts +1 -0
- package/src/fetchWithCookies.ts +22 -22
- package/src/getGroupedRenderedCookies.ts +7 -17
- package/src/getMergedRenderedCookies.ts +9 -20
- package/src/doSqliteQuery1.ts +0 -47
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.12",
|
|
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",
|
|
@@ -13,19 +13,16 @@
|
|
|
13
13
|
"scripts": {
|
|
14
14
|
"clean": "git clean -fdX && rm -rf node_modules dist && yarn install",
|
|
15
15
|
"build": "parcel build",
|
|
16
|
+
"postbuild": "npm run check",
|
|
16
17
|
"prepare": "npm run build && npm run prettier",
|
|
17
18
|
"test": "jest",
|
|
18
|
-
"postbuild": "npm run check",
|
|
19
19
|
"prepublish": "npm run build",
|
|
20
|
-
"
|
|
21
|
-
"install:global": "npm install --force --global .",
|
|
22
|
-
"postinstall:global": "find $NVM_BIN -exec chmod +x {} \\; || true",
|
|
20
|
+
"install:global": "npm run build && npm install --force --global .",
|
|
23
21
|
"prettier": "prettier --write 'src/{**/*,*}.{js,json,ts}'",
|
|
24
22
|
"check": "tsc --noEmit"
|
|
25
23
|
},
|
|
26
24
|
"targets": {
|
|
27
25
|
"main": {},
|
|
28
|
-
"types": {},
|
|
29
26
|
"library": {
|
|
30
27
|
"source": "src/index.ts",
|
|
31
28
|
"distDir": "dist",
|
|
@@ -33,6 +30,7 @@
|
|
|
33
30
|
"engines": {
|
|
34
31
|
"node": "16"
|
|
35
32
|
},
|
|
33
|
+
"sourceMap": true,
|
|
36
34
|
"includeNodeModules": false
|
|
37
35
|
},
|
|
38
36
|
"cli": {
|
|
@@ -42,6 +40,7 @@
|
|
|
42
40
|
"engines": {
|
|
43
41
|
"node": "16"
|
|
44
42
|
},
|
|
43
|
+
"sourceMap": false,
|
|
45
44
|
"includeNodeModules": [
|
|
46
45
|
"colorette"
|
|
47
46
|
]
|
|
@@ -79,7 +78,6 @@
|
|
|
79
78
|
"@types/node": "^18.8.2",
|
|
80
79
|
"@types/tough-cookie": "^4.0.2",
|
|
81
80
|
"@types/user-agents": "^1.0.2",
|
|
82
|
-
"jest": "^29.0.3",
|
|
83
81
|
"parcel": "^2.7.0",
|
|
84
82
|
"prettier": "^2.7.1",
|
|
85
83
|
"ts-node": "^10.9.1",
|
package/src/CookieSpec.ts
CHANGED
package/src/CookieStore.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { CookieJar } from "tough-cookie";
|
|
2
1
|
import { env } from "./global";
|
|
2
|
+
import { parsedArgs } from "./argv";
|
|
3
|
+
import { CookieJar, MemoryCookieStore, Store } from "tough-cookie";
|
|
4
|
+
|
|
3
5
|
const { FileCookieStore } = require("tough-cookie-file-store");
|
|
4
6
|
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
);
|
|
8
|
-
|
|
7
|
+
export let cookieStore: Store;
|
|
8
|
+
if (parsedArgs["cs"] == "memory") {
|
|
9
|
+
cookieStore = new MemoryCookieStore();
|
|
10
|
+
} else {
|
|
11
|
+
cookieStore = new FileCookieStore(`${env["HOME"]}/cookie-star.json`);
|
|
12
|
+
}
|
|
9
13
|
|
|
10
14
|
export const cookieJar = new CookieJar(cookieStore);
|
package/src/argv.ts
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import minimist from "minimist";
|
|
2
|
+
import { merge } from "lodash";
|
|
2
3
|
|
|
3
|
-
export
|
|
4
|
+
export interface MyParsedArgs extends minimist.ParsedArgs {
|
|
5
|
+
verbose: boolean;
|
|
6
|
+
render?: string;
|
|
7
|
+
fetch?: string;
|
|
8
|
+
cs?: string;
|
|
9
|
+
dump?: string;
|
|
10
|
+
"dump-request-headers"?: string;
|
|
11
|
+
"dump-response-headers"?: string;
|
|
12
|
+
"dump-response-body"?: string;
|
|
13
|
+
}
|
|
4
14
|
|
|
5
|
-
export const
|
|
15
|
+
export const argv: string[] = process.argv ?? [];
|
|
16
|
+
const minimistArgs: minimist.ParsedArgs = minimist(argv.slice(2));
|
|
17
|
+
const defaultOptions = {
|
|
18
|
+
verbose: false,
|
|
19
|
+
};
|
|
20
|
+
export const parsedArgs: MyParsedArgs = merge(defaultOptions, minimistArgs);
|
|
@@ -1,11 +1,10 @@
|
|
|
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";
|
|
@@ -13,7 +12,6 @@ import CookieRow from "../CookieRow";
|
|
|
13
12
|
import ExportedCookie from "../ExportedCookie";
|
|
14
13
|
import { stringToRegex } from "../StringToRegex";
|
|
15
14
|
import { parsedArgs } from "../argv";
|
|
16
|
-
import fs from "fs";
|
|
17
15
|
import * as sqlite3 from "sqlite3";
|
|
18
16
|
import { DoSqliteQuery1Params } from "../doSqliteQuery1Params";
|
|
19
17
|
|
|
@@ -113,11 +111,15 @@ Promise<ExportedCookie[]> {
|
|
|
113
111
|
meta: meta,
|
|
114
112
|
};
|
|
115
113
|
const expiry = cookieRow.expiry;
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
114
|
+
const mergeExpiry =
|
|
115
|
+
expiry != null && expiry > 0
|
|
116
|
+
? {
|
|
117
|
+
expiry: new Date(expiry),
|
|
118
|
+
}
|
|
119
|
+
: {
|
|
120
|
+
expiry: "Infinity",
|
|
121
|
+
};
|
|
122
|
+
merge(exportedCookie, mergeExpiry);
|
|
121
123
|
return exportedCookie;
|
|
122
124
|
});
|
|
123
125
|
const results: ExportedCookie[] = (await Promise.all(decrypted)).filter(
|
|
@@ -179,9 +181,16 @@ async function getEncryptedChromeCookie({
|
|
|
179
181
|
sql += `host_key LIKE '${sqlEmbedDomain}';`;
|
|
180
182
|
}
|
|
181
183
|
}
|
|
184
|
+
if (parsedArgs.verbose) {
|
|
185
|
+
console.log("sql", sql);
|
|
186
|
+
}
|
|
187
|
+
const domainRegexp: RegExp = stringToRegex(domain);
|
|
182
188
|
const sqliteQuery1: CookieRow[] = await doChromeSqliteQuery1({
|
|
183
189
|
file: file,
|
|
184
190
|
sql: sql,
|
|
191
|
+
rowFilter: (row) => {
|
|
192
|
+
return row["host_key"].match(domainRegexp) != null;
|
|
193
|
+
},
|
|
185
194
|
rowTransform: (row) => {
|
|
186
195
|
const cookieRow = {
|
|
187
196
|
expiry: (row["expires_utc"] / 1000000 - 11644473600) * 1000,
|
|
@@ -196,7 +205,7 @@ async function getEncryptedChromeCookie({
|
|
|
196
205
|
},
|
|
197
206
|
});
|
|
198
207
|
return sqliteQuery1.filter((row) => {
|
|
199
|
-
return row.domain.match(
|
|
208
|
+
return row.domain.match(domainRegexp) != null;
|
|
200
209
|
});
|
|
201
210
|
}
|
|
202
211
|
|
|
@@ -304,6 +313,7 @@ async function decrypt(
|
|
|
304
313
|
export async function doChromeSqliteQuery1({
|
|
305
314
|
file,
|
|
306
315
|
sql,
|
|
316
|
+
rowFilter = () => true,
|
|
307
317
|
rowTransform,
|
|
308
318
|
}: DoSqliteQuery1Params): Promise<CookieRow[]> {
|
|
309
319
|
if (!file || (file && !fs.existsSync(file))) {
|
|
@@ -322,15 +332,17 @@ export async function doChromeSqliteQuery1({
|
|
|
322
332
|
return;
|
|
323
333
|
}
|
|
324
334
|
if (Array.isArray(rows1)) {
|
|
325
|
-
const cookieRows: CookieRow[] = rows1
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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
|
+
});
|
|
334
346
|
resolve(cookieRows);
|
|
335
347
|
return;
|
|
336
348
|
}
|
|
@@ -2,7 +2,7 @@ import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
|
2
2
|
import CookieSpec from "../CookieSpec";
|
|
3
3
|
import ExportedCookie from "../ExportedCookie";
|
|
4
4
|
import { Cookie } from "tough-cookie";
|
|
5
|
-
import {
|
|
5
|
+
import { cookieJar, cookieStore } from "../CookieStore";
|
|
6
6
|
import { stringToRegex } from "../StringToRegex";
|
|
7
7
|
|
|
8
8
|
export default class CookieStoreQueryStrategy implements CookieQueryStrategy {
|
|
@@ -59,7 +59,7 @@ export default class CookieStoreQueryStrategy implements CookieQueryStrategy {
|
|
|
59
59
|
domain: cookie.domain ?? cookieSpec.domain,
|
|
60
60
|
name: cookie.key ?? cookieSpec.name,
|
|
61
61
|
value: cookie.value,
|
|
62
|
-
expiry: cookie.expires ?? Infinity,
|
|
62
|
+
expiry: cookie.expires ?? "Infinity",
|
|
63
63
|
meta: {
|
|
64
64
|
file: "tough-cookie",
|
|
65
65
|
},
|
|
@@ -1,22 +1,67 @@
|
|
|
1
1
|
import * as path from "path";
|
|
2
2
|
import CookieQueryStrategy from "./CookieQueryStrategy";
|
|
3
|
-
import {
|
|
4
|
-
import { existsSync } from "fs";
|
|
3
|
+
import { HOME } from "../global";
|
|
4
|
+
import fs, { existsSync } from "fs";
|
|
5
5
|
import { findAllFiles } from "../findAllFiles";
|
|
6
|
-
import { doSqliteQuery1 } from "../doSqliteQuery1";
|
|
7
6
|
import ExportedCookie from "../ExportedCookie";
|
|
8
7
|
import CookieRow from "../CookieRow";
|
|
9
8
|
import CookieSpec from "../CookieSpec";
|
|
10
9
|
import { specialCases } from "../SpecialCases";
|
|
10
|
+
import { parsedArgs } from "../argv";
|
|
11
|
+
import { DoSqliteQuery1Params } from "../doSqliteQuery1Params";
|
|
12
|
+
import * as sqlite3 from "sqlite3";
|
|
13
|
+
import { merge } from "lodash";
|
|
14
|
+
|
|
15
|
+
export async function doSqliteQuery1({
|
|
16
|
+
file,
|
|
17
|
+
sql,
|
|
18
|
+
rowTransform,
|
|
19
|
+
}: DoSqliteQuery1Params): Promise<CookieRow[]> {
|
|
20
|
+
if (!file || (file && !fs.existsSync(file))) {
|
|
21
|
+
throw new Error(`doSqliteQuery1: file ${file} does not exist`);
|
|
22
|
+
}
|
|
23
|
+
const db = new sqlite3.Database(file);
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
db.all(sql, (err: Error, rows: any[]) => {
|
|
26
|
+
if (err) {
|
|
27
|
+
reject(err);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const rows1: any[] = rows;
|
|
31
|
+
if (rows1 == null || rows1.length === 0) {
|
|
32
|
+
resolve([]);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (Array.isArray(rows1)) {
|
|
36
|
+
const cookieRows: CookieRow[] = rows1.map((row: any) => {
|
|
37
|
+
const newVar = {
|
|
38
|
+
meta: {
|
|
39
|
+
file: file,
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const cookieRow: CookieRow = rowTransform(row);
|
|
43
|
+
return merge(newVar, cookieRow);
|
|
44
|
+
});
|
|
45
|
+
resolve(cookieRows);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (parsedArgs.verbose) {
|
|
49
|
+
console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
|
|
50
|
+
}
|
|
51
|
+
resolve([rows1]);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
11
55
|
|
|
12
56
|
export default class FirefoxCookieQueryStrategy implements CookieQueryStrategy {
|
|
13
57
|
browserName = "Firefox";
|
|
14
58
|
|
|
15
59
|
async queryCookies(name: string, domain: string): Promise<ExportedCookie[]> {
|
|
16
60
|
if (process.platform !== "darwin") {
|
|
17
|
-
|
|
61
|
+
// TODO: implement
|
|
62
|
+
return [];
|
|
18
63
|
}
|
|
19
|
-
if (
|
|
64
|
+
if (parsedArgs.browser !== "firefox") {
|
|
20
65
|
return [];
|
|
21
66
|
}
|
|
22
67
|
const cookies = await this.#getFirefoxCookie({ name, domain });
|
|
@@ -49,7 +94,8 @@ export default class FirefoxCookieQueryStrategy implements CookieQueryStrategy {
|
|
|
49
94
|
const fn: (file: string) => Promise<CookieRow[]> = async (file: string) => {
|
|
50
95
|
return await this.#queryCookiesDb(file, name, domain);
|
|
51
96
|
};
|
|
52
|
-
const all:
|
|
97
|
+
const all: CookieRow[][] = await Promise.all(files.map(fn));
|
|
98
|
+
// flatten
|
|
53
99
|
return all.flat();
|
|
54
100
|
}
|
|
55
101
|
|
package/src/cli.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import { argv, parsedArgs } from "./argv";
|
|
4
|
-
import { queryCookies } from "./queryCookies";
|
|
5
4
|
import { groupBy } from "lodash";
|
|
6
5
|
import { green, red, yellow } from "colorette";
|
|
7
6
|
import { resultsRendered } from "./resultsRendered";
|
|
8
7
|
import { fetchWithCookies } from "./fetchWithCookies";
|
|
9
8
|
import { unpackHeaders } from "./unpackHeaders";
|
|
10
9
|
import CookieSpec from "./CookieSpec";
|
|
10
|
+
import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
|
|
11
|
+
import { cookieSpecsFromUrl } from "./cookieSpecsFromUrl";
|
|
11
12
|
|
|
12
|
-
async function cliQueryCookies(
|
|
13
|
+
async function cliQueryCookies(cookieSpec: CookieSpec | CookieSpec[]) {
|
|
13
14
|
try {
|
|
14
|
-
const results = await
|
|
15
|
+
const results = await comboQueryCookieSpec(cookieSpec);
|
|
15
16
|
if (results == null || results.length == 0) {
|
|
16
17
|
console.error(red("No results"));
|
|
17
18
|
return;
|
|
@@ -49,7 +50,7 @@ async function cliQueryCookies({ name, domain }: CookieSpec) {
|
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
function main() {
|
|
53
|
+
async function main() {
|
|
53
54
|
if (parsedArgs["help"] || parsedArgs["h"]) {
|
|
54
55
|
console.log(`Usage: ${argv[1]} [name] [domain] [options] `);
|
|
55
56
|
console.log(`Options:`);
|
|
@@ -61,6 +62,8 @@ function main() {
|
|
|
61
62
|
return;
|
|
62
63
|
}
|
|
63
64
|
|
|
65
|
+
parsedArgs["verbose"] = parsedArgs["verbose"] || parsedArgs["v"];
|
|
66
|
+
|
|
64
67
|
const fetchUrl: string = parsedArgs["fetch"] || parsedArgs["F"];
|
|
65
68
|
if (fetchUrl) {
|
|
66
69
|
let url: URL;
|
|
@@ -100,12 +103,24 @@ function main() {
|
|
|
100
103
|
return;
|
|
101
104
|
}
|
|
102
105
|
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
const cookieSpecs: CookieSpec[] = [];
|
|
107
|
+
const argUrl: string = parsedArgs["url"] || parsedArgs["u"];
|
|
108
|
+
if (argUrl) {
|
|
109
|
+
for (const cookieSpec of cookieSpecsFromUrl(argUrl)) {
|
|
110
|
+
cookieSpecs.push(cookieSpec);
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
cookieSpecs.push({
|
|
114
|
+
name: parsedArgs["name"] || parsedArgs["_"][0] || "%",
|
|
115
|
+
domain: parsedArgs["domain"] || parsedArgs["_"][1] || "%",
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (parsedArgs.verbose) {
|
|
120
|
+
console.log("cookieSpecs", cookieSpecs);
|
|
121
|
+
}
|
|
107
122
|
|
|
108
|
-
cliQueryCookies(
|
|
123
|
+
await cliQueryCookies(cookieSpecs).catch(console.error);
|
|
109
124
|
}
|
|
110
125
|
|
|
111
|
-
main();
|
|
126
|
+
main().then((r) => r, console.error);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import CookieSpec, { MultiCookieSpec } from "./CookieSpec";
|
|
2
|
+
import ExportedCookie from "./ExportedCookie";
|
|
3
|
+
import { queryCookies } from "./queryCookies";
|
|
4
|
+
import { uniqBy } from "lodash";
|
|
5
|
+
|
|
6
|
+
export async function comboQueryCookieSpec(
|
|
7
|
+
cookieSpec: MultiCookieSpec
|
|
8
|
+
): Promise<ExportedCookie[]> {
|
|
9
|
+
const cookies: ExportedCookie[] = [];
|
|
10
|
+
if (Array.isArray(cookieSpec)) {
|
|
11
|
+
const results: Awaited<ExportedCookie[]>[] = await Promise.all(
|
|
12
|
+
cookieSpec.map((cs) => {
|
|
13
|
+
return queryCookies(cs);
|
|
14
|
+
})
|
|
15
|
+
);
|
|
16
|
+
for (const exportedCookie of results.flat()) {
|
|
17
|
+
cookies.push(exportedCookie);
|
|
18
|
+
}
|
|
19
|
+
} else {
|
|
20
|
+
const singleQuery: ExportedCookie[] = await queryCookies(cookieSpec);
|
|
21
|
+
cookies.push(...singleQuery);
|
|
22
|
+
}
|
|
23
|
+
return uniqBy(cookies, JSON.stringify);
|
|
24
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import CookieSpec from "./CookieSpec";
|
|
2
|
+
import { uniqBy } from "lodash";
|
|
3
|
+
|
|
4
|
+
export function cookieSpecsFromUrl(url: URL | string): CookieSpec[] {
|
|
5
|
+
const url1 = typeof url == "string" ? new URL(url) : url;
|
|
6
|
+
const cookieSpecs = [];
|
|
7
|
+
const splits = url1.hostname.split(".");
|
|
8
|
+
const tld = splits.slice(-2).join(".");
|
|
9
|
+
const cookieSpec: CookieSpec = {
|
|
10
|
+
name: "%",
|
|
11
|
+
domain: "%." + tld,
|
|
12
|
+
};
|
|
13
|
+
cookieSpecs.push(cookieSpec);
|
|
14
|
+
const cookieSpec1: CookieSpec = {
|
|
15
|
+
name: "%",
|
|
16
|
+
domain: url1.hostname,
|
|
17
|
+
};
|
|
18
|
+
cookieSpecs.push(cookieSpec1);
|
|
19
|
+
// const isWww = splits.slice(-3)[0] === "www";
|
|
20
|
+
const cookieSpec2: CookieSpec = {
|
|
21
|
+
name: "%",
|
|
22
|
+
domain: tld,
|
|
23
|
+
};
|
|
24
|
+
cookieSpecs.push(cookieSpec2);
|
|
25
|
+
return uniqBy(cookieSpecs, JSON.stringify);
|
|
26
|
+
}
|
package/src/fetchWithCookies.ts
CHANGED
|
@@ -1,47 +1,43 @@
|
|
|
1
|
-
// noinspection JSUnusedGlobalSymbols
|
|
1
|
+
// noinspection JSUnusedGlobalSymbols,ExceptionCaughtLocallyJS
|
|
2
2
|
|
|
3
3
|
import { fetch as fetchImpl } from "cross-fetch";
|
|
4
4
|
import { merge } from "lodash";
|
|
5
5
|
// noinspection SpellCheckingInspection
|
|
6
6
|
import destr from "destr";
|
|
7
|
-
import CookieSpec from "./CookieSpec";
|
|
8
7
|
import { cookieJar } from "./CookieStore";
|
|
9
8
|
import UserAgent from "user-agents";
|
|
10
9
|
import { parsedArgs } from "./argv";
|
|
11
10
|
import { blue, yellow } from "colorette";
|
|
12
11
|
import { getMergedRenderedCookies } from "./getMergedRenderedCookies";
|
|
12
|
+
import { cookieSpecsFromUrl } from "./cookieSpecsFromUrl";
|
|
13
|
+
import CookieSpec from "./CookieSpec";
|
|
14
|
+
|
|
15
|
+
const userAgent = new UserAgent().toString();
|
|
13
16
|
|
|
14
17
|
export async function fetchWithCookies(
|
|
15
18
|
url: RequestInfo | URL,
|
|
16
19
|
options: RequestInit | undefined = {},
|
|
17
20
|
fetch: Function = fetchImpl
|
|
18
21
|
): Promise<Response> {
|
|
22
|
+
const headers = {
|
|
23
|
+
"User-Agent": userAgent,
|
|
24
|
+
};
|
|
19
25
|
const defaultOptions: RequestInit = {
|
|
20
|
-
headers
|
|
21
|
-
"User-Agent": new UserAgent().toString(),
|
|
22
|
-
},
|
|
26
|
+
headers,
|
|
23
27
|
redirect: "manual",
|
|
24
28
|
};
|
|
25
29
|
const url2: string = `${url}`;
|
|
26
30
|
const url1: URL = new URL(url2);
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
});
|
|
30
|
-
const cookieSpec: CookieSpec = {
|
|
31
|
-
name: "%",
|
|
32
|
-
domain: domain,
|
|
33
|
-
};
|
|
34
|
-
// const cookies: string[] = await getGroupedRenderedCookies(cookieSpec).catch(
|
|
35
|
-
// () => []
|
|
36
|
-
// );
|
|
37
|
-
// const cookie = cookies.pop();
|
|
38
|
-
const cookie = await getMergedRenderedCookies(cookieSpec).catch(() => "");
|
|
39
|
-
const headers = {};
|
|
31
|
+
const cookieSpecs: CookieSpec[] = cookieSpecsFromUrl(url1);
|
|
32
|
+
const cookie = await getMergedRenderedCookies(cookieSpecs).catch(() => "");
|
|
40
33
|
if (cookie.length > 0) {
|
|
41
34
|
merge(headers, {
|
|
42
35
|
Cookie: cookie,
|
|
43
36
|
});
|
|
44
37
|
}
|
|
38
|
+
if (parsedArgs["dump-request-headers"]) {
|
|
39
|
+
console.log(blue("Request headers:"), headers);
|
|
40
|
+
}
|
|
45
41
|
const newOptions1: RequestInit = merge(defaultOptions, options, { headers });
|
|
46
42
|
try {
|
|
47
43
|
const res: Response = await fetch(url2, newOptions1);
|
|
@@ -63,11 +59,15 @@ export async function fetchWithCookies(
|
|
|
63
59
|
}
|
|
64
60
|
|
|
65
61
|
const newUrl: string = res.headers.get("location") as string;
|
|
66
|
-
if (res.
|
|
67
|
-
if (
|
|
68
|
-
|
|
62
|
+
if (res.status >= 300 && res.status < 400) {
|
|
63
|
+
if (newUrl && newUrl !== url2) {
|
|
64
|
+
if (parsedArgs.verbose) {
|
|
65
|
+
console.log(blue(`Redirected to `), yellow(newUrl));
|
|
66
|
+
}
|
|
67
|
+
return fetchWithCookies(newUrl, newOptions1);
|
|
68
|
+
} else {
|
|
69
|
+
throw new Error("Redirected but no new location");
|
|
69
70
|
}
|
|
70
|
-
return fetchWithCookies(newUrl, newOptions1);
|
|
71
71
|
}
|
|
72
72
|
|
|
73
73
|
const arrayBuffer1: Promise<ArrayBuffer> = res.arrayBuffer();
|
|
@@ -1,27 +1,17 @@
|
|
|
1
1
|
import { groupBy } from "lodash";
|
|
2
|
-
import { queryCookies } from "./queryCookies";
|
|
3
2
|
import { resultsRendered } from "./resultsRendered";
|
|
4
|
-
import
|
|
5
|
-
import CookieSpec from "./CookieSpec";
|
|
3
|
+
import { MultiCookieSpec } from "./CookieSpec";
|
|
6
4
|
import ExportedCookie from "./ExportedCookie";
|
|
5
|
+
import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
|
|
7
6
|
|
|
8
|
-
export async function getGroupedRenderedCookies(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const cookies: ExportedCookie[] = await queryCookies(
|
|
13
|
-
{
|
|
14
|
-
name,
|
|
15
|
-
domain,
|
|
16
|
-
},
|
|
17
|
-
new CompositeCookieQueryStrategy()
|
|
18
|
-
//
|
|
19
|
-
);
|
|
7
|
+
export async function getGroupedRenderedCookies(
|
|
8
|
+
cookieSpec: MultiCookieSpec
|
|
9
|
+
): Promise<string[]> {
|
|
10
|
+
const cookies: ExportedCookie[] = await comboQueryCookieSpec(cookieSpec);
|
|
20
11
|
if (cookies.length == 0) {
|
|
21
12
|
throw new Error("Cookie not found");
|
|
22
13
|
}
|
|
23
|
-
const
|
|
24
|
-
const groupedByFile = groupBy(results, (r: ExportedCookie) => r.meta?.file);
|
|
14
|
+
const groupedByFile = groupBy(cookies, (r: ExportedCookie) => r.meta?.file);
|
|
25
15
|
return Object.keys(groupedByFile).map((file: string) => {
|
|
26
16
|
const results: ExportedCookie[] = groupedByFile[file];
|
|
27
17
|
return resultsRendered(results);
|
|
@@ -1,25 +1,14 @@
|
|
|
1
|
-
import { groupBy } from "lodash";
|
|
2
|
-
import { queryCookies } from "./queryCookies";
|
|
3
1
|
import { resultsRendered } from "./resultsRendered";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
2
|
+
import { comboQueryCookieSpec } from "./comboQueryCookieSpec";
|
|
3
|
+
import { MultiCookieSpec } from "./CookieSpec";
|
|
6
4
|
import ExportedCookie from "./ExportedCookie";
|
|
7
5
|
|
|
8
|
-
export async function getMergedRenderedCookies(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
name,
|
|
15
|
-
domain,
|
|
16
|
-
},
|
|
17
|
-
new CompositeCookieQueryStrategy()
|
|
18
|
-
//
|
|
19
|
-
);
|
|
20
|
-
if (cookies.length == 0) {
|
|
21
|
-
throw new Error("Cookie not found");
|
|
6
|
+
export async function getMergedRenderedCookies(
|
|
7
|
+
cookieSpec: MultiCookieSpec
|
|
8
|
+
): Promise<string> {
|
|
9
|
+
const cookies: ExportedCookie[] = await comboQueryCookieSpec(cookieSpec);
|
|
10
|
+
if (cookies.length > 0) {
|
|
11
|
+
return resultsRendered(cookies);
|
|
22
12
|
}
|
|
23
|
-
|
|
24
|
-
return resultsRendered(results);
|
|
13
|
+
return "";
|
|
25
14
|
}
|
package/src/doSqliteQuery1.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import * as fs from "fs";
|
|
2
|
-
import * as sqlite3 from "sqlite3";
|
|
3
|
-
import CookieRow from "./CookieRow";
|
|
4
|
-
import { merge } from "lodash";
|
|
5
|
-
import { parsedArgs } from "./argv";
|
|
6
|
-
import { DoSqliteQuery1Params } from "./doSqliteQuery1Params";
|
|
7
|
-
|
|
8
|
-
export async function doSqliteQuery1({
|
|
9
|
-
file,
|
|
10
|
-
sql,
|
|
11
|
-
rowTransform,
|
|
12
|
-
}: DoSqliteQuery1Params): Promise<CookieRow[]> {
|
|
13
|
-
if (!file || (file && !fs.existsSync(file))) {
|
|
14
|
-
throw new Error(`doSqliteQuery1: file ${file} does not exist`);
|
|
15
|
-
}
|
|
16
|
-
const db = new sqlite3.Database(file);
|
|
17
|
-
return new Promise((resolve, reject) => {
|
|
18
|
-
db.all(sql, (err: Error, rows: any[]) => {
|
|
19
|
-
if (err) {
|
|
20
|
-
reject(err);
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
const rows1: any[] = rows;
|
|
24
|
-
if (rows1 == null || rows1.length === 0) {
|
|
25
|
-
resolve([]);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
if (Array.isArray(rows1)) {
|
|
29
|
-
const cookieRows: CookieRow[] = rows1.map((row: any) => {
|
|
30
|
-
const newVar = {
|
|
31
|
-
meta: {
|
|
32
|
-
file: file,
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
const cookieRow: CookieRow = rowTransform(row);
|
|
36
|
-
return merge(newVar, cookieRow);
|
|
37
|
-
});
|
|
38
|
-
resolve(cookieRows);
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
if (parsedArgs.verbose) {
|
|
42
|
-
console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
|
|
43
|
-
}
|
|
44
|
-
resolve([rows1]);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
}
|