]>
git.ipfire.org Git - oddments/ddns.git/blob - 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 ###############################################################################
25 from __version__
import CLIENT_VERSION
28 # Initialize the logger.
30 logger
= logging
.getLogger("ddns.system")
33 class DDNSSystem(object):
35 The DDNSSystem class adds a layer of abstraction
36 between the ddns software and the system.
39 # The default useragent.
40 USER_AGENT
= "IPFireDDNSUpdater/%s" % CLIENT_VERSION
42 def __init__(self
, core
):
43 # Connection to the core of the program.
48 proxy
= self
.core
.settings
.get("proxy")
50 # Strip http:// at the beginning.
51 if proxy
and proxy
.startswith("http://"):
56 def guess_external_ipv6_address(self
):
58 Sends a request to an external web server
59 to determine the current default IP address.
61 response
= self
.send_request("http://checkip6.dns.lightningwirelabs.com")
63 if not response
.code
== 200:
66 match
= re
.search(r
"^Your IP address is: (.*)$", response
.read())
72 def guess_external_ipv4_address(self
):
74 Sends a request to the internet to determine
75 the public IP address.
77 XXX does not work for IPv6.
79 response
= self
.send_request("http://checkip4.dns.lightningwirelabs.com")
81 if response
.code
== 200:
82 match
= re
.search(r
"Your IP address is: (\d+.\d+.\d+.\d+)", response
.read())
88 def send_request(self
, url
, data
=None, timeout
=30):
89 logger
.debug("Sending request: %s" % url
)
91 logger
.debug(" data: %s" % data
)
93 req
= urllib2
.Request(url
, data
=data
)
96 req
.add_header("User-Agent", self
.USER_AGENT
)
98 # All requests should not be cached anywhere.
99 req
.add_header("Pragma", "no-cache")
101 # Set the upstream proxy if needed.
103 logger
.debug("Using proxy: %s" % self
.proxy
)
105 # Configure the proxy for this request.
106 req
.set_proxy(self
.proxy
, "http")
108 logger
.debug(_("Request header:"))
109 for k
, v
in req
.headers
.items():
110 logger
.debug(" %s: %s" % (k
, v
))
113 resp
= urllib2
.urlopen(req
)
115 # Log response header.
116 logger
.debug(_("Response header:"))
117 for k
, v
in resp
.info().items():
118 logger
.debug(" %s: %s" % (k
, v
))
120 # Return the entire response object.
123 except urllib2
.URLError
, e
:
126 def get_address(self
, proto
):
127 assert proto
in ("ipv6", "ipv4")
129 # Check if the external IP address should be guessed from
131 guess_ip
= self
.core
.settings
.get("guess_external_ip", "true")
133 # If the external IP address should be used, we just do
135 if guess_ip
in ("true", "yes", "1"):
137 return self
.guess_external_ipv6_address()
139 elif proto
== "ipv4":
140 return self
.guess_external_ipv4_address()