// netlify/functions/ical-proxy.js // Proxy server-side pentru iCal Booking.com - ocoleste restrictiile CORS const https = require('https'); const http = require('http'); exports.handler = async function(event) { var url = (event.queryStringParameters || {}).url; if (!url) { return { statusCode: 400, body: 'Missing url parameter' }; } // Permite doar Booking.com iCal pentru securitate if (!url.startsWith('https://ical.booking.com/') && !url.startsWith('http://ical.booking.com/')) { return { statusCode: 403, body: 'Only booking.com iCal URLs are allowed' }; } return new Promise(function(resolve) { var lib = url.startsWith('https') ? https : http; var options = { headers: { 'User-Agent': 'Googlebot/2.1 (+http://www.google.com/bot.html)', 'Accept': 'text/calendar, text/plain, */*', 'Accept-Language': 'en-US,en;q=0.9', 'Cache-Control': 'no-cache' } }; var req = lib.get(url, options, function(res) { // Urmareste redirecturi if (res.statusCode === 301 || res.statusCode === 302) { var redirectUrl = res.headers.location; if (!redirectUrl) { resolve({ statusCode: 502, body: 'Redirect fara Location header' }); return; } // Apel recursiv pentru redirect var redirectLib = redirectUrl.startsWith('https') ? https : http; redirectLib.get(redirectUrl, options, function(res2) { var data = []; res2.on('data', function(chunk) { data.push(chunk); }); res2.on('end', function() { var body = Buffer.concat(data).toString('utf8'); resolve({ statusCode: 200, headers: { 'Content-Type': 'text/calendar; charset=utf-8', 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'no-cache, no-store' }, body: body }); }); }).on('error', function(e) { resolve({ statusCode: 500, body: 'Redirect fetch error: ' + e.message }); }); return; } if (res.statusCode !== 200) { resolve({ statusCode: res.statusCode, body: 'Booking.com a returnat: ' + res.statusCode }); return; } var data = []; res.on('data', function(chunk) { data.push(chunk); }); res.on('end', function() { var body = Buffer.concat(data).toString('utf8'); // Verifica daca e un iCal valid if (!body.includes('BEGIN:VCALENDAR')) { resolve({ statusCode: 502, body: 'Raspuns invalid de la Booking.com: ' + body.slice(0, 200) }); return; } resolve({ statusCode: 200, headers: { 'Content-Type': 'text/calendar; charset=utf-8', 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'no-cache, no-store' }, body: body }); }); }); req.on('error', function(e) { resolve({ statusCode: 500, body: 'Eroare conexiune: ' + e.message }); }); req.setTimeout(15000, function() { req.destroy(); resolve({ statusCode: 504, body: 'Timeout - Booking.com nu a raspuns in 15s' }); }); }); };