|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default { |
|
|
async fetch(request, env, ctx) { |
|
|
|
|
|
const originalUrl = new URL(request.url); |
|
|
|
|
|
|
|
|
if (request.method === 'OPTIONS') { |
|
|
return new Response(null, { |
|
|
headers: { |
|
|
'Access-Control-Allow-Origin': '*', |
|
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization', |
|
|
}, |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (originalUrl.pathname.startsWith('/telegram-api')) { |
|
|
|
|
|
|
|
|
const telegramApiPath = originalUrl.pathname.replace('/telegram-api', ''); |
|
|
|
|
|
|
|
|
const telegramApiUrl = `https://api.telegram.org${telegramApiPath}`; |
|
|
|
|
|
|
|
|
|
|
|
const newHeaders = new Headers(request.headers); |
|
|
newHeaders.delete('cf-ray'); |
|
|
newHeaders.delete('cf-connecting-ip'); |
|
|
newHeaders.delete('x-forwarded-for'); |
|
|
|
|
|
const modifiedRequest = new Request(telegramApiUrl, { |
|
|
method: request.method, |
|
|
headers: newHeaders, |
|
|
body: request.body, |
|
|
redirect: 'follow' |
|
|
}); |
|
|
|
|
|
try { |
|
|
|
|
|
const response = await fetch(modifiedRequest); |
|
|
|
|
|
|
|
|
const modifiedResponse = new Response(response.body, { |
|
|
status: response.status, |
|
|
statusText: response.statusText, |
|
|
headers: { |
|
|
...response.headers, |
|
|
'Access-Control-Allow-Origin': '*', |
|
|
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', |
|
|
'Access-Control-Allow-Headers': 'Content-Type, Authorization', |
|
|
} |
|
|
}); |
|
|
|
|
|
return modifiedResponse; |
|
|
} catch (error) { |
|
|
console.error('Proxy error:', error); |
|
|
return new Response(JSON.stringify({ |
|
|
error: 'Proxy error', |
|
|
details: error.message, |
|
|
message: 'Failed to connect to Telegram API' |
|
|
}), { |
|
|
status: 502, |
|
|
headers: { |
|
|
'Content-Type': 'application/json', |
|
|
'Access-Control-Allow-Origin': '*', |
|
|
} |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return new Response('Telegram API Proxy Worker - Ready to forward requests to Telegram API', { |
|
|
status: 200, |
|
|
headers: { |
|
|
'Content-Type': 'text/plain', |
|
|
'Access-Control-Allow-Origin': '*', |
|
|
} |
|
|
}); |
|
|
} |
|
|
}; |