mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-07 00:48:41 +00:00
[#3348] Fix TypeError adding peers to torrents
Python3 has stricter type checking and passing a port as string results in libtorrent raising a TypeError. Fixed by casting port to int, along with refactoring to ensure ipv6 is correctly parsing and a useful error is output to user with invalid ip or port details. https://dev.deluge-torrent.org/ticket/3348
This commit is contained in:
parent
76f0bf2e04
commit
62d8749e74
4 changed files with 47 additions and 15 deletions
|
@ -1212,8 +1212,8 @@ class Torrent(object):
|
||||||
bool: True is successful, otherwise False
|
bool: True is successful, otherwise False
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
self.handle.connect_peer((peer_ip, peer_port), 0)
|
self.handle.connect_peer((peer_ip, int(peer_port)), 0)
|
||||||
except RuntimeError as ex:
|
except (RuntimeError, ValueError) as ex:
|
||||||
log.debug('Unable to connect to peer: %s', ex)
|
log.debug('Unable to connect to peer: %s', ex)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -345,3 +345,11 @@ class TorrentTestCase(BaseTestCase):
|
||||||
|
|
||||||
result = self.torrent.rename_files([[0, 'new_рбачёв']])
|
result = self.torrent.rename_files([[0, 'new_рбачёв']])
|
||||||
self.assertIsNone(result)
|
self.assertIsNone(result)
|
||||||
|
|
||||||
|
def test_connect_peer_port(self):
|
||||||
|
"""Test to ensure port is int for libtorrent"""
|
||||||
|
atp = self.get_torrent_atp('test_torrent.file.torrent')
|
||||||
|
handle = self.session.add_torrent(atp)
|
||||||
|
self.torrent = Torrent(handle, {})
|
||||||
|
self.assertFalse(self.torrent.connect_peer('127.0.0.1', 'text'))
|
||||||
|
self.assertTrue(self.torrent.connect_peer('127.0.0.1', '1234'))
|
||||||
|
|
|
@ -29,7 +29,7 @@ from gi.repository.Gtk import (
|
||||||
SortType,
|
SortType,
|
||||||
)
|
)
|
||||||
|
|
||||||
from deluge.common import PY2, get_pixmap, osx_check, windows_check
|
from deluge.common import PY2, get_pixmap, is_ip, osx_check, windows_check
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -393,3 +393,30 @@ def get_clipboard_text():
|
||||||
|
|
||||||
def windowing(like):
|
def windowing(like):
|
||||||
return like.lower() in str(type(Display.get_default())).lower()
|
return like.lower() in str(type(Display.get_default())).lower()
|
||||||
|
|
||||||
|
|
||||||
|
def parse_ip_port(text):
|
||||||
|
"""Return an IP and port from text.
|
||||||
|
|
||||||
|
Parses both IPv4 and IPv6.
|
||||||
|
|
||||||
|
Params:
|
||||||
|
text (str): Text to be parsed for IP and port.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
tuple: (ip (str), port (int))
|
||||||
|
|
||||||
|
"""
|
||||||
|
if '.' in text:
|
||||||
|
# ipv4
|
||||||
|
ip, __, port = text.rpartition(':')
|
||||||
|
elif '[' in text:
|
||||||
|
# ipv6
|
||||||
|
ip, __, port = text.partition('[')[2].partition(']:')
|
||||||
|
else:
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
if ip and is_ip(ip) and port.isdigit():
|
||||||
|
return ip, int(port)
|
||||||
|
else:
|
||||||
|
return None, None
|
||||||
|
|
|
@ -32,6 +32,7 @@ from .common import (
|
||||||
icon_downloading,
|
icon_downloading,
|
||||||
icon_seeding,
|
icon_seeding,
|
||||||
load_pickled_state_file,
|
load_pickled_state_file,
|
||||||
|
parse_ip_port,
|
||||||
save_pickled_state_file,
|
save_pickled_state_file,
|
||||||
)
|
)
|
||||||
from .torrentdetails import Tab
|
from .torrentdetails import Tab
|
||||||
|
@ -376,19 +377,15 @@ class PeersTab(Tab):
|
||||||
peer_dialog = builder.get_object('connect_peer_dialog')
|
peer_dialog = builder.get_object('connect_peer_dialog')
|
||||||
txt_ip = builder.get_object('txt_ip')
|
txt_ip = builder.get_object('txt_ip')
|
||||||
response = peer_dialog.run()
|
response = peer_dialog.run()
|
||||||
|
|
||||||
if response:
|
if response:
|
||||||
value = txt_ip.get_text()
|
value = txt_ip.get_text()
|
||||||
if value and ':' in value:
|
ip, port = parse_ip_port(value)
|
||||||
if ']' in value:
|
if ip and port:
|
||||||
# ipv6
|
log.info('Adding peer IP: %s port: %s to %s', ip, port, self.torrent_id)
|
||||||
ip = value.split(']')[0][1:]
|
client.core.connect_peer(self.torrent_id, ip, port)
|
||||||
port = value.split(']')[1][1:]
|
else:
|
||||||
else:
|
log.error('Error parsing peer "%s"', value)
|
||||||
# ipv4
|
|
||||||
ip = value.split(':')[0]
|
|
||||||
port = value.split(':')[1]
|
|
||||||
if deluge.common.is_ip(ip):
|
|
||||||
log.debug('adding peer %s to %s', value, self.torrent_id)
|
|
||||||
client.core.connect_peer(self.torrent_id, ip, port)
|
|
||||||
peer_dialog.destroy()
|
peer_dialog.destroy()
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue