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,24 +267,28 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
else: else:
drivers.log.debug('Not resetting %s.', self.irc) drivers.log.debug('Not resetting %s.', self.irc)
server = self._getNextServer() server = self._getNextServer()
socks_proxy = getattr(conf.supybot.networks, self.irc.network) \
.socksproxy()
resolver = None
try: try:
address = utils.net.getAddressFromHostname(server[0], if socks_proxy:
attempt=self._attempt) import socks
except socket.gaierror as e: except ImportError:
drivers.log.connectError(self.currentServer, e) log.error('Cannot use socks proxy (SocksiPy not installed), '
self.scheduleReconnect() 'using direct connection instead.')
return socks_proxy = ''
if socks_proxy:
address = server[0]
else:
try:
address = utils.net.getAddressFromHostname(server[0],
attempt=self._attempt)
except socket.gaierror as e:
drivers.log.connectError(self.currentServer, e)
self.scheduleReconnect()
return
drivers.log.connect(self.currentServer) drivers.log.connect(self.currentServer)
try: 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) self.conn = utils.net.getSocket(address, socks_proxy)
vhost = conf.supybot.protocols.irc.vhost() vhost = conf.supybot.protocols.irc.vhost()
self.conn.bind((vhost, 0)) 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 """Returns a socket of the correct AF_INET type (v4 or v6) in order to
communicate with host. communicate with host.
""" """
addrinfo = socket.getaddrinfo(host, None) if not socks_proxy:
host = addrinfo[0][4][0] addrinfo = socket.getaddrinfo(host, None)
host = addrinfo[0][4][0]
if socks_proxy: if socks_proxy:
import socks import socks
s = socks.socksocket() s = socks.socksocket()
hostname, port = socks_proxy.rsplit(':', 1) 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 return s
if isIPV4(host): if isIPV4(host):
return socket.socket(socket.AF_INET, socket.SOCK_STREAM) return socket.socket(socket.AF_INET, socket.SOCK_STREAM)