131 lines
4.1 KiB
PHP
131 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use App\Models\Proxy;
|
|
|
|
class TestProxies extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'proxies:test {--timeout=10} {--concurrent=10}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Test proxies against the test endpoint';
|
|
|
|
protected $testUrl;
|
|
protected $timeout;
|
|
protected $concurrent;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->testUrl = config('app.url') . '/proxy-test';
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->timeout = $this->option('timeout');
|
|
$this->concurrent = $this->option('concurrent');
|
|
|
|
$this->info('Starting proxy testing...');
|
|
$this->info("Test URL: {$this->testUrl}");
|
|
$this->info("Timeout: {$this->timeout}s");
|
|
$this->info("Concurrent tests: {$this->concurrent}");
|
|
|
|
$proxies = Proxy::all();
|
|
$this->info('Found ' . $proxies->count() . ' proxies to test');
|
|
|
|
$bar = $this->output->createProgressBar($proxies->count());
|
|
$bar->start();
|
|
|
|
$chunks = $proxies->chunk($this->concurrent);
|
|
$results = [
|
|
'working' => 0,
|
|
'failed' => 0,
|
|
'timeout' => 0
|
|
];
|
|
|
|
foreach ($chunks as $chunk) {
|
|
$promises = [];
|
|
foreach ($chunk as $proxy) {
|
|
$promises[] = $this->testProxy($proxy, $results);
|
|
}
|
|
|
|
// Wait for all promises to complete
|
|
foreach ($promises as $promise) {
|
|
$promise->wait();
|
|
}
|
|
|
|
$bar->advance($chunk->count());
|
|
}
|
|
|
|
$bar->finish();
|
|
$this->newLine();
|
|
$this->info('Proxy testing completed!');
|
|
$this->info("Working proxies: {$results['working']}");
|
|
$this->info("Failed proxies: {$results['failed']}");
|
|
$this->info("Timeout proxies: {$results['timeout']}");
|
|
}
|
|
|
|
protected function testProxy($proxy, &$results)
|
|
{
|
|
$client = new \GuzzleHttp\Client([
|
|
'timeout' => $this->timeout,
|
|
'verify' => false,
|
|
'http_errors' => false
|
|
]);
|
|
|
|
return $client->getAsync($this->testUrl, [
|
|
'proxy' => $proxy->proxy
|
|
])->then(
|
|
function($response) use ($proxy, &$results) {
|
|
$body = json_decode($response->getBody(), true);
|
|
|
|
if ($response->getStatusCode() === 200 && isset($body['unique_id'])) {
|
|
// Proxy is working, increase score
|
|
$proxy->increment('score');
|
|
$proxy->update(['last_tested_at' => now()]);
|
|
$results['working']++;
|
|
} else {
|
|
// Proxy returned invalid response
|
|
$proxy->decrement('score');
|
|
$proxy->update(['last_tested_at' => now()]);
|
|
$results['failed']++;
|
|
}
|
|
},
|
|
function($exception) use ($proxy, &$results) {
|
|
if ($exception instanceof \GuzzleHttp\Exception\ConnectException) {
|
|
// Connection failed
|
|
$proxy->decrement('score');
|
|
$proxy->update(['last_tested_at' => now()]);
|
|
$results['failed']++;
|
|
} elseif ($exception instanceof \GuzzleHttp\Exception\RequestException) {
|
|
if ($exception->getCode() === CURLE_OPERATION_TIMEDOUT) {
|
|
// Request timed out
|
|
$proxy->decrement('score');
|
|
$proxy->update(['last_tested_at' => now()]);
|
|
$results['timeout']++;
|
|
} else {
|
|
// Other request error
|
|
$proxy->decrement('score');
|
|
$proxy->update(['last_tested_at' => now()]);
|
|
$results['failed']++;
|
|
}
|
|
}
|
|
}
|
|
);
|
|
}
|
|
}
|