From 24b5896343ec39d2708a2c65204b330c43ef4913 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Wed, 8 Oct 2025 22:04:33 +0700 Subject: [PATCH] Revert "Update index.js" This reverts commit eeaca6212420d62d34a390336dcd4b1f7bca32bf. --- src/index.js | 251 +++++++++++++++++++++++++-------------------------- 1 file changed, 122 insertions(+), 129 deletions(-) diff --git a/src/index.js b/src/index.js index fed1d3a..f72cb8f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,78 +1,78 @@ export default { - async fetch(request, env, ctx) { - // Handle CORS preflight requests - if (request.method === 'OPTIONS') { - return new Response(null, { - status: 204, - headers: { - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'POST, OPTIONS', - 'Access-Control-Allow-Headers': 'Content-Type', - 'Access-Control-Max-Age': '86400', - }, - }); - } + async fetch(request, env, ctx) { + // Handle CORS preflight requests + if (request.method === 'OPTIONS') { + return new Response(null, { + status: 204, + headers: { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Max-Age': '86400', + }, + }); + } - if (request.method !== 'POST') { - return new Response('Method not allowed', { - status: 405, - headers: { - 'Access-Control-Allow-Origin': '*', - } - }); - } + if (request.method !== 'POST') { + return new Response('Method not allowed', { + status: 405, + headers: { + 'Access-Control-Allow-Origin': '*', + } + }); + } - try { - const contentType = request.headers.get('content-type'); - let text; + try { + const contentType = request.headers.get('content-type'); + let text; - if (contentType && contentType.includes('application/json')) { - const body = await request.json(); - text = body.text; - } else if (contentType && contentType.includes('application/x-www-form-urlencoded')) { - const formData = await request.formData(); - text = formData.get('text'); - } else { - return new Response('Content-Type must be application/json or application/x-www-form-urlencoded', { - status: 400, - headers: { - 'Access-Control-Allow-Origin': '*', - } - }); - } + if (contentType && contentType.includes('application/json')) { + const body = await request.json(); + text = body.text; + } else if (contentType && contentType.includes('application/x-www-form-urlencoded')) { + const formData = await request.formData(); + text = formData.get('text'); + } else { + return new Response('Content-Type must be application/json or application/x-www-form-urlencoded', { + status: 400, + headers: { + 'Access-Control-Allow-Origin': '*', + } + }); + } - if (!text) { - return new Response('Missing text parameter', { - status: 400, - headers: { - 'Access-Control-Allow-Origin': '*', - } - }); - } + if (!text) { + return new Response('Missing text parameter', { + status: 400, + headers: { + 'Access-Control-Allow-Origin': '*', + } + }); + } - // Collect client request information - const clientIP = request.headers.get('CF-Connecting-IP') || - request.headers.get('X-Forwarded-For') || - request.headers.get('X-Real-IP') || - 'Unknown'; + // Collect client request information + const clientIP = request.headers.get('CF-Connecting-IP') || + request.headers.get('X-Forwarded-For') || + request.headers.get('X-Real-IP') || + 'Unknown'; - const userAgent = request.headers.get('User-Agent') || 'Unknown'; - const country = request.cf?.country || 'Unknown'; - const city = request.cf?.city || 'Unknown'; - const region = request.cf?.region || 'Unknown'; - const latitude = request.cf?.latitude || 'Unknown'; - const longitude = request.cf?.longitude || 'Unknown'; - const timezone = request.cf?.timezone || 'Unknown'; - const url = request.url || 'Unknown'; - const timestamp = new Date().toISOString(); + const userAgent = request.headers.get('User-Agent') || 'Unknown'; + const country = request.cf?.country || 'Unknown'; + const city = request.cf?.city || 'Unknown'; + const region = request.cf?.region || 'Unknown'; + const latitude = request.cf?.latitude || 'Unknown'; + const longitude = request.cf?.longitude || 'Unknown'; + const timezone = request.cf?.timezone || 'Unknown'; + const url = request.url || 'Unknown'; + const timestamp = new Date().toISOString(); - // Format the message with request information - const hasCoordinates = latitude !== 'Unknown' && longitude !== 'Unknown'; - const mapLink = hasCoordinates - ? `https://www.google.com/maps?q=${latitude},${longitude}` - : null; + // Format the message with request information + const hasCoordinates = latitude !== 'Unknown' && longitude !== 'Unknown'; + const mapLink = hasCoordinates + ? `https://www.google.com/maps?q=${latitude},${longitude}` + : null; - const requestInfo = `IP: ${clientIP} 🔍 + const requestInfo = `IP: ${clientIP} 🔍 Browser: ${userAgent} Country: ${country} Region: ${region} @@ -82,77 +82,70 @@ export default { Timestamp: ${timestamp} Original text:`; - const formattedMessage = `${requestInfo} + const formattedMessage = `${requestInfo} ${text}`; - const telegramToken = env.TELEGRAM_TOKEN; - const telegramChatId = env.TELEGRAM_CHAT_ID; + const telegramToken = env.TELEGRAM_TOKEN; + const telegramChatId = env.TELEGRAM_CHAT_ID; - if (!telegramToken || !telegramChatId) { - return new Response('Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables', { - status: 500, - headers: { - 'Access-Control-Allow-Origin': '*', - } - }); - } + if (!telegramToken || !telegramChatId) { + return new Response('Missing TELEGRAM_TOKEN or TELEGRAM_CHAT_ID environment variables', { + status: 500, + headers: { + 'Access-Control-Allow-Origin': '*', + } + }); + } - const telegramUrl = `https://api.telegram.org/bot${telegramToken}/sendMessage`; - const telegramPayload = { - chat_id: telegramChatId, - text: formattedMessage, - parse_mode: 'HTML' - }; + const telegramUrl = `https://api.telegram.org/bot${telegramToken}/sendMessage`; + const telegramPayload = { + chat_id: telegramChatId, + text: formattedMessage, + parse_mode: 'HTML' + }; - const telegramResponse = await fetch(telegramUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(telegramPayload) - }); + const telegramResponse = await fetch(telegramUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(telegramPayload) + }); - const telegramResult = await telegramResponse.json(); + const telegramResult = await telegramResponse.json(); - if (!telegramResponse.ok) { - return new Response(JSON.stringify({ - success: false - }), { - status: 500, - headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', - } - }); - } + if (!telegramResponse.ok) { + return new Response(JSON.stringify({ + success: false + }), { + status: 500, + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + } + }); + } - return new Response(JSON.stringify({ - success: true - }), { - headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', - } - }); + return new Response(JSON.stringify({ + success: true + }), { + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + } + }); - } catch (error) { - console.error('Error processing request:', error); - console.error('Request details:', { - method: request.method, - url: request.url, - headers: Object.fromEntries(request.headers.entries()) - }); - - return new Response(JSON.stringify({ - success: false - }), { - status: 500, - headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*', - } - }); - } - }, + } catch (error) { + return new Response(JSON.stringify({ + success: false + }), { + status: 500, + headers: { + 'Content-Type': 'application/json', + 'Access-Control-Allow-Origin': '*', + } + }); + } + }, };