hobo/hobo/middleware/maintenance.py

51 lines
2.1 KiB
Python

# hobo - portal to configure and deploy applications
# Copyright (C) 2015-2022 Entr'ouvert
#
# 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/>.
import dns.exception
import dns.resolver
from django.conf import settings
from django.http import HttpResponse
from django.utils.translation import ugettext as _
def pass_through(remote_addr):
pass_through_ips = getattr(settings, 'MAINTENANCE_PASS_THROUGH_IPS', [])
if remote_addr in pass_through_ips:
return True
pass_through_ddns = getattr(settings, 'MAINTENANCE_PASS_THROUGH_DDNS', None)
if pass_through_ddns:
domain = '.'.join(reversed(remote_addr.split('.'))) + '.' + pass_through_ddns
try:
answers = dns.resolver.query(domain, 'A', lifetime=1)
return any(answer.address for answer in answers)
except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.exception.DNSException):
return False
return False
class MaintenanceMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
maintenance_mode = getattr(settings, 'MAINTENANCE_MODE', None)
if maintenance_mode:
remote_addr = request.META.get('REMOTE_ADDR')
if not (remote_addr and pass_through(remote_addr)):
maintenance_msg = _('The site is under maintenance')
return HttpResponse('<h1>%s</h1>' % maintenance_msg, status=503)
return self.get_response(request)