forked from nbhr/php-reverse-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.php
More file actions
150 lines (121 loc) · 3.05 KB
/
proxy.php
File metadata and controls
150 lines (121 loc) · 3.05 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
require_once("lib/Cache/Lite.php");
/* Function to show error and exit */
function error($msg) {
header("HTTP/1.0 404 Not Found");
echo $msg;
exit;
}
function my_parse_str($str) {
# Based on proper_parse_str() in https://www.php.net/manual/en/function.parse-str.php#76792
#
# In case of duplicate fields, PHP $_GET and parse_str() return the last one.
# This function returns the first one.
# result array
$arr = array();
# split on outer delimiter
$pairs = explode('&', $str);
# loop through each pair
foreach ($pairs as $i) {
# split into name and value
list($name,$value) = explode('=', $i, 2);
# if name already exists, ignore it
if( isset($arr[$name]) ) {
}
# otherwise, simply stick it in a scalar
else {
$arr[$name] = $value;
}
}
# return result array
return $arr;
}
/* Function to generate the key to store the timestamp of URL */
function timestamp_url_key($url) {
return $url . "<TIMESTAMP>";
}
/* # 0. Configure the proxy */
/*
stream_context_set_default(
array("http" => array(
"proxy" => "tcp://your.proxy.com:8080",
"request_fulluri" => TRUE,
),
)
);
*/
/* Hard-coded target host */
$url = "http://www.example.com/";
/* 1. Check the URL to retrieve */
if( isset( $url ) ) {
$url = $url . $_SERVER['QUERY_STRING'];
} else {
$my_GET = my_parse_str($_SERVER['QUERY_STRING']);
if( FALSE == isset($my_GET["dst"]) ) {
error("GET parameter dst is not set.");
} else {
$url = $my_GET["dst"];
$params = [];
foreach ($_GET as $key => $value){
if($key == "dst") continue;
array_push($params, $key.'='.$value);
}
$url .= '?' . join('&',$params);
}
}
/* 2. Add http:// to URL */
if(strpos($url, "http://") === 0 || strpos($url, "https://") === 0) {
} else {
$url = "http://" . $url;
}
/* 3. Check the timestamp */
$headers = get_headers($url, 1);
$ts_remote = null;
if($headers) {
if( array_key_exists('Last-Modified', $headers) ) {
// most of web sites
$ts_remote = strtotime($headers['Last-Modified']);
} else if( array_key_exists('etag', $headers) ) {
// dl.dropboxusercontent.com
$ts_remote = $headers['etag'];
} else if( array_key_exists('ETag', $headers) ) {
$ts_remote = $headers['ETag'];
}
}
header("X-Timestamp-Remote: $ts_remote");
if( array_key_exists('Content-Type', $headers) ) {
header("Content-Type: " . $headers['Content-Type']);
}
/* 4. Init the cache */
$options = array(
'lifeTime' => null,
'pearErrorMode' => CACHE_LITE_ERROR_DIE
);
$cache = new Cache_Lite($options);
/* 5. Check if cached or not */
if($ts_remote != null) {
$ts_local = $cache->get(timestamp_url_key($url));
if($ts_local) {
header("X-Timestamp-Local: $ts_local");
if($ts_remote == $ts_local) {
header("X-Cache-Used: Yes");
echo $cache->get($url);
exit;
}
}
}
/* 6. Retrieve the remote data */
$ret = file_get_contents($url, FALSE);
if( FALSE === $ret ) {
error("Cannot get $url .");
}
/* 7. Cache */
if( $ts_remote ) {
header("X-Cache-Updated: Yes");
$cache->save($ret, $url);
$cache->save($ts_remote, timestamp_url_key($url));
}
/* 8. Show */
header("X-Cache-Used: No");
echo $ret;
?>