]>
git.ipfire.org Git - ddns.git/blob - src/ddns/system.py
8fa5c42e7dbdd0fc8ed6b99136dffde2d57ddc83
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 ###############################################################################
27 from __version__
import CLIENT_VERSION
30 # Initialize the logger.
32 logger
= logging
.getLogger("ddns.system")
35 class DDNSSystem(object):
37 The DDNSSystem class adds a layer of abstraction
38 between the ddns software and the system.
41 # The default useragent.
42 USER_AGENT
= "IPFireDDNSUpdater/%s" % CLIENT_VERSION
44 def __init__(self
, core
):
45 # Connection to the core of the program.
50 proxy
= self
.core
.settings
.get("proxy")
52 # Strip http:// at the beginning.
53 if proxy
and proxy
.startswith("http://"):
58 def guess_external_ipv6_address(self
):
60 Sends a request to an external web server
61 to determine the current default IP address.
63 response
= self
.send_request("http://checkip6.dns.lightningwirelabs.com")
65 if not response
.code
== 200:
68 match
= re
.search(r
"^Your IP address is: (.*)$", response
.read())
74 def guess_external_ipv4_address(self
):
76 Sends a request to the internet to determine
77 the public IP address.
79 XXX does not work for IPv6.
81 response
= self
.send_request("http://checkip4.dns.lightningwirelabs.com")
83 if response
.code
== 200:
84 match
= re
.search(r
"Your IP address is: (\d+.\d+.\d+.\d+)", response
.read())
90 def send_request(self
, url
, method
="GET", data
=None, username
=None, password
=None, timeout
=30):
91 assert method
in ("GET", "POST")
93 # Add all arguments in the data dict to the URL and escape them properly.
94 if method
== "GET" and data
:
95 query_args
= self
._format
_query
_args
(data
)
98 url
= "%s?%s" % (url
, query_args
)
100 logger
.debug("Sending request (%s): %s" % (method
, url
))
102 logger
.debug(" data: %s" % data
)
104 req
= urllib2
.Request(url
, data
=data
)
106 if username
and password
:
107 basic_auth_header
= self
._make
_basic
_auth
_header
(username
, password
)
108 print repr(basic_auth_header
)
109 req
.add_header("Authorization", "Basic %s" % basic_auth_header
)
111 # Set the user agent.
112 req
.add_header("User-Agent", self
.USER_AGENT
)
114 # All requests should not be cached anywhere.
115 req
.add_header("Pragma", "no-cache")
117 # Set the upstream proxy if needed.
119 logger
.debug("Using proxy: %s" % self
.proxy
)
121 # Configure the proxy for this request.
122 req
.set_proxy(self
.proxy
, "http")
124 assert req
.get_method() == method
126 logger
.debug(_("Request header:"))
127 for k
, v
in req
.headers
.items():
128 logger
.debug(" %s: %s" % (k
, v
))
131 resp
= urllib2
.urlopen(req
)
133 # Log response header.
134 logger
.debug(_("Response header:"))
135 for k
, v
in resp
.info().items():
136 logger
.debug(" %s: %s" % (k
, v
))
138 # Return the entire response object.
141 except urllib2
.URLError
, e
:
144 def _format_query_args(self
, data
):
147 for k
, v
in data
.items():
148 arg
= "%s=%s" % (k
, urllib
.quote(v
))
151 return "&".join(args
)
153 def _make_basic_auth_header(self
, username
, password
):
154 authstring
= "%s:%s" % (username
, password
)
156 # Encode authorization data in base64.
157 authstring
= base64
.encodestring(authstring
)
159 # Remove any newline characters.
160 authstring
= authstring
.replace("\n", "")
164 def get_address(self
, proto
):
165 assert proto
in ("ipv6", "ipv4")
167 # Check if the external IP address should be guessed from
169 guess_ip
= self
.core
.settings
.get("guess_external_ip", "true")
171 # If the external IP address should be used, we just do
173 if guess_ip
in ("true", "yes", "1"):
175 return self
.guess_external_ipv6_address()
177 elif proto
== "ipv4":
178 return self
.guess_external_ipv4_address()