diff --git a/src/conf.py b/src/conf.py index 1a3adac78..01ceb93b1 100644 --- a/src/conf.py +++ b/src/conf.py @@ -34,6 +34,7 @@ import time import socket from . import ircutils, registry, utils +from .utils import minisix from .version import version from .i18n import PluginInternationalization _ = PluginInternationalization() @@ -1169,8 +1170,33 @@ registerGlobalValue(supybot.protocols.http, 'peekSize', similar. It'll give up after it reads this many bytes, even if it hasn't found what it was looking for."""))) +class HttpProxy(registry.String): + """Value must be a valid hostname:port string.""" + def setValue(self, v): + if minisix.PY2: + from urllib2 import build_opener, install_opener, ProxyHandler + else: + from urllib.request import build_opener, install_opener, ProxyHandler + proxies = {} + if v != "": + # TODO: improve checks + if ':' not in v: + self.error() + try: + int(v.rsplit(':', 1)[1]) + except ValueError: + self.error() + proxies = { + 'http': v, + 'https': v + } + proxyHandler = ProxyHandler(proxies) + proxyOpenerDirector = build_opener(proxyHandler) + install_opener(proxyOpenerDirector) + super(HttpProxy, self).setValue(v) + registerGlobalValue(supybot.protocols.http, 'proxy', - registry.String('', _("""Determines what proxy all HTTP requests should go + HttpProxy('', _("""Determines what proxy all HTTP requests should go through. The value should be of the form 'host:port'."""))) utils.web.proxy = supybot.protocols.http.proxy