Files
2025-03-30 19:37:01 +03:00

86 lines
2.5 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use App\Models\Proxy;
class FetchProxies extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'proxies:fetch';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fetch proxies from the CDN and save them to the database';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('Fetching proxies from CDN...');
try {
$response = Http::get('https://cdn.jsdelivr.net/gh/proxifly/free-proxy-list@main/proxies/all/data.json');
if (!$response->successful()) {
throw new \Exception('Failed to fetch proxies: ' . $response->status());
}
$proxies = $response->json();
$this->info('Found ' . count($proxies) . ' proxies');
$bar = $this->output->createProgressBar(count($proxies));
$bar->start();
$saved = 0;
$skipped = 0;
foreach ($proxies as $proxyData) {
// Check if proxy already exists
$exists = Proxy::where('proxy', $proxyData['proxy'])->exists();
if (!$exists) {
Proxy::create([
'proxy' => $proxyData['proxy'],
'protocol' => $proxyData['protocol'],
'ip' => $proxyData['ip'],
'port' => $proxyData['port'],
'https' => $proxyData['https'],
'anonymity' => $proxyData['anonymity'],
'score' => $proxyData['score'],
'country' => $proxyData['geolocation']['country'] ?? null,
'city' => $proxyData['geolocation']['city'] ?? null,
]);
$saved++;
} else {
$skipped++;
}
$bar->advance();
}
$bar->finish();
$this->newLine();
$this->info("Proxies imported successfully!");
$this->info("New proxies saved: {$saved}");
$this->info("Existing proxies skipped: {$skipped}");
} catch (\Exception $e) {
$this->error('Error: ' . $e->getMessage());
return 1;
}
return 0;
}
}