-
Notifications
You must be signed in to change notification settings - Fork 19
feat(adblock): implement device-based enforcement and migration scripts #1875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
m-dilorenzi
wants to merge
2
commits into
main
Choose a base branch
from
fix_dns_adblock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #!/bin/sh | ||
|
|
||
| # | ||
| # Copyright (C) 2026 Nethesis S.r.l. | ||
| # SPDX-License-Identifier: GPL-2.0-only | ||
| # | ||
|
|
||
| # Rebuild the adblock local DNS enforcement on systems upgraded from adblock 4.1.5, | ||
| # where adb_nftdevforce ended up holding firewall zone names instead of devices. | ||
|
|
||
| set -e | ||
|
|
||
| /usr/libexec/ts-dns-migrate-devices |
54 changes: 54 additions & 0 deletions
54
packages/ns-threat_shield/files/adblock-devices-migrate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| # | ||
| # Copyright (C) 2026 Nethesis S.r.l. | ||
| # SPDX-License-Identifier: GPL-2.0-only | ||
| # | ||
|
|
||
| # this script is supposed to be run by the 36_ns-threat_shield uci defaults | ||
| # | ||
| # Up to adblock 4.1.5 the local DNS enforcement was configured through adb_zonelist, | ||
| # holding firewall zone names, and adblock turned it into uci redirect sections | ||
| # letting fw4 resolve the zones. Since 4.5.5 the enforcement is rendered as nft rules | ||
| # matching on iifname, so adb_nftdevforce needs device names: the zone names stored | ||
| # there produce rules that never match and DNS traffic is no longer redirected. | ||
| # | ||
| # Move the selection to ns_tsdns_zones and rebuild adb_nftdevforce out of it. | ||
|
|
||
| import subprocess | ||
|
|
||
| from euci import EUci | ||
| from nethsec import utils | ||
|
|
||
|
|
||
| def migrate_zones(): | ||
| e_uci = EUci() | ||
|
|
||
| if list(e_uci.get('adblock', 'global', 'ns_tsdns_zones', list=True, default=[])): | ||
| # already migrated | ||
| return False | ||
|
|
||
| zones = list(e_uci.get('adblock', 'global', 'adb_nftdevforce', list=True, default=[])) | ||
| if not zones: | ||
| return False | ||
|
|
||
| devices = [] | ||
| for zone in zones: | ||
| for device in utils.get_all_devices_by_zone(e_uci, zone, exclude_aliases=True): | ||
| if device not in devices: | ||
| devices.append(device) | ||
|
|
||
| if not devices: | ||
| # the stored values are not zone names, or the zones have no interface: | ||
| # leave the configuration untouched rather than clearing it | ||
| return False | ||
|
|
||
| e_uci.set('adblock', 'global', 'ns_tsdns_zones', zones) | ||
| e_uci.set('adblock', 'global', 'adb_nftdevforce', sorted(devices)) | ||
| e_uci.commit('adblock') | ||
| return True | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if migrate_zones(): | ||
| subprocess.run(["/etc/init.d/adblock", "restart"], capture_output=True) |
40 changes: 40 additions & 0 deletions
40
packages/ns-threat_shield/files/configure-adblock-devices.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #!/usr/bin/python | ||
|
|
||
| # | ||
| # Copyright (C) 2026 Nethesis S.r.l. | ||
| # SPDX-License-Identifier: GPL-2.0-only | ||
| # | ||
|
|
||
| # This script keeps the adblock local DNS enforcement aligned with the network setup: | ||
| # adb_nftdevforce is rendered as an nft iifname match, so it must list the devices of | ||
| # the zones selected in ns_tsdns_zones. Without this, adding an interface to an | ||
| # enforced zone would silently leave its DNS traffic unfiltered. | ||
|
|
||
| # The changes variable is already within the scope from the caller | ||
| if 'adblock' in changes or 'firewall' in changes or 'network' in changes: | ||
| import syslog | ||
| from euci import EUci | ||
| from nethsec import utils | ||
|
|
||
| uci = EUci() | ||
| zones = list(uci.get('adblock', 'global', 'ns_tsdns_zones', list=True, default=[])) | ||
|
|
||
| if zones: | ||
| devices = [] | ||
| for zone in zones: | ||
| for device in utils.get_all_devices_by_zone(uci, zone, exclude_aliases=True): | ||
| if device not in devices: | ||
| devices.append(device) | ||
| # keep a stable order, the value is compared before being rewritten | ||
| devices = sorted(devices) | ||
|
|
||
| current = list(uci.get('adblock', 'global', 'adb_nftdevforce', list=True, default=[])) | ||
| if devices != current: | ||
| if devices: | ||
| uci.set('adblock', 'global', 'adb_nftdevforce', devices) | ||
| else: | ||
| uci.delete('adblock', 'global', 'adb_nftdevforce') | ||
| uci.save('adblock') | ||
| # adblock is reloaded by the uci reload_config at the end of the commit | ||
| if 'adblock' not in changes: | ||
| changes['adblock'] = {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.