def ipaddr(request):
addr = request.META.get('HTTP_X_FORWARDED_FOR', request.META['REMOTE_ADDR'])
try:
ret = _ip6cache[addr]
except KeyError:
if addr.find(':') == -1:
ret = _ip6cache[addr] = { 'raddr': addr, 'ip6': False, 'ip6class': None, }
else:
if addr.startswith("2002:"):
ret = _ip6cache[addr] = { 'raddr': addr, 'ip6': True, 'ip6class': '6to4', }
elif addr.startswith("2001:0:"):
ret = _ip6cache[addr] = { 'raddr': addr, 'ip6': True, 'ip6class': 'teredo', }
elif addr.startswith("2001:470:"):
ret = _ip6cache[addr] = { 'raddr': addr, 'ip6': True, 'ip6class': 'he', }
else:
pwhois = subprocess.Popen(["whois", addr], stdout=subprocess.PIPE)
pgrep = subprocess.Popen(["grep", "SixXS"], stdin=pwhois.stdout, stdout=subprocess.PIPE)
if os.waitpid(pgrep.pid, 0)[1] > 0:
ret = _ip6cache[addr] = { 'raddr': addr, 'ip6': True, 'ip6class': 'native', }
else:
ret = _ip6cache[addr] = { 'raddr': addr, 'ip6': True, 'ip6class': 'sixxs', }
ret['ip6text'] = {
None: _('You are using IPV4'),
'6to4': _('You are using IPV6 over a 6-to-4 tunnel'),
'teredo': _('You are using IPV6 over a Teredo tunnel'),
'he': _('You are using IPV6 over a Hurricane Electric tunnel'),
'sixxs': _('You are using IPV6 over a SixXS tunnel'),
'native': _('You are using native IPV6'),
}[ret['ip6class']]
return ret