-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.php
More file actions
123 lines (94 loc) · 2.93 KB
/
Copy pathproxy.php
File metadata and controls
123 lines (94 loc) · 2.93 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
<?php
require_once 'logger.php';
$log = new KLogger ( "proxy.log" , KLogger::DEBUG );
$log->LogInfo("Started proxy up");
$dest_host = "127.0.0.1:9200";
$proxy_base_url = '/';
$proxied_headers = array('Set-Cookie', 'Content-Type', 'Cookie', 'Location');
$rest_json = file_get_contents("php://input");
$log->LogDebug("JSON In: $rest_json");
$json_rest_decode = json_decode($rest_json, true);
$_POST = $json_rest_decode;
//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 proxy.php from request_uri
$proxy_request_url = substr($_SERVER['REQUEST_URI'], strlen($proxy_base_url_canonical));
if( strpos($proxy_request_url, 'proxy.php') === 0 )
{
$proxy_request_url = ltrim(substr($proxy_request_url, strlen('proxy.php')), '/');
}
//final proxied request url
$proxy_request_url = "http://" . rtrim($dest_host, '/ ') . '/' . $proxy_request_url;
$log->LogDebug("Proxy Request URL: $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']);
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, $rest_json);
$log->LogDebug("JSON_POST: $rest_json");
}
$res = curl_exec($ch);
$log->LogDebug("Response: $res");
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);