mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
privoxy: Use privileged decorator for actions
Tests: - App installation works. Proxying works when configured with Firefox. listen-address and permit-access directives are set as expected in the configuration file. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
2 files changed
+10
-38
No files matched your search
@@ -6,7 +6,7 @@ FreedomBox app to configure Privoxy.
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from plinth import action_utils, actions
|
||||
from plinth import action_utils
|
||||
from plinth import app as app_module
|
||||
from plinth import cfg, frontpage, menu
|
||||
from plinth.daemon import Daemon
|
||||
@@ -17,7 +17,7 @@ from plinth.modules.users.components import UsersAndGroups
|
||||
from plinth.package import Packages
|
||||
from plinth.utils import format_lazy
|
||||
|
||||
from . import manifest
|
||||
from . import manifest, privileged
|
||||
|
||||
_description = [
|
||||
_('Privoxy is a non-caching web proxy with advanced filtering '
|
||||
@@ -97,9 +97,9 @@ class PrivoxyApp(app_module.App):
|
||||
|
||||
def setup(helper, old_version=None):
|
||||
"""Install and configure the module."""
|
||||
helper.call('pre', actions.superuser_run, 'privoxy', ['pre-install'])
|
||||
helper.call('pre', privileged.pre_install)
|
||||
app.setup(old_version)
|
||||
helper.call('post', actions.superuser_run, 'privoxy', ['setup'])
|
||||
helper.call('post', privileged.setup)
|
||||
helper.call('post', app.enable)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
"""Configure privoxy."""
|
||||
|
||||
import pathlib
|
||||
|
||||
import augeas
|
||||
|
||||
from plinth import action_utils
|
||||
from plinth.actions import privileged
|
||||
|
||||
PRIVOXY_CONF_PATH = pathlib.Path('/etc/privoxy/config')
|
||||
|
||||
|
||||
@privileged
|
||||
def pre_install():
|
||||
"""Preseed debconf values before packages are installed."""
|
||||
action_utils.debconf_set_selections(
|
||||
['privoxy privoxy/listen-address string [::]:8118'])
|
||||
|
||||
|
||||
@privileged
|
||||
def setup():
|
||||
"""Setup Privoxy configuration after installing it."""
|
||||
_restrict_access()
|
||||
|
||||
|
||||
def _load_augeus():
|
||||
"""Initialize Augeas."""
|
||||
aug = augeas.Augeas(flags=augeas.Augeas.NO_LOAD +
|
||||
augeas.Augeas.NO_MODL_AUTOLOAD)
|
||||
aug.transform('Spacevars', str(PRIVOXY_CONF_PATH))
|
||||
aug.set('/augeas/context', '/files' + str(PRIVOXY_CONF_PATH))
|
||||
aug.load()
|
||||
return aug
|
||||
|
||||
|
||||
def _restrict_access():
|
||||
"""Make sure Privoxy isn't available over the Internet."""
|
||||
# https://en.wikipedia.org/wiki/localhost
|
||||
# https://en.wikipedia.org/wiki/Private_network
|
||||
# https://en.wikipedia.org/wiki/Link-local_address
|
||||
# https://en.wikipedia.org/wiki/Unique_local_address
|
||||
ip_ranges = [
|
||||
'127.0.0.0/8', # IPv4 loopback address
|
||||
'10.0.0.0/8', # IPv4 private address
|
||||
'172.16.0.0/12', # IPv4 private address
|
||||
'192.168.0.0/16', # IPv4 private address
|
||||
'169.254.0.0/16', # IPv4 auto-configuration
|
||||
'[::1]', # IPv4 loopback address
|
||||
'[fc00::]/7', # IPv6 unique local addresses
|
||||
'[fe80::]/10', # IPv6 auto-configuration
|
||||
]
|
||||
aug = _load_augeus()
|
||||
for ip_range in ip_ranges:
|
||||
matches = [
|
||||
match for match in aug.match('permit-access')
|
||||
if aug.get(match) == ip_range
|
||||
]
|
||||
if not any(matches):
|
||||
aug.set('permit-access[last() + 1]', ip_range)
|
||||
|
||||
aug.save()
|
||||
Reference in new issue
Block a user