remove stale IPs from session, so we don't show outdated information
could happen if there once was a IPv6 connection, but now is not any more. it now kills infos older than 3 minutes from the session. it also shows the age of the infos now on the hosts overview view (but not on home view due to cosmetic reasons). optimization: only request the fake images for ipv4/v6 detection if we don't have a fresh IP already. added example settings for using detectip on (ip6-)localhost
This commit is contained in:
parent
22028c24b4
commit
2529263a48
@ -8,6 +8,7 @@ from django.conf import settings
|
||||
from django.contrib.auth.hashers import check_password
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.sessions.backends.db import SessionStore
|
||||
from django.utils.timezone import now
|
||||
|
||||
from ..main.models import Host
|
||||
from ..main.dnstools import update, SameIpError, check_ip
|
||||
@ -49,6 +50,8 @@ def DetectIpView(request, secret=None):
|
||||
ipaddr = request.META['REMOTE_ADDR']
|
||||
key = check_ip(ipaddr)
|
||||
s[key] = ipaddr
|
||||
s[key+'_timestamp'] = now()
|
||||
logger.debug("detected %s: %s" % (key, ipaddr))
|
||||
s.save()
|
||||
return HttpResponse(status=204)
|
||||
|
||||
|
@ -1,5 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.timezone import now
|
||||
|
||||
MAX_IP_AGE = 180 # seconds
|
||||
|
||||
|
||||
def add_settings(request):
|
||||
@ -8,3 +14,33 @@ def add_settings(request):
|
||||
context['WWW_IPV4_HOST'] = settings.WWW_IPV4_HOST
|
||||
context['WWW_IPV6_HOST'] = settings.WWW_IPV6_HOST
|
||||
return context
|
||||
|
||||
|
||||
def remove_stale_ips(request):
|
||||
"""
|
||||
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
|
||||
t_now = now()
|
||||
for key in ['ipv4', 'ipv6', ]:
|
||||
timestamp_key = "%s_timestamp" % key
|
||||
try:
|
||||
timestamp = s[timestamp_key]
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
stale = timestamp + timedelta(seconds=MAX_IP_AGE) < t_now
|
||||
except (ValueError, TypeError):
|
||||
# invalid timestamp in session
|
||||
del s[timestamp_key]
|
||||
else:
|
||||
if stale:
|
||||
print "ts: %s now: %s - killing %s (was: %s)" % (timestamp, t_now, key, s[key])
|
||||
# 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
|
||||
s[key] = ''
|
||||
# update the timestamp, so we can retry after a while
|
||||
s[timestamp_key] = t_now
|
||||
return {}
|
||||
|
@ -36,8 +36,12 @@
|
||||
<div class="col-lg-8">
|
||||
<div class="well well-sm">
|
||||
<h3>Your current IPs</h3>
|
||||
<p><b>Your IPv4:</b> <span class="ipv4addr">{{ request.session.ipv4 }}</span></p>
|
||||
<p><b>Your IPv6:</b> <span class="ipv6addr">{{ request.session.ipv6 }}</span></p>
|
||||
{% if request.session.ipv4 %}
|
||||
<p><b>IPv4:</b> <span class="ipv4addr">{{ request.session.ipv4 }}</span> (seen {{request.session.ipv4_timestamp|timesince}} ago)</p>
|
||||
{% endif %}
|
||||
{% if request.session.ipv6 %}
|
||||
<p><b>IPv6:</b> <span class="ipv6addr">{{ request.session.ipv6 }}</span> (seen {{request.session.ipv6_timestamp|timesince}} ago)</p>
|
||||
{% endif %}
|
||||
<p>Note: We try to get your IP addresses through a trick, by hosting two fake images:</p>
|
||||
<ol>
|
||||
<li>on a IPv4-only server</li>
|
||||
|
@ -34,6 +34,10 @@ WWW_HOST = BASEDOMAIN
|
||||
WWW_IPV4_HOST = 'ipv4.' + BASEDOMAIN
|
||||
WWW_IPV6_HOST = 'ipv6.' + BASEDOMAIN
|
||||
|
||||
# for debugging IP detection on localhost
|
||||
#WWW_IPV4_HOST = 'localhost:8000'
|
||||
#WWW_IPV6_HOST = 'ip6-localhost:8000'
|
||||
|
||||
BAD_AGENTS = set() # useragent blacklist for /nic/update service
|
||||
|
||||
# Hosts/domain names that are valid for this site; required if DEBUG is False
|
||||
@ -119,6 +123,7 @@ MIDDLEWARE_CLASSES = (
|
||||
TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
|
||||
'django.core.context_processors.request',
|
||||
'nsupdate.context_processors.add_settings',
|
||||
'nsupdate.context_processors.remove_stale_ips',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'nsupdate.urls'
|
||||
|
@ -100,15 +100,21 @@
|
||||
</div>
|
||||
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
|
||||
<div style="display: none">
|
||||
<img id="v4_img" alt="fake image used for ip v4 address detection" />
|
||||
<img id="v6_img" alt="fake image used for ip v6 address detection" />
|
||||
<script type="text/javascript">
|
||||
function insert_src() {
|
||||
$('#v4_img').attr('src', "//{{ WWW_IPV4_HOST }}/detectip/{{ request.session.session_key }}/");
|
||||
$('#v6_img').attr('src', "//{{ WWW_IPV6_HOST }}/detectip/{{ request.session.session_key }}/");
|
||||
};
|
||||
$(setTimeout("insert_src()", 1000));
|
||||
</script>
|
||||
{% if not request.session.ipv4 %}
|
||||
<img id="v4_img" alt="fake image used for ip v4 address detection" />
|
||||
{% endif %}
|
||||
{% if not request.session.ipv6 %}
|
||||
<img id="v6_img" alt="fake image used for ip v6 address detection" />
|
||||
{% endif %}
|
||||
{% if not request.session.ipv4 or not request.session.ipv6 %}
|
||||
<script type="text/javascript">
|
||||
function insert_src() {
|
||||
$('#v4_img').attr('src', "//{{ WWW_IPV4_HOST }}/detectip/{{ request.session.session_key }}/");
|
||||
$('#v6_img').attr('src', "//{{ WWW_IPV6_HOST }}/detectip/{{ request.session.session_key }}/");
|
||||
};
|
||||
$(setTimeout("insert_src()", 1000));
|
||||
</script>
|
||||
{% endif %}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
x
Reference in New Issue
Block a user