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) { return new \GuzzleHttp\Promise\Promise(function() use ($proxy, &$results) { try { $client = new \GuzzleHttp\Client([ 'proxy' => $proxy->proxy, 'timeout' => $this->timeout, 'verify' => false, 'http_errors' => false ]); $response = $client->get($this->testUrl); $body = json_decode($response->getBody(), true); if ($response->getStatusCode() === 200 && isset($body['unique_id'])) { // Proxy is working, increase score $proxy->increment('score'); $results['working']++; } else { // Proxy returned invalid response $proxy->decrement('score'); $results['failed']++; } } catch (\GuzzleHttp\Exception\ConnectException $e) { // Connection failed $proxy->decrement('score'); $results['failed']++; } catch (\GuzzleHttp\Exception\RequestException $e) { if ($e->getCode() === CURLE_OPERATION_TIMEDOUT) { // Request timed out $proxy->decrement('score'); $results['timeout']++; } else { // Other request error $proxy->decrement('score'); $results['failed']++; } } }); } }