feat: add switchy rulelist format support

This commit is contained in:
Rekryt
2024-09-03 11:50:17 +03:00
parent 0246415cac
commit 08e03194c8
2 changed files with 63 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
<?php
namespace OpenCCK\App\Controller;
use OpenCCK\Domain\Factory\SiteFactory;
class SwitchyController extends AbstractIPListController {
/**
* @return string
*/
public function getBody(): string {
$this->setHeaders(['content-type' => 'text/plain']);
$sites = SiteFactory::normalizeArray($this->request->getQueryParameters()['site'] ?? []);
$data = $this->request->getQueryParameter('data') ?? '';
if ($data != 'domains') {
return "# Error: The 'data' GET parameter is must be 'domains'";
}
$response = [
'; Switchy - RuleList',
'; Date: ' . date('Y-m-d'),
'; URL: ' .
$this->getBaseURL() .
'/?format=switchy&data=domains' .
(count($sites)
? '&' . implode('&', array_map(fn(string $site) => 'site=' . urlencode($site), $sites))
: ''),
'',
'#BEGIN',
'',
'[Wildcard]',
];
$domains = [];
if (count($sites)) {
foreach ($sites as $site) {
$domains = array_merge(
$domains,
array_map($this->wildcardFormat(...), $this->service->sites[$site]->$data)
);
}
} else {
foreach ($this->service->sites as $siteEntity) {
$domains = array_merge($domains, array_map($this->wildcardFormat(...), $siteEntity->$data));
}
}
$response = array_merge($response, SiteFactory::normalizeArray($domains));
$response = array_merge($response, ['', '#END']);
return implode("\n", $response);
}
/**
* @param string $domain
* @return string
*/
private function wildcardFormat(string $domain): string {
return '*' . $domain . '/*';
}
}

View File

@@ -614,6 +614,7 @@ use OpenCCK\App\Controller\TextController;
<option value="mikrotik">MikroTik</option>
<option value="text">Text</option>
<option value="comma">Comma</option>
<option value="switchy">Switchy RuleList</option>
</select>
</label>
<label class="main-formItem">