check proxy status with security and multiple proxy formats

 Here's a PHP script to check proxy status with security considerations and support for multiple proxy formats:

php
Copy
Download
<?php
// Proxy Checker Script
header('Content-Type: application/json');

// Security measures
$allowed_ips = ['YOUR_VPS_IP']; // Restrict access to your VPS IP
if (!in_array($_SERVER['REMOTE_ADDR'], $allowed_ips)) {
    die(json_encode(['error' => 'Access denied']));
}

// Rate limiting (1 request per second)
session_start();
if (isset($_SESSION['last_request']) && (time() - $_SESSION['last_request'] < 1)) {
    die(json_encode(['error' => 'Too many requests']));
}
$_SESSION['last_request'] = time();

// Validate input
if (!isset($_GET['proxy']) || empty($_GET['proxy'])) {
    die(json_encode(['error' => 'Proxy parameter required']));
}

$proxy = $_GET['proxy'];
$components = [
    'ip' => '',
    'port' => '',
    'user' => '',
    'pass' => ''
];

try {
    // Parse proxy string
    if (strpos($proxy, '@') !== false) {
        list($auth, $server) = explode('@', $proxy, 2);
        list($components['user'], $components['pass']) = explode(':', $auth, 2);
        list($components['ip'], $components['port']) = explode(':', $server, 2);
    } else {
        $parts = explode(':', $proxy);
        if (count($parts) === 4) {
            list($components['ip'], $components['port'], $components['user'], $components['pass']) = $parts;
        } elseif (count($parts) === 2) {
            list($components['ip'], $components['port']) = $parts;
        } else {
            throw new Exception('Invalid proxy format');
        }
    }

    // Validate components
    if (!filter_var($components['ip'], FILTER_VALIDATE_IP)) {
        throw new Exception('Invalid IP address');
    }

    $port = (int)$components['port'];
    if ($port < 1 || $port > 65535) {
        throw new Exception('Invalid port number');
    }

    // Test proxy connection
    $ch = curl_init();
    $test_url = 'https://www.google.com'; // Test URL
    $curl_options = [
        CURLOPT_URL => $test_url,
        CURLOPT_PROXY => $components['ip'].':'.$components['port'],
        CURLOPT_PROXYUSERPWD => $components['user'].':'.$components['pass'],
        CURLOPT_TIMEOUT => 10,
        CURLOPT_CONNECTTIMEOUT => 5,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_MAXREDIRS => 3
    ];

    curl_setopt_array($ch, $curl_options);
    $response = curl_exec($ch);
    $error = curl_error($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    // Determine status
    $status = $error ? 'offline' : 'live';
    $result = [
        'status' => $status,
        'proxy' => $components,
        'response_code' => $http_code,
        'error' => $error ?: null
    ];

    echo json_encode($result, JSON_PRETTY_PRINT);

} catch (Exception $e) {
    echo json_encode([
        'error' => $e->getMessage(),
        'received_proxy' => $proxy
    ], JSON_PRETTY_PRINT);
}
?>

Key Features:

  1. Supports multiple proxy formats:

    • user:pass@ip:port

    • ip:port:user:pass

    • ip:port

  2. Security Measures:

    • IP whitelisting

    • Rate limiting

    • Input validation

    • Error handling

  3. Connection Testing:

    • Tests actual proxy functionality using HTTPS

    • Verifies credentials

    • Checks response from a reliable test URL

  4. Output:

    • JSON formatted response

    • Detailed status information

    • Error reporting

Usage:

bash
Copy
Download
http://yourdomain.com/proxy-checker.php?proxy=UeG0Cr:hZQOyx@173.212.239.181:38875

Response Examples:
Success:

json
Copy
Download
{
    "status": "live",
    "proxy": {
        "ip": "173.212.239.181",
        "port": "38875",
        "user": "UeG0Cr",
        "pass": "hZQOyx"
    },
    "response_code": 200,
    "error": null
}

Error:

json
Copy
Download
{
    "error": "Invalid proxy format",
    "received_proxy": "invalid:proxy:string"
}

Requirements:

  1. PHP 7.4+ with cURL enabled

  2. VPS with outgoing network access

  3. Proper security configuration (firewall, etc.)

Important Notes:

  1. Replace YOUR_VPS_IP with your actual VPS IP address

  2. Consider adding authentication for the endpoint

  3. Monitor usage to prevent abuse

  4. Adjust timeouts and test URL as needed

  5. Regularly update the script for security patches

This script provides a balance between functionality and security while supporting the different proxy formats you specified.

Post a Comment

Previous Post Next Post