فيما يلي التنفيذ الأساسي لوكيل الويب/أداة إخفاء الهوية في PHP:
<?php
// Function to fetch the content from the target URL and return it
function fetchURL($url, $headers, $method, $data = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Set method and data for POST requests
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Check if URL parameter is provided
if (isset($_GET['url'])) {
$url = $_GET['url'];
// Forward request headers
$headers = getallheaders();
// Forward request method
$method = $_SERVER['REQUEST_METHOD'];
// Forward request data for POST requests
$data = null;
if ($method === 'POST') {
$data = file_get_contents('php://input');
}
// Fetch content from the target URL
$targetResponse = fetchURL($url, $headers, $method, $data);
// Extract response headers and content
list($targetHeaders, $targetContent) = explode("\r\n\r\n", $targetResponse, 2);
// Forward response headers to the client
$headerLines = explode("\r\n", $targetHeaders);
foreach ($headerLines as $header) {
header($header);
}
// Output the content to the client
echo $targetContent;
} else {
// URL parameter is missing
http_response_code(400);
echo "Missing URL parameter";
}
?>
يعمل برنامج PHP النصي هذا بمثابة وكيل ويب أساسي/مخفي الهوية. يستمع للطلبات ويعيد توجيهها إلى عنوان URL المحدد. يجب توفير عنوان URL المطلوب كمعلمة استعلام مسماة url
.
لاستخدام هذا الوكيل، قم بحفظ البرنامج النصي على الخادم الخاص بك والوصول إليه عبر المتصفح أو إرسال طلبات HTTP إليه برمجيًا. على سبيل المثال:
GET http://example.com/proxy.php?url=https://example.com
سيؤدي هذا إلى استرداد المحتوى من https://example.com
من خلال الوكيل. لاحظ أن هذا تنفيذ بسيط وقد لا يتعامل مع جميع أنواع الطلبات أو الاستجابات. بالإضافة إلى ذلك، فهو لا يتضمن ميزات مثل التخزين المؤقت أو إخفاء الهوية.
يمكنك إضافة ميزات التخزين المؤقت وإخفاء الهوية الأساسية إلى البرنامج النصي لوكيل PHP. وهنا النسخة المحدثة:
<?php
// Function to fetch the content from the target URL and return it
function fetchURL($url, $headers, $method, $data = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Set method and data for POST requests
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Function to cache response content
function cacheContent($url, $content) {
// Implement caching logic here
// For simplicity, you can use file-based caching
$cacheFileName = md5($url) . '.cache';
file_put_contents($cacheFileName, $content);
}
// Function to retrieve cached content
function getCachedContent($url) {
// Implement caching logic here
$cacheFileName = md5($url) . '.cache';
if (file_exists($cacheFileName)) {
return file_get_contents($cacheFileName);
}
return null;
}
// Check if URL parameter is provided
if (isset($_GET['url'])) {
$url = $_GET['url'];
// Check if content is cached
$cachedContent = getCachedContent($url);
if ($cachedContent !== null) {
// Output cached content
echo $cachedContent;
exit();
}
// Forward request headers
$headers = getallheaders();
// Forward request method
$method = $_SERVER['REQUEST_METHOD'];
// Forward request data for POST requests
$data = null;
if ($method === 'POST') {
$data = file_get_contents('php://input');
}
// Fetch content from the target URL
$targetResponse = fetchURL($url, $headers, $method, $data);
// Extract response headers and content
list($targetHeaders, $targetContent) = explode("\r\n\r\n", $targetResponse, 2);
// Forward response headers to the client
$headerLines = explode("\r\n", $targetHeaders);
foreach ($headerLines as $header) {
header($header);
}
// Output the content to the client
echo $targetContent;
// Cache the response content
cacheContent($url, $targetContent);
} else {
// URL parameter is missing
http_response_code(400);
echo "Missing URL parameter";
}
?>
في هذا الإصدار المحدث، أضفت وظيفة التخزين المؤقت الأساسية باستخدام التخزين المؤقت المستند إلى الملف. يتم تخزين الاستجابات المخزنة مؤقتًا في ملفات تحمل اسم تجزئة MD5 لعنوان URL. بالإضافة إلى ذلك، يتحقق البرنامج النصي من المحتوى المخزن مؤقتًا قبل جلبه من عنوان URL الهدف، وإذا تم العثور على المحتوى في ذاكرة التخزين المؤقت، فسيتم تقديمه مباشرة إلى العميل.
التعليقات (0)
لا توجد تعليقات هنا حتى الآن، يمكنك أن تكون الأول!