Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,14 @@ private function getBlocklistDescription($shortcode)
return null;
}

private function getFlattenedCustomDomains($key)
{
return array_values(array_unique(array_filter(
array_merge(
...array_values(array_map(fn($v) => array_keys($v[$key] ?? []), $this->nodes))
),
'strlen'
)));
}

public function isEnabledAction()
{
$config = Config::getInstance()->object();
return [
'enabled' => $this->mdl->getNodes()['general']['stats']
];
}

/* XXX deprecated and to be removed for 26.7 */
public function isBlockListEnabledAction()
{
return ['enabled' => (bool)array_filter($this->nodes, fn($v) => $v['enabled'])];
Expand All @@ -98,9 +88,6 @@ public function totalsAction($maximum)
$parsed['top_blocked'][$domain]['blocklist'] ??= $this->getBlocklistDescription($props['blocklist']);
}

$parsed['allowlisted_domains'] = $this->getFlattenedCustomDomains('allowlists');
$parsed['blocklisted_domains'] = $this->getFlattenedCustomDomains('blocklists');

return $parsed;
}

Expand Down Expand Up @@ -142,9 +129,25 @@ public function searchQueriesAction()
}

$response = $this->searchRecordsetBase($parsed);
$response['allowlisted_domains'] = $this->getFlattenedCustomDomains('allowlists');
$response['blocklisted_domains'] = $this->getFlattenedCustomDomains('blocklists');

return $response;
}

public function getPoliciesAction($uuid = null)
{
$response = ['policies' => []];

foreach ($this->mdl->dnsbl->blocklist->iterateItems() as $policy_uuid => $policy) {
if ($uuid !== null && $uuid == $policy_uuid) {
$response = $policy->getNodeContent();
$response['uuid'] = $policy_uuid;
break;
}

$response['policies'][$policy_uuid] = $policy->getNodeContent();
}

$response['blocklistDescriptions'] = $this->types;
return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ class SettingsController extends ApiMutableModelControllerBase
public function updateBlocklistAction()
{
$result = ["status" => "failed"];
if ($this->request->isPost() && $this->request->hasPost('domain') && $this->request->hasPost('type')) {
if ($this->request->isPost() && $this->request->hasPost('domain') && $this->request->hasPost('type') && $this->request->hasPost('uuid')) {
Config::getInstance()->lock();
$domain = $this->request->getPost('domain');
$type = $this->request->getPost('type');
$uuid = $this->request->getPost('uuid');
$action = 'block';
$mdl = $this->getModel();

$remove = function ($csv, $part) {
Expand All @@ -56,42 +58,44 @@ public function updateBlocklistAction()
return implode(',', $csv);
};

foreach ($mdl->dnsbl->blocklist->iterateItems() as $uuid => $node) {
$node = $mdl->getNodeByReference('dnsbl.blocklist.' . $uuid);
$modelType = $node->$type;

if ($node != null) {
// strip off any trailing dot
$value = rtrim($domain, '.');

$existing_domains = explode(',', (string)$modelType);
if (in_array($value, $existing_domains)) {
// value already in model
continue;
}

$wl = explode(',', (string)$node->allowlists);
$bl = explode(',', (string)$node->blocklists);

// Check if domains should be switched around in the model
if ($type == 'allowlists' && in_array($value, $bl)) {
$node->blocklists = $remove($bl, $value);
} elseif ($type == 'blocklists' && in_array($value, $wl)) {
$node->allowlists = $remove($wl, $value);
}

// update the model
$list = array_filter($existing_domains); // removes all empty entries
$list[] = $value;
$node->$type = implode(',', $list);
}
$node = $mdl->getNodeByReference('dnsbl.blocklist.' . $uuid);
if ($node == null) {
return $result;
}

$modelType = $node->$type;
// strip off any trailing dot
$value = rtrim($domain, '.');

$existing_domains = explode(',', (string)$modelType);
if (in_array($value, $existing_domains)) {
// value already in model
return $result;
}

$wl = explode(',', (string)$node->allowlists);
$bl = explode(',', (string)$node->blocklists);

// Check if domains should be switched around in the model
if ($type == 'allowlists' && in_array($value, $bl)) {
$node->blocklists = $remove($bl, $value);
} elseif ($type == 'blocklists' && in_array($value, $wl)) {
$node->allowlists = $remove($wl, $value);
}

// update the model
$list = array_filter($existing_domains); // removes all empty entries
$list[] = $value;
$node->$type = implode(',', $list);

$mdl->serializeToConfig();
Config::getInstance()->save();

$service = new \OPNsense\Unbound\Api\ServiceController();
$result = $service->dnsblAction();
if ($type == 'allowlists') {
$action = 'allow';
}

$result = (new Backend())->configdpRun('unbound dnsblquick', [$uuid, $domain, $action]);
}

return $result;
Expand Down
Loading