@mherod/get-cookie 1.3.0 → 2.0.0-beta.3

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/src/cli.js CHANGED
@@ -2,57 +2,89 @@
2
2
 
3
3
  import { version } from "../package.json";
4
4
  import { env } from "./global";
5
- const { getCookie } = require("./index");
6
-
7
- const argv = process.argv;
8
- if (argv) {
9
- if (argv.length > 2) {
10
- if (argv.includes("--version") || argv.includes("-v")) {
11
- console.log(version);
12
- return;
13
- }
5
+ import { queryCookies } from "./queryCookies";
6
+ import { argv } from "./argv";
7
+ import { groupBy, uniqBy } from "lodash";
8
+ import { blue, green } from "colorette";
14
9
 
15
- const name = argv[2];
10
+ function combinedString(results) {
11
+ return blue(
12
+ uniqBy(results, (r) => r.name)
13
+ .map((r) => r.name + "=" + r.value)
14
+ .join("; ")
15
+ );
16
+ }
16
17
 
17
- let domain;
18
- if (argv[3] != null && argv[3].indexOf(".") > -1) {
19
- domain = argv[3];
18
+ async function cliQueryCookies(name, domain) {
19
+ try {
20
+ const results = await queryCookies({ name, domain });
21
+ if (results.length > 0) {
22
+ if (argv.includes("--combined-string")) {
23
+ console.log(combinedString(results));
24
+ } else if (argv.includes("--dump")) {
25
+ console.log(results);
26
+ } else if (argv.includes("--dump-grouped")) {
27
+ const groupedByFile = groupBy(results, (r) => r.meta?.file);
28
+ console.log(green(JSON.stringify(groupedByFile, null, 2)));
29
+ } else if (argv.includes("--combined-string-grouped")) {
30
+ const groupedByFile = groupBy(results, (r) => r.meta?.file);
31
+ for (const file of Object.keys(groupedByFile)) {
32
+ let results = groupedByFile[file];
33
+ console.log(green(file) + ": ", combinedString(results));
34
+ }
35
+ } else {
36
+ for (const result of results) {
37
+ console.log(result.value);
38
+ }
39
+ }
20
40
  } else {
21
- domain = "%";
41
+ console.error("No results");
22
42
  }
43
+ } catch (e) {
44
+ console.error(e);
45
+ }
46
+ }
23
47
 
24
- if (argv.includes("--require-jwt")) {
25
- env.REQUIRE_JWT = "true";
26
- }
27
- if (argv.includes("--verbose")) {
28
- env.VERBOSE = "true";
29
- }
30
- if (argv.includes("--chrome-only")) {
31
- env.CHROME_ONLY = "true";
32
- }
33
- if (argv.includes("--firefox-only")) {
34
- env.FIREFOX_ONLY = "true";
35
- }
36
- if (argv.includes("--ignore-expired")) {
37
- env.IGNORE_EXPIRED = "true";
38
- }
48
+ if (argv && argv.length > 2) {
49
+ if (argv.includes("--version") || argv.includes("-v")) {
50
+ console.log(version);
51
+ return;
52
+ }
39
53
 
40
- if (env.VERBOSE) {
41
- console.log("Verbose mode", argv);
42
- }
54
+ const name = argv[2];
43
55
 
44
- getCookie({
45
- name: name,
46
- domain: domain,
47
- requireJwt: env.REQUIRE_JWT === "true",
48
- })
49
- .then((cookie) => {
50
- if (typeof cookie === "string") {
51
- console.log(cookie);
52
- }
53
- })
54
- .catch((err) => {
55
- console.error(err);
56
- });
56
+ let domain;
57
+ if (argv[3] != null && argv[3].indexOf(".") > -1) {
58
+ domain = argv[3];
59
+ } else {
60
+ domain = "%";
61
+ }
62
+
63
+ const tru = `${true}`;
64
+
65
+ if (argv.includes("--require-jwt")) {
66
+ env.REQUIRE_JWT = tru;
67
+ }
68
+ if (argv.includes("--verbose")) {
69
+ env.VERBOSE = tru;
70
+ }
71
+ if (argv.includes("--chrome-only")) {
72
+ env.CHROME_ONLY = tru;
73
+ }
74
+ if (argv.includes("--firefox-only")) {
75
+ env.FIREFOX_ONLY = tru;
57
76
  }
77
+ if (argv.includes("--ignore-expired")) {
78
+ env.IGNORE_EXPIRED = tru;
79
+ }
80
+
81
+ if (argv.includes("--single")) {
82
+ env.SINGLE = tru;
83
+ }
84
+
85
+ if (env.VERBOSE) {
86
+ console.log("Verbose mode", argv);
87
+ }
88
+
89
+ cliQueryCookies(name, domain).catch(console.error);
58
90
  }
@@ -0,0 +1,47 @@
1
+ import * as fs from "fs";
2
+ import * as sqlite3 from "sqlite3";
3
+ import {CookieRow} from "./CookieRow";
4
+
5
+ export async function doSqliteQuery1(file: string, sql: string): Promise<CookieRow[]> {
6
+ if (!fs.existsSync(file)) {
7
+ throw new Error(`doSqliteQuery1: file ${file} does not exist`);
8
+ }
9
+ const db = new sqlite3.Database(file);
10
+ return new Promise((resolve, reject) => {
11
+ db.all(sql, (err: Error, rows: any[]) => {
12
+ if (err) {
13
+ if (process.env.VERBOSE) {
14
+ console.log(`doSqliteQuery1: error ${err}`);
15
+ }
16
+ reject(err);
17
+ return;
18
+ }
19
+ const rows1: any[] = rows;
20
+ if (rows1 == null || rows1.length === 0) {
21
+ if (process.env.VERBOSE) {
22
+ console.log(`doSqliteQuery1: no rows`);
23
+ }
24
+ resolve([]);
25
+ return;
26
+ }
27
+ if (Array.isArray(rows1)) {
28
+ const cookieRows: CookieRow[] = rows1.map((row) => {
29
+ return {
30
+ domain: row["host_key"],
31
+ name: row["name"],
32
+ value: row["encrypted_value"],
33
+ meta: {
34
+ file: file
35
+ }
36
+ }
37
+ })
38
+ resolve(cookieRows);
39
+ return;
40
+ }
41
+ if (process.env.VERBOSE) {
42
+ console.log(`doSqliteQuery1: rows ${JSON.stringify(rows1)}`);
43
+ }
44
+ resolve([rows1]);
45
+ });
46
+ });
47
+ }
@@ -0,0 +1,68 @@
1
+ import { env } from "./global";
2
+ import * as fs from "fs";
3
+
4
+ export async function findAllFiles(
5
+ {
6
+ path,
7
+ name,
8
+ maxDepth = 2
9
+ //
10
+ }: {
11
+ path: string;
12
+ name: string;
13
+ maxDepth?: number;
14
+ }
15
+ ): Promise<string[]> {
16
+ const rootSegments = path.split("/").length;
17
+
18
+ if (env.VERBOSE) {
19
+ console.log(`Searching for ${name} in ${path}`);
20
+ }
21
+ const files: string[] = [];
22
+ let readdirSync;
23
+ try {
24
+ readdirSync = fs.readdirSync(path);
25
+ } catch (e) {
26
+ if (env.VERBOSE) {
27
+ console.log(`Error reading ${path}`, e);
28
+ }
29
+ return [];
30
+ }
31
+ for (const file of readdirSync) {
32
+ const filePath = path + "/" + file;
33
+ let stat;
34
+ try {
35
+ stat = fs.statSync(filePath);
36
+ } catch (e) {
37
+ if (env.VERBOSE) {
38
+ console.error(`Error getting stat for ${filePath}`, e);
39
+ }
40
+ continue;
41
+ }
42
+ if (stat.isDirectory()) {
43
+ if (filePath.split("/").length < rootSegments + maxDepth) {
44
+ try {
45
+ const subFiles = await findAllFiles({
46
+ path: filePath,
47
+ name: name,
48
+ maxDepth: 2
49
+ });
50
+ files.push(...subFiles);
51
+ } catch (e) {
52
+ if (env.VERBOSE) {
53
+ console.error(e);
54
+ }
55
+ }
56
+ }
57
+ } else if (file === name) {
58
+ files.push(filePath);
59
+ }
60
+ }
61
+ if (env.VERBOSE) {
62
+ if (files.length > 0) {
63
+ console.log(`Found ${files.length} ${name} files`);
64
+ console.log(files);
65
+ }
66
+ }
67
+ return files;
68
+ }
package/src/global.ts ADDED
@@ -0,0 +1,7 @@
1
+ const { merge } = require("lodash");
2
+ export const env: any = {};
3
+ merge(env, process.env);
4
+ export const HOME: string = env["HOME"];
5
+ if (!HOME) {
6
+ throw new Error("HOME environment variable is not set");
7
+ }