feat: add net PAC format

This commit is contained in:
Rekryt
2024-12-20 12:49:14 +03:00
parent 1fefcc07e5
commit 3993c02c65
3 changed files with 102 additions and 26 deletions

View File

@@ -18,19 +18,20 @@ Demo URL: [https://iplist.opencck.org](https://iplist.opencck.org)
- **whois, ipcalc (Linux utilities)**
# Formats of Output
| format | description |
|----------|-----------------------|
| json | JSON format |
| text | Newline-separated |
| comma | Comma-separated |
| mikrotik | MikroTik Script |
| switchy | SwitchyOmega RuleList |
| nfset | Dnsmasq nfset |
| ipset | Dnsmasq ipset |
| clashx | ClashX |
| kvas | Keenetic KVAS |
| bat | Keenetic Routes .bat |
| amnezia | Amnezia filter list |
| format | description |
|----------|-------------------------------|
| json | JSON format |
| text | Newline-separated |
| comma | Comma-separated |
| mikrotik | MikroTik Script |
| switchy | SwitchyOmega RuleList |
| nfset | Dnsmasq nfset |
| ipset | Dnsmasq ipset |
| clashx | ClashX |
| kvas | Keenetic KVAS |
| bat | Keenetic Routes .bat |
| amnezia | Amnezia filter list |
| pac | Proxy Auto-Configuration file |
## Configuration

View File

@@ -22,19 +22,20 @@ Demo URL: [https://iplist.opencck.org](https://iplist.opencck.org)
- whois, ipcalc (linux)
# Форматы выгрузки
| формат | описание |
|----------|--------------------------|
| json | JSON формат |
| text | Разделение новой строкой |
| comma | Разделение запятыми |
| mikrotik | MikroTik Script |
| switchy | SwitchyOmega RuleList |
| nfset | Dnsmasq nfset |
| ipset | Dnsmasq ipset |
| clashx | ClashX |
| kvas | Keenetic KVAS |
| bat | Keenetic Routes .bat |
| amnezia | Amnezia filter list |
| формат | описание |
|----------|-------------------------------|
| json | JSON формат |
| text | Разделение новой строкой |
| comma | Разделение запятыми |
| mikrotik | MikroTik Script |
| switchy | SwitchyOmega RuleList |
| nfset | Dnsmasq nfset |
| ipset | Dnsmasq ipset |
| clashx | ClashX |
| kvas | Keenetic KVAS |
| bat | Keenetic Routes .bat |
| amnezia | Amnezia filter list |
| pac | Proxy Auto-Configuration file |
## Настройки
Конфигурационные файлы хранятся в `config/<группа>/<портал>.json`. Каждый JSON файл представляет собой конфигурацию для конкретного портала, задавая домены для мониторинга и источники начальных данных по IP и CIDR.

View File

@@ -0,0 +1,74 @@
<?php
namespace OpenCCK\App\Controller;
use OpenCCK\Domain\Factory\SiteFactory;
use OpenCCK\Domain\Helper\IP4Helper;
class PacController extends AbstractIPListController {
/**
* @return string
*/
public function getBody(): string {
$this->setHeaders(['content-type' => 'text/javascript']);
$sites = SiteFactory::normalizeArray($this->request->getQueryParameters()['site'] ?? []);
$data = $this->request->getQueryParameter('data') ?? '';
$template = $this->request->getQueryParameter('template') ?? 'PROXY 127.0.0.1:2080; DIRECT';
if (!in_array($data, ['domains', 'cidr4'])) {
return "# Error: The 'data' GET parameter is must be 'domains' or 'cidr4'";
}
$items = [];
if (count($sites)) {
foreach ($sites as $site) {
$items = array_merge($items, $this->getSites()[$site]->$data);
}
} else {
foreach ($this->getSites() as $siteEntity) {
$items = array_merge($items, $siteEntity->$data);
}
}
$response = ['const items = ['];
$response[] = implode(
",\n",
$data == 'cidr4'
? array_map(function (string $item) {
$parts = explode('/', $item);
$mask = IP4Helper::formatShortIpMask($parts[1] ?? '');
return '["' . $parts[0] . '","' . $mask . '"]';
}, SiteFactory::normalizeArray($items, true))
: array_map(fn(string $item) => '"' . $item . '"', SiteFactory::normalizeArray($items))
);
$response[] = '];';
if ($data == 'cidr4') {
$response = array_merge($response, [
'function FindProxyForURL(url, host) {',
' for (cidr of items) {',
' if (isInNet(host, cidr[0], cidr[1])) {',
' return "' . $template . '";',
' }',
' }',
'',
' return "DIRECT";',
'}',
]);
} else {
$response = array_merge($response, [
'function FindProxyForURL(url, host) {',
' for (domain of items) {',
' if (host === domain || shExpMatch(host, "*." + domain)) {',
' return "' . $template . '";',
' }',
' }',
'',
' return "DIRECT";',
'}',
]);
}
return implode("\n", $response);
}
}