forked from yitd/Any-Proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
146 lines (146 loc) · 7.23 KB
/
index.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
<?php
if (strstr($_SERVER['REQUEST_URI'], "*q") !== false) {
foreach ($_COOKIE as $key => $value) {
setcookie($key, null);
}
header("Location: https://" . $_SERVER['HTTP_HOST']);
exit;
}
if (substr($_SERVER['REQUEST_URI'], 1, 4) == "http" || $_POST['urlss']) {
if ($_POST['urlss']) {
$url = $_POST['urlss'];
} else {
$url = substr($_SERVER['REQUEST_URI'], 1);
}
if (strstr($url, "http") === false) {
$url = "http:https://" . $url;
}
$PageUrl = parse_url($url);
$PageUrl['query'] ? $query = "?" . $PageUrl['query'] : $query = "";
$http = $PageUrl['scheme'] . ":https://";
$PageUrls = $http . $_SERVER['HTTP_HOST'];
$PageUrls = $PageUrls . $PageUrl['path'] . $query;
foreach ($_COOKIE as $key => $value) {
setcookie($key, null);
}
setcookie("urlss", $http . $PageUrl['host'], "0", "/");
header("Location: " . $PageUrls);
exit;
}
if (!$_COOKIE['urlss']) {
exit('<html><head><meta charset="utf-8"><meta name="viewport" content="width=520, user-scalable=no, target-densitydpi=device-dpi"><title>代理访问_Any-Proxy</title><link rel="stylesheet" type="text/css" href="//s0.pstatp.com/cdn/expire-1-M/bootswatch/3.4.0/paper/bootstrap.min.css"><style type="text/css">.row{margin-top:100px}.page-header{margin-bottom:90px}.expand-transition{margin-top:150px;-webkit-transition:all.5s ease;transition:all.5s ease}</style></head><body><div id="app" class="container"><div class="alert top top-xs alert-dismissible alert-success expand-transition" style="display:none" id="success-tips"></div><div class="alert top top-xs alert-dismissible alert-danger expand-transition" style="display:none" id="error-tips"></div><div class="row row-xs"><div class="col-lg-6 col-md-6 col-sm-6 col-xs-10 col-xs-offset-1 col-sm-offset-3 col-md-offset-3 col-lg-offset-3"><div class="page-header"><h3 class="text-center h3-xs">Any-Proxy</h3></div><form method="post"><div class="form-group " id="input-wrap"><label class="control-label" for="inputContent">请输入需访问的链接:</label><input type="text" id="inputContent" class="form-control" name="urlss" placeholder="http:https://"></div><div class="text-right"><input type="submit" class="input_group_addon btn btn-primary" value="GO"></div></div></form></div></div><div align="center" class="expand-transition"><p>你可以直接在当前链接后面输入 *q 退出当前页面返回首页</p><p>可直接在此域名后面加上链接地址访问,如 https://' . $_SERVER['HTTP_HOST'] . '/http:https://ip38.com </p></div></div><footer class="footer navbar-fixed-bottom" style="text-align:center"><div class="container"><p>请勿访问您当地法律所禁止的网页,否则后果自负。</p><p>©Powered by <a href="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/yitd/Any-Proxy">Any-Proxy</a></p></div></footer></body></html>');
}
//代理的域名及使用的协议最后不用加/
$target_host = $_COOKIE['urlss'];
if (strstr($target_host, "http") === false) {
$target_host = "http:https://" . $target_host;
}
//解决中文乱码
#header("Content-Type:text/html;charset=gb2312");
//处理代理的主机得到协议和主机名称
$protocal_host = parse_url($target_host);
//以.分割域名字符串
$rootdomain = explode(".", $_SERVER['HTTP_HOST']);
//获取数组的长度
$lenth = count($rootdomain);
//获取顶级域名
$top = "." . $rootdomain[$lenth - 1];
//获取主域名
$root = "." . $rootdomain[$lenth - 2];
$aAccess = curl_init();
// --------------------
// set URL and other appropriate options
curl_setopt($aAccess, CURLOPT_URL, $protocal_host['scheme'] . ":https://" . $protocal_host['host'] . $_SERVER['REQUEST_URI']);
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);
//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 urldecode($string);
}
if ($_SERVER['HTTP_REFERER']) {
$referer = str_replace("https://" . $_SERVER['HTTP_HOST'], $protocal_host['scheme'] . ":https://" . $protocal_host['host'], $_SERVER['HTTP_REFERER']);
}
if (empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$remoteip = $_SERVER['REMOTE_ADDR'];
} else {
$remoteip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//$headers = get_client_header();
$headers = array();
$headers[] = "Accept-language: " . $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$headers[] = "Referer: " . $referer;
$headers[] = "CLIENT-IP: " . $remoteip;
$headers[] = "X-FORWARDED-FOR: " . $remoteip;
$headers[] = "Cookie: " . array_to_str($_COOKIE);
$headers[] = "user-agent: " . $_SERVER['HTTP_USER_AGENT'];
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);
$headarr = explode("\r\n", $headerstr);
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, 'Set-Cookie') !== false) {
$targetcookie = $h . ";";
$res_cookie = preg_replace("/domain=.*?;/", "domain=" . $root . $top . ";", $targetcookie);
$h = substr($res_cookie, 0, strlen($res_cookie) - 1);
}
header($h);
}
}
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') 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;
}
// close cURL resource, and free up system resources
$sResponse = str_replace("http:https://" . $protocal_host['host']."/", "https://" . $_SERVER['HTTP_HOST']."/", $sResponse);
$sResponse = str_replace("https://" . $protocal_host['host']."/", "https://" . $_SERVER['HTTP_HOST']."/", $sResponse);
curl_close($aAccess);
echo $sResponse;
?>