Use the Lightning Wire Labs DNS service for guessing IP addresses.
[oddments/ddns.git] / ddns / system.py
CommitLineData
f22ab085 1#!/usr/bin/python
3fdcb9d1
MT
2###############################################################################
3# #
4# ddns - A dynamic DNS client for IPFire #
5# Copyright (C) 2012 IPFire development team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
f22ab085
MT
21
22import re
23import urllib2
24
25from __version__ import CLIENT_VERSION
26from i18n import _
27
28# Initialize the logger.
29import logging
30logger = logging.getLogger("ddns.system")
31logger.propagate = 1
32
33class DDNSSystem(object):
34 """
35 The DDNSSystem class adds a layer of abstraction
36 between the ddns software and the system.
37 """
38
39 # The default useragent.
40 USER_AGENT = "IPFireDDNSUpdater/%s" % CLIENT_VERSION
41
42 def __init__(self, core):
43 # Connection to the core of the program.
44 self.core = core
45
46 @property
47 def proxy(self):
48 proxy = self.core.settings.get("proxy")
49
50 # Strip http:// at the beginning.
fd898828 51 if proxy and proxy.startswith("http://"):
f22ab085
MT
52 proxy = proxy[7:]
53
54 return proxy
55
30270391
MT
56 def guess_external_ipv6_address(self):
57 """
58 Sends a request to an external web server
59 to determine the current default IP address.
60 """
61 response = self.send_request("http://checkip6.dns.lightningwirelabs.com")
62
63 if not response.code == 200:
64 return
65
66 match = re.search(r"^Your IP address is: (.*)$", response.read())
67 if match is None:
68 return
69
70 return match.group(1)
71
f22ab085
MT
72 def guess_external_ipv4_address(self):
73 """
74 Sends a request to the internet to determine
75 the public IP address.
76
77 XXX does not work for IPv6.
78 """
30270391 79 response = self.send_request("http://checkip4.dns.lightningwirelabs.com")
f22ab085
MT
80
81 if response.code == 200:
30270391 82 match = re.search(r"Your IP address is: (\d+.\d+.\d+.\d+)", response.read())
f22ab085
MT
83 if match is None:
84 return
85
86 return match.group(1)
87
88 def send_request(self, url, data=None, timeout=30):
89 logger.debug("Sending request: %s" % url)
90 if data:
91 logger.debug(" data: %s" % data)
92
93 req = urllib2.Request(url, data=data)
94
95 # Set the user agent.
96 req.add_header("User-Agent", self.USER_AGENT)
97
98 # All requests should not be cached anywhere.
99 req.add_header("Pragma", "no-cache")
100
101 # Set the upstream proxy if needed.
102 if self.proxy:
103 logger.debug("Using proxy: %s" % self.proxy)
104
105 # Configure the proxy for this request.
106 req.set_proxy(self.proxy, "http")
107
108 logger.debug(_("Request header:"))
109 for k, v in req.headers.items():
110 logger.debug(" %s: %s" % (k, v))
111
112 try:
113 resp = urllib2.urlopen(req)
114
115 # Log response header.
116 logger.debug(_("Response header:"))
117 for k, v in resp.info().items():
118 logger.debug(" %s: %s" % (k, v))
119
120 # Return the entire response object.
121 return resp
122
123 except urllib2.URLError, e:
124 raise
125
126 def get_address(self, proto):
127 assert proto in ("ipv6", "ipv4")
128
30270391
MT
129 # Check if the external IP address should be guessed from
130 # a remote server.
131 guess_ip = self.core.settings.get("guess_external_ip", "true")
132
133 # If the external IP address should be used, we just do
134 # that.
135 if guess_ip in ("true", "yes", "1"):
136 if proto == "ipv6":
137 return self.guess_external_ipv6_address()
f22ab085 138
30270391 139 elif proto == "ipv4":
f22ab085
MT
140 return self.guess_external_ipv4_address()
141
142 # XXX TODO
143 assert False