]> git.ipfire.org Git - oddments/ddns.git/blobdiff - ddns/system.py
send_request(): Allow passing a dict with data for GET requests.
[oddments/ddns.git] / ddns / system.py
index 400d38f23c80095133cc0073ab65e5e822a3daec..6057ef48006b437b1a3786cbfa71c1c6b30c7545 100644 (file)
@@ -20,6 +20,7 @@
 ###############################################################################
 
 import re
+import urllib
 import urllib2
 
 from __version__ import CLIENT_VERSION
@@ -48,11 +49,27 @@ class DDNSSystem(object):
                proxy = self.core.settings.get("proxy")
 
                # Strip http:// at the beginning.
-               if proxy.startswith("http://"):
+               if proxy and proxy.startswith("http://"):
                        proxy = proxy[7:]
 
                return proxy
 
+       def guess_external_ipv6_address(self):
+               """
+                       Sends a request to an external web server
+                       to determine the current default IP address.
+               """
+               response = self.send_request("http://checkip6.dns.lightningwirelabs.com")
+
+               if not response.code == 200:
+                       return
+
+               match = re.search(r"^Your IP address is: (.*)$", response.read())
+               if match is None:
+                       return
+
+               return match.group(1)
+
        def guess_external_ipv4_address(self):
                """
                        Sends a request to the internet to determine
@@ -60,17 +77,26 @@ class DDNSSystem(object):
 
                        XXX does not work for IPv6.
                """
-               response = self.send_request("http://checkip.dyndns.org/")
+               response = self.send_request("http://checkip4.dns.lightningwirelabs.com")
 
                if response.code == 200:
-                       match = re.search(r"Current IP Address: (\d+.\d+.\d+.\d+)", response.read())
+                       match = re.search(r"Your IP address is: (\d+.\d+.\d+.\d+)", response.read())
                        if match is None:
                                return
 
                        return match.group(1)
 
-       def send_request(self, url, data=None, timeout=30):
-               logger.debug("Sending request: %s" % url)
+       def send_request(self, url, method="GET", data=None, timeout=30):
+               assert method in ("GET", "POST")
+
+               # Add all arguments in the data dict to the URL and escape them properly.
+               if method == "GET" and data:
+                       query_args = self._format_query_args(data)
+                       data = None
+
+                       url = "%s?%s" % (url, query_args)
+
+               logger.debug("Sending request (%s): %s" % (method, url))
                if data:
                        logger.debug("  data: %s" % data)
 
@@ -89,6 +115,8 @@ class DDNSSystem(object):
                        # Configure the proxy for this request.
                        req.set_proxy(self.proxy, "http")
 
+               assert req.get_method() == method
+
                logger.debug(_("Request header:"))
                for k, v in req.headers.items():
                        logger.debug("  %s: %s" % (k, v))
@@ -107,17 +135,29 @@ class DDNSSystem(object):
                except urllib2.URLError, e:
                        raise
 
+       def _format_query_args(self, data):
+               args = []
+
+               for k, v in data.items():
+                       arg = "%s=%s" % (k, urllib.quote(v))
+                       args.append(arg)
+
+               return "&".join(args)
+
        def get_address(self, proto):
                assert proto in ("ipv6", "ipv4")
 
-               if proto == "ipv4":
-                       # Check if the external IP address should be guessed from
-                       # a remote server.
-                       guess_ip = self.core.settings.get("guess_external_ip", "")
+               # Check if the external IP address should be guessed from
+               # a remote server.
+               guess_ip = self.core.settings.get("guess_external_ip", "true")
+
+               # If the external IP address should be used, we just do
+               # that.
+               if guess_ip in ("true", "yes", "1"):
+                       if proto == "ipv6":
+                               return self.guess_external_ipv6_address()
 
-                       # If the external IP address should be used, we just do
-                       # that.
-                       if guess_ip in ("true", "yes", "1"):
+                       elif proto == "ipv4":
                                return self.guess_external_ipv4_address()
 
                # XXX TODO