56 lines
2.3 KiB
Python
56 lines
2.3 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/>.
|
|
|
|
from ipaddress import ip_address, ip_network
|
|
|
|
from django.conf import settings
|
|
from django.template.response import TemplateResponse
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
def pass_through(request):
|
|
remote_addr = request.META.get('REMOTE_ADDR')
|
|
if remote_addr:
|
|
pass_through_ips = getattr(settings, 'MAINTENANCE_PASS_THROUGH_IPS', [])
|
|
if remote_addr in pass_through_ips:
|
|
return True
|
|
else:
|
|
for network in [x for x in pass_through_ips if '/' in x]:
|
|
try:
|
|
if ip_address(remote_addr) in ip_network(network, strict=False):
|
|
return True
|
|
except ValueError: # bad remote_addr or network syntax
|
|
pass
|
|
pass_through_header = getattr(settings, 'MAINTENANCE_PASS_THROUGH_HEADER', '')
|
|
if pass_through_header and pass_through_header in request.headers:
|
|
return True
|
|
return False
|
|
|
|
|
|
class MaintenanceMiddleware:
|
|
def __init__(self, get_response):
|
|
self.get_response = get_response
|
|
|
|
def __call__(self, request):
|
|
maintenance_mode = getattr(settings, 'MAINTENANCE_PAGE', None)
|
|
if maintenance_mode and not pass_through(request):
|
|
maintenance_message = getattr(settings, 'MAINTENANCE_PAGE_MESSAGE', '')
|
|
context = {'maintenance_message': maintenance_message}
|
|
return TemplateResponse(
|
|
request, 'hobo/maintenance/maintenance_page.html', context=context, status=503
|
|
).render()
|
|
return self.get_response(request)
|