// Cloudflare Worker Script (worker.js) const UPTIMEROBOT_API_ENDPOINT = 'https://api.uptimerobot.com/v2/getMonitors'; // Replace with the ID of your Plex monitor in UptimeRobot // You can find this ID in UptimeRobot dashboard by clicking on the monitor const PLEX_MONITOR_ID = '800933230'; // <<< IMPORTANT: Replace this addEventListener('fetch', event => { event.respondWith(handleRequest(event.request, event.env, event.ctx)); }); async function handleRequest(request, env, ctx) { // Only allow GET requests to avoid abuse if (request.method !== 'GET') { return new Response('Method Not Allowed', { status: 405 }); } // Check for the secret API key environment variable const API_KEY = env.UPTIMEROBOT_API_KEY; // This comes from Cloudflare Worker secrets if (!API_KEY) { return new Response('Configuration Error: API Key or Monitor ID missing', { status: 500 }); } try { const formData = new FormData(); formData.append('api_key', API_KEY); formData.append('monitors', PLEX_MONITOR_ID); // Request only your specific monitor formData.append('format', 'json'); formData.append('logs', '0'); // Don't need logs for just status formData.append('customUptimeRatio', '7'); // Get 7-day uptime ratio, optional const response = await fetch(UPTIMEROBOT_API_ENDPOINT, { method: 'POST', // UptimeRobot API uses POST even for 'get' actions body: formData, }); const data = await response.json(); if (data.stat === 'ok' && data.monitors && data.monitors.length > 0) { const monitor = data.monitors[0]; let status = 'offline'; // Default to offline let statusText = 'Offline'; // UptimeRobot Status Codes: // 0 = Paused // 1 = Not yet checked // 2 = Up // 8 = Seems Down (UptimeRobot is still checking) // 9 = Down if (monitor.status === 2) { status = 'online'; statusText = 'Online'; } else if (monitor.status === 0) { status = 'paused'; statusText = 'Paused'; } else if (monitor.status === 1 || monitor.status === 8) { status = 'checking'; statusText = 'Checking...'; } else { status = 'offline'; statusText = 'Offline'; } return new Response(JSON.stringify({ status: status, statusText: statusText, uptimeRatio: monitor.customuptimeratio, // If requested lastChecked: new Date().toISOString() }), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'https://noplexzone.com', // Replace with your actual domain 'Access-Control-Allow-Methods': 'GET', 'Cache-Control': 'no-store, no-cache, must-revalidate, proxy-revalidate', 'Pragma': 'no-cache', 'Expires': '0', }, status: 200 }); } else { console.error('UptimeRobot API error:', data); return new Response(JSON.stringify({ status: 'error', message: 'Failed to retrieve monitor status from UptimeRobot.' }), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'https://noplexzone.com' }, status: 500 }); } } catch (error) { console.error('Worker error:', error); return new Response(JSON.stringify({ status: 'error', message: `Server error: ${error.message}` }), { headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': 'https://noplexzone.com' }, status: 500 }); } }