forked from abcdlzy/PHPProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpproxy.php
executable file
·299 lines (237 loc) · 10.3 KB
/
phpproxy.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
*/
class DataTransport
{
public static $response="";
public static $header="";
public static $errMsg="";
public static $CURL_enable_jsonp = false;
public static $CURL_enable_native = true;
public static $CURL_valid_url_regex = '/.*/';
public static $CURL_SendCookie="";
public static $CURL_SendSession="";
public static $CURL_mode="native";
public static $CURL_CallBack="";
public static $CURL_user_agent="";
public static $CURL_full_headers="1";
public static $CURL_full_status='1';
public static function hook_url($url,$response) {
//解决因为请求资源而导致的异常
if(strpos($url, '.css')||strpos($url, '.js')){
// 获取当前url的根地址
$hook_url_temp = $_SERVER['HTTP_REFERER'];
while($hook_url_temp[strlen($hook_url_temp) - 1] != '/' && $hook_url_temp != '') {
$hook_url_temp = substr($hook_url_temp,0,strlen($hook_url_temp) - 1);
}
$hook_target = $hook_url_temp;
//解决因为例如https://github.com后面没有加/而导致的hook路径错误问题
preg_match("~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?~i", $url, $urlmatches);
@$checkrooturl=$hook_url_temp.$urlmatches[4];
if(@$checkrooturl==$url){
$hook_target = $hook_target . $urlmatches[4].'/';
}
}
else
{
$hook_target =$_SERVER['PHP_SELF'] . '?url=';
// 获取当前url的根地址
$hook_url_temp = $url;
while($hook_url_temp[strlen($hook_url_temp) - 1] != '/' && $hook_url_temp != '') {
$hook_url_temp = substr($hook_url_temp,0,strlen($hook_url_temp) - 1);
}
$hook_target = $hook_target . $hook_url_temp;
//解决因为例如https://github.com后面没有加/而导致的hook路径错误问题
preg_match("~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?~i", $url, $urlmatches);
@$checkrooturl=$hook_url_temp.$urlmatches[4];
if(@$checkrooturl==$url){
$hook_target = $hook_target . $urlmatches[4].'/';
}
}
//因为需要篡改页面,所以需要去除integrity的限定
$response = preg_replace('/integrity=(\'|\")\S*(\'|\")/i', '' , $response);
// 替换基本的 / 根引用 成本网址的根引用
$response = preg_replace('/href=\'\//is', 'href=\'' . $hook_target , $response);
$response = preg_replace('/href=\"\//is', 'href="' . $hook_target , $response);
$response = preg_replace('/src=\'\//is', 'src=\'' . $hook_target , $response);
$response = preg_replace('/src=\"\//is', 'src="' . $hook_target , $response);
$response = preg_replace("/url\('\//is", 'url(\'' . $hook_target , $response);
$response = preg_replace('/url\(\"\//is', 'url("' . $hook_target , $response);
// 替换 http绝对引用 为 本网址的相对引用
$http_abs_ref = $_SERVER['PHP_SELF'] . '?url=http';
$response = preg_replace('/content=\'http/i', 'content="' .$http_abs_ref , $response);
$response = preg_replace('/content=\"http/i', 'content="' .$http_abs_ref , $response);
$response = preg_replace('/href=\'http/i', 'href="' .$http_abs_ref , $response);
$response = preg_replace('/src=\'http/i','src="' . $http_abs_ref , $response);
$response = preg_replace('/href=\"http/i', 'href="' .$http_abs_ref , $response);
$response = preg_replace('/src=\"http/i', 'src="' .$http_abs_ref , $response);
return $response;
}
public static function fix_request_url($url){
$url=preg_replace('/http:\/\/\//i','http://',$url);
$url=preg_replace('/https:\/\/\//i','https://',$url);
return $url;
}
public static function go($url, $postdata='',$mode="native")
{
if(function_exists("curl_init")){
return self::Post_CURL($url, $postdata,$mode);
}
else
{
return self::Post_FILE_GET_CONTENTS($url, $postdata);
}
}
private static function Post_CURL($url, $postdata=null,$mode="native"){
self::$errMsg="";
self::$response="";
self::$header="";
if ( !$url ) {
// Passed url not specified.
$contents = 'ERROR: url not specified';
$status = array( 'http_code' => 'ERROR' );
} else if ( !preg_match( self::$CURL_valid_url_regex, $url ) ) {
// Passed url doesn't match $valid_url_regex.
$contents = 'ERROR: invalid url';
$status = array( 'http_code' => 'ERROR' );
} else {
$ch = curl_init( $url );
// 设置post数据
if ( $postdata!=null ) {
curl_setopt( $ch, CURLOPT_POST, true );
@curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
}
// 设置cookie数据
if ( self::$CURL_SendCookie ) {
$cookie = array();
foreach ( $_COOKIE as $key => $value ) {
$cookie[] = $key . '=' . $value;
}
if ( self::$CURL_SendSession ) {
$cookie[] = SID;
}
$cookie = implode( '; ', $cookie );
curl_setopt( $ch, CURLOPT_COOKIE, $cookie );
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, '1.0'); // 使用 Http1.0 避免chunked
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 设置超时
curl_setopt( $ch, CURLOPT_USERAGENT, self::$CURL_user_agent ? self::$CURL_user_agent : @$_SERVER['HTTP_USER_AGENT'] );
$getresponse = curl_exec($ch);
list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', $getresponse, 2 );
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
self::$header = substr($getresponse, 0, $headerSize);
self::$response = substr($getresponse, $headerSize);
}
$status = curl_getinfo( $ch );
curl_close( $ch );
}
// Split header text into an array.
$header_text = preg_split( '/[\r\n]+/', $header );
if ( $mode == 'native') {
if ( !self::$CURL_enable_native) {
$contents = 'ERROR: invalid mode';
$status = array( 'http_code' => 'ERROR' );
}
// Propagate headers to response.
/* foreach ( $header_text as $header ) {
header( $header );
}
*/
return $contents;
} else {
// $data will be serialized into JSON data.
$data = array();
// Propagate all HTTP headers into the JSON data object.
if (self::$CURL_full_headers) {
$data['headers'] = array();
foreach ( $header_text as $header ) {
preg_match( '/^(.+?):\s+(.*)$/', $header, $matches );
if ( $matches ) {
$data['headers'][ $matches[1] ] = $matches[2];
}
}
}
// Propagate all cURL request / response info to the JSON data object.
if ( self::$CURL_full_status) {
$data['status'] = $status;
} else {
$data['status'] = array();
$data['status']['http_code'] = $status['http_code'];
}
// Set the JSON data object contents, decoding it from JSON if possible.
$decoded_json = json_decode( $contents );
$data['contents'] = $decoded_json ? $decoded_json : $contents;
// Generate appropriate content-type header.
$is_xhr = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
header( 'Content-type: application/' . ( $is_xhr ? 'json' : 'x-javascript' ) );
// Get JSONP callback.
$jsonp_callback = self::$CURL_enable_jsonp && isset(self::$CURL_CallBack) ? self::$CURL_CallBack : null;
// Generate JSON/JSONP string
$json = json_encode( $data );
return $jsonp_callback ? "$jsonp_callback($json)" : $json;
}
}
private static function Post_FILE_GET_CONTENTS($url, $post = null)
{
$context = array();
if (is_array($post)) {
ksort($post);
$context['http'] = array (
'timeout'=>10,
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($post, '', '&'),
);
}
self::$response=file_get_contents($url, false, stream_context_create($context));
return self::$response;
}
}
ob_start();
@$URL=$_REQUEST['url'];
if(!empty($URL))
{
//处理出现https:///或者http:///的问题,不是根本解决办法,有点偷懒
$URL=DataTransport::fix_request_url($URL);
$postArray=array();
//文件处理
if(!empty($_FILES)){
foreach($_FILES as $key=>$value){
move_uploaded_file($value['tmp_name'], $value['name']);
$postArray[$key]='@'.realpath($value['name']);
}
}
//处理POST数据
foreach($_POST as $key=>$value){
$postArray[$key]=$value;
}
//获取数据
DataTransport::go($URL,$postArray);
//处理数据
foreach ( preg_split( '/[\r\n]+/', DataTransport::$header ) as $headertext ) {
//处理因为Content-Security-Policy而导致的资源不能加载的情况
$pos = strpos($headertext, 'Content-Security-Policy');
if ($pos === false) {
header($headertext);
}
}
//Hook所有url
DataTransport::$response = DataTransport::hook_url($URL, DataTransport::$response);
print(DataTransport::$response);
//删除临时文件
if(!empty($_FILES)){
foreach($_FILES as $key=>$value){
unlink($value['name']);
}
}
} else{
echo 'url为空';
}
ob_end_flush();
?>