@mherod/get-cookie 1.2.0 → 2.0.0-beta.1
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/.idea/prettier.xml +8 -0
- package/dist/cli.js +3 -0
- package/dist/cli.js.map +1 -0
- package/dist/main.js +499 -0
- package/dist/main.js.map +1 -0
- package/package.json +38 -5
- package/src/browsers/AbstractCookieQueryStrategy.js +8 -0
- package/src/browsers/ChromeCookieQueryStrategy.js +263 -0
- package/src/browsers/CompositeCookieQueryStrategy.js +28 -0
- package/src/browsers/FirefoxCookieQueryStrategy.js +77 -0
- package/src/browsers/SafariCookieQueryStrategy.js +3 -0
- package/src/cli.js +70 -0
- package/src/findAllFiles.js +73 -0
- package/src/global.js +7 -0
- package/src/index.js +61 -0
- package/src/isValidJwt.js +17 -0
- package/src/queryCookies.js +28 -0
- package/src/utils.js +181 -0
- package/cli.js +0 -48
- package/index.js +0 -424
- package/utils.js +0 -175
package/src/global.js
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// noinspection JSUnusedGlobalSymbols
|
|
3
|
+
|
|
4
|
+
import { queryCookies } from "./queryCookies";
|
|
5
|
+
import FirefoxCookieQueryStrategy from "./browsers/FirefoxCookieQueryStrategy";
|
|
6
|
+
import ChromeCookieQueryStrategy from "./browsers/ChromeCookieQueryStrategy";
|
|
7
|
+
import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param params
|
|
12
|
+
* @returns {Promise<string>}
|
|
13
|
+
*/
|
|
14
|
+
export async function getCookie(params) {
|
|
15
|
+
const cookies = await queryCookies(
|
|
16
|
+
params,
|
|
17
|
+
new CompositeCookieQueryStrategy()
|
|
18
|
+
//
|
|
19
|
+
);
|
|
20
|
+
if (Array.isArray(cookies) && cookies.length > 0) {
|
|
21
|
+
return cookies.find((cookie) => cookie != null);
|
|
22
|
+
} else {
|
|
23
|
+
throw new Error("Cookie not found");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param params
|
|
30
|
+
* @returns {Promise<*>}
|
|
31
|
+
*/
|
|
32
|
+
export async function getFirefoxCookie(params) {
|
|
33
|
+
const cookies = await queryCookies(
|
|
34
|
+
params,
|
|
35
|
+
new FirefoxCookieQueryStrategy()
|
|
36
|
+
//
|
|
37
|
+
);
|
|
38
|
+
if (Array.isArray(cookies) && cookies.length > 0) {
|
|
39
|
+
return cookies.find((cookie) => cookie != null);
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error("Cookie not found");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
*
|
|
47
|
+
* @param params
|
|
48
|
+
* @returns {Promise<*>}
|
|
49
|
+
*/
|
|
50
|
+
export async function getChromeCookie(params) {
|
|
51
|
+
const cookies = await queryCookies(
|
|
52
|
+
params,
|
|
53
|
+
new ChromeCookieQueryStrategy()
|
|
54
|
+
//
|
|
55
|
+
);
|
|
56
|
+
if (Array.isArray(cookies) && cookies.length > 0) {
|
|
57
|
+
return cookies.find((cookie) => cookie != null);
|
|
58
|
+
} else {
|
|
59
|
+
throw new Error("Cookie not found");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import jsonwebtoken from "jsonwebtoken";
|
|
2
|
+
import { env } from "./global";
|
|
3
|
+
|
|
4
|
+
export default function isValidJwt(token) {
|
|
5
|
+
if (typeof token !== "string") {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
try {
|
|
9
|
+
const result = jsonwebtoken.decode(token, { complete: true });
|
|
10
|
+
if (env.VERBOSE) {
|
|
11
|
+
console.log(result);
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
} catch (err) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import CompositeCookieQueryStrategy from "./browsers/CompositeCookieQueryStrategy";
|
|
2
|
+
import { uniq } from "lodash";
|
|
3
|
+
import { env } from "./global";
|
|
4
|
+
import isValidJwt from "./isValidJwt";
|
|
5
|
+
|
|
6
|
+
export async function queryCookies(
|
|
7
|
+
{ name, domain },
|
|
8
|
+
strategy = new CompositeCookieQueryStrategy()
|
|
9
|
+
) {
|
|
10
|
+
const results = await strategy.queryCookies(name, domain);
|
|
11
|
+
const results1 = uniq(results).map((cookie) => {
|
|
12
|
+
return {
|
|
13
|
+
name: name,
|
|
14
|
+
domain: domain,
|
|
15
|
+
value: cookie,
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
const jwtCookies = [];
|
|
19
|
+
for (const result of results1) {
|
|
20
|
+
const value = result.value;
|
|
21
|
+
if (isValidJwt(value)) {
|
|
22
|
+
jwtCookies.push(result);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const results2 = env.REQUIRE_JWT ? jwtCookies : results1;
|
|
26
|
+
const resultsUniq = uniq(results2).map((cookie) => cookie.value);
|
|
27
|
+
return env.SINGLE ? [resultsUniq[0]] : resultsUniq;
|
|
28
|
+
}
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// noinspection JSUnusedGlobalSymbols
|
|
2
|
+
|
|
3
|
+
import { exec } from "child_process";
|
|
4
|
+
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
|
|
7
|
+
export async function execSimple(command) {
|
|
8
|
+
if (typeof command !== "string") {
|
|
9
|
+
throw new TypeError("execSimple: command must be a string");
|
|
10
|
+
}
|
|
11
|
+
if (process.env.VERBOSE) {
|
|
12
|
+
console.log(command);
|
|
13
|
+
}
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
exec(
|
|
16
|
+
command,
|
|
17
|
+
{ encoding: "binary", maxBuffer: 5 * 1024 },
|
|
18
|
+
(error, stdout, stderr) => {
|
|
19
|
+
if (error) {
|
|
20
|
+
reject(error);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (stderr) {
|
|
24
|
+
reject(error);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (stdout) {
|
|
28
|
+
resolve(stdout.toString("utf8").trim());
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function execAsBuffer(command) {
|
|
36
|
+
if (typeof command !== "string") {
|
|
37
|
+
throw new TypeError("execAsBuffer: command must be a string");
|
|
38
|
+
}
|
|
39
|
+
if (process.env.VERBOSE) {
|
|
40
|
+
console.log(command);
|
|
41
|
+
}
|
|
42
|
+
return await new Promise((resolve, reject) => {
|
|
43
|
+
exec(
|
|
44
|
+
command,
|
|
45
|
+
{ encoding: "binary", maxBuffer: 5 * 1024 },
|
|
46
|
+
(error, stdout, stderr) => {
|
|
47
|
+
if (error) {
|
|
48
|
+
reject(error);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (stderr) {
|
|
52
|
+
reject(error);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
let stdoutAsBuffer = stdout;
|
|
56
|
+
if (typeof stdoutAsBuffer === "string" && stdoutAsBuffer.length > 0) {
|
|
57
|
+
// noinspection JSCheckFunctionSignatures
|
|
58
|
+
stdoutAsBuffer = Buffer.from(stdoutAsBuffer, "binary").slice(0, -1);
|
|
59
|
+
}
|
|
60
|
+
if (stdoutAsBuffer && stdoutAsBuffer.length > 0) {
|
|
61
|
+
resolve(stdoutAsBuffer);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function toStringValue(r) {
|
|
69
|
+
if (process.env.VERBOSE) {
|
|
70
|
+
console.log("Printing value", r);
|
|
71
|
+
}
|
|
72
|
+
if (r) {
|
|
73
|
+
if (typeof r === "string") {
|
|
74
|
+
return r;
|
|
75
|
+
} else if (r.toString) {
|
|
76
|
+
// noinspection JSCheckFunctionSignatures
|
|
77
|
+
return r.toString("utf8");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function printStringValue(r) {
|
|
83
|
+
if (process.env.VERBOSE) {
|
|
84
|
+
console.log("Printing value", r);
|
|
85
|
+
}
|
|
86
|
+
if (r) {
|
|
87
|
+
if (typeof r === "string") {
|
|
88
|
+
console.log(r);
|
|
89
|
+
} else if (r.toString) {
|
|
90
|
+
// noinspection JSCheckFunctionSignatures
|
|
91
|
+
console.log(r.toString("utf8"));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
*
|
|
98
|
+
* @param result
|
|
99
|
+
* @returns {string|null}
|
|
100
|
+
*/
|
|
101
|
+
export function toStringOrNull(result) {
|
|
102
|
+
if (result == null) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
if (process.env.VERBOSE) {
|
|
106
|
+
console.log("result", result);
|
|
107
|
+
}
|
|
108
|
+
if (typeof result === "string" && result.length > 0) {
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
if (result.slice && result.toString) {
|
|
112
|
+
// noinspection JSCheckFunctionSignatures
|
|
113
|
+
return result.toString("utf8");
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
*
|
|
120
|
+
* @param {string} file
|
|
121
|
+
* @param {string} sql
|
|
122
|
+
* @returns {Promise<Buffer[]>}
|
|
123
|
+
*/
|
|
124
|
+
export async function doSqliteQuery1(file, sql) {
|
|
125
|
+
if (typeof sql !== "string") {
|
|
126
|
+
throw new TypeError("doSqliteQuery1: sql must be a string");
|
|
127
|
+
}
|
|
128
|
+
if (typeof file !== "string") {
|
|
129
|
+
throw new TypeError("doSqliteQuery1: file must be a string");
|
|
130
|
+
}
|
|
131
|
+
if (!fs.existsSync(file)) {
|
|
132
|
+
throw new Error(`doSqliteQuery1: file ${file} does not exist`);
|
|
133
|
+
}
|
|
134
|
+
if (process.env.VERBOSE) {
|
|
135
|
+
console.log(`doSqliteQuery1: file ${file}`);
|
|
136
|
+
console.log(`doSqliteQuery1: sql ${sql}`);
|
|
137
|
+
}
|
|
138
|
+
const sqlite3 = require("sqlite3");
|
|
139
|
+
const db = new sqlite3.Database(file);
|
|
140
|
+
return new Promise((resolve, reject) => {
|
|
141
|
+
db.all(sql, (err, rows) => {
|
|
142
|
+
if (err) {
|
|
143
|
+
if (process.env.VERBOSE) {
|
|
144
|
+
console.log(`doSqliteQuery1: error ${err}`);
|
|
145
|
+
}
|
|
146
|
+
reject(err);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const rows1 = rows;
|
|
150
|
+
if (rows1 == null || rows1.length === 0) {
|
|
151
|
+
if (process.env.VERBOSE) {
|
|
152
|
+
console.log(`doSqliteQuery1: no rows`);
|
|
153
|
+
}
|
|
154
|
+
resolve([]);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (Array.isArray(rows1)) {
|
|
158
|
+
if (process.env.VERBOSE) {
|
|
159
|
+
console.log(`doSqliteQuery1: ${rows1.length} rows`);
|
|
160
|
+
}
|
|
161
|
+
// noinspection JSCheckFunctionSignatures
|
|
162
|
+
const buffers = rows1
|
|
163
|
+
.flatMap((row) => Object.values(row))
|
|
164
|
+
.map((v) => Buffer.from(v, "binary"));
|
|
165
|
+
if (process.env.VERBOSE) {
|
|
166
|
+
console.log(`doSqliteQuery1: ${buffers.length} buffers`);
|
|
167
|
+
}
|
|
168
|
+
resolve(buffers);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
if (process.env.VERBOSE) {
|
|
172
|
+
console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
|
|
173
|
+
}
|
|
174
|
+
resolve([rows1]);
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function invalidString(input) {
|
|
180
|
+
return typeof input !== "string" || input.length === 0;
|
|
181
|
+
}
|
package/cli.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const {getCookie} = require("./index");
|
|
4
|
-
|
|
5
|
-
if (process.argv) {
|
|
6
|
-
if (process.argv.length > 2) {
|
|
7
|
-
const name = process.argv[2];
|
|
8
|
-
|
|
9
|
-
let domain;
|
|
10
|
-
if (process.argv[3] != null && process.argv[3].indexOf(".") > -1) {
|
|
11
|
-
domain = process.argv[3];
|
|
12
|
-
} else {
|
|
13
|
-
domain = '%';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
if (process.argv.includes("--require-jwt")) {
|
|
17
|
-
process.env.REQUIRE_JWT = "true";
|
|
18
|
-
}
|
|
19
|
-
if (process.argv.includes('--verbose')) {
|
|
20
|
-
process.env.VERBOSE = "true";
|
|
21
|
-
}
|
|
22
|
-
if (process.argv.includes("--chrome-only")) {
|
|
23
|
-
process.env.CHROME_ONLY = "true";
|
|
24
|
-
}
|
|
25
|
-
if (process.argv.includes("--firefox-only")) {
|
|
26
|
-
process.env.FIREFOX_ONLY = "true";
|
|
27
|
-
}
|
|
28
|
-
if (process.argv.includes("--ignore-expired")) {
|
|
29
|
-
process.env.IGNORE_EXPIRED = "true";
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (process.env.VERBOSE) {
|
|
33
|
-
console.log("Verbose mode", process.argv);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
getCookie({
|
|
37
|
-
name: name,
|
|
38
|
-
domain: domain,
|
|
39
|
-
requireJwt: (process.env.REQUIRE_JWT === "true"),
|
|
40
|
-
}).then(cookie => {
|
|
41
|
-
if (typeof cookie === "string") {
|
|
42
|
-
console.log(cookie);
|
|
43
|
-
}
|
|
44
|
-
}).catch(err => {
|
|
45
|
-
console.error(err);
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
}
|