2025-04-13 00:31:57 +07:00
|
|
|
import { devices, type BrowserContext, type Locator } from "patchright";
|
2025-04-12 19:38:54 +07:00
|
|
|
import logger from "../logger";
|
|
|
|
|
import * as twitch from "../website/twitch";
|
|
|
|
|
import * as constants from "../constants";
|
|
|
|
|
|
2025-04-13 00:43:55 +07:00
|
|
|
async function spawn(context: BrowserContext, targetUrl: string, changeViewport: boolean = false, spawnId: string = "unknown") {
|
2025-04-12 19:38:54 +07:00
|
|
|
const server = constants.REFLECT4_SERVERS[Math.floor(Math.random() * constants.REFLECT4_SERVERS.length)];
|
|
|
|
|
logger.debug(`[${spawnId}] Using reflect4 server: ${server}`);
|
|
|
|
|
const page = await context.newPage();
|
2025-04-13 00:43:55 +07:00
|
|
|
if (changeViewport) {
|
|
|
|
|
logger.debug(`[${spawnId}] Changing viewport size...`);
|
|
|
|
|
const deviceName = Object.keys(devices)[Math.floor(Math.random() * Object.keys(devices).length)];
|
|
|
|
|
const device = devices[deviceName];
|
|
|
|
|
logger.debug(`[${spawnId}] Using device: ${deviceName}`);
|
|
|
|
|
await page.setViewportSize(device.viewport);
|
|
|
|
|
}
|
2025-04-12 21:58:19 +07:00
|
|
|
try {
|
|
|
|
|
await page.goto(server);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
logger.error(`[${spawnId}] Error while navigating to proxy website: ${e}`);
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
2025-04-12 19:38:54 +07:00
|
|
|
let targetInput: Locator | null = null;
|
|
|
|
|
const allInput = await page.locator("input").all();
|
|
|
|
|
for (const input of allInput) {
|
|
|
|
|
const placeholder = await input.getAttribute("placeholder");
|
|
|
|
|
if (placeholder?.includes("URL")) {
|
|
|
|
|
targetInput = input;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!targetInput) {
|
|
|
|
|
logger.error(`[${spawnId}] Failed to find input field for URL input`);
|
2025-04-12 21:58:19 +07:00
|
|
|
throw new Error(`Failed to find input field for URL input`);
|
2025-04-12 19:38:54 +07:00
|
|
|
}
|
|
|
|
|
await targetInput.fill(targetUrl);
|
|
|
|
|
await targetInput.press("Enter");
|
2025-04-12 21:48:25 +07:00
|
|
|
logger.info(`[${spawnId}] Navigating to ${targetUrl}`);
|
|
|
|
|
await page.waitForTimeout(15000); // Wait for 15 second to let the page load
|
2025-04-12 19:38:54 +07:00
|
|
|
// Keep-alive the page open for 5 minutes then refresh
|
|
|
|
|
if (targetUrl.startsWith("https://www.twitch.tv/")) {
|
2025-04-13 00:34:57 +07:00
|
|
|
logger.info(`[${spawnId}] Twitch URL detected, using Twitch mode...`);
|
2025-04-12 21:46:26 +07:00
|
|
|
await twitch.keepAlive(page, spawnId);
|
2025-04-12 19:38:54 +07:00
|
|
|
} else {
|
|
|
|
|
logger.warn(`[${spawnId}] Unsupported URL: ${targetUrl}`);
|
|
|
|
|
logger.warn(`[${spawnId}] Will try to keep alive, but no guarantees`);
|
|
|
|
|
while (true) {
|
|
|
|
|
await page.waitForTimeout(5 * 60 * 1000); // 5 minutes
|
|
|
|
|
await page.reload();
|
|
|
|
|
logger.debug(`[${spawnId}] Reloaded page`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
await page.close();
|
|
|
|
|
logger.info(`[${spawnId}] Proxy with the server ${server} closed`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { spawn };
|