]>
git.ipfire.org Git - 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
.startswith("http://"):
56 def guess_external_ipv4_address(self
):
58 Sends a request to the internet to determine
59 the public IP address.
61 XXX does not work for IPv6.
63 response
= self
.send_request("http://checkip.dyndns.org/")
65 if response
.code
== 200:
66 match
= re
.search(r
"Current IP Address: (\d+.\d+.\d+.\d+)", response
.read())
72 def send_request(self
, url
, data
=None, timeout
=30):
73 logger
.debug("Sending request: %s" % url
)
75 logger
.debug(" data: %s" % data
)
77 req
= urllib2
.Request(url
, data
=data
)
80 req
.add_header("User-Agent", self
.USER_AGENT
)
82 # All requests should not be cached anywhere.
83 req
.add_header("Pragma", "no-cache")
85 # Set the upstream proxy if needed.
87 logger
.debug("Using proxy: %s" % self
.proxy
)
89 # Configure the proxy for this request.
90 req
.set_proxy(self
.proxy
, "http")
92 logger
.debug(_("Request header:"))
93 for k
, v
in req
.headers
.items():
94 logger
.debug(" %s: %s" % (k
, v
))
97 resp
= urllib2
.urlopen(req
)
99 # Log response header.
100 logger
.debug(_("Response header:"))
101 for k
, v
in resp
.info().items():
102 logger
.debug(" %s: %s" % (k
, v
))
104 # Return the entire response object.
107 except urllib2
.URLError
, e
:
110 def get_address(self
, proto
):
111 assert proto
in ("ipv6", "ipv4")
114 # Check if the external IP address should be guessed from
116 guess_ip
= self
.core
.settings
.get("guess_external_ip", "")
118 # If the external IP address should be used, we just do
120 if guess_ip
in ("true", "yes", "1"):
121 return self
.guess_external_ipv4_address()