79 lines
2.3 KiB
PHP
79 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Services\ProxyService;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use GuzzleHttp\Client;
|
||
|
|
use GuzzleHttp\Exception\RequestException;
|
||
|
|
|
||
|
|
class ProxyCheckerController extends Controller
|
||
|
|
{
|
||
|
|
protected $proxyService;
|
||
|
|
|
||
|
|
public function __construct(ProxyService $proxyService)
|
||
|
|
{
|
||
|
|
$this->proxyService = $proxyService;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
return view('proxy-checker.index');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function check(Request $request)
|
||
|
|
{
|
||
|
|
$request->validate([
|
||
|
|
'ip' => 'required|ip',
|
||
|
|
'port' => 'required|integer|min:1|max:65535',
|
||
|
|
'type' => 'required|in:http,https,socks4,socks5',
|
||
|
|
'test_url' => 'required|url'
|
||
|
|
]);
|
||
|
|
|
||
|
|
$proxy = [
|
||
|
|
'ip' => $request->ip,
|
||
|
|
'port' => $request->port,
|
||
|
|
'type' => $request->type,
|
||
|
|
'url' => $request->test_url
|
||
|
|
];
|
||
|
|
|
||
|
|
try {
|
||
|
|
$client = new Client([
|
||
|
|
'timeout' => 10,
|
||
|
|
'verify' => false,
|
||
|
|
'proxy' => [
|
||
|
|
'http' => "{$proxy['type']}://{$proxy['ip']}:{$proxy['port']}",
|
||
|
|
'https' => "{$proxy['type']}://{$proxy['ip']}:{$proxy['port']}"
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
|
||
|
|
$startTime = microtime(true);
|
||
|
|
$response = $client->get($proxy['url']);
|
||
|
|
$endTime = microtime(true);
|
||
|
|
|
||
|
|
$speed = round(($endTime - $startTime) * 1000, 2); // Convert to milliseconds
|
||
|
|
|
||
|
|
return response()->json([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Proxy is working!',
|
||
|
|
'data' => [
|
||
|
|
'status_code' => $response->getStatusCode(),
|
||
|
|
'speed' => $speed,
|
||
|
|
'headers' => $response->getHeaders(),
|
||
|
|
'content_type' => $response->getHeaderLine('Content-Type')
|
||
|
|
]
|
||
|
|
]);
|
||
|
|
} catch (RequestException $e) {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => 'Proxy check failed: ' . $e->getMessage(),
|
||
|
|
'error' => $e->getCode()
|
||
|
|
], 400);
|
||
|
|
} catch (\Exception $e) {
|
||
|
|
return response()->json([
|
||
|
|
'success' => false,
|
||
|
|
'message' => 'An error occurred: ' . $e->getMessage()
|
||
|
|
], 500);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|