Immediate HTTP(S) proxy (de)activation

Introduction of HttpProxy class which is based off of the SocksProxy class.
Allows for immediate HTTP(S) proxy activation and deactivation, as the configuration is changed.
This commit is contained in:
Junaid Loonat 2016-04-25 21:50:52 +02:00
parent 654d98c125
commit 8d2da9aa3a

View File

@ -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