-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathindex.php
More file actions
134 lines (106 loc) · 3.43 KB
/
index.php
File metadata and controls
134 lines (106 loc) · 3.43 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
<?php
define('CONFIG_FILE', 'simple-php-proxy_config.php');
/*
Installation note:
Copy example.simple-php-proxy_config.php to simple-php-proxy_config.php
in the same folder as this script or to one or two levels above it.
Optionally you can change the variables right here and not use the config file.
For an explanation of the config variables see example.simple-php-proxy_config.php
*/
$dest_host = "bash.org";
$proxy_base_url = '/';
$proxied_headers = array('Set-Cookie', 'Content-Type', 'Cookie', 'Location');
// Variables you specify in the config file overwrite variables set above.
foreach( array('./', '../', '../../') as $path_rel )
{
if( file_exists(dirname(__file__)."/$path_rel" . CONFIG_FILE) )
{
include(dirname(__file__)."/$path_rel" . CONFIG_FILE);
break;
}
}
//canonical trailing slash
$proxy_base_url_canonical = rtrim($proxy_base_url, '/ ') . '/';
//check if valid
if( strpos($_SERVER['REQUEST_URI'], $proxy_base_url) !== 0 )
{
die("The config paramter \$prox_base_url \"$proxy_base_url\" that you specified
does not match the beginning of the request URI: ".
$_SERVER['REQUEST_URI']);
}
//remove base_url and optional index.php from request_uri
$proxy_request_url = substr($_SERVER['REQUEST_URI'], strlen($proxy_base_url_canonical));
if( strpos($proxy_request_url, 'index.php') === 0 )
{
$proxy_request_url = ltrim(substr($proxy_request_url, strlen('index.php')), '/');
}
//final proxied request url
//for ssl uncomment this line
// /$proxy_request_url = "https://" . rtrim($dest_host, '/ ') . '/' . $proxy_request_url;
// and comment this line
$proxy_request_url = "http://" . rtrim($dest_host, '/ ') . '/' . $proxy_request_url;
/* Init CURL */
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $proxy_request_url);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//for ssl uncomment this line
//curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
/* Collect and pass client request headers */
if(isset($_SERVER['HTTP_COOKIE']))
{
$hdrs[]="Cookie: " . $_SERVER['HTTP_COOKIE'];
}
if(isset($_SERVER['HTTP_USER_AGENT']))
{
$hdrs[]="User-Agent: " . $_SERVER['HTTP_USER_AGENT'];
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $hdrs);
/* pass POST params */
if( sizeof($_POST) > 0 )
{
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
}
$res = curl_exec($ch);
curl_close($ch);
/* parse response */
list($headers, $body) = explode("\r\n\r\n", $res, 2);
$headers = explode("\r\n", $headers);
$hs = array();
foreach($headers as $header)
{
if( false !== strpos($header, ':') )
{
list($h, $v) = explode(':', $header);
$hs[$h][] = $v;
}
else
{
$header1 = $header;
}
}
/* set headers */
list($proto, $code, $text) = explode(' ', $header1);
header($_SERVER['SERVER_PROTOCOL'] . ' ' . $code . ' ' . $text);
foreach($proxied_headers as $hname)
{
if( isset($hs[$hname]) )
{
foreach( $hs[$hname] as $v )
{
if( $hname === 'Set-Cookie' )
{
header($hname.": " . $v, false);
}
else
{
header($hname.": " . $v);
}
}
}
}
die($body);