]> git.ipfire.org Git - people/stevee/ddns.git/blame - ddns/system.py
Fix crash when no proxy is configured.
[people/stevee/ddns.git] / ddns / system.py
CommitLineData
f22ab085 1#!/usr/bin/python
3fdcb9d1
MT
2###############################################################################
3# #
4# ddns - A dynamic DNS client for IPFire #
5# Copyright (C) 2012 IPFire development team #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
f22ab085
MT
21
22import re
23import urllib2
24
25from __version__ import CLIENT_VERSION
26from i18n import _
27
28# Initialize the logger.
29import logging
30logger = logging.getLogger("ddns.system")
31logger.propagate = 1
32
33class DDNSSystem(object):
34 """
35 The DDNSSystem class adds a layer of abstraction
36 between the ddns software and the system.
37 """
38
39 # The default useragent.
40 USER_AGENT = "IPFireDDNSUpdater/%s" % CLIENT_VERSION
41
42 def __init__(self, core):
43 # Connection to the core of the program.
44 self.core = core
45
46 @property
47 def proxy(self):
48 proxy = self.core.settings.get("proxy")
49
50 # Strip http:// at the beginning.
fd898828 51 if proxy and proxy.startswith("http://"):
f22ab085
MT
52 proxy = proxy[7:]
53
54 return proxy
55
56 def guess_external_ipv4_address(self):
57 """
58 Sends a request to the internet to determine
59 the public IP address.
60
61 XXX does not work for IPv6.
62 """
63 response = self.send_request("http://checkip.dyndns.org/")
64
65 if response.code == 200:
66 match = re.search(r"Current IP Address: (\d+.\d+.\d+.\d+)", response.read())
67 if match is None:
68 return
69
70 return match.group(1)
71
72 def send_request(self, url, data=None, timeout=30):
73 logger.debug("Sending request: %s" % url)
74 if data:
75 logger.debug(" data: %s" % data)
76
77 req = urllib2.Request(url, data=data)
78
79 # Set the user agent.
80 req.add_header("User-Agent", self.USER_AGENT)
81
82 # All requests should not be cached anywhere.
83 req.add_header("Pragma", "no-cache")
84
85 # Set the upstream proxy if needed.
86 if self.proxy:
87 logger.debug("Using proxy: %s" % self.proxy)
88
89 # Configure the proxy for this request.
90 req.set_proxy(self.proxy, "http")
91
92 logger.debug(_("Request header:"))
93 for k, v in req.headers.items():
94 logger.debug(" %s: %s" % (k, v))
95
96 try:
97 resp = urllib2.urlopen(req)
98
99 # Log response header.
100 logger.debug(_("Response header:"))
101 for k, v in resp.info().items():
102 logger.debug(" %s: %s" % (k, v))
103
104 # Return the entire response object.
105 return resp
106
107 except urllib2.URLError, e:
108 raise
109
110 def get_address(self, proto):
111 assert proto in ("ipv6", "ipv4")
112
113 if proto == "ipv4":
114 # Check if the external IP address should be guessed from
115 # a remote server.
116 guess_ip = self.core.settings.get("guess_external_ip", "")
117
118 # If the external IP address should be used, we just do
119 # that.
120 if guess_ip in ("true", "yes", "1"):
121 return self.guess_external_ipv4_address()
122
123 # XXX TODO
124 assert False