-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.php
More file actions
116 lines (95 loc) · 3.17 KB
/
Copy pathproxy.php
File metadata and controls
116 lines (95 loc) · 3.17 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
const WORKER_URL = 'https://proxy-embed.nethriondev.workers.dev';
function handler($request) {
try {
$url = parse_url($request->getRequestTarget());
$path = $url['path'] ?? '/';
$query = isset($url['query']) ? '?' . $url['query'] : '';
$targetUrl = WORKER_URL . $path . $query;
$headers = [];
foreach ($request->getHeaders() as $name => $values) {
$headers[$name] = implode(', ', $values);
}
$fetchOptions = [
'method' => $request->getMethod(),
'headers' => $headers,
];
$method = $request->getMethod();
if ($method !== 'GET' && $method !== 'HEAD') {
$fetchOptions['body'] = $request->getBody()->getContents();
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $targetUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_map(
fn($key, $value) => "$key: $value",
array_keys($headers),
$headers
));
if (isset($fetchOptions['body'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $fetchOptions['body']);
}
$responseBody = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$responseHeaders = curl_getinfo($ch, CURLINFO_HEADER_OUT);
curl_close($ch);
$responseHeaders = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Expose-Headers' => '*',
];
return new Response($responseBody, $statusCode, $responseHeaders);
} catch (Exception $error) {
return new Response(
'Bad Gateway: Could not reach upstream',
502,
[
'Content-Type' => 'text/plain',
'Access-Control-Allow-Origin' => '*'
]
);
}
}
class Response {
private $body;
private $status;
private $headers;
public function __construct($body, $status = 200, $headers = []) {
$this->body = $body;
$this->status = $status;
$this->headers = $headers;
}
public function send() {
http_response_code($this->status);
foreach ($this->headers as $key => $value) {
header("$key: $value");
}
echo $this->body;
}
}
class Request {
public function getMethod() {
return $_SERVER['REQUEST_METHOD'];
}
public function getRequestTarget() {
return $_SERVER['REQUEST_URI'];
}
public function getHeaders() {
$headers = [];
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$name = str_replace('_', '-', substr($key, 5));
$headers[$name] = $value;
}
}
return $headers;
}
public function getBody() {
return file_get_contents('php://input');
}
}
$request = new Request();
$response = handler($request);
$response->send();
?>