From f02aef8d005e05f844a24f91e6689f918a583a71 Mon Sep 17 00:00:00 2001 From: lyc8503 Date: Mon, 14 Apr 2025 00:33:51 +0800 Subject: [PATCH] DurableObject remote checker, binding TODO --- worker/src/index.ts | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/worker/src/index.ts b/worker/src/index.ts index 8772302..74fc868 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -2,9 +2,11 @@ import { workerConfig } from '../../uptime.config' import { formatStatusChangeNotification, getWorkerLocation, notifyWithApprise } from './util' import { MonitorState, MonitorTarget } from '../../uptime.types' import { getStatus } from './monitor' +import { DurableObject } from 'cloudflare:workers' export interface Env { UPTIMEFLARE_STATE: KVNamespace + REMOTE_CHECKER_DO: DurableObjectNamespace } export default { @@ -77,16 +79,26 @@ export default { let status if (monitor.checkProxy) { - // Initiate a check using proxy (Geo-specific check) + // Initiate a check using proxy (Geo-specific monitoring) try { console.log('Calling check proxy: ' + monitor.checkProxy) - const resp = await ( - await fetch(monitor.checkProxy, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(monitor), + let resp + if (monitor.checkProxy.startsWith("worker://")) { + const doLoc = monitor.checkProxy.replace("worker://", "") + const doId = env.REMOTE_CHECKER_DO.idFromName(doLoc) + const doStub = env.REMOTE_CHECKER_DO.get(doId, { + locationHint: doLoc as DurableObjectLocationHint }) - ).json<{ location: string; status: { ping: number; up: boolean; err: string } }>() + resp = await doStub.getLocationAndStatus(monitor) + } else { + resp = await ( + await fetch(monitor.checkProxy, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(monitor), + }) + ).json<{location: string; status: {ping: number; up: boolean; err: string}}>() + } checkLocation = resp.location status = resp.status } catch (err) { @@ -299,3 +311,19 @@ export default { } }, } + +export class RemoteChecker extends DurableObject { + constructor(ctx: DurableObjectState, env: Env) { + super(ctx, env) + } + + async getLocationAndStatus(monitor: MonitorTarget): Promise<{location: string; status: {ping: number; up: boolean; err: string}}> { + const colo = await getWorkerLocation() as string + console.log(`Running remote checker (DurableObject) at ${colo}...`) + const status = await getStatus(monitor) + return { + location: colo, + status: status, + } + } +}