Skip to content

Commit

Permalink
feat: locally caching of booking sessions
Browse files Browse the repository at this point in the history
Allows for the ability to resume a session without the need to BankID identification if not yet expired.
  • Loading branch information
kalkih committed Mar 19, 2024
1 parent 2b82dff commit 42e14bd
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dist
build
.*.log
proxies.txt
.config.local.json
.config.local.json
.session.json
82 changes: 68 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@types/node": "^17.0.23",
"@types/node-fetch": "^2.6.1",
"@types/qrcode-terminal": "^0.12.0",
"@types/tough-cookie": "^4.0.5",
"@types/yargs": "^17.0.10",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
Expand All @@ -45,6 +46,7 @@
"https-proxy-agent": "^5.0.0",
"node-fetch": "^2.6.7",
"qrcode-terminal": "^0.12.0",
"tough-cookie": "^4.1.3",
"winston": "^3.6.0",
"yargs": "^17.4.0"
},
Expand Down
41 changes: 41 additions & 0 deletions src/configuration/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from "fs";

import { logger } from "../logger";
import { getPath } from "../utils";

interface SessionData {
cookie: string;
hash: string;
}

export const saveSession = (hash: string, cookie: string) => {
try {
const configPath = getPath("./.session.json");

if (configPath) {
fs.writeFileSync(configPath, JSON.stringify({ cookie, hash }), "utf-8");
}
} catch (error) {
logger.verbose(`Failed to save session: ${error}`);
}
};

export const getSession = (hash: string) => {
try {
const sessionPath = getPath("./.session.json");
const data: SessionData = JSON.parse(
fs.readFileSync(sessionPath).toString()
);

if (data.hash !== hash) {
logger.verbose(
"Session hash mismatch, ignoring recovering from last session"
);
return;
}

return data.cookie;
} catch (error) {
// quiet, there might be no session...
}
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Location, readConfig, validateConfig } from "./configuration";
import { BookingService } from "./services/bookingService/bookingService";
import {
addDays,
generateHash,
getMaxDate,
getShortDate,
getStartOfWeekDate,
Expand Down Expand Up @@ -55,6 +56,7 @@ async function init(locations: Location[], date: Date) {
useProxy: config.useProxies,
proxyTimeout: config.proxyTimeout,
proxyRetries: config.proxyRetries,
hash: generateHash(JSON.stringify(config)),
};

const bookingService = config.booking_number
Expand Down
Loading

0 comments on commit 42e14bd

Please sign in to comment.