2013-09-28 23:31:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-12-15 17:09:22 +01:00
|
|
|
"""
|
|
|
|
context processors for injecting some settings into the template context and
|
|
|
|
also for keeping the IP addrs in the session fresh.
|
|
|
|
"""
|
2013-11-01 04:03:34 +01:00
|
|
|
|
2013-11-01 04:14:06 +01:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-11-03 09:52:11 +01:00
|
|
|
import time
|
2013-11-01 04:03:34 +01:00
|
|
|
|
2013-11-14 02:11:44 +01:00
|
|
|
from .main.dnstools import put_ip_into_session
|
2014-09-29 06:16:52 +02:00
|
|
|
from .main.iptools import normalize_ip
|
2013-11-14 02:11:44 +01:00
|
|
|
|
2013-09-28 23:31:33 +02:00
|
|
|
from django.conf import settings
|
2013-11-01 04:03:34 +01:00
|
|
|
|
|
|
|
MAX_IP_AGE = 180 # seconds
|
2013-09-28 23:31:33 +02:00
|
|
|
|
2013-09-29 01:21:44 +02:00
|
|
|
|
2013-09-28 23:31:33 +02:00
|
|
|
def add_settings(request):
|
|
|
|
context = {}
|
2013-10-07 20:02:00 +02:00
|
|
|
context['WWW_HOST'] = settings.WWW_HOST
|
2013-09-28 23:31:33 +02:00
|
|
|
context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST
|
|
|
|
context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST
|
2013-11-03 00:02:02 +01:00
|
|
|
context['SERVICE_CONTACT'] = settings.SERVICE_CONTACT # about view
|
2014-05-30 01:18:50 +02:00
|
|
|
context['WE_HAVE_TLS'] = settings.WE_HAVE_TLS
|
2013-11-03 10:55:49 +01:00
|
|
|
context['COOKIE_SECURE'] = settings.SESSION_COOKIE_SECURE or settings.CSRF_COOKIE_SECURE
|
2013-09-28 23:31:33 +02:00
|
|
|
return context
|
2013-11-01 04:03:34 +01:00
|
|
|
|
|
|
|
|
2013-11-14 02:11:44 +01:00
|
|
|
def update_ips(request):
|
2013-11-01 04:03:34 +01:00
|
|
|
"""
|
2013-11-14 02:11:44 +01:00
|
|
|
Update the IPs in the session using REMOTE_ADDR.
|
2013-11-01 04:03:34 +01:00
|
|
|
Check the session if there are stale IPs and if so, remove them.
|
|
|
|
"""
|
|
|
|
# XXX is a context processor is the right place for this?
|
|
|
|
s = request.session
|
2013-11-03 09:52:11 +01:00
|
|
|
t_now = int(time.time())
|
2013-11-14 02:11:44 +01:00
|
|
|
# update and keep fresh using info from the request we have anyway:
|
2014-09-29 06:16:52 +02:00
|
|
|
ipaddr = normalize_ip(request.META['REMOTE_ADDR'])
|
2013-11-14 02:11:44 +01:00
|
|
|
put_ip_into_session(s, ipaddr, max_age=MAX_IP_AGE / 2)
|
|
|
|
# remove stale data to not show outdated IPs (e.g. after losing IPv6 connectivity):
|
2013-11-01 04:03:34 +01:00
|
|
|
for key in ['ipv4', 'ipv6', ]:
|
|
|
|
timestamp_key = "%s_timestamp" % key
|
|
|
|
try:
|
|
|
|
timestamp = s[timestamp_key]
|
|
|
|
except KeyError:
|
2013-11-01 04:14:06 +01:00
|
|
|
# should be always there, initialize it:
|
2013-11-14 02:11:44 +01:00
|
|
|
put_ip_into_session(s, '', kind=key)
|
2013-11-01 04:03:34 +01:00
|
|
|
else:
|
|
|
|
try:
|
2013-11-03 09:52:11 +01:00
|
|
|
stale = timestamp + MAX_IP_AGE < t_now
|
2013-11-01 04:03:34 +01:00
|
|
|
except (ValueError, TypeError):
|
|
|
|
# invalid timestamp in session
|
2013-11-14 02:11:44 +01:00
|
|
|
put_ip_into_session(s, '', kind=key)
|
2013-11-01 04:03:34 +01:00
|
|
|
else:
|
|
|
|
if stale:
|
2013-11-01 04:14:06 +01:00
|
|
|
logger.debug("ts: %s now: %s - killing stale %s (was: %s)" % (timestamp, t_now, key, s[key]))
|
2013-11-01 04:03:34 +01:00
|
|
|
# kill the IP, it is not up-to-date any more
|
|
|
|
# note: it is used to fill form fields, so set it to empty string
|
2013-11-14 02:11:44 +01:00
|
|
|
put_ip_into_session(s, '', kind=key)
|
2013-11-15 01:16:13 +01:00
|
|
|
if s.session_key is None:
|
|
|
|
# if we have a new session (== not loaded from database / storage), we
|
|
|
|
# MUST save it here to create its session_key as the base.html template
|
|
|
|
# uses .session_key to build the URL for detectip:
|
|
|
|
s.save()
|
2013-11-01 04:03:34 +01:00
|
|
|
return {}
|