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 <sunil@medhas.org>
Reviewed-by: Benedek Nagy <contact@nbenedek.me>
This commit is contained in:
Sunil Mohan Adapa 2025-01-03 15:36:09 -08:00 committed by Benedek Nagy
parent 15291fdb8a
commit 81c5268ff6
No known key found for this signature in database
GPG Key ID: E167EC84BC1CDBBF
4 changed files with 29 additions and 4 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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')]

View File

@ -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)