-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
76 lines (66 loc) · 1.9 KB
/
index.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
76
<?php
/**
* A simple php forwarding proxy
*
* See README.gm for details
*
* Released under GPL v2
* - Ryc O'Chet <[email protected]>
*/
$domains = array();
include_once "config.inc.php";
$hostcrumbs = array_reverse(explode(".", $_SERVER["HTTP_HOST"]));
$host = array_shift($hostcrumbs);
$found = false;
foreach ($hostcrumbs as $crumb) {
$host = $crumb . "." . $host;
if (array_key_exists($host, $domains)) {
$found = true;
break;
}
}
if (!$found) {
header("HTTP/1.0 404 Not Found");
die;
}
$proxy = $domains[$host];
$disallow = array(// Headers that we don't allow through the proxy
"Transfer-Encoding",
"Accept-Encoding" // so we can filter the reply without having to un-gzip it
);
$httpheader = array();
foreach ($_SERVER as $key => $value) {
if (substr($key, 0, 5) == "HTTP_") {
$key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))));
if (array_search($key, $disallow) === false) {
$httpheader[] = $key . ": " . str_replace(array($host), split("/", $proxy, 2), $value);
}
}
}
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_URL => (isset($_SERVER["HTTPS"]) ? "https" : "http") . "://" . $proxy . $_SERVER["REQUEST_URI"],
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => $httpheader,
CURLOPT_COOKIE => $_SERVER["HTTP_COOKIE"]
));
if ($_POST) {
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $_POST
));
}
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);
foreach (explode("\r\n", str_replace($proxy, $host, substr($response, 0, $header_size))) as $header) {
if (!empty($header)) {
$key = explode(":", $header);
if (array_search($key[0], $disallow) === false) {
header($header, false);
}
}
}
echo str_replace(array_values($domains), array_keys($domains), substr($response, $header_size));