mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
Name Services module
This commit is contained in:
1 parent
5dfa2d0626
commit
e0bfd1401f
14 files changed
+524
-15
No files matched your search
@@ -0,0 +1,121 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module to configure name services
|
||||
"""
|
||||
|
||||
from gettext import gettext as _
|
||||
import logging
|
||||
|
||||
from plinth import cfg
|
||||
from plinth.signals import domain_added, domain_removed
|
||||
|
||||
|
||||
SERVICES = [
|
||||
('http', _('HTTP'), 80),
|
||||
('https', _('HTTPS'), 443),
|
||||
('ssh', _('SSH'), 22),
|
||||
]
|
||||
|
||||
depends = ['plinth.modules.system']
|
||||
|
||||
domain_types = {}
|
||||
domains = {}
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def init():
|
||||
"""Initialize the names module."""
|
||||
menu = cfg.main_menu.get('system:index')
|
||||
menu.add_urlname(_('Name Services'), 'glyphicon-th', 'names:index', 19)
|
||||
|
||||
domain_added.connect(on_domain_added)
|
||||
domain_removed.connect(on_domain_removed)
|
||||
|
||||
|
||||
def on_domain_added(sender, domain_type, name='', description='',
|
||||
services=None, **kwargs):
|
||||
"""Add domain to global list."""
|
||||
if not domain_type:
|
||||
return
|
||||
domain_types[domain_type] = description
|
||||
|
||||
if not name:
|
||||
return
|
||||
if not services:
|
||||
services = []
|
||||
|
||||
if domain_type not in domains:
|
||||
# new domain_type
|
||||
domains[domain_type] = {}
|
||||
domains[domain_type][name] = services
|
||||
logger.info('Added domain %s of type %s with services %s',
|
||||
name, domain_type, str(services))
|
||||
|
||||
|
||||
def on_domain_removed(sender, domain_type, name='', **kwargs):
|
||||
"""Remove domain from global list."""
|
||||
if domain_type in domains:
|
||||
if name == '': # remove all domains of this type
|
||||
domains[domain_type] = {}
|
||||
logger.info('Removed all domains of type %s', domain_type)
|
||||
elif name in domains[domain_type]:
|
||||
del domains[domain_type][name]
|
||||
logger.info('Removed domain %s of type %s', name, domain_type)
|
||||
|
||||
|
||||
def get_domain_types():
|
||||
"""Get list of domain_types."""
|
||||
return list(domain_types.keys())
|
||||
|
||||
|
||||
def get_description(domain_type):
|
||||
"""Get description of a domain_type, if available."""
|
||||
if domain_type in domain_types:
|
||||
return domain_types[domain_type]
|
||||
else:
|
||||
return domain_type
|
||||
|
||||
|
||||
def get_domain(domain_type):
|
||||
"""
|
||||
Get domain of type domain_type.
|
||||
|
||||
This function is meant for use with single-domain domain_types. If there is
|
||||
more than one domain, any one of the domains may be returned.
|
||||
"""
|
||||
if domain_type in domains and len(domains[domain_type]) > 0:
|
||||
return list(domains[domain_type].keys())[0]
|
||||
else:
|
||||
return _('Not Available')
|
||||
|
||||
|
||||
def get_services(domain_type, domain):
|
||||
"""Get list of enabled services for a domain."""
|
||||
try:
|
||||
return domains[domain_type][domain]
|
||||
except KeyError:
|
||||
# domain_type or domain not registered
|
||||
return []
|
||||
|
||||
|
||||
def get_services_status(domain_type, domain):
|
||||
"""Get list of whether each service is enabled for a domain."""
|
||||
enabled = get_services(domain_type, domain)
|
||||
return [service[0] in enabled for service in SERVICES]
|
||||
@@ -0,0 +1,63 @@
|
||||
{% extends "base.html" %}
|
||||
{% comment %}
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
{% endcomment %}
|
||||
|
||||
{% load bootstrap %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<h2>Name Services</h2>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-5">
|
||||
<table class="table table-bordered table-condensed table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<td></td>
|
||||
{% for service in status.services %}
|
||||
<td>
|
||||
<div class="text-center">{{ service }}</div>
|
||||
</td>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for name_service in status.name_services %}
|
||||
<tr>
|
||||
<td>
|
||||
<b>{{ name_service.type }}</b></br>
|
||||
<i>{{ name_service.name }}</i>
|
||||
</td>
|
||||
{% for service in name_service.services_enabled %}
|
||||
<td>
|
||||
{% if service %}
|
||||
<span class="label label-success">Enabled</span>
|
||||
{% else %}
|
||||
<span class="label label-warning">Disabled<span>
|
||||
{% endif %}
|
||||
</td>
|
||||
{% endfor %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
Whitespace-only changes.
@@ -0,0 +1,96 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Tests for names module.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
from .. import domain_types, domains
|
||||
from .. import on_domain_added, on_domain_removed
|
||||
from .. import get_domain_types, get_description
|
||||
from .. import get_domain, get_services, get_services_status
|
||||
|
||||
|
||||
class TestNames(unittest.TestCase):
|
||||
"""Test cases for testing the names module."""
|
||||
def test_on_domain_added(self):
|
||||
"""Test adding a domain to the global list."""
|
||||
on_domain_added('', '')
|
||||
self.assertNotIn('', domain_types)
|
||||
self.assertNotIn('', domains)
|
||||
|
||||
on_domain_added('', 'hiddenservice', 'ddddd.onion')
|
||||
on_domain_added('', 'hiddenservice', 'eeeee.onion')
|
||||
self.assertIn('ddddd.onion', domains['hiddenservice'])
|
||||
self.assertIn('eeeee.onion', domains['hiddenservice'])
|
||||
|
||||
def test_on_domain_removed(self):
|
||||
"""Test removing a domain from the global list."""
|
||||
on_domain_added('', 'domainname', 'fffff')
|
||||
on_domain_removed('', 'domainname', 'fffff')
|
||||
self.assertNotIn('fffff', domains['domainname'])
|
||||
|
||||
on_domain_added('', 'pagekite', 'ggggg.pagekite.me')
|
||||
on_domain_added('', 'pagekite', 'hhhhh.pagekite.me')
|
||||
on_domain_removed('', 'pagekite')
|
||||
self.assertNotIn('ggggg.pagekite.me', domains['pagekite'])
|
||||
self.assertNotIn('hhhhh.pagekite.me', domains['pagekite'])
|
||||
|
||||
# try to remove things that don't exist
|
||||
on_domain_removed('', '')
|
||||
on_domain_removed('', 'domainname', 'iiiii')
|
||||
|
||||
def test_get_domain_types(self):
|
||||
"""Test getting domain types."""
|
||||
on_domain_added('', 'domainname')
|
||||
self.assertIn('domainname', get_domain_types())
|
||||
|
||||
def test_get_description(self):
|
||||
"""Test getting domain type description."""
|
||||
on_domain_added('', 'pagekite', '', 'Pagekite')
|
||||
self.assertEqual(get_description('pagekite'), 'Pagekite')
|
||||
|
||||
self.assertEqual('asdfasdf', get_description('asdfasdf'))
|
||||
|
||||
def test_get_domain(self):
|
||||
"""Test getting a domain of domain_type."""
|
||||
on_domain_added('', 'hiddenservice', 'aaaaa.onion')
|
||||
self.assertEqual(get_domain('hiddenservice'), 'aaaaa.onion')
|
||||
|
||||
self.assertEqual('Not Available', get_domain('abcdef'))
|
||||
|
||||
on_domain_removed('', 'hiddenservice')
|
||||
self.assertEqual('Not Available', get_domain('hiddenservice'))
|
||||
|
||||
def test_get_services(self):
|
||||
"""Test getting enabled services for a domain."""
|
||||
on_domain_added('', 'domainname', 'bbbbb', '',
|
||||
['http', 'https', 'ssh'])
|
||||
self.assertEqual(get_services('domainname', 'bbbbb'),
|
||||
['http', 'https', 'ssh'])
|
||||
|
||||
self.assertEqual(get_services('xxxxx', 'yyyyy'), [])
|
||||
self.assertEqual(get_services('domainname', 'zzzzz'), [])
|
||||
|
||||
def test_get_services_status(self):
|
||||
"""Test getting whether each service is enabled for a domain."""
|
||||
on_domain_added('', 'pagekite', 'ccccc.pagekite.me', '',
|
||||
['http', 'https'])
|
||||
self.assertEqual(get_services_status('pagekite', 'ccccc.pagekite.me'),
|
||||
[True, True, False])
|
||||
@@ -0,0 +1,28 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
URLs for the name services module
|
||||
"""
|
||||
|
||||
from django.conf.urls import patterns, url
|
||||
|
||||
|
||||
urlpatterns = patterns(
|
||||
'plinth.modules.names.views',
|
||||
url(r'^sys/names/$', 'index', name='index'),
|
||||
)
|
||||
@@ -0,0 +1,52 @@
|
||||
#
|
||||
# This file is part of Plinth.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as
|
||||
# published by the Free Software Foundation, either version 3 of the
|
||||
# License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
|
||||
"""
|
||||
Plinth module for name services
|
||||
"""
|
||||
|
||||
from django.template.response import TemplateResponse
|
||||
from gettext import gettext as _
|
||||
|
||||
from . import SERVICES, get_domain_types, get_description
|
||||
from . import get_domain, get_services_status
|
||||
|
||||
|
||||
def index(request):
|
||||
"""Serve name services page."""
|
||||
status = get_status()
|
||||
|
||||
return TemplateResponse(request, 'names.html',
|
||||
{'title': _('Name Services'),
|
||||
'status': status})
|
||||
|
||||
|
||||
def get_status():
|
||||
"""Get configured services per name."""
|
||||
name_services = []
|
||||
for domain_type in sorted(get_domain_types()):
|
||||
domain = get_domain(domain_type)
|
||||
name_services.append({
|
||||
'type': get_description(domain_type),
|
||||
'name': domain,
|
||||
'services_enabled': get_services_status(domain_type, domain),
|
||||
})
|
||||
|
||||
return {
|
||||
'services': [service[1] for service in SERVICES],
|
||||
'name_services': name_services,
|
||||
}
|
||||
Reference in new issue
Block a user