Catch various network/server errors.
[oddments/ddns.git] / src / ddns / system.py
1 #!/usr/bin/python
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 ###############################################################################
21
22 import base64
23 import re
24 import socket
25 import urllib
26 import urllib2
27
28 from __version__ import CLIENT_VERSION
29 from .errors import *
30 from i18n import _
31
32 # Initialize the logger.
33 import logging
34 logger = logging.getLogger("ddns.system")
35 logger.propagate = 1
36
37 class DDNSSystem(object):
38 """
39 The DDNSSystem class adds a layer of abstraction
40 between the ddns software and the system.
41 """
42
43 # The default useragent.
44 USER_AGENT = "IPFireDDNSUpdater/%s" % CLIENT_VERSION
45
46 def __init__(self, core):
47 # Connection to the core of the program.
48 self.core = core
49
50 @property
51 def proxy(self):
52 proxy = self.core.settings.get("proxy")
53
54 # Strip http:// at the beginning.
55 if proxy and proxy.startswith("http://"):
56 proxy = proxy[7:]
57
58 return proxy
59
60 def guess_external_ipv6_address(self):
61 """
62 Sends a request to an external web server
63 to determine the current default IP address.
64 """
65 try:
66 response = self.send_request("http://checkip6.dns.lightningwirelabs.com", timeout=10)
67
68 # If the server could not be reached, we will return nothing.
69 except DDNSNetworkError:
70 return
71
72 if not response.code == 200:
73 return
74
75 match = re.search(r"^Your IP address is: (.*)$", response.read())
76 if match is None:
77 return
78
79 return match.group(1)
80
81 def guess_external_ipv4_address(self):
82 """
83 Sends a request to the internet to determine
84 the public IP address.
85
86 XXX does not work for IPv6.
87 """
88 try:
89 response = self.send_request("http://checkip4.dns.lightningwirelabs.com", timeout=10)
90
91 # If the server could not be reached, we will return nothing.
92 except DDNSNetworkError:
93 return
94
95 if response.code == 200:
96 match = re.search(r"Your IP address is: (\d+.\d+.\d+.\d+)", response.read())
97 if match is None:
98 return
99
100 return match.group(1)
101
102 def send_request(self, url, method="GET", data=None, username=None, password=None, timeout=30):
103 assert method in ("GET", "POST")
104
105 # Add all arguments in the data dict to the URL and escape them properly.
106 if method == "GET" and data:
107 query_args = self._format_query_args(data)
108 data = None
109
110 url = "%s?%s" % (url, query_args)
111
112 logger.debug("Sending request (%s): %s" % (method, url))
113 if data:
114 logger.debug(" data: %s" % data)
115
116 req = urllib2.Request(url, data=data)
117
118 if username and password:
119 basic_auth_header = self._make_basic_auth_header(username, password)
120 print repr(basic_auth_header)
121 req.add_header("Authorization", "Basic %s" % basic_auth_header)
122
123 # Set the user agent.
124 req.add_header("User-Agent", self.USER_AGENT)
125
126 # All requests should not be cached anywhere.
127 req.add_header("Pragma", "no-cache")
128
129 # Set the upstream proxy if needed.
130 if self.proxy:
131 logger.debug("Using proxy: %s" % self.proxy)
132
133 # Configure the proxy for this request.
134 req.set_proxy(self.proxy, "http")
135
136 assert req.get_method() == method
137
138 logger.debug(_("Request header:"))
139 for k, v in req.headers.items():
140 logger.debug(" %s: %s" % (k, v))
141
142 try:
143 resp = urllib2.urlopen(req, timeout=timeout)
144
145 # Log response header.
146 logger.debug(_("Response header:"))
147 for k, v in resp.info().items():
148 logger.debug(" %s: %s" % (k, v))
149
150 # Return the entire response object.
151 return resp
152
153 except urllib2.HTTPError, e:
154 # 503 - Service Unavailable
155 if e.code == 503:
156 raise DDNSServiceUnavailableError
157
158 # Raise all other unhandled exceptions.
159 raise
160
161 except urllib2.URLError, e:
162 if e.reason:
163 # Network Unreachable (e.g. no IPv6 access)
164 if e.reason.errno == 101:
165 raise DDNSNetworkUnreachableError
166 elif e.reason.errno == 111:
167 raise DDNSConnectionRefusedError
168
169 # Raise all other unhandled exceptions.
170 raise
171
172 except socket.timeout, e:
173 logger.debug(_("Connection timeout"))
174
175 raise DDNSConnectionTimeoutError
176
177 def _format_query_args(self, data):
178 args = []
179
180 for k, v in data.items():
181 arg = "%s=%s" % (k, urllib.quote(v))
182 args.append(arg)
183
184 return "&".join(args)
185
186 def _make_basic_auth_header(self, username, password):
187 authstring = "%s:%s" % (username, password)
188
189 # Encode authorization data in base64.
190 authstring = base64.encodestring(authstring)
191
192 # Remove any newline characters.
193 authstring = authstring.replace("\n", "")
194
195 return authstring
196
197 def get_address(self, proto):
198 assert proto in ("ipv6", "ipv4")
199
200 # Check if the external IP address should be guessed from
201 # a remote server.
202 guess_ip = self.core.settings.get("guess_external_ip", "true")
203
204 # If the external IP address should be used, we just do
205 # that.
206 if guess_ip in ("true", "yes", "1"):
207 if proto == "ipv6":
208 return self.guess_external_ipv6_address()
209
210 elif proto == "ipv4":
211 return self.guess_external_ipv4_address()
212
213 # XXX TODO
214 assert False
215
216 def resolve(self, hostname, proto=None):
217 addresses = []
218
219 if proto is None:
220 family = 0
221 elif proto == "ipv6":
222 family = socket.AF_INET6
223 elif proto == "ipv4":
224 family = socket.AF_INET
225 else:
226 raise ValueError("Protocol not supported: %s" % proto)
227
228 # Resolve the host address.
229 try:
230 response = socket.getaddrinfo(hostname, None, family)
231 except socket.gaierror, e:
232 # Name or service not known
233 if e.errno == -2:
234 return []
235
236 raise
237
238 # Handle responses.
239 for family, socktype, proto, canonname, sockaddr in response:
240 # IPv6
241 if family == socket.AF_INET6:
242 address, port, flow_info, scope_id = sockaddr
243
244 # Only use the global scope.
245 if not scope_id == 0:
246 continue
247
248 # IPv4
249 elif family == socket.AF_INET:
250 address, port = sockaddr
251
252 # Ignore everything else...
253 else:
254 continue
255
256 # Add to repsonse list if not already in there.
257 if not address in addresses:
258 addresses.append(address)
259
260 return addresses