From b3404d18a1a46c3384ca53ec9332a5db0fa65639 Mon Sep 17 00:00:00 2001 From: kirinnee Date: Wed, 28 Feb 2024 20:00:55 +0800 Subject: [PATCH] feat: emulate rotating proxy --- Taskfile.yaml | 1 + src/domain/search_core.ts | 14 +++++++++++--- src/lib/populator.ts | 2 -- tasks/Taskfile.stop.yml | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 tasks/Taskfile.stop.yml diff --git a/Taskfile.yaml b/Taskfile.yaml index 09ead63..6545f48 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -13,6 +13,7 @@ includes: tear: tasks/Taskfile.tear.yml helm: Taskfile.helm.yml util: tasks/Taskfile.util.yml + stop: tasks/Taskfile.stop.yml tasks: setup: diff --git a/src/domain/search_core.ts b/src/domain/search_core.ts index c4322d1..19eae63 100644 --- a/src/domain/search_core.ts +++ b/src/domain/search_core.ts @@ -70,6 +70,14 @@ class SearchCore { this.#logger = logger; } + get proxy(): string | undefined { + const p = this.#config.proxy; + if (p == null) return undefined; + const arr = p.split(";"); + const randomIndex = Math.floor(Math.random() * arr.length); + return arr[randomIndex]; + } + async mainKTMBPage(): Promise { const referer = "https://online.ktmb.com.my/"; const resp = await f("https://shuttleonline.ktmb.com.my/Home/Shuttle", { @@ -78,7 +86,7 @@ class SearchCore { ...htmlHeaders, Referer: referer, }, - proxy: this.#config.proxy, + proxy: this.proxy, method: "GET", }); const text = await resp.text(); @@ -131,7 +139,7 @@ class SearchCore { ...defaultHeaders, Referer: referer, }, - proxy: this.#config.proxy, + proxy: this.proxy, body: stringify(queryParams), method: "POST", }); @@ -166,7 +174,7 @@ class SearchCore { RequestVerificationToken: token, Referer: referer, }, - proxy: this.#config.proxy, + proxy: this.proxy, body: JSON.stringify({ SearchData: searchData, FormValidationCode: formValidation, diff --git a/src/lib/populator.ts b/src/lib/populator.ts index f75f4a9..fb91c70 100644 --- a/src/lib/populator.ts +++ b/src/lib/populator.ts @@ -2,7 +2,6 @@ import { Logger } from "pino"; import { SearcherBuilder } from "../domain/searcher/builder.ts"; import { addDays, differenceInDays } from "date-fns"; import { RetrieveResult } from "./interfaces.ts"; -import { __ } from "../utility.ts"; class Populator { constructor( @@ -44,7 +43,6 @@ class Populator { this.logger.error({ error: e }, "Failed to retrieve schedule"); break; } - await __(2); } return ret; } diff --git a/tasks/Taskfile.stop.yml b/tasks/Taskfile.stop.yml new file mode 100644 index 0000000..c5e6de3 --- /dev/null +++ b/tasks/Taskfile.stop.yml @@ -0,0 +1,16 @@ +version: "3" + +tasks: + default: + desc: Stop all everything + cmds: + - task: dev + - task: test + dev: + desc: Stop the development workflow + cmds: + - tilt down + test: + desc: Stop the test workflow + cmds: + - tilt down -- --config config/test.yaml --action test