]>
git.ipfire.org Git - people/ms/ddns.git/blob - ddns/system.py
6 from __version__
import CLIENT_VERSION
9 # Initialize the logger.
11 logger
= logging
.getLogger("ddns.system")
14 class DDNSSystem(object):
16 The DDNSSystem class adds a layer of abstraction
17 between the ddns software and the system.
20 # The default useragent.
21 USER_AGENT
= "IPFireDDNSUpdater/%s" % CLIENT_VERSION
23 def __init__(self
, core
):
24 # Connection to the core of the program.
29 proxy
= self
.core
.settings
.get("proxy")
31 # Strip http:// at the beginning.
32 if proxy
.startswith("http://"):
37 def guess_external_ipv4_address(self
):
39 Sends a request to the internet to determine
40 the public IP address.
42 XXX does not work for IPv6.
44 response
= self
.send_request("http://checkip.dyndns.org/")
46 if response
.code
== 200:
47 match
= re
.search(r
"Current IP Address: (\d+.\d+.\d+.\d+)", response
.read())
53 def send_request(self
, url
, data
=None, timeout
=30):
54 logger
.debug("Sending request: %s" % url
)
56 logger
.debug(" data: %s" % data
)
58 req
= urllib2
.Request(url
, data
=data
)
61 req
.add_header("User-Agent", self
.USER_AGENT
)
63 # All requests should not be cached anywhere.
64 req
.add_header("Pragma", "no-cache")
66 # Set the upstream proxy if needed.
68 logger
.debug("Using proxy: %s" % self
.proxy
)
70 # Configure the proxy for this request.
71 req
.set_proxy(self
.proxy
, "http")
73 logger
.debug(_("Request header:"))
74 for k
, v
in req
.headers
.items():
75 logger
.debug(" %s: %s" % (k
, v
))
78 resp
= urllib2
.urlopen(req
)
80 # Log response header.
81 logger
.debug(_("Response header:"))
82 for k
, v
in resp
.info().items():
83 logger
.debug(" %s: %s" % (k
, v
))
85 # Return the entire response object.
88 except urllib2
.URLError
, e
:
91 def get_address(self
, proto
):
92 assert proto
in ("ipv6", "ipv4")
95 # Check if the external IP address should be guessed from
97 guess_ip
= self
.core
.settings
.get("guess_external_ip", "")
99 # If the external IP address should be used, we just do
101 if guess_ip
in ("true", "yes", "1"):
102 return self
.guess_external_ipv4_address()