다음은 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";
}

?>

이번 업데이트 버전에서는 파일 기반 캐싱을 사용하여 기본 캐싱 기능을 추가했습니다. 캐시된 응답은 URL의 MD5 해시 이름을 딴 파일에 저장됩니다. 또한 스크립트는 대상 URL에서 캐시된 콘텐츠를 가져오기 전에 캐시된 콘텐츠를 확인하고, 콘텐츠가 캐시에서 발견되면 클라이언트에 직접 제공됩니다.

댓글 (0)

여기에는 아직 댓글이 없습니다. 첫 번째 댓글이 되실 수 있습니다!

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다


프록시 선택 및 구매

데이터센터 프록시

회전 프록시

UDP 프록시

전 세계 10,000명 이상의 고객이 신뢰함

대리 고객
대리 고객
대리 고객 flowch.ai
대리 고객
대리 고객
대리 고객