-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgproxy.php
75 lines (61 loc) · 2.02 KB
/
tgproxy.php
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
<?php
// Set appropriate security headers
header('X-Frame-Options: DENY');
header('X-XSS-Protection: 1; mode=block');
header('X-Content-Type-Options: nosniff');
header('Strict-Transport-Security: max-age=63072000');
header('Content-Type: application/json');
header('X-Robots-Tag: noindex, nofollow', true);
// Set the base URL of the Telegram API
$base_url = 'https://api.telegram.org';
// Check if it's a GET request for webhook setup
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
echo json_encode(["message" => "This is a webhook setup endpoint."]);
exit();
}
// Get the POST data from the request
$post_data = file_get_contents('php://input');
//if ($post_data === false || empty($post_data)) {
// http_response_code(400);
// echo json_encode(["error" => "No or empty POST data received."]);
// exit();
//}
// Get the API endpoint URL from the request
$endpoint_url = $_GET['url'] ?? '';
if (!$endpoint_url) {
http_response_code(400);
echo json_encode(["error" => "API endpoint URL not provided."]);
exit();
}
// Check if the provided URL is a valid Telegram API endpoint
if (strpos($endpoint_url, $base_url) !== 0) {
http_response_code(403);
echo json_encode(["error" => "Access to the provided URL is not allowed."]);
exit();
}
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt_array($ch, [
CURLOPT_URL => $endpoint_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
]);
// Execute cURL request
$response = curl_exec($ch);
if ($response === false) {
http_response_code(500);
echo json_encode(["error" => "cURL error: " . curl_error($ch)]);
exit();
}
// Get HTTP status code of the response
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Set the HTTP status code of the response
http_response_code($http_status);
// Output the response from the Telegram API
echo $response;
?>