Dưới đây là cách triển khai cơ bản của proxy/ẩn danh web trong 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";
}
?>
Tập lệnh PHP này hoạt động như một proxy/ẩn danh web cơ bản. Nó lắng nghe các yêu cầu và chuyển tiếp chúng đến URL được chỉ định. URL được yêu cầu phải được cung cấp dưới dạng tham số truy vấn có tên url
.
Để sử dụng proxy này, hãy lưu tập lệnh trên máy chủ của bạn và truy cập tập lệnh đó qua trình duyệt hoặc gửi yêu cầu HTTP tới tập lệnh đó theo chương trình. Ví dụ:
GET http://example.com/proxy.php?url=https://example.com
Điều này sẽ lấy nội dung từ https://example.com
thông qua proxy. Lưu ý rằng đây là cách triển khai đơn giản và có thể không xử lý được tất cả các loại yêu cầu hoặc phản hồi. Ngoài ra, nó không bao gồm các tính năng như bộ nhớ đệm hoặc ẩn danh.
Bạn có thể thêm các tính năng ẩn danh và bộ nhớ đệm cơ bản vào tập lệnh proxy PHP. Đây là phiên bản cập nhật:
<?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";
}
?>
Trong phiên bản cập nhật này, tôi đã thêm chức năng lưu vào bộ nhớ đệm cơ bản bằng cách sử dụng bộ nhớ đệm dựa trên tệp. Phản hồi được lưu trong bộ nhớ đệm được lưu trữ trong các tệp được đặt tên theo hàm băm MD5 của URL. Ngoài ra, tập lệnh sẽ kiểm tra nội dung được lưu trong bộ nhớ đệm trước khi tìm nạp nội dung đó từ URL mục tiêu và nếu tìm thấy nội dung đó trong bộ nhớ đệm thì nội dung đó sẽ được cung cấp trực tiếp cho máy khách.
Bình luận (0)
Chưa có bình luận nào ở đây, bạn có thể là người đầu tiên!