i2p: Use privileged decorator for actions

Tests:

- Functional tests work.
- Initial setup works
  - Sometimes fails to write tunnel configuration (See #2127).
  - Favorites are created as listed in FAVORITES in resources.py
  - Tunnels are created: I2P HTTP Proxy, I2P HTTPS Proxy, Irc2P

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-08-25 11:49:00 -07:00 committed by James Valleroy
parent 1027b624aa
commit 24af41e6a8
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 32 additions and 105 deletions

View File

@ -1,82 +0,0 @@
#!/usr/bin/python3
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Wrapper to list and handle system services
"""
import argparse
import os
from plinth import cfg
from plinth.modules.i2p.helpers import RouterEditor, TunnelEditor
cfg.read()
module_config_path = os.path.join(cfg.config_dir, 'modules-enabled')
I2P_CONF_DIR = '/var/lib/i2p/i2p-config'
def parse_arguments():
"""Return parsed command line arguments as dictionary."""
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='subcommand', help='Sub command')
subparser = subparsers.add_parser(
'add-favorite', help='Add an eepsite to the list of favorites')
subparser.add_argument('--name', help='Name of the entry', required=True)
subparser.add_argument('--url', help='URL of the entry', required=True)
subparser.add_argument('--description', help='Short description',
required=False)
subparser.add_argument('--icon', help='URL to icon', required=False)
subparser = subparsers.add_parser('set-tunnel-property',
help='Modify configuration of a tunnel')
subparser.add_argument('--name', help='Name of the tunnel', required=True)
subparser.add_argument('--property', help='Property to modify',
required=True)
subparser.add_argument('--value', help='Value to assign', required=True)
subparsers.required = True
return parser.parse_args()
def subcommand_set_tunnel_property(arguments):
"""Modify the configuration file for a certain tunnel."""
editor = TunnelEditor()
editor \
.read_conf() \
.set_tunnel_idx(arguments.name) \
.set_tunnel_prop(arguments.property, arguments.value) \
.write_conf()
print('Updated "{property}" of {filename} to {value}'.format(
property=editor.calc_prop_path(arguments.property),
filename=editor.conf_filename, value=arguments.value))
def subcommand_add_favorite(arguments):
"""
Adds a favorite to the router.config
:param arguments:
:type arguments:
"""
url = arguments.url
editor = RouterEditor()
editor.read_conf().add_favorite(arguments.name, url, arguments.description,
arguments.icon).write_conf()
print('Added {} to favorites'.format(url))
def main():
"""Parse arguments and perform all duties."""
arguments = parse_arguments()
subcommand = arguments.subcommand.replace('-', '_')
subcommand_method = globals()['subcommand_' + subcommand]
subcommand_method(arguments)
if __name__ == '__main__':
main()

View File

@ -1,11 +1,8 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
FreedomBox app to configure I2P.
"""
"""FreedomBox app to configure I2P."""
from django.utils.translation import gettext_lazy as _
from plinth import actions
from plinth import app as app_module
from plinth import frontpage, menu
from plinth.daemon import Daemon
@ -16,7 +13,7 @@ from plinth.modules.i2p.resources import FAVORITES
from plinth.modules.users.components import UsersAndGroups
from plinth.package import Packages
from . import manifest
from . import manifest, privileged
_description = [
_('The Invisible Internet Project is an anonymous network layer intended '
@ -103,25 +100,11 @@ class I2PApp(app_module.App):
self.disable()
# Add favorites to the configuration
for fav in FAVORITES:
args = [
'add-favorite',
'--name',
fav.get('name'),
'--url',
fav.get('url'),
]
if 'icon' in fav:
args.extend(['--icon', fav.get('icon')])
if 'description' in fav:
args.extend(['--description', fav.get('description')])
actions.superuser_run('i2p', args)
privileged.add_favorite(fav['name'], fav['url'],
fav.get('description'), fav.get('icon'))
# Tunnels to all interfaces
for tunnel in tunnels_to_manage:
actions.superuser_run('i2p', [
'set-tunnel-property', '--name', tunnel, '--property',
'interface', '--value', '0.0.0.0'
])
privileged.set_tunnel_property(tunnel, 'interface', '0.0.0.0')
self.enable()

View File

@ -0,0 +1,26 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Configure I2P."""
from typing import Optional
from plinth.actions import privileged
from plinth.modules.i2p.helpers import RouterEditor, TunnelEditor
@privileged
def set_tunnel_property(name: str, property_: str, value: str):
"""Modify the configuration file for a certain tunnel."""
editor = TunnelEditor()
editor \
.read_conf() \
.set_tunnel_idx(name) \
.set_tunnel_prop(property_, value) \
.write_conf()
@privileged
def add_favorite(name: str, url: str, description: Optional[str],
icon: Optional[str]):
"""Add a favorite to router.config."""
editor = RouterEditor()
editor.read_conf().add_favorite(name, url, description, icon).write_conf()