Here's a PHP script to check proxy status with security considerations and support for multiple proxy formats:
<?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:
Supports multiple proxy formats:
user:pass@ip:port
ip:port:user:pass
ip:port
Security Measures:
IP whitelisting
Rate limiting
Input validation
Error handling
Connection Testing:
Tests actual proxy functionality using HTTPS
Verifies credentials
Checks response from a reliable test URL
Output:
JSON formatted response
Detailed status information
Error reporting
Usage:
http://yourdomain.com/proxy-checker.php?proxy=UeG0Cr:hZQOyx@173.212.239.181:38875
Response Examples:
Success:
{ "status": "live", "proxy": { "ip": "173.212.239.181", "port": "38875", "user": "UeG0Cr", "pass": "hZQOyx" }, "response_code": 200, "error": null }
Error:
{ "error": "Invalid proxy format", "received_proxy": "invalid:proxy:string" }
Requirements:
PHP 7.4+ with cURL enabled
VPS with outgoing network access
Proper security configuration (firewall, etc.)
Important Notes:
Replace
YOUR_VPS_IP
with your actual VPS IP addressConsider adding authentication for the endpoint
Monitor usage to prevent abuse
Adjust timeouts and test URL as needed
Regularly update the script for security patches
This script provides a balance between functionality and security while supporting the different proxy formats you specified.
إرسال تعليق