Socket: fix 91a38887a with backported ipaddress on Python 2

Closes GH-1342.
This commit is contained in:
James Lu 2018-07-09 03:36:39 +00:00
parent 00b8b6b51e
commit c5175110e0
1 changed files with 8 additions and 2 deletions

View File

@ -39,9 +39,10 @@ import time
import errno import errno
import select import select
import socket import socket
import sys
try: try:
import ipaddress import ipaddress # Python >= 3.3 or backported ipaddress
except ImportError: except ImportError:
# Python < 3.3 # Python < 3.3
ipaddress = None ipaddress = None
@ -284,9 +285,14 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
self.conn.connect((address, port)) self.conn.connect((address, port))
if network_config.ssl(): if network_config.ssl():
self.starttls() self.starttls()
# Suppress this warning for loopback IPs. # Suppress this warning for loopback IPs.
targetip = address
if sys.version_info[0] < 3:
# Backported Python 2 ipaddress demands unicode instead of str
targetip = targetip.decode('utf-8')
elif (not network_config.requireStarttls()) and \ elif (not network_config.requireStarttls()) and \
(ipaddress is None or not ipaddress.ip_address(address).is_loopback): (ipaddress is None or not ipaddress.ip_address(targetip).is_loopback):
drivers.log.warning(('Connection to network %s ' drivers.log.warning(('Connection to network %s '
'does not use SSL/TLS, which makes it vulnerable to ' 'does not use SSL/TLS, which makes it vulnerable to '
'man-in-the-middle attacks and passive eavesdropping. ' 'man-in-the-middle attacks and passive eavesdropping. '