From 6a7effe66828e7bf89328a1797d14bd93fab511b Mon Sep 17 00:00:00 2001 From: lyc8503 Date: Fri, 11 Apr 2025 17:23:15 +0800 Subject: [PATCH] WIP: check proxy on pages --- next.config.js | 4 ++++ pages/api/check.ts | 19 +++++++++++++++++++ pages/api/data.ts | 6 +++--- worker/src/index.ts | 30 ------------------------------ worker/src/monitor.ts | 2 +- 5 files changed, 27 insertions(+), 34 deletions(-) create mode 100644 pages/api/check.ts diff --git a/next.config.js b/next.config.js index 42e9320..0da4b75 100644 --- a/next.config.js +++ b/next.config.js @@ -1,6 +1,10 @@ /** @type {import('next').NextConfig} */ const nextConfig = { reactStrictMode: true, + webpack: (config, { webpack }) => { + config.externals["cloudflare:sockets"] = "cloudflare:sockets" + return config + }, } module.exports = nextConfig diff --git a/pages/api/check.ts b/pages/api/check.ts new file mode 100644 index 0000000..09ba1a9 --- /dev/null +++ b/pages/api/check.ts @@ -0,0 +1,19 @@ +import { NextRequest } from "next/server" +import { getStatus } from "@/worker/src/monitor" +import { MonitorTarget } from "@/uptime.types" + +export const runtime = 'edge' + +// Check proxy if moved from Workers to Pages, https://github.com/lyc8503/UptimeFlare/issues/86#issuecomment-2655187257 +export default async function handler(req: NextRequest): Promise { + const target = await req.json() + const colo = req.headers.get("cf-ray")?.split("-")[1] + console.log("Check proxy running at " + colo + ", Target: " + target) + return new Response( + JSON.stringify({ + location: colo, + status: await getStatus(target), + }), + { headers: { "Content-Type": "application/json" } } + ) +} diff --git a/pages/api/data.ts b/pages/api/data.ts index eb1be94..6186e02 100644 --- a/pages/api/data.ts +++ b/pages/api/data.ts @@ -1,6 +1,6 @@ -import { workerConfig } from "@/uptime.config"; -import { MonitorState } from "@/uptime.types"; -import { NextRequest } from "next/server"; +import { workerConfig } from "@/uptime.config" +import { MonitorState } from "@/uptime.types" +import { NextRequest } from "next/server" export const runtime = 'edge' diff --git a/worker/src/index.ts b/worker/src/index.ts index 5878b46..8772302 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -8,36 +8,6 @@ export interface Env { } export default { - async fetch(request: Request): Promise { - const workerLocation = request.cf?.colo - console.log(`Handling request event at ${workerLocation}...`) - - if (request.method !== 'POST') { - return new Response('Remote worker is working...', { status: 405 }) - } - - const targetId = (await request.json<{ target: string }>())['target'] - const target = workerConfig.monitors.find((m) => m.id === targetId) - - if (target === undefined) { - return new Response('Target Not Found', { status: 404 }) - } - - const status = await getStatus(target) - - return new Response( - JSON.stringify({ - location: workerLocation, - status: status, - }), - { - headers: { - 'content-type': 'application/json;charset=UTF-8', - }, - } - ) - }, - async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise { const workerLocation = (await getWorkerLocation()) || 'ERROR' console.log(`Running scheduled event on ${workerLocation}...`) diff --git a/worker/src/monitor.ts b/worker/src/monitor.ts index d235850..7db3514 100644 --- a/worker/src/monitor.ts +++ b/worker/src/monitor.ts @@ -1,4 +1,3 @@ -import { connect } from "cloudflare:sockets"; import { MonitorTarget } from "../../uptime.types"; import { withTimeout, fetchTimeout } from "./util"; @@ -16,6 +15,7 @@ export async function getStatus( if (monitor.method === 'TCP_PING') { // TCP port endpoint monitor try { + const connect = await import(/* webpackIgnore: true */ "cloudflare:sockets").then((sockets) => sockets.connect) // This is not a real https connection, but we need to add a dummy `https://` to parse the hostname & port const parsed = new URL("https://" + monitor.target) const socket = connect({ hostname: parsed.hostname, port: Number(parsed.port) })