From: Michael Tremer Date: Thu, 2 Dec 2010 19:59:04 +0000 (+0100) Subject: Add support for upstream proxy. X-Git-Tag: v0.5~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a3560310b2cbb8d30bee2ab472c33674ca929d67;p=oddments%2Ffireinfo.git Add support for upstream proxy. Does not support authentication. Closes #106. --- diff --git a/sendprofile b/sendprofile index 0c8a0b5..74eb7e7 100644 --- a/sendprofile +++ b/sendprofile @@ -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"