WIP: check proxy on pages

pull/106/head
lyc8503 2025-04-11 17:23:15 +08:00
parent 8b07dfe30e
commit 6a7effe668
5 changed files with 27 additions and 34 deletions

View File

@ -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

19
pages/api/check.ts Normal file
View File

@ -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<Response> {
const target = await req.json<MonitorTarget>()
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" } }
)
}

View File

@ -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'

View File

@ -8,36 +8,6 @@ export interface Env {
}
export default {
async fetch(request: Request): Promise<Response> {
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<void> {
const workerLocation = (await getWorkerLocation()) || 'ERROR'
console.log(`Running scheduled event on ${workerLocation}...`)

View File

@ -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) })