misc: run prettier

pull/106/head
lyc8503 2025-04-19 21:50:43 +08:00
parent 751373d5dd
commit 4bcf4173ea
22 changed files with 402 additions and 302 deletions

View File

@ -1,8 +1,8 @@
name: 'issue-translator'
on:
issue_comment:
on:
issue_comment:
types: [created]
issues:
issues:
types: [opened]
jobs:

View File

@ -37,7 +37,7 @@ jobs:
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Fetch latest code
git clone https://github.com/lyc8503/UptimeFlare /tmp/latest
rm -rf /tmp/latest/.git
@ -49,8 +49,8 @@ jobs:
git add .
git commit -m "Sync latest code from upstream"
git push
- name: Trigger deployment
uses: benc-uk/workflow-dispatch@v1
with:
workflow: deploy.yml
workflow: deploy.yml

View File

@ -2,4 +2,4 @@ trailingComma: 'es5'
tabWidth: 2
semi: false
singleQuote: true
printWidth: 100
printWidth: 100

View File

@ -8,6 +8,7 @@
A more advanced, serverless, and free uptime monitoring & status page solution, powered by Cloudflare Workers, complete with a user-friendly interface.
## ⭐Features
- Open-source, easy to deploy (in under 10 minutes, no local tools required), and free
- Monitoring capabilities
- Up to 50 checks at 1-minute intervals
@ -52,7 +53,7 @@ Please refer to [Wiki](https://github.com/lyc8503/UptimeFlare/wiki)
- [x] ~~Self-host Dockerfile~~
- [x] Incident history
- [x] Improve `checkLocationWorkerRoute` and fix possible `proxy failed`
- [x] Groups
- [x] Groups
- [x] Remove old incidents
- [x] ~~Known issue~~: `fetch` doesn't support non-standard port (resolved after CF update)
- [ ] Compatibility date update

View File

@ -8,6 +8,7 @@
一个由 Cloudflare Workers 驱动的功能丰富、Serverless 且免费的 Uptime 监控及状态页面。
## ⭐功能
- 开源,易于部署(全程无需本地工具,耗时不到 10 分钟),且完全免费
- 监控功能
- 最多支持 50 个 1 分钟精度的检查

View File

@ -50,13 +50,20 @@ export default function DetailBar({
if (overlap > 0) {
for (let i = 0; i < incident.error.length; i++) {
let partStart = incident.start[i]
let partEnd = i === incident.error.length - 1 ? (incident.end ?? currentTime) : incident.start[i + 1]
let partEnd =
i === incident.error.length - 1 ? incident.end ?? currentTime : incident.start[i + 1]
partStart = Math.max(partStart, dayStart)
partEnd = Math.min(partEnd, dayEnd)
if (overlapLen(dayStart, dayEnd, partStart, partEnd) > 0) {
const startStr = new Date(partStart * 1000).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})
const endStr = new Date(partEnd * 1000).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})
const startStr = new Date(partStart * 1000).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})
const endStr = new Date(partEnd * 1000).toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})
incidentReasons.push(`[${startStr}-${endStr}] ${incident.error[i]}`)
}
}
@ -77,7 +84,10 @@ export default function DetailBar({
<>
<div>{dayPercent + '% at ' + new Date(dayStart * 1000).toLocaleDateString()}</div>
{dayDownTime > 0 && (
<div>{`Down for ${moment.preciseDiff(moment(0), moment(dayDownTime * 1000))} (click for detail)`}</div>
<div>{`Down for ${moment.preciseDiff(
moment(0),
moment(dayDownTime * 1000)
)} (click for detail)`}</div>
)}
</>
)
@ -94,10 +104,14 @@ export default function DetailBar({
}}
onClick={() => {
if (dayDownTime > 0) {
setModalTitle(`🚨 ${monitor.name} incidents at ${new Date(dayStart * 1000).toLocaleDateString()}`)
setModalTitle(
`🚨 ${monitor.name} incidents at ${new Date(dayStart * 1000).toLocaleDateString()}`
)
setModelContent(
<>
{incidentReasons.map((reason, index) => (<div key={index}>{reason}</div>))}
{incidentReasons.map((reason, index) => (
<div key={index}>{reason}</div>
))}
</>
)
setModalOpened(true)
@ -110,7 +124,12 @@ export default function DetailBar({
return (
<>
<Modal opened={modalOpened} onClose={() => setModalOpened(false)} title={modalTitle} size={'40em'}>
<Modal
opened={modalOpened}
onClose={() => setModalOpened(false)}
title={modalTitle}
size={'40em'}
>
{modelContent}
</Modal>
<Box

View File

@ -44,7 +44,11 @@ export default function MonitorDetail({
const monitorNameElement = (
<Text mt="sm" fw={700} style={{ display: 'inline-flex', alignItems: 'center' }}>
{monitor.statusPageLink ? (
<a href={monitor.statusPageLink} target="_blank" style={{ display: 'inline-flex', alignItems: 'center', color: 'inherit' }}>
<a
href={monitor.statusPageLink}
target="_blank"
style={{ display: 'inline-flex', alignItems: 'center', color: 'inherit' }}
>
{statusIcon} {monitor.name}
</a>
) : (

View File

@ -1,7 +1,7 @@
import { MonitorState, MonitorTarget } from '@/uptime.types'
import { Accordion, Card, Center, Text } from '@mantine/core'
import MonitorDetail from './MonitorDetail'
import { pageConfig } from '@/uptime.config';
import { pageConfig } from '@/uptime.config'
function countDownCount(state: MonitorState, ids: string[]) {
let downCount = 0
@ -28,45 +28,61 @@ function getStatusTextColor(state: MonitorState, ids: string[]) {
}
}
export default function MonitorList({ monitors, state }: { monitors: MonitorTarget[]; state: MonitorState }) {
export default function MonitorList({
monitors,
state,
}: {
monitors: MonitorTarget[]
state: MonitorState
}) {
// @ts-ignore
let group: any = pageConfig.group
let groupedMonitor = group && Object.keys(group).length > 0
let content
if (groupedMonitor) {
// Grouped monitors
content = (
<Accordion multiple defaultValue={Object.keys(group)} variant='contained'>
{
Object.keys(group).map(groupName => (
<Accordion.Item key={groupName} value={groupName}>
<Accordion.Control>
<div style={{ display: 'flex', justifyContent: 'space-between', width: '100%', alignItems: 'center' }}>
<div>{groupName}</div>
<Text fw={500} style={{ display: 'inline', paddingRight: '5px', color: getStatusTextColor(state, group[groupName])}}>
{group[groupName].length - countDownCount(state, group[groupName])}
/{group[groupName].length} Operational
</Text>
</div>
</Accordion.Control>
<Accordion.Panel>
{
monitors
.filter(monitor => group[groupName].includes(monitor.id))
.sort((a, b) => group[groupName].indexOf(a.id) - group[groupName].indexOf(b.id))
.map(monitor => (
<div key={monitor.id}>
<Card.Section ml="xs" mr="xs">
<MonitorDetail monitor={monitor} state={state} />
</Card.Section>
</div>
))
}
</Accordion.Panel>
</Accordion.Item>
))
}
<Accordion multiple defaultValue={Object.keys(group)} variant="contained">
{Object.keys(group).map((groupName) => (
<Accordion.Item key={groupName} value={groupName}>
<Accordion.Control>
<div
style={{
display: 'flex',
justifyContent: 'space-between',
width: '100%',
alignItems: 'center',
}}
>
<div>{groupName}</div>
<Text
fw={500}
style={{
display: 'inline',
paddingRight: '5px',
color: getStatusTextColor(state, group[groupName]),
}}
>
{group[groupName].length - countDownCount(state, group[groupName])}/
{group[groupName].length} Operational
</Text>
</div>
</Accordion.Control>
<Accordion.Panel>
{monitors
.filter((monitor) => group[groupName].includes(monitor.id))
.sort((a, b) => group[groupName].indexOf(a.id) - group[groupName].indexOf(b.id))
.map((monitor) => (
<div key={monitor.id}>
<Card.Section ml="xs" mr="xs">
<MonitorDetail monitor={monitor} state={state} />
</Card.Section>
</div>
))}
</Accordion.Panel>
</Accordion.Item>
))}
</Accordion>
)
} else {

View File

@ -1,24 +1,24 @@
import { Center, Title } from '@mantine/core'
import { IconCircleCheck, IconAlertCircle } from '@tabler/icons-react'
import { useEffect, useState } from 'react';
import { useEffect, useState } from 'react'
function useWindowVisibility() {
const [isVisible, setIsVisible] = useState(true);
const [isVisible, setIsVisible] = useState(true)
useEffect(() => {
const handleVisibilityChange = () => {
console.log('visibility change', document.visibilityState);
setIsVisible(document.visibilityState === 'visible');
};
console.log('visibility change', document.visibilityState)
setIsVisible(document.visibilityState === 'visible')
}
document.addEventListener('visibilitychange', handleVisibilityChange);
document.addEventListener('visibilitychange', handleVisibilityChange)
return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
}, []);
document.removeEventListener('visibilitychange', handleVisibilityChange)
}
}, [])
return isVisible;
return isVisible
}
export default function OverallStatus({
@ -36,12 +36,14 @@ export default function OverallStatus({
statusString = 'All systems operational'
icon = <IconCircleCheck style={{ width: 64, height: 64, color: '#059669' }} />
} else {
statusString = `Some systems not operational (${state.overallDown} out of ${state.overallUp + state.overallDown})`
statusString = `Some systems not operational (${state.overallDown} out of ${
state.overallUp + state.overallDown
})`
}
const [openTime] = useState(Math.round(Date.now() / 1000))
const [currentTime, setCurrentTime] = useState(Math.round(Date.now() / 1000))
const isWindowVisible = useWindowVisibility();
const isWindowVisible = useWindowVisibility()
useEffect(() => {
const interval = setInterval(() => {
@ -66,7 +68,9 @@ export default function OverallStatus({
</Title>
<Title mt="sm" style={{ textAlign: 'center', color: '#70778c' }} order={5}>
Last updated on:{' '}
{`${new Date(state.lastUpdate * 1000).toLocaleString()} (${currentTime - state.lastUpdate} sec ago)`}
{`${new Date(state.lastUpdate * 1000).toLocaleString()} (${
currentTime - state.lastUpdate
} sec ago)`}
</Title>
</>
)

12
env.d.ts vendored
View File

@ -1,9 +1,9 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
UPTIMEFLARE_STATE: KVNamespace;
}
}
namespace NodeJS {
interface ProcessEnv {
UPTIMEFLARE_STATE: KVNamespace
}
}
}
export {};
export {}

View File

@ -1,7 +1,7 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { workerConfig } from './uptime.config'
export async function middleware(request: NextRequest) {
// @ts-ignore
const passwordProtection = workerConfig.passwordProtection
@ -12,15 +12,16 @@ export async function middleware(request: NextRequest) {
if (authHeader && authHeader.length === expected.length) {
// a simple timing-safe compare
authenticated = true;
authenticated = true
for (let i = 0; i < authHeader.length; i++) {
if (authHeader[i] !== expected[i]) authenticated = false;
if (authHeader[i] !== expected[i]) authenticated = false
}
}
if (!authenticated) {
return NextResponse.json(
{ code: 401, message: "Not authenticated" }, { status: 401, headers: { 'WWW-Authenticate': 'Basic' } }
{ code: 401, message: 'Not authenticated' },
{ status: 401, headers: { 'WWW-Authenticate': 'Basic' } }
)
}
}

View File

@ -1,6 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true
reactStrictMode: true,
}
module.exports = nextConfig
@ -11,8 +11,8 @@ if (process.env.NODE_ENV === 'development') {
bindings: {
UPTIMEFLARE_STATE: {
type: 'kv',
id: 'UPTIMEFLARE_STATE'
}
}
id: 'UPTIMEFLARE_STATE',
},
},
})
}

View File

@ -1,11 +1,10 @@
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'
export default async function handler(req: NextRequest): Promise<Response> {
const { UPTIMEFLARE_STATE } = process.env as unknown as {
UPTIMEFLARE_STATE: KVNamespace
}
@ -15,11 +14,11 @@ export default async function handler(req: NextRequest): Promise<Response> {
return new Response(JSON.stringify({ error: 'No data available' }), {
status: 500,
headers: {
'Content-Type': 'application/json'
}
'Content-Type': 'application/json',
},
})
}
const state = (JSON.parse(stateStr)) as unknown as MonitorState
const state = JSON.parse(stateStr) as unknown as MonitorState
let monitors: any = {}
@ -29,7 +28,7 @@ export default async function handler(req: NextRequest): Promise<Response> {
up: isUp,
latency: state.latency[monitor.id].recent.slice(-1)[0].ping,
location: state.latency[monitor.id].recent.slice(-1)[0].loc,
message: isUp ? "OK" : state.incident[monitor.id].slice(-1)[0].error.slice(-1)[0]
message: isUp ? 'OK' : state.incident[monitor.id].slice(-1)[0].error.slice(-1)[0],
}
}
@ -37,12 +36,12 @@ export default async function handler(req: NextRequest): Promise<Response> {
up: state.overallUp,
down: state.overallDown,
updatedAt: state.lastUpdate,
monitors: monitors
monitors: monitors,
}
return new Response(JSON.stringify(ret), {
headers: {
'Content-Type': 'application/json'
}
'Content-Type': 'application/json',
},
})
}
}

View File

@ -22,21 +22,17 @@ export default function Home({
tooltip?: string
statusPageLink?: string
}) {
let state;
let state
if (stateStr !== undefined) {
state = JSON.parse(stateStr) as MonitorState
}
// Specify monitorId in URL hash to view a specific monitor (can be used in iframe)
const monitorId = window.location.hash.substring(1);
const monitorId = window.location.hash.substring(1)
if (monitorId) {
const monitor = monitors.find((monitor) => monitor.id === monitorId);
const monitor = monitors.find((monitor) => monitor.id === monitorId)
if (!monitor || !state) {
return (
<Text fw={700}>
Monitor with id {monitorId} not found!
</Text>
)
return <Text fw={700}>Monitor with id {monitorId} not found!</Text>
}
return (
<div style={{ maxWidth: '810px' }}>
@ -70,9 +66,14 @@ export default function Home({
)}
<Divider mt="lg" />
<Text size="xs" mt="xs" mb="xs" style={{
textAlign: 'center'
}}>
<Text
size="xs"
mt="xs"
mb="xs"
style={{
textAlign: 'center',
}}
>
Open-source monitoring and status page powered by{' '}
<a href="https://github.com/lyc8503/UptimeFlare" target="_blank">
Uptimeflare

View File

@ -1,7 +1,7 @@
// This is a Node.js implementation of status monitoring
const location = ''
const defaultTimeout = 5000 // 5 seconds, a lower default for deployments on platforms like Vercel
const defaultTimeout = 5000 // 5 seconds, a lower default for deployments on platforms like Vercel
const express = require('express')
const net = require('net')
@ -39,31 +39,30 @@ async function getStatus(monitor) {
try {
// This is not a real https connection, but we need to add a dummy `https://` to parse the hostname & port
// TODO: ipv6 buggy
const parsed = new URL("https://" + monitor.target)
const parsed = new URL('https://' + monitor.target)
host = parsed.hostname
port = parsed.port
await new Promise((resolve, reject) => {
const socket = net.createConnection({ host: host, port: Number(port) })
const socket = net.createConnection({ host: host, port: Number(port) })
const timer = setTimeout(() => {
socket.destroy()
reject(new Error(`Timeout after ${monitor.timeout || defaultTimeout}ms`))
}, monitor.timeout || defaultTimeout)
socket.on('connect', () => {
clearTimeout(timer)
socket.end()
resolve(null)
})
const timer = setTimeout(() => {
socket.destroy()
reject(new Error(`Timeout after ${monitor.timeout || defaultTimeout}ms`))
}, monitor.timeout || defaultTimeout)
socket.on('error', (err) => {
clearTimeout(timer)
socket.destroy()
reject(err)
})
}
)
socket.on('connect', () => {
clearTimeout(timer)
socket.end()
resolve(null)
})
socket.on('error', (err) => {
clearTimeout(timer)
socket.destroy()
reject(err)
})
})
status.up = true
status.err = ''
@ -90,8 +89,9 @@ async function getStatus(monitor) {
if (!monitor.expectedCodes.includes(response.status)) {
console.log(`${monitor.name} expected ${monitor.expectedCodes}, got ${response.status}`)
status.up = false
status.err = `Expected codes: ${JSON.stringify(monitor.expectedCodes)}, Got: ${response.status
}`
status.err = `Expected codes: ${JSON.stringify(monitor.expectedCodes)}, Got: ${
response.status
}`
return status
}
} else {
@ -109,17 +109,28 @@ async function getStatus(monitor) {
// MUST contain responseKeyword
if (monitor.responseKeyword && !responseBody.includes(monitor.responseKeyword)) {
console.log(`${monitor.name} expected keyword ${monitor.responseKeyword}, not found in response (truncated to 100 chars): ${responseBody.slice(0, 100)}`)
console.log(
`${monitor.name} expected keyword ${
monitor.responseKeyword
}, not found in response (truncated to 100 chars): ${responseBody.slice(0, 100)}`
)
status.up = false
status.err = "HTTP response doesn't contain the configured keyword"
return status
}
// MUST NOT contain responseForbiddenKeyword
if (monitor.responseForbiddenKeyword && responseBody.includes(monitor.responseForbiddenKeyword)) {
console.log(`${monitor.name} forbidden keyword ${monitor.responseForbiddenKeyword}, found in response (truncated to 100 chars): ${responseBody.slice(0, 100)}`)
if (
monitor.responseForbiddenKeyword &&
responseBody.includes(monitor.responseForbiddenKeyword)
) {
console.log(
`${monitor.name} forbidden keyword ${
monitor.responseForbiddenKeyword
}, found in response (truncated to 100 chars): ${responseBody.slice(0, 100)}`
)
status.up = false
status.err = "HTTP response contains the configured forbidden keyword"
status.err = 'HTTP response contains the configured forbidden keyword'
return status
}
}
@ -142,19 +153,17 @@ async function getStatus(monitor) {
return status
}
app.use(express.json());
app.use(express.json())
app.post('/', async (req, res) => {
res.json(
{
location: await getWorkerLocation(),
status: await getStatus(req.body),
}
)
res.json({
location: await getWorkerLocation(),
status: await getStatus(req.body),
})
})
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})
module.exports = app;
module.exports = app

View File

@ -1 +1 @@
{ "version": 2, "rewrites": [{ "source": "/(.*)", "destination": "/api" }] }
{ "version": 2, "rewrites": [{ "source": "/(.*)", "destination": "/api" }] }

View File

@ -11,8 +11,8 @@ const pageConfig = {
// If not specified, all monitors will be shown in a single list
// If specified, monitors will be grouped and ordered, not-listed monitors will be invisble (but still monitored)
group: {
"🌐 Public (example group name)": ['foo_monitor', 'bar_monitor', 'more monitor ids...'],
"🔐 Private": ['test_tcp_monitor'],
'🌐 Public (example group name)': ['foo_monitor', 'bar_monitor', 'more monitor ids...'],
'🔐 Private': ['test_tcp_monitor'],
},
}
@ -77,12 +77,12 @@ const workerConfig = {
notification: {
// [Optional] apprise API server URL
// if not specified, no notification will be sent
appriseApiServer: "https://apprise.example.com/notify",
appriseApiServer: 'https://apprise.example.com/notify',
// [Optional] recipient URL for apprise, refer to https://github.com/caronc/apprise
// if not specified, no notification will be sent
recipientUrl: "tgram://bottoken/ChatID",
recipientUrl: 'tgram://bottoken/ChatID',
// [Optional] timezone used in notification messages, default to "Etc/GMT"
timeZone: "Asia/Shanghai",
timeZone: 'Asia/Shanghai',
// [Optional] grace period in minutes before sending a notification
// notification will be sent only if the monitor is down for N continuous checks after the initial failure
// if not specified, notification will be sent immediately
@ -101,7 +101,6 @@ const workerConfig = {
) => {
// This callback will be called when there's a status change for any monitor
// Write any Typescript code here
// This will not follow the grace period settings and will be called immediately when the status changes
// You need to handle the grace period manually if you want to implement it
},

View File

@ -2,4 +2,4 @@ trailingComma: 'es5'
tabWidth: 2
semi: false
singleQuote: true
printWidth: 100
printWidth: 100

View File

@ -26,7 +26,9 @@ export default {
// @ts-ignore
const skipList: string[] = workerConfig.notification?.skipNotificationIds
if (skipList && skipList.includes(monitor.id)) {
console.log(`Skipping notification for ${monitor.name} (${monitor.id} in skipNotificationIds)`)
console.log(
`Skipping notification for ${monitor.name} (${monitor.id} in skipNotificationIds)`
)
return
}
@ -46,7 +48,9 @@ export default {
notification.body
)
} else {
console.log(`Apprise API server or recipient URL not set, skipping apprise notification for ${monitor.name}`)
console.log(
`Apprise API server or recipient URL not set, skipping apprise notification for ${monitor.name}`
)
}
}
@ -83,11 +87,11 @@ export default {
try {
console.log('Calling check proxy: ' + monitor.checkProxy)
let resp
if (monitor.checkProxy.startsWith("worker://")) {
const doLoc = monitor.checkProxy.replace("worker://", "")
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
locationHint: doLoc as DurableObjectLocationHint,
})
resp = await doStub.getLocationAndStatus(monitor)
} else {
@ -97,7 +101,7 @@ export default {
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(monitor),
})
).json<{location: string; status: {ping: number; up: boolean; err: string}}>()
).json<{ location: string; status: { ping: number; up: boolean; err: string } }>()
}
checkLocation = resp.location
status = resp.status
@ -144,17 +148,14 @@ export default {
// grace period not set OR ...
workerConfig.notification?.gracePeriod === undefined ||
// only when we have sent a notification for DOWN status, we will send a notification for UP status (within 30 seconds of possible drift)
currentTimeSecond - lastIncident.start[0] >= (workerConfig.notification.gracePeriod + 1) * 60 - 30
currentTimeSecond - lastIncident.start[0] >=
(workerConfig.notification.gracePeriod + 1) * 60 - 30
) {
await formatAndNotify(
monitor,
true,
lastIncident.start[0],
currentTimeSecond,
'OK'
)
await formatAndNotify(monitor, true, lastIncident.start[0], currentTimeSecond, 'OK')
} else {
console.log(`grace period (${workerConfig.notification?.gracePeriod}m) not met, skipping apprise UP notification for ${monitor.name}`)
console.log(
`grace period (${workerConfig.notification?.gracePeriod}m) not met, skipping apprise UP notification for ${monitor.name}`
)
}
console.log('Calling config onStatusChange callback...')
@ -195,22 +196,20 @@ export default {
try {
if (
// monitor status changed AND...
(monitorStatusChanged && (
(monitorStatusChanged &&
// grace period not set OR ...
workerConfig.notification?.gracePeriod === undefined ||
// have sent a notification for DOWN status
currentTimeSecond - currentIncident.start[0] >= (workerConfig.notification.gracePeriod + 1) * 60 - 30
))
||
(
// grace period is set AND...
workerConfig.notification?.gracePeriod !== undefined &&
(
// grace period is met
currentTimeSecond - currentIncident.start[0] >= workerConfig.notification.gracePeriod * 60 - 30 &&
currentTimeSecond - currentIncident.start[0] < workerConfig.notification.gracePeriod * 60 + 30
)
)) {
(workerConfig.notification?.gracePeriod === undefined ||
// have sent a notification for DOWN status
currentTimeSecond - currentIncident.start[0] >=
(workerConfig.notification.gracePeriod + 1) * 60 - 30)) ||
// grace period is set AND...
(workerConfig.notification?.gracePeriod !== undefined &&
// grace period is met
currentTimeSecond - currentIncident.start[0] >=
workerConfig.notification.gracePeriod * 60 - 30 &&
currentTimeSecond - currentIncident.start[0] <
workerConfig.notification.gracePeriod * 60 + 30)
) {
await formatAndNotify(
monitor,
false,
@ -219,7 +218,14 @@ export default {
status.err
)
} else {
console.log(`Grace period (${workerConfig.notification?.gracePeriod}m) not met (currently down for ${currentTimeSecond - currentIncident.start[0]}s, changed ${monitorStatusChanged}), skipping apprise DOWN notification for ${monitor.name}`)
console.log(
`Grace period (${workerConfig.notification
?.gracePeriod}m) not met (currently down for ${
currentTimeSecond - currentIncident.start[0]
}s, changed ${monitorStatusChanged}), skipping apprise DOWN notification for ${
monitor.name
}`
)
}
if (monitorStatusChanged) {
@ -274,40 +280,45 @@ export default {
// discard old incidents
let incidentList = state.incident[monitor.id]
while (incidentList.length > 0 && incidentList[0].end && incidentList[0].end < currentTimeSecond - 90 * 24 * 60 * 60) {
while (
incidentList.length > 0 &&
incidentList[0].end &&
incidentList[0].end < currentTimeSecond - 90 * 24 * 60 * 60
) {
incidentList.shift()
}
if (incidentList.length == 0 || (
incidentList[0].start[0] > currentTimeSecond - 90 * 24 * 60 * 60 &&
incidentList[0].error[0] != 'dummy'
)) {
if (
incidentList.length == 0 ||
(incidentList[0].start[0] > currentTimeSecond - 90 * 24 * 60 * 60 &&
incidentList[0].error[0] != 'dummy')
) {
// put the dummy incident back
incidentList.unshift(
{
start: [currentTimeSecond - 90 * 24 * 60 * 60],
end: currentTimeSecond - 90 * 24 * 60 * 60,
error: ['dummy'],
}
)
incidentList.unshift({
start: [currentTimeSecond - 90 * 24 * 60 * 60],
end: currentTimeSecond - 90 * 24 * 60 * 60,
error: ['dummy'],
})
}
state.incident[monitor.id] = incidentList
statusChanged ||= monitorStatusChanged
}
console.log(`statusChanged: ${statusChanged}, lastUpdate: ${state.lastUpdate}, currentTime: ${currentTimeSecond}`)
console.log(
`statusChanged: ${statusChanged}, lastUpdate: ${state.lastUpdate}, currentTime: ${currentTimeSecond}`
)
// Update state
// Allow for a cooldown period before writing to KV
if (
statusChanged ||
currentTimeSecond - state.lastUpdate >= workerConfig.kvWriteCooldownMinutes * 60 - 10 // Allow for 10 seconds of clock drift
currentTimeSecond - state.lastUpdate >= workerConfig.kvWriteCooldownMinutes * 60 - 10 // Allow for 10 seconds of clock drift
) {
console.log("Updating state...")
console.log('Updating state...')
state.lastUpdate = currentTimeSecond
await env.UPTIMEFLARE_STATE.put('state', JSON.stringify(state))
} else {
console.log("Skipping state update due to cooldown period.")
console.log('Skipping state update due to cooldown period.')
}
},
}
@ -317,8 +328,10 @@ export class RemoteChecker extends DurableObject {
super(ctx, env)
}
async getLocationAndStatus(monitor: MonitorTarget): Promise<{location: string; status: {ping: number; up: boolean; err: string}}> {
const colo = await getWorkerLocation() as string
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 {

View File

@ -1,5 +1,5 @@
import { MonitorTarget } from "../../uptime.types";
import { withTimeout, fetchTimeout } from "./util";
import { MonitorTarget } from '../../uptime.types'
import { withTimeout, fetchTimeout } from './util'
export async function getStatus(
monitor: MonitorTarget
@ -15,9 +15,11 @@ 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)
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 parsed = new URL('https://' + monitor.target)
const socket = connect({ hostname: parsed.hostname, port: Number(parsed.port) })
// Now we have an `opened` promise!
@ -58,8 +60,9 @@ export async function getStatus(
if (!monitor.expectedCodes.includes(response.status)) {
console.log(`${monitor.name} expected ${monitor.expectedCodes}, got ${response.status}`)
status.up = false
status.err = `Expected codes: ${JSON.stringify(monitor.expectedCodes)}, Got: ${response.status
}`
status.err = `Expected codes: ${JSON.stringify(monitor.expectedCodes)}, Got: ${
response.status
}`
return status
}
} else {
@ -77,17 +80,28 @@ export async function getStatus(
// MUST contain responseKeyword
if (monitor.responseKeyword && !responseBody.includes(monitor.responseKeyword)) {
console.log(`${monitor.name} expected keyword ${monitor.responseKeyword}, not found in response (truncated to 100 chars): ${responseBody.slice(0, 100)}`)
console.log(
`${monitor.name} expected keyword ${
monitor.responseKeyword
}, not found in response (truncated to 100 chars): ${responseBody.slice(0, 100)}`
)
status.up = false
status.err = "HTTP response doesn't contain the configured keyword"
return status
}
// MUST NOT contain responseForbiddenKeyword
if (monitor.responseForbiddenKeyword && responseBody.includes(monitor.responseForbiddenKeyword)) {
console.log(`${monitor.name} forbidden keyword ${monitor.responseForbiddenKeyword}, found in response (truncated to 100 chars): ${responseBody.slice(0, 100)}`)
if (
monitor.responseForbiddenKeyword &&
responseBody.includes(monitor.responseForbiddenKeyword)
) {
console.log(
`${monitor.name} forbidden keyword ${
monitor.responseForbiddenKeyword
}, found in response (truncated to 100 chars): ${responseBody.slice(0, 100)}`
)
status.up = false
status.err = "HTTP response contains the configured forbidden keyword"
status.err = 'HTTP response contains the configured forbidden keyword'
return status
}
}

View File

@ -43,7 +43,7 @@ function formatStatusChangeNotification(
timeZone: timeZone,
})
let downtimeDuration = Math.round((timeNow - timeIncidentStart) / 60);
let downtimeDuration = Math.round((timeNow - timeIncidentStart) / 60)
const timeNowFormatted = dateFormatter.format(new Date(timeNow * 1000))
const timeIncidentStartFormatted = dateFormatter.format(new Date(timeIncidentStart * 1000))
@ -60,7 +60,9 @@ function formatStatusChangeNotification(
} else {
return {
title: `🔴 ${monitor.name} is still down.`,
body: `Service is unavailable since ${timeIncidentStartFormatted} (${downtimeDuration} minutes). Issue: ${reason || 'unspecified'}`,
body: `Service is unavailable since ${timeIncidentStartFormatted} (${downtimeDuration} minutes). Issue: ${
reason || 'unspecified'
}`,
}
}
}
@ -71,7 +73,16 @@ async function notifyWithApprise(
title: string,
body: string
) {
console.log('Sending Apprise notification: ' + title + '-' + body + ' to ' + recipientUrl + ' via ' + appriseApiServer)
console.log(
'Sending Apprise notification: ' +
title +
'-' +
body +
' to ' +
recipientUrl +
' via ' +
appriseApiServer
)
try {
const resp = await fetchTimeout(appriseApiServer, 5000, {
method: 'POST',
@ -83,12 +94,14 @@ async function notifyWithApprise(
title,
body,
type: 'warning',
format: 'text'
format: 'text',
}),
})
if (!resp.ok) {
console.log('Error calling apprise server, code: ' + resp.status + ', response: ' + await resp.text())
console.log(
'Error calling apprise server, code: ' + resp.status + ', response: ' + (await resp.text())
)
} else {
console.log('Apprise notification sent successfully, code: ' + resp.status)
}
@ -97,4 +110,10 @@ async function notifyWithApprise(
}
}
export { getWorkerLocation, fetchTimeout, withTimeout, notifyWithApprise, formatStatusChangeNotification }
export {
getWorkerLocation,
fetchTimeout,
withTimeout,
notifyWithApprise,
formatStatusChangeNotification,
}

View File

@ -1,105 +1,105 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Projects */
// "incremental": true, /* Enable incremental compilation */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Projects */
// "incremental": true, /* Enable incremental compilation */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"lib": [
"es2021"
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
"jsx": "react" /* Specify what JSX code is generated. */,
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
/* Language and Environment */
"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"lib": [
"es2021"
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
"jsx": "react" /* Specify what JSX code is generated. */,
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
/* Modules */
"module": "es2022" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
"types": [
"@cloudflare/workers-types"
] /* Specify type package names to be included without being referenced in a source file. */,
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
"resolveJsonModule": true /* Enable importing .json files */,
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
/* Modules */
"module": "es2022" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
"types": [
"@cloudflare/workers-types"
] /* Specify type package names to be included without being referenced in a source file. */,
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
"resolveJsonModule": true /* Enable importing .json files */,
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
/* JavaScript Support */
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
"noEmit": true /* Disable emitting files from a compilation. */,
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
"noEmit": true /* Disable emitting files from a compilation. */,
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
// "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
/* Interop Constraints */
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
// "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}