-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
proxy.php
64 lines (55 loc) · 1.76 KB
/
proxy.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
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Expose-Headers: X-Set-Cookie");
$url = $_GET["url"];
if (preg_match("#^https?://#", $url) !== 1) {
die("Only http requests are allowed.");
}
$headers =
"User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36" .
"\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" .
"\r\nAccept-Language: en-US,en;q=0.5" .
"\r\nReferer: $url";
if (isset($_GET["cookies"])) {
$headers .= "\r\nCookie: " . str_replace("\n", "", $_GET["cookies"]);
}
$opts = array(
"http" => array(
"header" => $headers
),
"ssl" => array(
"verify_peer"=>false,
"verify_peer_name"=>false,
)
);
$context = stream_context_create($opts);
$last_error = NULL;
// Save errors
set_error_handler(function($errno, $errstr, $errfile, $errline) {
global $last_error;
$last_error = new ErrorException($errstr, 0, $errno, $errfile, $errline);
});
$f = fopen($url, 'r', false, $context);
restore_error_handler();
if (false !== $f) {
// Header management
$meta = stream_get_meta_data($f);
$cookies = "";
foreach($meta['wrapper_data'] as $header) {
if (preg_match("/Set-Cookie:\s*([^;]+)/i", $header, $m)) {
$cookies .= $m[1] . ';';
} else if (strpos($header, "Content-Type:") === 0) {
header($header);
}
}
// Javascript cannot access cookies directly, so we change the header name
if ($cookies !== "") header("X-Set-Cookie: " . $cookies);
// Pipe the stream from the webpage to our output
stream_copy_to_stream($f, fopen("php://output", 'w'));
fclose($f);
} else {
/// Unable to open the connection
http_response_code(500);
header("Content-Type: text/plain");
echo $last_error->getMessage();
}