File size: 3,607 Bytes
e421274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Cloudflare Worker script to proxy Telegram API requests
// This script intercepts requests to the Telegram Bot API and forwards them
// allowing your bot to work even when direct access to api.telegram.org is blocked

export default {
    async fetch(request, env, ctx) {
        // Get the original request URL
        const originalUrl = new URL(request.url);

        // Handle CORS preflight requests
        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',
                },
            });
        }

        // Check if this is a request to our worker
        // If the path starts with /telegram-api, forward to Telegram API
        if (originalUrl.pathname.startsWith('/telegram-api')) {
            // Extract the path after /telegram-api
            // The full path should be like /telegram-api/bot{TOKEN}/{METHOD}
            const telegramApiPath = originalUrl.pathname.replace('/telegram-api', '');

            // Construct the target URL for Telegram API
            const telegramApiUrl = `https://api.telegram.org${telegramApiPath}`;

            // Clone the original request to modify it
            // Remove the Cloudflare-specific headers that might cause issues
            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 {
                // Send request to Telegram API
                const response = await fetch(modifiedRequest);

                // Return the response from Telegram API with CORS headers
                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, // Bad Gateway
                    headers: {
                        'Content-Type': 'application/json',
                        'Access-Control-Allow-Origin': '*',
                    }
                });
            }
        }

        // If not a Telegram API request, return info about the proxy
        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': '*',
            }
        });
    }
};