mirror of
https://github.com/hyperknot/openfreemap.git
synced 2026-05-21 14:02:15 +00:00
scripts -> modules
This commit is contained in:
66
modules/debug_proxy/src/index.js
Normal file
66
modules/debug_proxy/src/index.js
Normal file
@@ -0,0 +1,66 @@
|
||||
async function sendTelegramMessage(message, botToken, chatId) {
|
||||
const url = `https://api.telegram.org/bot${botToken}/sendMessage`
|
||||
const payload = {
|
||||
chat_id: chatId,
|
||||
text: message,
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(payload),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('Failed to send message:', await response.text())
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error sending Telegram message:', error)
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
async fetch(request, env, ctx) {
|
||||
const url = new URL(request.url)
|
||||
const userIP = request.headers.get('CF-Connecting-IP')
|
||||
|
||||
if (url.pathname === '/b') {
|
||||
url.pathname = '/styles/bright'
|
||||
}
|
||||
|
||||
// // no failure, just warning
|
||||
// if (request.method !== 'GET') {
|
||||
// const warningMessage = `Non-GET request ${request.method} ${url.pathname} ${userIP}`
|
||||
// console.error(warningMessage)
|
||||
// await sendTelegramMessage(warningMessage, env.TELEGRAM_TOKEN, env.TELEGRAM_CHAT_ID)
|
||||
// }
|
||||
|
||||
if (!url.pathname.startsWith('/styles')) {
|
||||
const errorMessage = 'Bad path'
|
||||
return new Response(errorMessage, { status: 500 })
|
||||
}
|
||||
|
||||
const proxyUrl = new URL(url.pathname, 'https://tiles.openfreemap.org')
|
||||
|
||||
try {
|
||||
const response = await fetch(proxyUrl)
|
||||
|
||||
if (response.status !== 200) {
|
||||
const errorMessage = `Proxy error: Bad status ${response.status} ${url.pathname} ${userIP}`
|
||||
console.error(errorMessage)
|
||||
await sendTelegramMessage(errorMessage, env.TELEGRAM_TOKEN, env.TELEGRAM_CHAT_ID)
|
||||
return new Response('Proxy error: Bad status', { status: 500 })
|
||||
}
|
||||
|
||||
return response
|
||||
} catch (error) {
|
||||
const errorMessage = `Proxy error: ${error.message} ${url.pathname} ${userIP}`
|
||||
console.error(errorMessage)
|
||||
await sendTelegramMessage(errorMessage, env.TELEGRAM_TOKEN, env.TELEGRAM_CHAT_ID)
|
||||
return new Response('Proxy error: Fetch failed', { status: 500 })
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user