mirror of
https://github.com/rekryt/iplist.git
synced 2025-10-13 08:59:34 +03:00
feat: use groups for mikrotik lists names, update script format
This commit is contained in:
@@ -45,4 +45,15 @@ abstract class AbstractIPListController extends AbstractController {
|
|||||||
return $site;
|
return $site;
|
||||||
}, $this->service->sites);
|
}, $this->service->sites);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, array<string, Site>>
|
||||||
|
*/
|
||||||
|
protected function getGroups(): array {
|
||||||
|
$groups = [];
|
||||||
|
foreach ($this->service->sites as $siteEntity) {
|
||||||
|
$groups[$siteEntity->group][$siteEntity->name] = $siteEntity;
|
||||||
|
}
|
||||||
|
return $groups;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,21 +6,11 @@ use OpenCCK\Domain\Entity\Site;
|
|||||||
use OpenCCK\Domain\Factory\SiteFactory;
|
use OpenCCK\Domain\Factory\SiteFactory;
|
||||||
|
|
||||||
class MainController extends AbstractIPListController {
|
class MainController extends AbstractIPListController {
|
||||||
/**
|
|
||||||
* @var array<string, array<string, Site>>
|
|
||||||
*/
|
|
||||||
private array $groups = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getBody(): string {
|
public function getBody(): string {
|
||||||
$this->setHeaders(['content-type' => 'text/html; charset=utf-8']);
|
$this->setHeaders(['content-type' => 'text/html; charset=utf-8']);
|
||||||
|
|
||||||
foreach ($this->service->sites as $siteEntity) {
|
|
||||||
$this->groups[$siteEntity->group][$siteEntity->name] = $siteEntity;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->renderTemplate('index');
|
return $this->renderTemplate('index');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace OpenCCK\App\Controller;
|
namespace OpenCCK\App\Controller;
|
||||||
|
|
||||||
|
use OpenCCK\Domain\Entity\Site;
|
||||||
use OpenCCK\Domain\Factory\SiteFactory;
|
use OpenCCK\Domain\Factory\SiteFactory;
|
||||||
|
|
||||||
class MikrotikController extends AbstractIPListController {
|
class MikrotikController extends AbstractIPListController {
|
||||||
@@ -18,36 +19,44 @@ class MikrotikController extends AbstractIPListController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$response = [];
|
$response = [];
|
||||||
|
foreach ($this->getGroups() as $groupName => $groupSites) {
|
||||||
if (count($sites)) {
|
if (count($sites)) {
|
||||||
foreach ($sites as $site) {
|
$groupSites = array_filter($groupSites, fn(Site $siteEntity) => in_array($siteEntity->name, $sites));
|
||||||
$response = array_merge($response, $this->generateList($site, $this->getSites()[$site]->$data));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
foreach ($this->getSites() as $siteEntity) {
|
|
||||||
$response = array_merge($response, $this->generateList($siteEntity->name, $siteEntity->$data));
|
|
||||||
}
|
}
|
||||||
|
if (!count($groupSites)) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode(
|
$response = array_merge($response, [
|
||||||
"\n",
|
'/ip firewall address-list remove [find list="' . $groupName . '"];',
|
||||||
array_merge(
|
'/ip firewall address-list',
|
||||||
['/ip firewall address-list'],
|
]);
|
||||||
SiteFactory::normalizeArray($response, in_array($data, ['ipv4', 'ipv6', 'cidr4', 'cidr6']))
|
$items = [];
|
||||||
)
|
foreach ($groupSites as $siteName => $siteEntity) {
|
||||||
);
|
if (count($sites) && !in_array($siteName, $sites)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$items = array_merge($items, $this->generateList($siteEntity, $siteEntity->$data));
|
||||||
|
}
|
||||||
|
$items = SiteFactory::normalizeArray($items, in_array($data, ['ip4', 'ip6', 'cidr4', 'cidr6']));
|
||||||
|
$items[count($items) - 1] = $items[count($items) - 1] . ';';
|
||||||
|
|
||||||
|
$response = array_merge($response, $items, ['']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode("\n", $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $site
|
* @param Site $siteEntity
|
||||||
* @param array $array
|
* @param array $array
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function generateList(string $site, array $array): array {
|
private function generateList(Site $siteEntity, array $array): array {
|
||||||
$response = [];
|
$items = [];
|
||||||
$listName = str_replace(' ', '', $site);
|
|
||||||
foreach ($array as $item) {
|
foreach ($array as $item) {
|
||||||
$response[] = 'add list=' . $listName . ' address=' . $item . ' comment=' . $listName;
|
$items[] = 'add list=' . $siteEntity->group . ' address=' . $item . ' comment=' . $siteEntity->name;
|
||||||
}
|
}
|
||||||
return $response;
|
return $items;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -647,7 +647,7 @@ use OpenCCK\App\Controller\MainController;
|
|||||||
<span>
|
<span>
|
||||||
Site:
|
Site:
|
||||||
<select name="site" class="main-formSelect main-formSelect_site" multiple>
|
<select name="site" class="main-formSelect main-formSelect_site" multiple>
|
||||||
<?php foreach ($this->groups as $group => $items): ?>
|
<?php foreach ($this->getGroups() as $group => $items): ?>
|
||||||
<optgroup label="<?= $group ?>">
|
<optgroup label="<?= $group ?>">
|
||||||
<?php foreach ($items as $site): ?>
|
<?php foreach ($items as $site): ?>
|
||||||
<option value="<?= $site->name ?>"><?= $site->name ?></option>
|
<option value="<?= $site->name ?>"><?= $site->name ?></option>
|
||||||
|
Reference in New Issue
Block a user