-
Notifications
You must be signed in to change notification settings - Fork 30
/
index_test.php
232 lines (208 loc) · 6.91 KB
/
index_test.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
<?php
$target_host="https://www.google.com/";
//处理代理的主机得到协议和主机名称
$protocal_host=parse_url($target_host);
//以.分割域名字符串
$rootdomain=explode(".",$_SERVER["SERVER_NAME"]);
//获取数组的长度
$lenth=count($rootdomain);
//获取顶级域名
$top=".".$rootdomain[$lenth-1];
//获取主域名
$root=".".$rootdomain[$lenth-2];
//
//header('Content-type: text/html; charset=UTF-8');
//
$aAccess = curl_init() ;
// --------------------
// set URL and other appropriate options
//
$next_domain_url=$_GET["next_domain_url"];
//echo $next_domain_url;
if (empty($next_domain_url))
{
curl_setopt($aAccess, CURLOPT_URL,$protocal_host['scheme']."://".$protocal_host['host'].$_SERVER["REQUEST_URI"]);
//debug_to_console($protocal_host['scheme']."://".$protocal_host['host'].$_SERVER["REQUEST_URI"]);
}
else
{
$spec_url=explode('/',$next_domain_url);
$next_common_url=base64_decode($spec_url[0]);
//
$req_all_param=$_SERVER["REQUEST_URI"];
//debug_to_console("req_all_param:".$req_all_param);
$req_ori_param=preg_replace("/\/index.php\?next_domain_url=.*?\//","",$req_all_param);
//debug_to_console("req_ori_param:".$req_ori_param);
//
$next_full_url=$next_common_url."/".$req_ori_param;
curl_setopt($aAccess, CURLOPT_URL,$next_full_url);
//
//debug_to_console("next_full_url:".$next_full_url);
}
//$cookie_jar = tempnam('/tmp','koalabear');
//curl_setopt($aAccess, CURLOPT_COOKIEJAR, $cookie_jar);
//curl_setopt($aAccess, CURLOPT_COOKIE, $cookie_jar);
curl_setopt($aAccess, CURLOPT_HEADER, true);
curl_setopt($aAccess, CURLOPT_RETURNTRANSFER, true);
curl_setopt($aAccess, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($aAccess, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($aAccess, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($aAccess, CURLOPT_TIMEOUT, 60);
curl_setopt($aAccess, CURLOPT_BINARYTRANSFER, true);
curl_setopt($aAccess, CURLOPT_COOKIESESSION, true);
//curl_setopt($aAccess, CURLOPT_ENCODING, "UTF-8");
//if(!empty($_SERVER['HTTP_REFERER']))
//curl_setopt($aAccess,CURLOPT_REFERER,$_SERVER['HTTP_REFERER']) ;
//关系数组转换成字符串,每个键值对中间用=连接,以; 分割
function array_to_str ($array)
{
$string="";
if (is_array($array))
{
foreach ($array as $key => $value)
{
if(!empty($string))
$string.="; ".$key."=".$value;
else
$string.=$key."=".$value;
}
} else
{
$string = $array;
}
return $string;
}
//$headers=get_client_header();
$headers = array();
$headers[]="Accept-language: ".$_SERVER['HTTP_ACCEPT_LANGUAGE'];//$_SERVER['HTTP_ACCEPT_LANGUAGE'];//zh-CN,zh
$headers[]="Cookie: ".$_SERVER['HTTP_COOKIE'];//array_to_str($_COOKIE);
//
//when proxy google,this should be special,or can not access google,there are some browser security issues...
//so far i don't know how to process.
//$headers[]="user-agent: Php_client/1.0";//.$_SERVER['HTTP_USER_AGENT'];
$headers[]="Host: ".$protocal_host['host'];
$headers[]="Referer: ".$protocal_host['scheme']."://".$protocal_host['host'];
//print_r($headers);
if( $_SERVER['REQUEST_METHOD']=='POST' )
{
$headers[]="Content-Type: ".$_SERVER['CONTENT_TYPE'];
curl_setopt($aAccess, CURLOPT_POST, 1);
curl_setopt($aAccess, CURLOPT_POSTFIELDS, http_build_query($_POST));
}
curl_setopt($aAccess,CURLOPT_HTTPHEADER,$headers) ;
// grab URL and pass it to the browser
$sResponse = curl_exec($aAccess);
list($headerstr,$sResponse)=parse_header($sResponse);
//
function get_client_header(){
$headers=array();
foreach($_SERVER as $k=>$v){
if(strpos($k,'HTTP_')===0){
$k=strtolower(preg_replace('/^HTTP/', '', $k));
$k=preg_replace_callback('/_\w/','header_callback',$k);
$k=preg_replace('/^_/','',$k);
$k=str_replace('_','-',$k);
if($k=='Host')
{
$headers[]="$k: ".$protocal_host['host'];
continue;
}
if($k=='Referer')
{
$headers[]="$k: ".$protocal_host['host'];
continue;
}
$headers[]="$k: $v";
}
}
return $headers;
}
function header_callback($str){
return strtoupper($str[0]);
}
function parse_header($sResponse){
list($headerstr,$sResponse)=explode("\r\n\r\n",$sResponse, 2);
$ret=array($headerstr,$sResponse);
if(preg_match('/^HTTP\/1\.1 \d{3}/', $sResponse)){
$ret=parse_header($sResponse);
}
return $ret;
}
//debug to console
function debug_to_console_old($data) {
if(is_array($data) || is_object($data))
{
echo("<script>console.log('PHP: ".json_encode($data)."');</script>");
} else {
echo("<script>console.log('PHP: ".$data."');</script>");
}
}
function debug_to_console($data) {
$output = $data;
if (is_array($output))
$output = implode(',', $output);
echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}
// close cURL resource, and free up system resources
//
$sResponse=str_replace($protocal_host['host'],$_SERVER["SERVER_NAME"],$sResponse);
//special replace
//$sResponse=str_replace("//google.com","//localhost",$sResponse);
//$sResponse=str_replace("consent.google.com?","consent.google.com/?",$sResponse);
//$sResponse=str_replace("https://consent.google.com","/index.php?next_domain_url=".base64_encode("https://consent.google.com"),$sResponse);
//$sResponse=str_replace("https://ogs.google.com","/index.php?next_domain_url=".base64_encode("https://ogs.google.com"),$sResponse);
//文本转码
$content_mime=curl_getinfo($aAccess,CURLINFO_CONTENT_TYPE);
/*
if(strlen($content_mime)>0)
{
if(strpos($content_mime,'text/html')!==false)
{
$charlen = stripos($sResponse, "charset");
if (stristr(substr($sResponse, $charlen, 18) , "GBK") || stristr(substr($sResponse, $charlen, 18) , "GB2312")) {
$sResponse = mb_convert_encoding($sResponse, "UTF-8", "GBK,GB2312,BIG5");
}
}
}*/
//
curl_close($aAccess);
//search other link in this content
preg_match_all('/((http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+))([\/]?)/i',$sResponse,$m);
$need_replace=$m[1];
//replace other link to base64_encode
/*
foreach ($need_replace as $val) {
$sResponse=str_replace($val,"/index.php?next_domain_url=".base64_encode($val),$sResponse);
}*/
//output
$headarr= explode("\r\n", $headerstr);
$debug="";
foreach($headarr as $h){
if(strlen($h)>0){
if(strpos($h,'Content-Length')!==false) continue;
if(strpos($h,'Transfer-Encoding')!==false) continue;
if(strpos($h,'Connection')!==false) continue;
if(strpos($h,'HTTP/1.1 100 Continue')!==false) continue;
//
if(strpos($h,'Content-Security-Policy')!==false) continue;
if(strpos($h,'Content-Security-Policy-Report-Only')!==false) continue;
if(strpos($h,'Clear-Site-Data')!==false) continue;
//
if(strpos($h,'Set-Cookie')!==false)
{
$targetcookie=$h.";";
$res_cookie=preg_replace("/domain=.*?;/","domain=".$root.$top.";",$targetcookie);
$h=substr($res_cookie,0,strlen($res_cookie)-1);
$debug=$debug.$h;
}
header($h);
}
}
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
echo $sResponse;
//debug_to_console("hello,world");
//debug_to_console($headarr);
//debug_to_console($debug);
//
?>