Skip to content

Commit

Permalink
Merge pull request #1235 from JunaidLoonat/master
Browse files Browse the repository at this point in the history
Use HTTP proxy for HTTPS requests as well
  • Loading branch information
progval committed Apr 29, 2016
2 parents c9e5486 + 3a2b603 commit fa5552e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/conf.py
Expand Up @@ -34,9 +34,15 @@
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
Expand Down Expand Up @@ -1169,8 +1175,25 @@ def makeBanmask(self, hostmask, options=None, channel=None):
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

Expand Down
11 changes: 11 additions & 0 deletions src/utils/net.py
Expand Up @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions src/utils/web.py
Expand Up @@ -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:
Expand Down

0 comments on commit fa5552e

Please sign in to comment.