Merge pull request #1235 from JunaidLoonat/master

Use HTTP proxy for HTTPS requests as well
This commit is contained in:
Valentin Lorentz 2016-04-29 22:09:17 +02:00
commit fa5552ee5f
3 changed files with 35 additions and 4 deletions

View File

@ -34,9 +34,15 @@ import time
import socket
from . import ircutils, registry, utils
from .utils import minisix
from .utils.net import isSocketAddress
from .version import version
from .i18n import PluginInternationalization
_ = PluginInternationalization()
if minisix.PY2:
from urllib2 import build_opener, install_opener, ProxyHandler
else:
from urllib.request import build_opener, install_opener, ProxyHandler
###
# *** The following variables are affected by command-line options. They are
@ -1169,8 +1175,25 @@ 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):
proxies = {}
if v != "":
if isSocketAddress(v):
proxies = {
'http': v,
'https': v
}
else:
self.error()
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

View File

@ -77,6 +77,17 @@ def getSocket(host, port=None, socks_proxy=None, vhost=None, vhostv6=None):
else:
raise socket.error('Something wonky happened.')
def isSocketAddress(s):
if ':' in s:
host, port = s.rsplit(':', 1)
try:
int(port)
sock = getSocket(host, port)
return True
except (ValueError, socket.error):
pass
return False
def isIP(s):
"""Returns whether or not a given string is an IP address.

View File

@ -143,9 +143,6 @@ def getUrlFd(url, headers=None, data=None, timeout=None):
else:
request = url
request.add_data(data)
httpProxy = force(proxy)
if httpProxy:
request.set_proxy(httpProxy, 'http')
fd = urlopen(request, timeout=timeout)
return fd
except socket.timeout as e: