60 lines
2.1 KiB
PHP
60 lines
2.1 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Http\Controllers;
|
||
|
|
|
||
|
|
use App\Models\Proxy;
|
||
|
|
use Illuminate\Http\Response;
|
||
|
|
|
||
|
|
class PacFileController extends Controller
|
||
|
|
{
|
||
|
|
public function generate()
|
||
|
|
{
|
||
|
|
// Get active proxies from the database
|
||
|
|
$proxies = Proxy::where('is_active', true)
|
||
|
|
->orderBy('score', 'desc')
|
||
|
|
->limit(10)
|
||
|
|
->get();
|
||
|
|
|
||
|
|
// Generate PAC file content
|
||
|
|
$pacContent = $this->generatePacContent($proxies);
|
||
|
|
|
||
|
|
// Return the PAC file with proper headers
|
||
|
|
return response($pacContent)
|
||
|
|
->header('Content-Type', 'application/x-ns-proxy-autoconfig')
|
||
|
|
->header('Content-Disposition', 'attachment; filename="proxy.pac"');
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function generatePacContent($proxies)
|
||
|
|
{
|
||
|
|
// Start with the PAC file header
|
||
|
|
$content = "function FindProxyForURL(url, host) {\n";
|
||
|
|
$content .= " // List of proxy servers\n";
|
||
|
|
$content .= " var proxyList = [\n";
|
||
|
|
|
||
|
|
// Add proxy servers to the list
|
||
|
|
foreach ($proxies as $proxy) {
|
||
|
|
$content .= " 'PROXY {$proxy->ip}:{$proxy->port}',\n";
|
||
|
|
}
|
||
|
|
|
||
|
|
// Add fallback to DIRECT if no proxies are available
|
||
|
|
$content .= " 'DIRECT'\n";
|
||
|
|
$content .= " ];\n\n";
|
||
|
|
|
||
|
|
// Add proxy selection logic
|
||
|
|
$content .= " // Don't proxy localhost\n";
|
||
|
|
$content .= " if (isPlainHostName(host) ||\n";
|
||
|
|
$content .= " shExpMatch(host, '*.local') ||\n";
|
||
|
|
$content .= " isInNet(dnsResolve(host), '10.0.0.0', '255.0.0.0') ||\n";
|
||
|
|
$content .= " isInNet(dnsResolve(host), '172.16.0.0', '255.240.0.0') ||\n";
|
||
|
|
$content .= " isInNet(dnsResolve(host), '192.168.0.0', '255.255.0.0') ||\n";
|
||
|
|
$content .= " isInNet(dnsResolve(host), '127.0.0.0', '255.0.0.0'))\n";
|
||
|
|
$content .= " return 'DIRECT';\n\n";
|
||
|
|
|
||
|
|
// Add proxy selection based on URL
|
||
|
|
$content .= " // Use proxy for all other URLs\n";
|
||
|
|
$content .= " return proxyList.join('; ');\n";
|
||
|
|
$content .= "}\n";
|
||
|
|
|
||
|
|
return $content;
|
||
|
|
}
|
||
|
|
}
|