From a4a183f7022d4a69fc896467a8ff367f539acdc9 Mon Sep 17 00:00:00 2001 From: Fabian Faessler Date: Sat, 28 Sep 2013 13:04:21 +0200 Subject: [PATCH 1/4] added URL and simple view to get the user IP in a json format {'ip': '127.0.0.1'} --- nsupdate/main/urls.py | 3 ++- nsupdate/main/views.py | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/nsupdate/main/urls.py b/nsupdate/main/urls.py index 944d2f8..d50c085 100644 --- a/nsupdate/main/urls.py +++ b/nsupdate/main/urls.py @@ -1,6 +1,7 @@ from django.conf.urls import patterns, include, url -from main.views import HomeView +from main.views import HomeView, MyIpView urlpatterns = patterns('', url(r'^$', HomeView.as_view(), name="home"), + url(r'^myip/', MyIpView) ) diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index d8ec9df..506a129 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- - from django.views.generic import TemplateView +from django.http import HttpResponse +import json class HomeView(TemplateView): template_name = "base.html" @@ -9,3 +10,6 @@ class HomeView(TemplateView): context = super(HomeView, self).get_context_data(*args, **kwargs) context['nav_home'] = True return context + +def MyIpView(request): + return HttpResponse(json.dumps({'ip':request.META['REMOTE_ADDR']}), content_type="application/json") \ No newline at end of file From 9800d12812dee785a14941ba4d4338f43c8949bc Mon Sep 17 00:00:00 2001 From: Fabian Faessler Date: Sat, 28 Sep 2013 13:14:11 +0200 Subject: [PATCH 2/4] changed tabs to spaces --- nsupdate/main/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index 506a129..eb36792 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -12,4 +12,4 @@ class HomeView(TemplateView): return context def MyIpView(request): - return HttpResponse(json.dumps({'ip':request.META['REMOTE_ADDR']}), content_type="application/json") \ No newline at end of file + return HttpResponse(json.dumps({'ip':request.META['REMOTE_ADDR']}), content_type="application/json") \ No newline at end of file From d71122aa1cf49a506ed37f27c9ea81ecf2de8d57 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 28 Sep 2013 13:47:49 +0200 Subject: [PATCH 3/4] dns: implement querying directly the master server --- nsupdate/main/_tests/test_dnstools.py | 27 +++++++++++++++++++++ nsupdate/main/dnstools.py | 34 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 nsupdate/main/_tests/test_dnstools.py create mode 100644 nsupdate/main/dnstools.py diff --git a/nsupdate/main/_tests/test_dnstools.py b/nsupdate/main/_tests/test_dnstools.py new file mode 100644 index 0000000..a69b265 --- /dev/null +++ b/nsupdate/main/_tests/test_dnstools.py @@ -0,0 +1,27 @@ +""" +Tests for dnstools module. +""" + +import pytest + +from nsupdate.main.dnstools import (query_ns, NONEXISTING_HOST, WWW_HOST, WWW_IPV4_HOST, WWW_IPV4_IP, + WWW_IPV6_HOST, WWW_IPV6_IP, ) + +from dns.resolver import NXDOMAIN + + +class Tests(object): + def test_queries_ok(self): + """ + check some simple dns lookups + """ + assert query_ns(WWW_IPV4_HOST, 'A') == WWW_IPV4_IP # v4 ONLY + assert query_ns(WWW_IPV6_HOST, 'AAAA') == WWW_IPV6_IP # v6 ONLY + assert query_ns(WWW_HOST, 'A') == WWW_IPV4_IP # v4 and v6, query v4 + assert query_ns(WWW_HOST, 'AAAA') == WWW_IPV6_IP # v4 and v6, query v6 + + def test_queries_failing(self): + with pytest.raises(NXDOMAIN): + query_ns(NONEXISTING_HOST, 'A') + with pytest.raises(NXDOMAIN): + query_ns(NONEXISTING_HOST, 'AAAA') diff --git a/nsupdate/main/dnstools.py b/nsupdate/main/dnstools.py new file mode 100644 index 0000000..1cd5ce4 --- /dev/null +++ b/nsupdate/main/dnstools.py @@ -0,0 +1,34 @@ +""" +Misc. DNS related code: query, dynamic update, etc. +""" + +SERVER = '85.10.192.104' # ns1.thinkmo.de (master / dynamic upd server for nsupdate.info) +BASEDOMAIN = 'nsupdate.info' + +NONEXISTING_HOST = 'nonexisting.' + BASEDOMAIN +WWW_HOST = 'www.' + BASEDOMAIN +WWW_IPV4_HOST = 'www.ipv4.' + BASEDOMAIN +WWW_IPV6_HOST = 'www.ipv6.' + BASEDOMAIN +WWW_IPV4_IP = '178.32.221.14' +WWW_IPV6_IP = '2001:41d0:8:e00e::1' + +import dns.name +import dns.resolver + +def query_ns(qname, rdtype): + """ + query a dns name from our master server + + :param qname: the query name + :type qname: dns.name.Name object or str + :param rdtype: the query type + :type rdtype: int or str + :return: IP (as str) + """ + resolver = dns.resolver.Resolver(configure=False) + # we do not configure it from resolv.conf, but patch in the values we + # want into the documented attributes: + resolver.nameservers = [SERVER, ] + resolver.search = [dns.name.from_text(BASEDOMAIN), ] + answer = resolver.query(qname, rdtype) + return str(list(answer)[0]) From d5bbc8ac59145ca8aa06989b1e4fb5ba4f266a45 Mon Sep 17 00:00:00 2001 From: Fabian Faessler Date: Sat, 28 Sep 2013 13:56:54 +0200 Subject: [PATCH 4/4] added javascript example for /myip and added a view OverviewView. JS fails for dev server, because of the Access-Control-Allow-Origin --- nsupdate/main/urls.py | 3 ++- nsupdate/main/views.py | 9 +++++++ nsupdate/nsupdate/templates/base.html | 2 ++ nsupdate/nsupdate/templates/overview.html | 29 +++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 nsupdate/nsupdate/templates/overview.html diff --git a/nsupdate/main/urls.py b/nsupdate/main/urls.py index d50c085..2222b69 100644 --- a/nsupdate/main/urls.py +++ b/nsupdate/main/urls.py @@ -1,7 +1,8 @@ from django.conf.urls import patterns, include, url -from main.views import HomeView, MyIpView +from main.views import HomeView, MyIpView, OverviewView urlpatterns = patterns('', url(r'^$', HomeView.as_view(), name="home"), + url(r'^overview/', OverviewView.as_view(), name="overview"), url(r'^myip/', MyIpView) ) diff --git a/nsupdate/main/views.py b/nsupdate/main/views.py index eb36792..24b02bd 100644 --- a/nsupdate/main/views.py +++ b/nsupdate/main/views.py @@ -11,5 +11,14 @@ class HomeView(TemplateView): context['nav_home'] = True return context +class OverviewView(TemplateView): + template_name = "overview.html" + + def get_context_data(self, *args, **kwargs): + context = super(OverviewView, self).get_context_data(*args, **kwargs) + context['nav_overview'] = True + return context + + def MyIpView(request): return HttpResponse(json.dumps({'ip':request.META['REMOTE_ADDR']}), content_type="application/json") \ No newline at end of file diff --git a/nsupdate/nsupdate/templates/base.html b/nsupdate/nsupdate/templates/base.html index 2140bb8..4e10175 100644 --- a/nsupdate/nsupdate/templates/base.html +++ b/nsupdate/nsupdate/templates/base.html @@ -11,6 +11,7 @@ + @@ -27,6 +28,7 @@