-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathindex.php
More file actions
134 lines (114 loc) · 4.09 KB
/
index.php
File metadata and controls
134 lines (114 loc) · 4.09 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
// ---------------- CONFIG ----------------
$VCSKY_BASE_URL = "https://cdn.dos.zone/vcsky/";
$BR_BASE_URL = "https://br.cdn.dos.zone/vcsky/";
$BASE_PATH = ''; // public base URL
$DIST_PATH = __DIR__ . '/dist'; // local directory for SPA
// ---------------- HELPERS ----------------
// Build URL with query string
function build_url($base, $path) {
$query = $_SERVER['QUERY_STRING'];
$url = rtrim($base, '/') . '/' . ltrim($path, '/');
if ($query) {
$url .= '?' . $query;
}
return $url;
}
// Proxy request using cURL
function proxy_request($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $_SERVER['REQUEST_METHOD']);
// Forward headers
$headers = [];
foreach (getallheaders() as $key => $value) {
if (!in_array(strtolower($key), ['host', 'content-length'])) {
$headers[] = "$key: $value";
}
}
$headers[] = "Cross-Origin-Opener-Policy: same-origin";
$headers[] = "Cross-Origin-Embedder-Policy: require-corp";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Forward body for POST/PUT/PATCH
if (!in_array($_SERVER['REQUEST_METHOD'], ['GET', 'HEAD'])) {
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header_text = substr($response, 0, $header_size);
$body = substr($response, $header_size);
foreach (explode("\r\n", $header_text) as $header) {
if ($header && stripos($header, 'Content-Length') === false) {
header($header, false);
}
}
echo $body;
curl_close($ch);
}
// ---------------- ROUTING ----------------
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Proxy /vcsky/*
if (preg_match('#^' . $BASE_PATH . '/vcsky/(.*)#', $uri, $matches)) {
$url = build_url($VCSKY_BASE_URL, $matches[1]);
proxy_request($url);
exit;
}
// Proxy /vcbr/*
if (preg_match('#^' . $BASE_PATH . '/vcbr/(.*)#', $uri, $matches)) {
$url = build_url($BR_BASE_URL, $matches[1]);
proxy_request($url);
exit;
}
// Serve static files under $BASE_PATH
if (strpos($uri, $BASE_PATH) === 0) {
$relativePath = substr($uri, strlen($BASE_PATH));
if ($relativePath === '' || $relativePath === '/') {
$relativePath = '/index.html';
}
$localFile = realpath($DIST_PATH . $relativePath);
// Ensure the file exists AND is inside $DIST_PATH
if ($localFile && is_file($localFile) && str_starts_with($localFile, realpath($DIST_PATH))) {
// Set MIME type manually for common extensions
$ext = pathinfo($localFile, PATHINFO_EXTENSION);
switch (strtolower($ext)) {
case 'js':
$mimeType = 'application/javascript';
break;
case 'css':
$mimeType = 'text/css';
break;
case 'html':
case 'htm':
$mimeType = 'text/html';
break;
case 'json':
$mimeType = 'application/json';
break;
case 'wasm':
$mimeType = 'application/wasm';
break;
default:
$mimeType = mime_content_type($localFile) ?: 'application/octet-stream';
}
header("Content-Type: $mimeType");
header("Cross-Origin-Opener-Policy: same-origin");
header("Cross-Origin-Embedder-Policy: require-corp");
readfile($localFile);
exit;
}
// SPA fallback only for HTML requests
if (str_ends_with($uri, '/') || preg_match('/\.html$/', $uri)) {
$indexFile = $DIST_PATH . '/index.html';
if (is_file($indexFile)) {
header("Content-Type: text/html");
header("Cross-Origin-Opener-Policy: same-origin");
header("Cross-Origin-Embedder-Policy: require-corp");
readfile($indexFile);
exit;
}
}
}
// 404 fallback
http_response_code(404);
echo "Not Found";