]> git.ipfire.org Git - oddments/fireinfo.git/commitdiff
Add support for upstream proxy.
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 2 Dec 2010 19:59:04 +0000 (20:59 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 2 Dec 2010 19:59:04 +0000 (20:59 +0100)
Does not support authentication.

Closes #106.

sendprofile

index 0c8a0b5cc580f07504d6a1c17215ef46be150906..74eb7e784f02a7e7e2299bd176505b886a807127 100644 (file)
@@ -11,6 +11,7 @@ import urllib2
 import fireinfo
 
 ENABLED_FILE = "/var/ipfire/main/send_profile"
+PROXY_SETTINGS = "/var/ipfire/proxy/advanced/settings"
 
 log_level = logging.INFO
 if "-d" in sys.argv:
@@ -26,15 +27,38 @@ for handler in log.handlers:
 
 PROFILE_URL = "http://stasy.ipfire.org/send/%(public_id)s"
 
+def get_upstream_proxy():
+       if not os.path.exists(PROXY_SETTINGS):
+               return
+
+       proxy_settings = {}
+       with open(PROXY_SETTINGS) as f:
+               for line in f.readlines():
+                       k, v = line.split("=", 1)
+                       proxy_settings[k] = v.strip()
+
+       return {
+               "host" : proxy_settings.get("UPSTREAM_PROXY", ""),
+               "user" : proxy_settings.get("UPSTREAM_USER", ""),
+               "pass" : proxy_settings.get("UPSTREAM_PASSWORD", ""),
+       }
+
 def send_profile(profile):
        logging.debug("Sending profile:")
        for line in json.dumps(profile, sort_keys=True, indent=4).splitlines():
                logging.debug(line)
 
+       request = urllib2.Request(PROFILE_URL % profile,
+               data = urllib.urlencode({"profile" : json.dumps(profile)}),
+       )
+
+       # Set upstream proxy if we have one.
+       # XXX this cannot handle authentication
+       proxy = get_upstream_proxy()
+       if proxy["host"]:
+               request.set_proxy(proxy["host"], "http")
+
        try:
-               request = urllib2.Request(PROFILE_URL % profile,
-                       data = urllib.urlencode({"profile" : json.dumps(profile)}),
-               )
                urllib2.urlopen(request, timeout=60)
        except (urllib2.HTTPError, urllib2.URLError), e:
                reason = "Unknown reason"