From 8b07dfe30ebdd4f88fd04d1adfbc55aef8f83258 Mon Sep 17 00:00:00 2001 From: lyc8503 Date: Fri, 11 Apr 2025 01:29:46 +0800 Subject: [PATCH] SHOULD WORK: worker support proxy --- uptime.config.ts | 8 +++++--- uptime.types.ts | 3 ++- worker/src/index.ts | 22 +++++++++++++--------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/uptime.config.ts b/uptime.config.ts index 321dcf3..de1b564 100644 --- a/uptime.config.ts +++ b/uptime.config.ts @@ -54,9 +54,11 @@ const workerConfig = { responseKeyword: 'success', // [OPTIONAL] if specified, the response must NOT contains the keyword to be considered as operational. responseForbiddenKeyword: 'bad gateway', - // [OPTIONAL] if specified, the check will run in your specified region, - // refer to docs https://github.com/lyc8503/UptimeFlare/wiki/Geo-specific-checks-setup before setting this value - checkLocationWorkerRoute: 'https://xxx.example.com', + // [OPTIONAL] if specified, will call the check proxy to check the monitor, mainly for geo-specific checks + // refer to docs **TODO** before setting this value + checkProxy: 'https://xxx.example.com', + // [OPTIONAL] if true, the check will fallback to local if the specified proxy is down + checkProxyFallback: true, }, // Example TCP Monitor { diff --git a/uptime.types.ts b/uptime.types.ts index 73cf3cc..f287622 100644 --- a/uptime.types.ts +++ b/uptime.types.ts @@ -35,8 +35,9 @@ type MonitorTarget = { target: string // url for http, hostname:port for tcp tooltip?: string statusPageLink?: string - checkLocationWorkerRoute?: string hideLatencyChart?: boolean + checkProxy?: string + checkProxyFallback?: boolean // HTTP Code expectedCodes?: number[] diff --git a/worker/src/index.ts b/worker/src/index.ts index 76ff581..5878b46 100644 --- a/worker/src/index.ts +++ b/worker/src/index.ts @@ -106,23 +106,27 @@ export default { let checkLocation = workerLocation let status - if (monitor.checkLocationWorkerRoute) { - // Initiate a check from a different location + if (monitor.checkProxy) { + // Initiate a check using proxy (Geo-specific check) try { - console.log('Calling worker: ' + monitor.checkLocationWorkerRoute) + console.log('Calling check proxy: ' + monitor.checkProxy) const resp = await ( - await fetch(monitor.checkLocationWorkerRoute, { + await fetch(monitor.checkProxy, { method: 'POST', - body: JSON.stringify({ - target: monitor.id, - }), + 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) { - console.log('Error calling worker: ' + err) - status = { ping: 0, up: false, err: 'Error initiating check from remote worker' } + console.log('Error calling proxy: ' + err) + if (monitor.checkProxyFallback) { + console.log('Falling back to local check...') + status = await getStatus(monitor) + } else { + status = { ping: 0, up: false, err: 'Error initiating check from remote worker' } + } } } else { // Initiate a check from the current location