forked from lyc8503/UptimeFlare
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuptime.config.ts
286 lines (269 loc) · 10.4 KB
/
uptime.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const pageConfig = {
title: "Kongying Tavern Status Page",
// Links shown at the header of your status page, could set `highlight` to `true`
links: [
{ link: 'https://yuanshen.site/docs/', label: 'Main Site' },
{ link: 'https://github.com/kongying-tavern', label: 'GitHub' },
{ link: 'https://space.bilibili.com/518076785', label: 'Bilibili' },
{ link: 'https://yuanshen.site/docs/join', highligh: true, label: 'Join Us' }
],
}
const workerConfig = {
// Write KV at most every 3 minutes unless the status changed.
kvWriteCooldownMinutes: 3,
// Define all your monitors here
monitors: [
// Example HTTP Monitor
{
// `id` should be unique, history will be kept if the `id` remains constant
id: 'map-webpage-v3',
// `name` is used at status page and callback message
name: 'Genshin Interactive Map V3 Webpage',
// `method` should be a valid HTTP Method
method: 'GET',
// `target` is a valid URL
target: 'https://v3.yuanshen.site/',
// [OPTIONAL] `tooltip` is ONLY used at status page to show a tooltip
tooltip: 'Genshin Interactive Map A Completionist\'s Interactive Map by Kongying Tavern',
// [OPTIONAL] `statusPageLink` is ONLY used for clickable link at status page
statusPageLink: 'https://v3.yuanshen.site/',
// [OPTIONAL] `expectedCodes` is an array of acceptable HTTP response codes, if not specified, default to 2xx
expectedCodes: [200],
// [OPTIONAL] `timeout` in millisecond, if not specified, default to 10000
timeout: 10000,
// [OPTIONAL] headers to be sent
headers: {
'User-Agent': 'Uptimeflare'
},
},
{
// `id` should be unique, history will be kept if the `id` remains constant
id: 'map-assets',
// `name` is used at status page and callback message
name: 'Genshin Interactive Map Assets',
// `method` should be a valid HTTP Method
method: 'GET',
// `target` is a valid URL
target: 'https://assets.yuanshen.site/web-map.json',
// [OPTIONAL] `tooltip` is ONLY used at status page to show a tooltip
tooltip: 'Genshin Interactive Map Assets',
// [OPTIONAL] `statusPageLink` is ONLY used for clickable link at status page
statusPageLink: 'https://v3.yuanshen.site/',
// [OPTIONAL] `expectedCodes` is an array of acceptable HTTP response codes, if not specified, default to 2xx
expectedCodes: [200],
// [OPTIONAL] `timeout` in millisecond, if not specified, default to 10000
timeout: 10000,
// [OPTIONAL] headers to be sent
headers: {
'User-Agent': 'Uptimeflare'
},
},
{
// `id` should be unique, history will be kept if the `id` remains constant
id: 'map-ecould',
// `name` is used at status page and callback message
name: 'Genshin Interactive Map Ecould',
// `method` should be a valid HTTP Method
method: 'POST',
// `target` is a valid URL
target: 'https://cloud.yuanshen.site/api/item_type/get/list/1',
// [OPTIONAL] `tooltip` is ONLY used at status page to show a tooltip
tooltip: 'Genshin Interactive Map Ecould',
// [OPTIONAL] `statusPageLink` is ONLY used for clickable link at status page
statusPageLink: 'https://v3.yuanshen.site/',
// [OPTIONAL] `expectedCodes` is an array of acceptable HTTP response codes, if not specified, default to 2xx
expectedCodes: [200],
// [OPTIONAL] `timeout` in millisecond, if not specified, default to 10000
timeout: 10000,
// [OPTIONAL] headers to be sent
headers: {
'User-Agent': 'Uptimeflare'
},
},
{
// `id` should be unique, history will be kept if the `id` remains constant
id: 'map-alist',
// `name` is used at status page and callback message
name: 'Genshin Interactive Map Alist',
// `method` should be a valid HTTP Method
method: 'GET',
// `target` is a valid URL
target: 'https://tiles.yuanshen.site/d/MainMap/md5.json',
// [OPTIONAL] `tooltip` is ONLY used at status page to show a tooltip
tooltip: 'Genshin Interactive Map Alist',
// [OPTIONAL] `statusPageLink` is ONLY used for clickable link at status page
statusPageLink: 'https://tiles.yuanshen.site/',
// [OPTIONAL] `expectedCodes` is an array of acceptable HTTP response codes, if not specified, default to 2xx
expectedCodes: [200],
// [OPTIONAL] `timeout` in millisecond, if not specified, default to 10000
timeout: 10000,
// [OPTIONAL] headers to be sent
headers: {
'User-Agent': 'Uptimeflare'
},
},
{
// `id` should be unique, history will be kept if the `id` remains constant
id: 'map-docs-api',
// `name` is used at status page and callback message
name: 'Genshin Interactive Map Docs Api',
// `method` should be a valid HTTP Method
method: 'GET',
// `target` is a valid URL
target: 'https://doc-apis.yuanshen.site/',
// [OPTIONAL] `tooltip` is ONLY used at status page to show a tooltip
tooltip: 'Genshin Interactive Map Docs Api',
// [OPTIONAL] `statusPageLink` is ONLY used for clickable link at status page
statusPageLink: 'https://yuanshen.site/docs',
// [OPTIONAL] `expectedCodes` is an array of acceptable HTTP response codes, if not specified, default to 2xx
expectedCodes: [200, 401],
// [OPTIONAL] `timeout` in millisecond, if not specified, default to 10000
timeout: 10000,
// [OPTIONAL] headers to be sent
headers: {
'User-Agent': 'Uptimeflare'
},
// [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',
},
// // Example TCP Monitor
// {
// id: 'test_tcp_monitor',
// name: 'Example TCP Monitor',
// // `method` should be `TCP_PING` for tcp monitors
// method: 'TCP_PING',
// // `target` should be `host:port` for tcp monitors
// target: '1.2.3.4:22',
// tooltip: 'My production server SSH',
// statusPageLink: 'https://example.com',
// timeout: 5000,
// },
],
callbacks: {
onStatusChange: async (
env: any,
monitor: any,
isUp: boolean,
timeIncidentStart: number,
timeNow: number,
reason: string
) => {
// This callback will be called when there's a status change for any monitor
// Write any Typescript code here
// By default, this sends Bark and Telegram notification on every status change if you setup Cloudflare env variables correctly.
await notify(env, monitor, isUp, timeIncidentStart, timeNow, reason)
},
onIncident: async (
env: any,
monitor: any,
timeIncidentStart: number,
timeNow: number,
reason: string
) => {
// This callback will be called EVERY 1 MINTUE if there's an on-going incident for any monitor
// Write any Typescript code here
},
},
}
// Below is code for sending Telegram & Bark notification
// You can safely ignore them
const escapeMarkdown = (text: string) => {
return text.replace(/[_*[\](){}~`>#+\-=|.!\\]/g, '\\$&');
};
async function notify(
env: any,
monitor: any,
isUp: boolean,
timeIncidentStart: number,
timeNow: number,
reason: string,
) {
const dateFormatter = new Intl.DateTimeFormat('en-US', {
month: 'numeric',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hour12: false,
timeZone: 'Asia/Shanghai',
});
let downtimeDuration = Math.round((timeNow - timeIncidentStart) / 60);
const timeIncidentStartFormatted = dateFormatter.format(new Date(timeIncidentStart * 1000));
let statusText = isUp
? `The service is up again after being down for ${downtimeDuration} minutes.`
: `Service became unavailable at ${timeIncidentStartFormatted}. Issue: ${reason || 'unspecified'}`;
console.log('Notifying: ', monitor.name, statusText);
if (env.BARK_SERVER && env.BARK_DEVICE_KEY) {
try {
let title = isUp ? `✅ ${monitor.name} is up again!` : `🔴 ${monitor.name} is currently down.`;
await sendBarkNotification(env, monitor, title, statusText);
} catch (error) {
console.error('Error sending Bark notification:', error);
}
}
if (env.SECRET_TELEGRAM_CHAT_ID && env.SECRET_TELEGRAM_API_TOKEN) {
try {
let operationalLabel = isUp ? 'Up' : 'Down';
let statusEmoji = isUp ? '✅' : '🔴';
let telegramText = `*${escapeMarkdown(
monitor.name,
)}* is currently *${operationalLabel}*\n${statusEmoji} ${escapeMarkdown(statusText)}`;
await notifyTelegram(env, monitor, isUp, telegramText);
} catch (error) {
console.error('Error sending Telegram notification:', error);
}
}
}
export async function notifyTelegram(env: any, monitor: any, operational: boolean, text: string) {
const chatId = env.SECRET_TELEGRAM_CHAT_ID;
const apiToken = env.SECRET_TELEGRAM_API_TOKEN;
const payload = new URLSearchParams({
chat_id: chatId,
parse_mode: 'MarkdownV2',
text: text,
});
try {
const response = await fetch(`https://api.telegram.org/bot${apiToken}/sendMessage`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: payload.toString(),
});
if (!response.ok) {
console.error(
`Failed to send Telegram notification "${text}", ${response.status} ${response.statusText
} ${await response.text()}`,
);
}
} catch (error) {
console.error('Error sending Telegram notification:', error);
}
}
async function sendBarkNotification(env: any, monitor: any, title: string, body: string, group: string = '') {
const barkServer = env.BARK_SERVER;
const barkDeviceKey = env.BARK_DEVICE_KEY;
const barkUrl = `${barkServer}/push`;
const data = {
title: title,
body: body,
group: group,
url: monitor.url,
device_key: barkDeviceKey,
};
const response = await fetch(barkUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
if (response.ok) {
console.log('Bark notification sent successfully.');
} else {
const respText = await response.text();
console.error('Failed to send Bark notification:', response.status, response.statusText, respText);
}
}
// Don't forget this, otherwise compilation fails.
export { pageConfig, workerConfig }