From 2be5474f3a128a62b6f5a4f262a335d810ffdcee Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Tue, 11 Feb 2014 16:23:27 +0000 Subject: [PATCH] [#2082] Validate ip address for listen_interface entry This ensures that only ip addresses are accepted for listen_interface as libtorrent cannot accept interface names (eth0) and will cause unexpected results for user. --- deluge/common.py | 8 ++++---- deluge/core/core.py | 2 +- deluge/core/daemon.py | 4 ++-- deluge/ui/gtkui/preferences.py | 5 ++++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/deluge/common.py b/deluge/common.py index 295be98a0..8b92e014f 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -52,7 +52,7 @@ try: except ImportError: import simplejson as json -from deluge.error import * +from deluge.error import InvalidPathError log = logging.getLogger(__name__) @@ -593,10 +593,10 @@ def is_ip(ip): #first we test ipv4 try: if windows_check(): - if socket.inet_aton("%s" % (ip)): + if socket.inet_aton(ip): return True else: - if socket.inet_pton(socket.AF_INET, "%s" % (ip)): + if socket.inet_pton(socket.AF_INET, ip): return True except socket.error: if not socket.has_ipv6: @@ -607,7 +607,7 @@ def is_ip(ip): log.warning("ipv6 check unavailable on windows") return True else: - if socket.inet_pton(socket.AF_INET6, "%s" % (ip)): + if socket.inet_pton(socket.AF_INET6, ip): return True except socket.error: return False diff --git a/deluge/core/core.py b/deluge/core/core.py index 634583657..581ed7dd0 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -137,7 +137,7 @@ class Core(component.Component): # If there was an interface value from the command line, use it, but # store the one in the config so we can restore it on shutdown self.__old_interface = None - if listen_interface: + if listen_interface and deluge.common.is_ip(listen_interface): self.__old_interface = self.config["listen_interface"] self.config["listen_interface"] = listen_interface diff --git a/deluge/core/daemon.py b/deluge/core/daemon.py index 98ef2dbbd..93aba8bb1 100644 --- a/deluge/core/daemon.py +++ b/deluge/core/daemon.py @@ -37,7 +37,7 @@ from twisted.internet import reactor import deluge.component as component from deluge.configmanager import get_config_dir -from deluge.common import get_version, windows_check +from deluge.common import get_version, windows_check, is_ip from deluge.core.rpcserver import RPCServer, export from deluge.error import DaemonRunningError from deluge.core.core import Core @@ -110,7 +110,7 @@ class Daemon(object): SetConsoleCtrlHandler(win_handler) listen_interface = None - if options and options.listen_interface: + if options and options.listen_interface and is_ip(options.listen_interface): listen_interface = options.listen_interface # Start the core as a thread and join it until it's done diff --git a/deluge/ui/gtkui/preferences.py b/deluge/ui/gtkui/preferences.py index 83e1be9a0..7ac653114 100644 --- a/deluge/ui/gtkui/preferences.py +++ b/deluge/ui/gtkui/preferences.py @@ -611,7 +611,10 @@ class Preferences(component.Component): new_core_config["outgoing_ports"] = outgoing_ports new_core_config["random_outgoing_ports"] = \ self.builder.get_object("chk_random_outgoing_ports").get_active() - new_core_config["listen_interface"] = self.builder.get_object("entry_interface").get_text() + + incoming_address = self.builder.get_object("entry_interface").get_text() + if deluge.common.is_ip(incoming_address) or not incoming_address: + new_core_config["listen_interface"] = incoming_address new_core_config["peer_tos"] = self.builder.get_object("entry_peer_tos").get_text() new_core_config["dht"] = self.builder.get_object("chk_dht").get_active() new_core_config["upnp"] = self.builder.get_object("chk_upnp").get_active()