Socket: Forward DNS queries via the socks proxy (if any).

This commit is contained in:
Valentin Lorentz 2013-08-17 15:47:27 +02:00
parent 921ff2ae07
commit dd37f8dd3f
2 changed files with 24 additions and 18 deletions

View File

@ -267,6 +267,19 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
else:
drivers.log.debug('Not resetting %s.', self.irc)
server = self._getNextServer()
socks_proxy = getattr(conf.supybot.networks, self.irc.network) \
.socksproxy()
resolver = None
try:
if socks_proxy:
import socks
except ImportError:
log.error('Cannot use socks proxy (SocksiPy not installed), '
'using direct connection instead.')
socks_proxy = ''
if socks_proxy:
address = server[0]
else:
try:
address = utils.net.getAddressFromHostname(server[0],
attempt=self._attempt)
@ -276,15 +289,6 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
return
drivers.log.connect(self.currentServer)
try:
socks_proxy = getattr(conf.supybot.networks, self.irc.network) \
.socksproxy()
try:
if socks_proxy:
import socks
except ImportError:
log.error('Cannot use socks proxy (SocksiPy not installed), '
'using direct connection instead.')
socks_proxy = ''
self.conn = utils.net.getSocket(address, socks_proxy)
vhost = conf.supybot.protocols.irc.vhost()
self.conn.bind((vhost, 0))

View File

@ -92,13 +92,15 @@ def getSocket(host, socks_proxy=None):
"""Returns a socket of the correct AF_INET type (v4 or v6) in order to
communicate with host.
"""
if not socks_proxy:
addrinfo = socket.getaddrinfo(host, None)
host = addrinfo[0][4][0]
if socks_proxy:
import socks
s = socks.socksocket()
hostname, port = socks_proxy.rsplit(':', 1)
s.setproxy(socks.PROXY_TYPE_SOCKS5, hostname, int(port))
s.setproxy(socks.PROXY_TYPE_SOCKS5, hostname, int(port),
rdns=True)
return s
if isIPV4(host):
return socket.socket(socket.AF_INET, socket.SOCK_STREAM)