-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser.php
More file actions
105 lines (91 loc) · 3.22 KB
/
Copy pathuser.php
File metadata and controls
105 lines (91 loc) · 3.22 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
<?php
/**
* Simple proxy for enabling public user pages for all IITD EE students.
* Forward requests to actual user page at IITD internal-web.
*
* See : http://www.cc.iitd.ernet.in/CSC/index.php?option=com_content&view=article&id=107&Itemid=146
*
* @author Abhishek Kedia (kedia.abhishek10 [at] gmail.com)
*/
require __DIR__."/vendor/autoload.php";
function get_path_prefix() {
$script_name = basename(__FILE__, ".php");
$script_path = $_SERVER["SCRIPT_NAME"];
$prefix_regex = "/^(.*)\/".$script_name."\.php$/";
$is_matched = preg_match($prefix_regex, $script_path, $matches);
if (!$is_matched) {
http_response_code(500);
die("Cannot find path prefix");
}
return $matches[1];
}
function get_request_path() {
$PREFIX = get_path_prefix();
// $PATH_REGEX = "^(\/~ee1130431\/(?:user.php\/|u\/|~))(?:((?:ee|EE)\w\d{6})((?:\/?|\/.*)))?$";
$PATH_REGEX = "/^(".preg_quote($PREFIX, "/")."\/(?:".basename(__FILE__, ".php").".php\/|u\/|~))((?:ee|EE)\w\d{6})((?:\/?|\/.*))$/";
$uri = $_SERVER["REQUEST_URI"];
$is_matched = preg_match($PATH_REGEX, $uri, $matches);
if (!$is_matched) {
http_response_code(400);
die("Invalid URL format");
}
return [
"serverBase" => $matches[1],
"userId" => $matches[2],
"userPath" => $matches[3]
];
}
function get_client_ip() {
$ipaddress = '';
if (isset($_SERVER['HTTP_CLIENT_IP']))
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_X_FORWARDED']))
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
else if(isset($_SERVER['HTTP_FORWARDED']))
$ipaddress = $_SERVER['HTTP_FORWARDED'];
else if(isset($_SERVER['REMOTE_ADDR']))
$ipaddress = $_SERVER['REMOTE_ADDR'];
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
function get_upstream_response($userId, $userPath) {
$url = "http://privateweb.iitd.ac.in/~".$userId."/ees_home".$userPath;
$method = $_SERVER["REQUEST_METHOD"];
$request_headers = getallheaders();
$request_headers["Connection"] = "close"; //keep-alive connections take too long (~ 5s) to respond. Also see - https://github.com/guzzle/guzzle/issues/1348
$request_headers["X-Forwarded-For"] = get_client_ip(); //Send client IP upstream
$client = new GuzzleHttp\Client();
return $client->request($method, $url, [
"headers" => $request_headers,
"body" => fopen("php://input", "r"),
"http_errors" => false,
"stream" => true
]);
}
function set_headers($headers) {
unset($headers["Connection"]); //use default value (supplied by incoming request) for connection header
foreach ($headers as $name => $values) {
header($name . ': ' . implode(', ', $values));
}
}
function main() {
$request_path = get_request_path();
$userId = $request_path["userId"];
$userPath = $request_path["userPath"];
$response = get_upstream_response($userId, $userPath);
http_response_code($response->getStatusCode());
set_headers($response->getHeaders());
// echo $response->getBody();
// Stream response instead
$bodyStream = $response->getBody();
while (!$bodyStream->eof()) {
echo $bodyStream->read(1024);
}
}
main();
?>