-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathredirect.php
More file actions
390 lines (317 loc) · 10.3 KB
/
Copy pathredirect.php
File metadata and controls
390 lines (317 loc) · 10.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
ini_set('display_errors', '0');
ini_set('log_errors', '1');
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
// --------------------------------------------------
// Configuration
// --------------------------------------------------
$affiliateParams = [
'ref', 'ref_', 'utm_source', 'utm_medium',
'utm_campaign', 'utm_term', 'utm_content', 'aff'
];
$maxUrlLength = 4096;
$selfHosts = ['anonymz.io', 'www.anonymz.io'];
// Toggle failure telemetry (privacy-safe)
$enableFailureWebhook = true;
// Internal webhook proxy (NOT the real webhook)
$failureWebhookEndpoint = 'https://anonymz.io/internal/error-webhook.php';
// --------------------------------------------------
// Helpers
// --------------------------------------------------
function shouldDelayRedirect(array $queryParams): bool {
return count($queryParams) > 3 || strlen(http_build_query($queryParams)) > 100;
}
/**
* Returns [url, needsDecode]. PHP already URL-decodes $_GET values once
* when parsing the query string, so that path needs no further decoding.
* The raw REQUEST_URI passthrough is untouched by PHP and carries exactly
* one layer of encoding (added by assets/app.js), so it needs one decode.
*/
function normalizeInputUrl(): array {
if (!empty($_GET['url'])) {
// Direct access: /redirect.php?url=https://example.com
return [trim($_GET['url']), false];
}
// Root passthrough: https://anonymz.io/?https://example.com
$uri = $_SERVER['REQUEST_URI'] ?? '';
return [trim(ltrim($uri, '/?')), true];
}
/**
* Privacy-safe failure telemetry
*/
function sendFailureWebhook(string $error, string $inputUrl): void
{
global $enableFailureWebhook, $failureWebhookEndpoint;
if (!$enableFailureWebhook || !$failureWebhookEndpoint) {
return;
}
// One-way fingerprint (non-reversible)
$fingerprint = hash('sha256', $inputUrl);
$payload = [
'embeds' => [[
'title' => 'AnonymZ Redirect Failure',
'color' => 15158332,
'fields' => [
[
'name' => 'Error',
'value' => $error,
'inline' => true
],
[
'name' => 'Fingerprint',
'value' => substr($fingerprint, 0, 12),
'inline' => true
],
[
'name' => 'Time',
'value' => gmdate('Y-m-d H:i:s') . ' UTC'
]
]
]]
];
$ch = curl_init($failureWebhookEndpoint);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-Internal-Hook: 1'
],
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => false,
CURLOPT_TIMEOUT_MS => 300,
]);
@curl_exec($ch);
@curl_close($ch);
}
// --------------------------------------------------
// Init
// --------------------------------------------------
[$inputUrl, $needsDecode] = normalizeInputUrl();
$finalUrl = '';
$queryParams = [];
$error = null;
// --------------------------------------------------
// Input handling
// --------------------------------------------------
if ($inputUrl === '') {
$error = 'Missing destination URL.';
} else {
// Decode exactly one layer of encoding. Looping "until stable" here
// would keep eating into the destination URL's own percent-encoding
// (e.g. %20 in a query value, or %25 in a Wikipedia title), corrupting
// any destination that legitimately contains encoded characters.
if ($needsDecode) {
$inputUrl = urldecode($inputUrl);
}
// Normalize spaces after decoding
$inputUrl = str_replace(' ', '%20', $inputUrl);
// Length limit
if (strlen($inputUrl) > $maxUrlLength) {
$error = 'URL too long.';
}
// Reject dangerous or unsupported schemes BEFORE forcing https
if (
!$error &&
preg_match('#^(ftp|file|data|javascript):#i', $inputUrl)
) {
$error = 'Unsupported URL scheme.';
}
// Reject multiple http(s) schemes
if (
!$error &&
preg_match('#^(https?://){2,}#i', $inputUrl)
) {
$error = 'Invalid URL format.';
}
// Force https ONLY if no scheme exists
if (
!$error &&
!preg_match('#^https?://#i', $inputUrl)
) {
$inputUrl = 'https://' . $inputUrl;
}
}
// --------------------------------------------------
// Validation & cleanup
// --------------------------------------------------
if (!$error && filter_var($inputUrl, FILTER_VALIDATE_URL)) {
$parsedUrl = parse_url($inputUrl);
if (
empty($parsedUrl['scheme']) ||
empty($parsedUrl['host']) ||
!in_array(strtolower($parsedUrl['scheme']), ['http', 'https'], true)
) {
$error = 'Invalid or unsupported URL.';
}
// Block userinfo abuse
elseif (strpos($parsedUrl['host'], '@') !== false) {
$error = 'Invalid URL host.';
}
// Prevent self-redirect loops
elseif (in_array(strtolower($parsedUrl['host']), $selfHosts, true)) {
$error = 'Self redirects are not allowed.';
}
else {
// Normalize host
if (strpos($parsedUrl['host'], 'www.') === 0) {
$parsedUrl['host'] = substr($parsedUrl['host'], 4);
}
// Parse query string
if (!empty($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $queryParams);
}
// Google search hardening
if (strpos($parsedUrl['host'], 'google.') !== false) {
$queryParams = array_intersect_key($queryParams, ['q' => true]);
}
// Strip tracking / affiliate params
foreach ($affiliateParams as $param) {
unset($queryParams[$param]);
}
// --------------------------------------------------
// Rebuild clean URL
// --------------------------------------------------
$finalUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host'];
if (!empty($parsedUrl['port'])) {
$finalUrl .= ':' . $parsedUrl['port'];
}
if (!empty($parsedUrl['path'])) {
$finalUrl .= $parsedUrl['path'];
}
if (!empty($queryParams)) {
$finalUrl .= '?' . http_build_query($queryParams);
}
if (!empty($parsedUrl['fragment'])) {
$finalUrl .= '#' . $parsedUrl['fragment'];
}
}
} elseif (!$error) {
$error = 'Invalid URL. Please provide a valid destination URL.';
}
// --------------------------------------------------
// Headers
// --------------------------------------------------
header('Content-Type: text/html; charset=UTF-8');
header('Referrer-Policy: no-referrer');
header('X-Content-Type-Options: nosniff');
// --------------------------------------------------
// Redirect (ONLY if final URL is valid)
// --------------------------------------------------
if ($finalUrl && filter_var($finalUrl, FILTER_VALIDATE_URL)) {
if (shouldDelayRedirect($queryParams)) {
header("Refresh: 1; url={$finalUrl}");
} else {
header("Location: {$finalUrl}", true, 302);
}
exit;
}
// If we reach here, treat as handled error
$finalUrl = '';
$error = $error ?: 'The destination URL is invalid.';
// --------------------------------------------------
// Failure telemetry (ONLY on error)
// --------------------------------------------------
if ($error) {
sendFailureWebhook($error, $inputUrl);
}
// --------------------------------------------------
// Error output
// --------------------------------------------------
http_response_code(200);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?= $finalUrl ? 'Redirecting…' : 'Unable to Redirect'; ?></title>
<?php if ($finalUrl && shouldDelayRedirect($queryParams)): ?>
<?php endif; ?>
<link rel="icon" type="image/png" href="/favicon.png" />
<style>
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
background-color: #23272A;
color: #FFFFFF;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.container {
background-color: #2C2F33;
border-radius: 12px;
width: 420px;
height: 320px;
padding: 24px;
box-shadow: 0 8px 20px rgba(0,0,0,0.35);
display: flex;
flex-direction: column;
justify-content: space-between;
text-align: center;
}
h1 {
margin: 0 0 8px 0;
font-size: 1.4rem;
font-weight: 600;
}
p {
margin: 0;
color: #DCDDDE;
line-height: 1.5;
font-size: 0.95rem;
}
.content {
display: flex;
flex-direction: column;
gap: 12px;
}
.action {
margin-top: 8px;
}
a {
color: #7289DA;
text-decoration: none;
font-weight: 500;
}
a:hover {
text-decoration: underline;
}
.footer {
font-size: 0.8rem;
color: #9DA3A6;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<?php if ($finalUrl): ?>
<h1>Redirecting</h1>
<p>
You are being securely redirected to your destination.
</p>
<div class="action">
<a href="<?= htmlspecialchars($finalUrl, ENT_QUOTES, 'UTF-8'); ?>">
Continue manually
</a>
</div>
<?php else: ?>
<h1>Unable to Redirect</h1>
<p>
The provided link could not be processed.
This usually happens if the URL is malformed or unsupported.
</p>
<div class="action">
<a href="/">Return Home</a>
</div>
<?php endif; ?>
</div>
<div class="footer">
Powered by <a href="https://anonymz.io/">AnonymZ</a>
</div>
</div>
</body>
</html>
<?php exit; ?>