From 81c5268ff6f9b7b424cff339693a0c0b73160917 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 3 Jan 2025 15:36:09 -0800 Subject: [PATCH] privacy: Add option in UI to set lookup URL for public IPs - This will act as centralized location to set the URL to lookup IP addresses. Tests: - When no value is set initially, the URL for Foundation is shown. This also happens when the value is removed from 'plinth_kvstore' sqlite3 table. - When empty value is set it, the empty value is set in DB and show in the UI. - When no value is changed and form is updated, message is shown that settings have not changed. - When value is changed and form is updated the updated value is shown in the form, message is shown that configuration has been updated. - Updated URL value reflects in the email app and errors change accordingly. Signed-off-by: Sunil Mohan Adapa Reviewed-by: Benedek Nagy --- plinth/modules/privacy/__init__.py | 10 ++++++++-- plinth/modules/privacy/forms.py | 14 ++++++++++++++ plinth/modules/privacy/manifest.py | 2 +- plinth/modules/privacy/views.py | 7 ++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/plinth/modules/privacy/__init__.py b/plinth/modules/privacy/__init__.py index c8d7e83f7..ee0c0efa5 100644 --- a/plinth/modules/privacy/__init__.py +++ b/plinth/modules/privacy/__init__.py @@ -9,7 +9,7 @@ from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_noop from plinth import app as app_module -from plinth import menu +from plinth import kvstore, menu from plinth.config import DropinConfigs from plinth.modules.backups.components import BackupRestore from plinth.package import Packages @@ -94,7 +94,13 @@ def _show_privacy_notification(): def get_ip_lookup_url(): """Return the URL to use to lookup external IP address.""" - return 'https://ddns.freedombox.org/ip/' + return kvstore.get_default('ip_lookup_url', + 'https://ddns.freedombox.org/ip/') + + +def set_ip_lookup_url(ip_lookup_url: str): + """Set the URL to use to lookup external IP address.""" + kvstore.set('ip_lookup_url', ip_lookup_url) def lookup_public_address(ip_type: Literal['ipv4', 'ipv6']) -> str: diff --git a/plinth/modules/privacy/forms.py b/plinth/modules/privacy/forms.py index 1bcebe0fc..7312e2835 100644 --- a/plinth/modules/privacy/forms.py +++ b/plinth/modules/privacy/forms.py @@ -2,6 +2,7 @@ """FreedomBox privacy app.""" from django import forms +from django.core import validators from django.utils.translation import gettext_lazy as _ from plinth import cfg @@ -12,6 +13,14 @@ from plinth.utils import format_lazy class PrivacyForm(forms.Form): """Privacy configuration form.""" + help_ip_lookup_url = format_lazy( + _('Optional Value. This URL is used to determine the publicly visible ' + 'IP address of your {box_name}. The URL should simply return the ' + 'IPv4 or IPv6 address where the client request comes from. Default ' + 'is to use the service provided by the FreedomBox Foundation at ' + 'https://ddns.freedombox.org/ip/. If empty, lookups are disabled ' + 'and some functionality will fail.'), box_name=_(cfg.box_name)) + enable_popcon = forms.BooleanField( label=_('Periodically submit a list of apps used (suggested)'), required=False, help_text=format_lazy( @@ -32,6 +41,11 @@ class PrivacyForm(forms.Form): 'internet connectivity is available. Can be disabled in most ' 'cases if network connectivity is stable and reliable.')) + ip_lookup_url = forms.CharField( + label=_('URL to look up public IP address'), required=False, + help_text=help_ip_lookup_url, + validators=[validators.URLValidator(schemes=['http', 'https'])]) + def __init__(self, *args, **kwargs): """Disable DNS fallback field if necessary.""" super().__init__(*args, **kwargs) diff --git a/plinth/modules/privacy/manifest.py b/plinth/modules/privacy/manifest.py index a0019680e..9b31f5e07 100644 --- a/plinth/modules/privacy/manifest.py +++ b/plinth/modules/privacy/manifest.py @@ -7,4 +7,4 @@ from . import privileged backup = {'config': {'files': [str(privileged.CONFIG_FILE)]}} -tags = [_('Usage reporting'), _('Fallback DNS')] +tags = [_('Usage reporting'), _('External services'), _('Fallback DNS')] diff --git a/plinth/modules/privacy/views.py b/plinth/modules/privacy/views.py index 5dd7c6f65..b6fb7490d 100644 --- a/plinth/modules/privacy/views.py +++ b/plinth/modules/privacy/views.py @@ -5,7 +5,7 @@ from django.contrib import messages from django.utils.translation import gettext as _ import plinth.modules.names.privileged as names_privileged -from plinth.modules import names +from plinth.modules import names, privacy from plinth.modules.privacy.forms import PrivacyForm from plinth.views import AppView @@ -22,6 +22,7 @@ class PrivacyAppView(AppView): """Return the values to fill in the form.""" initial = super().get_initial() initial.update(privileged.get_configuration()) + initial['ip_lookup_url'] = privacy.get_ip_lookup_url() if names.is_resolved_installed(): initial.update(names_privileged.get_resolved_configuration()) @@ -43,6 +44,10 @@ class PrivacyAppView(AppView): dns_fallback=new_config['dns_fallback']) is_changed = True + if old_config['ip_lookup_url'] != new_config['ip_lookup_url']: + privacy.set_ip_lookup_url(new_config['ip_lookup_url']) + is_changed = True + if changes: privileged.set_configuration(**changes)