mirror of
https://github.com/rekryt/iplist.git
synced 2025-10-12 16:39:35 +03:00
feat: add switchy rulelist format support
This commit is contained in:
62
src/App/Controller/SwitchyController.php
Normal file
62
src/App/Controller/SwitchyController.php
Normal 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 . '/*';
|
||||
}
|
||||
}
|
@@ -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">
|
||||
|
Reference in New Issue
Block a user