]>
git.ipfire.org Git - ddns.git/blob - src/ddns/system.py
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 ###############################################################################
26 from __version__
import CLIENT_VERSION
29 # Initialize the logger.
31 logger
= logging
.getLogger("ddns.system")
34 class DDNSSystem(object):
36 The DDNSSystem class adds a layer of abstraction
37 between the ddns software and the system.
40 # The default useragent.
41 USER_AGENT
= "IPFireDDNSUpdater/%s" % CLIENT_VERSION
43 def __init__(self
, core
):
44 # Connection to the core of the program.
49 proxy
= self
.core
.settings
.get("proxy")
51 # Strip http:// at the beginning.
52 if proxy
and proxy
.startswith("http://"):
57 def guess_external_ipv6_address(self
):
59 Sends a request to an external web server
60 to determine the current default IP address.
62 response
= self
.send_request("http://checkip6.dns.lightningwirelabs.com")
64 if not response
.code
== 200:
67 match
= re
.search(r
"^Your IP address is: (.*)$", response
.read())
73 def guess_external_ipv4_address(self
):
75 Sends a request to the internet to determine
76 the public IP address.
78 XXX does not work for IPv6.
80 response
= self
.send_request("http://checkip4.dns.lightningwirelabs.com")
82 if response
.code
== 200:
83 match
= re
.search(r
"Your IP address is: (\d+.\d+.\d+.\d+)", response
.read())
89 def send_request(self
, url
, method
="GET", data
=None, timeout
=30):
90 assert method
in ("GET", "POST")
92 # Add all arguments in the data dict to the URL and escape them properly.
93 if method
== "GET" and data
:
94 query_args
= self
._format
_query
_args
(data
)
97 url
= "%s?%s" % (url
, query_args
)
99 logger
.debug("Sending request (%s): %s" % (method
, url
))
101 logger
.debug(" data: %s" % data
)
103 req
= urllib2
.Request(url
, data
=data
)
105 # Set the user agent.
106 req
.add_header("User-Agent", self
.USER_AGENT
)
108 # All requests should not be cached anywhere.
109 req
.add_header("Pragma", "no-cache")
111 # Set the upstream proxy if needed.
113 logger
.debug("Using proxy: %s" % self
.proxy
)
115 # Configure the proxy for this request.
116 req
.set_proxy(self
.proxy
, "http")
118 assert req
.get_method() == method
120 logger
.debug(_("Request header:"))
121 for k
, v
in req
.headers
.items():
122 logger
.debug(" %s: %s" % (k
, v
))
125 resp
= urllib2
.urlopen(req
)
127 # Log response header.
128 logger
.debug(_("Response header:"))
129 for k
, v
in resp
.info().items():
130 logger
.debug(" %s: %s" % (k
, v
))
132 # Return the entire response object.
135 except urllib2
.URLError
, e
:
138 def _format_query_args(self
, data
):
141 for k
, v
in data
.items():
142 arg
= "%s=%s" % (k
, urllib
.quote(v
))
145 return "&".join(args
)
147 def get_address(self
, proto
):
148 assert proto
in ("ipv6", "ipv4")
150 # Check if the external IP address should be guessed from
152 guess_ip
= self
.core
.settings
.get("guess_external_ip", "true")
154 # If the external IP address should be used, we just do
156 if guess_ip
in ("true", "yes", "1"):
158 return self
.guess_external_ipv6_address()
160 elif proto
== "ipv4":
161 return self
.guess_external_ipv4_address()