PHP检测网站炸没炸

C 2019-6-11 2753

[index.html]

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
<meta name="keywords" content="网站炸了没,炸了没,Crash" />
<meta name="description" content="网站炸了没,炸了没,Crash" />
<link rel="icon" href="data:," />
<title>网站炸了没?</title>
</head>
<body>
<b>网站炸了没?</b><br />
<input id="url" />
<input type="button" value="瞅瞅" onclick="crash();">
<div id="see"></div>
<script>
function crash(){
var url=document.getElementById('url');
var see=document.getElementById('see');
see.innerHTML='';
if(!url.value){return;};
var xhr=new XMLHttpRequest();
xhr.open('GET','//proxy.example.com/proxy.php?url='+encodeURIComponent(window.btoa(url.value)),true);
xhr.onerror=function(){alert('查询服务器废了');};
xhr.onreadystatechange=function(){if(this.readyState===4 && this.status===200){
var json=JSON.parse(this.responseText);
if(json.status=='success'){see.innerHTML='美滋滋……<br />IP:&nbsp;'+json.primary_ip+'<br />Time:&nbsp;'+json.total_time+'<br />';}
else{see.innerHTML='悲剧了……';};
}};
xhr.send(null);
};
</script>
</body>
</html>

[proxy.php] //代理主机,公网可以自由访问

<?php
function chk($url){
$r=array();
$c=curl_init($url);
curl_setopt($c,CURLOPT_TIMEOUT,10);
curl_setopt($c,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($c,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
curl_setopt($c,CURLOPT_HEADER,0);
curl_setopt($c,CURLOPT_NOBODY,0);
$e=curl_exec($c);
if(curl_errno($c)){$e='{"status":"error"}';}
curl_close($c);
return $e;
}
header('Access-Control-Allow-Origin: *');
if(empty($_GET['url'])){echo '{"status":"error"}';die;}
echo chk('https://touch.example.com/touch.php?url='.trim($_GET['url']));
?>

[touch.php] //放到国内主机,并将代理主机添加进白名单

<?php
function chk($url){
$r=array();
$c=curl_init($url);
curl_setopt($c,CURLOPT_TIMEOUT,5);
curl_setopt($c,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($c,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
curl_setopt($c,CURLOPT_HEADER,0);
curl_setopt($c,CURLOPT_NOBODY,1);
curl_exec($c);
if(curl_errno($c)){$r['status']='failed';return $r;}
$i=curl_getinfo($c);
curl_close($c);
$r['status']='success';
$r['primary_ip']=isset($i['primary_ip'])?$i['primary_ip']:null;
$r['total_time']=isset($i['total_time'])?$i['total_time']:null;
return $r;
}
if(empty($_GET['url'])){echo '{"status":"failed"}';die;}
echo json_encode(chk(rawurldecode(base64_decode($_GET['url']))));
?>

我部署的模式是:

index.html 放到 github pages

proxy.php 放到 hostdie

touch.php 放到 mopaas


最新回复 (0)