2 ###############################################################################
4 # ddns - A dynamic DNS client for IPFire #
5 # Copyright (C) 2012 IPFire development team #
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. #
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. #
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/>. #
20 ###############################################################################
28 from __version__
import CLIENT_VERSION
32 # Initialize the logger.
34 logger
= logging
.getLogger("ddns.system")
37 class DDNSSystem(object):
39 The DDNSSystem class adds a layer of abstraction
40 between the ddns software and the system.
43 # The default useragent.
44 USER_AGENT
= "IPFireDDNSUpdater/%s" % CLIENT_VERSION
46 def __init__(self
, core
):
47 # Connection to the core of the program.
52 proxy
= self
.core
.settings
.get("proxy")
54 # Strip http:// at the beginning.
55 if proxy
and proxy
.startswith("http://"):
60 def guess_external_ipv6_address(self
):
62 Sends a request to an external web server
63 to determine the current default IP address.
66 response
= self
.send_request("http://checkip6.dns.lightningwirelabs.com", timeout
=10)
68 # If the server could not be reached, we will return nothing.
69 except DDNSNetworkError
:
72 if not response
.code
== 200:
75 match
= re
.search(r
"^Your IP address is: (.*)$", response
.read())
81 def guess_external_ipv4_address(self
):
83 Sends a request to the internet to determine
84 the public IP address.
86 XXX does not work for IPv6.
89 response
= self
.send_request("http://checkip4.dns.lightningwirelabs.com", timeout
=10)
91 # If the server could not be reached, we will return nothing.
92 except DDNSNetworkError
:
95 if response
.code
== 200:
96 match
= re
.search(r
"Your IP address is: (\d+.\d+.\d+.\d+)", response
.read())
100 return match
.group(1)
102 def send_request(self
, url
, method
="GET", data
=None, username
=None, password
=None, timeout
=30):
103 assert method
in ("GET", "POST")
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
)
110 url
= "%s?%s" % (url
, query_args
)
112 logger
.debug("Sending request (%s): %s" % (method
, url
))
114 logger
.debug(" data: %s" % data
)
116 req
= urllib2
.Request(url
, data
=data
)
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
)
123 # Set the user agent.
124 req
.add_header("User-Agent", self
.USER_AGENT
)
126 # All requests should not be cached anywhere.
127 req
.add_header("Pragma", "no-cache")
129 # Set the upstream proxy if needed.
131 logger
.debug("Using proxy: %s" % self
.proxy
)
133 # Configure the proxy for this request.
134 req
.set_proxy(self
.proxy
, "http")
136 assert req
.get_method() == method
138 logger
.debug(_("Request header:"))
139 for k
, v
in req
.headers
.items():
140 logger
.debug(" %s: %s" % (k
, v
))
143 resp
= urllib2
.urlopen(req
, timeout
=timeout
)
145 # Log response header.
146 logger
.debug(_("Response header:"))
147 for k
, v
in resp
.info().items():
148 logger
.debug(" %s: %s" % (k
, v
))
150 # Return the entire response object.
153 except urllib2
.HTTPError
, e
:
154 # 503 - Service Unavailable
156 raise DDNSServiceUnavailableError
158 # Raise all other unhandled exceptions.
161 except urllib2
.URLError
, e
:
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
169 # Raise all other unhandled exceptions.
172 except socket
.timeout
, e
:
173 logger
.debug(_("Connection timeout"))
175 raise DDNSConnectionTimeoutError
177 def _format_query_args(self
, data
):
180 for k
, v
in data
.items():
181 arg
= "%s=%s" % (k
, urllib
.quote(v
))
184 return "&".join(args
)
186 def _make_basic_auth_header(self
, username
, password
):
187 authstring
= "%s:%s" % (username
, password
)
189 # Encode authorization data in base64.
190 authstring
= base64
.encodestring(authstring
)
192 # Remove any newline characters.
193 authstring
= authstring
.replace("\n", "")
197 def get_address(self
, proto
):
198 assert proto
in ("ipv6", "ipv4")
200 # Check if the external IP address should be guessed from
202 guess_ip
= self
.core
.settings
.get("guess_external_ip", "true")
204 # If the external IP address should be used, we just do
206 if guess_ip
in ("true", "yes", "1"):
208 return self
.guess_external_ipv6_address()
210 elif proto
== "ipv4":
211 return self
.guess_external_ipv4_address()
216 def resolve(self
, hostname
, proto
=None):
221 elif proto
== "ipv6":
222 family
= socket
.AF_INET6
223 elif proto
== "ipv4":
224 family
= socket
.AF_INET
226 raise ValueError("Protocol not supported: %s" % proto
)
228 # Resolve the host address.
230 response
= socket
.getaddrinfo(hostname
, None, family
)
231 except socket
.gaierror
, e
:
232 # Name or service not known
239 for family
, socktype
, proto
, canonname
, sockaddr
in response
:
241 if family
== socket
.AF_INET6
:
242 address
, port
, flow_info
, scope_id
= sockaddr
244 # Only use the global scope.
245 if not scope_id
== 0:
249 elif family
== socket
.AF_INET
:
250 address
, port
= sockaddr
252 # Ignore everything else...
256 # Add to repsonse list if not already in there.
257 if not address
in addresses
:
258 addresses
.append(address
)