tor, torproxy: Export settings from old to new app

Tests:

- Install old version of tor app, enable apt transport tor and bridges. Upgrade
to latest code. Notice that new torproxy app is installed, apt transport tor is
enabled and bridges are set. Remaining tor settings are unchanged.

- Install old version of tor app, disable apt transport tor and enable bridges.
Upgrade to latest code. Notice that new torproxy app is installed, apt transport
tor is disabled and bridges are set. Remaining tor settings are unchanged.

- Install old version of tor app, disable tor app and set bridges. Upgrade to
latest code. Notice that new torproxy app is not installed. Remaining tor
settings are unchanged.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
[jvalleroy: Minor update to comment and log message]
Signed-off-by: James Valleroy <jvalleroy@mailbox.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2023-07-25 12:15:09 -07:00 committed by James Valleroy
parent 97b579c4fc
commit 655e4aff1b
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 32 additions and 14 deletions

View File

@ -1,16 +1,18 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""FreedomBox app to configure Tor."""
import json
import logging
from django.utils.translation import gettext_lazy as _
from plinth import action_utils
from plinth import app as app_module
from plinth import cfg, menu
from plinth import cfg, kvstore, menu
from plinth import setup as setup_module
from plinth.daemon import (Daemon, app_is_running, diagnose_netcat,
diagnose_port_listening)
from plinth.modules import torproxy
from plinth.modules.apache.components import Webserver
from plinth.modules.backups.components import BackupRestore
from plinth.modules.firewall.components import Firewall
@ -173,7 +175,8 @@ class TorApp(app_module.App):
"""Install and configure the app."""
super().setup(old_version)
privileged.setup(old_version)
update_hidden_service_domain(utils.get_status())
status = utils.get_status()
update_hidden_service_domain(status)
# Enable/disable Onion-Location component based on app status.
# Component was introduced in version 6.
@ -188,16 +191,20 @@ class TorApp(app_module.App):
component.disable()
# The SOCKS proxy and "Download software packages using Tor" features
# were moved into a new app, Tor Proxy, in version 7. If the "Download
# software packages using Tor" option was enabled, then install and
# enable Tor Proxy, to avoid any issues for apt.
if old_version and old_version < 7:
if self.is_enabled() and is_apt_transport_tor_enabled():
logger.info(
'Tor Proxy app will be installed for apt-transport-tor')
# This creates the operation, which will run after the current
# operation (Tor setup) is completed.
setup_module.run_setup_on_app('torproxy')
# were moved into a new app, Tor Proxy, in version 7. If Tor is
# enabled, then store the relevant configuration, and install Tor
# Proxy.
if old_version and old_version < 7 and self.is_enabled():
logger.info('Tor Proxy app will be installed')
config = {
'use_upstream_bridges': status['use_upstream_bridges'],
'upstream_bridges': status['upstream_bridges'],
'apt_transport_tor': is_apt_transport_tor_enabled()
}
kvstore.set(torproxy.PREINSTALL_CONFIG_KEY, json.dumps(config))
# This creates the operation, which will run after the current
# operation (Tor setup) is completed.
setup_module.run_setup_on_app('torproxy')
if not old_version:
logger.info('Enabling Tor app')

View File

@ -1,13 +1,14 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""FreedomBox app to configure Tor Proxy."""
import json
import logging
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from plinth import app as app_module
from plinth import cfg, frontpage, menu
from plinth import cfg, frontpage, kvstore, menu
from plinth.daemon import Daemon
from plinth.modules.apache.components import diagnose_url
from plinth.modules.backups.components import BackupRestore
@ -20,6 +21,8 @@ from . import manifest, privileged
logger = logging.getLogger(__name__)
PREINSTALL_CONFIG_KEY = 'torproxy_preinstall_config'
_description = [
_('Tor is an anonymous communication system. You can learn more '
'about it from the <a href="https://www.torproject.org/">Tor '
@ -111,9 +114,17 @@ class TorProxyApp(app_module.App):
privileged.setup(old_version)
if not old_version:
logger.info('Enabling apt-transport-tor')
privileged.configure(apt_transport_tor=True)
config = kvstore.get_default(PREINSTALL_CONFIG_KEY, '{}')
config = json.loads(config)
config = {
'use_upstream_bridges': config.get('use_upstream_bridges'),
'upstream_bridges': config.get('upstream_bridges'),
'apt_transport_tor': config.get('apt_transport_tor', True),
}
privileged.configure(**config)
logger.info('Enabling Tor Proxy app')
self.enable()
kvstore.delete(PREINSTALL_CONFIG_KEY, ignore_missing=True)
def uninstall(self):
"""De-configure and uninstall the app."""