Spaces:
Sleeping
Sleeping
Create service-worker-(old).ts
Browse files- service-worker-(old).ts +89 -0
service-worker-(old).ts
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Cloudflare/Deno Worker script to proxy Telegram API requests
|
| 2 |
+
// This script intercepts requests to the Telegram Bot API and forwards them
|
| 3 |
+
// allowing your bot to work even when direct access to api.telegram.org is blocked
|
| 4 |
+
|
| 5 |
+
export default {
|
| 6 |
+
async fetch(request, env, ctx) {
|
| 7 |
+
// Get the original request URL
|
| 8 |
+
const originalUrl = new URL(request.url);
|
| 9 |
+
|
| 10 |
+
// Handle CORS preflight requests
|
| 11 |
+
if (request.method === 'OPTIONS') {
|
| 12 |
+
return new Response(null, {
|
| 13 |
+
headers: {
|
| 14 |
+
'Access-Control-Allow-Origin': '*',
|
| 15 |
+
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
| 16 |
+
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
| 17 |
+
},
|
| 18 |
+
});
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
// Check if this is a request to our worker
|
| 22 |
+
// If the path starts with /telegram-api, forward to Telegram API
|
| 23 |
+
if (originalUrl.pathname.startsWith('/telegram-api')) {
|
| 24 |
+
// Extract the path after /telegram-api
|
| 25 |
+
// The full path should be like /telegram-api/bot{TOKEN}/{METHOD}
|
| 26 |
+
const telegramApiPath = originalUrl.pathname.replace('/telegram-api', '');
|
| 27 |
+
|
| 28 |
+
// Construct the target URL for Telegram API
|
| 29 |
+
// const telegramApiUrl = `https://api.telegram.org${telegramApiPath}`;
|
| 30 |
+
const telegramApiUrl = `https://api.telegram.org${telegramApiPath}${originalUrl.search}`;
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
// Clone the original request to modify it
|
| 34 |
+
// Remove the Cloudflare-specific headers that might cause issues
|
| 35 |
+
const newHeaders = new Headers(request.headers);
|
| 36 |
+
newHeaders.delete('cf-ray');
|
| 37 |
+
newHeaders.delete('cf-connecting-ip');
|
| 38 |
+
newHeaders.delete('x-forwarded-for');
|
| 39 |
+
|
| 40 |
+
const modifiedRequest = new Request(telegramApiUrl, {
|
| 41 |
+
method: request.method,
|
| 42 |
+
headers: newHeaders,
|
| 43 |
+
body: request.body,
|
| 44 |
+
redirect: 'follow'
|
| 45 |
+
});
|
| 46 |
+
|
| 47 |
+
try {
|
| 48 |
+
// Send request to Telegram API
|
| 49 |
+
const response = await fetch(modifiedRequest);
|
| 50 |
+
|
| 51 |
+
// Return the response from Telegram API with CORS headers
|
| 52 |
+
const modifiedResponse = new Response(response.body, {
|
| 53 |
+
status: response.status,
|
| 54 |
+
statusText: response.statusText,
|
| 55 |
+
headers: {
|
| 56 |
+
...response.headers,
|
| 57 |
+
'Access-Control-Allow-Origin': '*',
|
| 58 |
+
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
| 59 |
+
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
| 60 |
+
}
|
| 61 |
+
});
|
| 62 |
+
|
| 63 |
+
return modifiedResponse;
|
| 64 |
+
} catch (error) {
|
| 65 |
+
console.error('Proxy error:', error);
|
| 66 |
+
return new Response(JSON.stringify({
|
| 67 |
+
error: 'Proxy error',
|
| 68 |
+
details: error.message,
|
| 69 |
+
message: 'Failed to connect to Telegram API'
|
| 70 |
+
}), {
|
| 71 |
+
status: 502, // Bad Gateway
|
| 72 |
+
headers: {
|
| 73 |
+
'Content-Type': 'application/json',
|
| 74 |
+
'Access-Control-Allow-Origin': '*',
|
| 75 |
+
}
|
| 76 |
+
});
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
// If not a Telegram API request, return info about the proxy
|
| 81 |
+
return new Response('Telegram API Proxy Worker - Ready to forward requests to Telegram API', {
|
| 82 |
+
status: 200,
|
| 83 |
+
headers: {
|
| 84 |
+
'Content-Type': 'text/plain',
|
| 85 |
+
'Access-Control-Allow-Origin': '*',
|
| 86 |
+
}
|
| 87 |
+
});
|
| 88 |
+
}
|
| 89 |
+
};
|