From 21b5a15e5df0f4aefd37d7f2a6c46b72e4c652aa Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Sat, 2 Jun 2018 19:03:12 +0100 Subject: [PATCH] Fix and cleanup outgoing interface code There was a misunderstand about outgoing interface setting in libtorrent and instead of being able to take both IP and adapater names, it only accepts adapter names and errors with an IP address, which was the default of '0.0.0.0' in code. This fixes the code to not accept IP address and use empty string if it is given one. Also includes a bit of code cleanup. --- deluge/core/core.py | 24 +++++++++++-------- deluge/core/daemon.py | 2 +- deluge/core/daemon_entry.py | 8 +++++-- deluge/core/preferencesmanager.py | 14 ++++------- .../modes/preferences/preference_panes.py | 14 +++++------ deluge/ui/gtkui/glade/preferences_dialog.ui | 6 +++-- deluge/ui/gtkui/preferences.py | 7 +++--- .../js/deluge-all/preferences/NetworkPage.js | 5 ++-- deluge/ui/web/js/gettext.js | 2 +- 9 files changed, 44 insertions(+), 38 deletions(-) diff --git a/deluge/core/core.py b/deluge/core/core.py index 80c14f6a2..db20f6d29 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -157,20 +157,24 @@ 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 + self._old_listen_interface = None if listen_interface: if deluge.common.is_ip(listen_interface): - self.__old_interface = self.config['listen_interface'] + self._old_listen_interface = self.config['listen_interface'] self.config['listen_interface'] = listen_interface else: log.error('Invalid listen interface (must be IP Address): %s', listen_interface) - self.__old_outgoing_interface = None + + self._old_outgoing_interface = None if outgoing_interface: - if deluge.common.is_ip(outgoing_interface): - self.__old_outgoing_interface = self.config['outgoing_interface'] + if not deluge.common.is_ip(outgoing_interface): + self._old_outgoing_interface = self.config['outgoing_interface'] self.config['outgoing_interface'] = outgoing_interface else: - log.error('Invalid outgoing interface (must be IP Address): %s', outgoing_interface) + log.error( + 'Invalid outgoing interface (must be adapter name): %s', + outgoing_interface, + ) # New release check information self.__new_release = None @@ -202,11 +206,11 @@ class Core(component.Component): self._save_session_state() # We stored a copy of the old interface value - if self.__old_interface: - self.config['listen_interface'] = self.__old_interface + if self._old_listen_interface is None: + self.config['listen_interface'] = self._old_listen_interface - if self.__old_outgoing_interface: - self.config['outgoing_interface'] = self.__old_outgoing_interface + if self._old_outgoing_interface is None: + self.config['outgoing_interface'] = self._old_outgoing_interface # Make sure the config file has been saved self.config.save() diff --git a/deluge/core/daemon.py b/deluge/core/daemon.py index e92377b21..bd203d940 100644 --- a/deluge/core/daemon.py +++ b/deluge/core/daemon.py @@ -73,7 +73,7 @@ class Daemon(object): Args: listen_interface (str, optional): The IP address to listen to bittorrent connections on. outgoing_interface (str, optional): The IP address to open outgoing BitTorrent connections on. - interface (str, optional): The IP address the daemon will listen for UI connections on. + interface (str, optional): Adapter name the daemon will listen for UI connections on. port (int, optional): The port the daemon will listen for UI connections on. standalone (bool, optional): If True the client is in Standalone mode otherwise, if False, start the daemon as separate process. diff --git a/deluge/core/daemon_entry.py b/deluge/core/daemon_entry.py index 0500986f3..eee14b32e 100644 --- a/deluge/core/daemon_entry.py +++ b/deluge/core/daemon_entry.py @@ -36,8 +36,12 @@ def add_daemon_options(parser): help=_('IP address to listen for BitTorrent connections'), ) group.add_argument( - '-o', '--outinterface', metavar='', dest='outgoing_interface', - action='store', help=_('The IP address for outgoing BitTorrent connections.'), + '-o', + '--outgoing-interface', + metavar='', + dest='outgoing_interface', + action='store', + help=_('The interface adapter name for outgoing BitTorrent connections.'), ) group.add_argument( '--read-only-config-keys', metavar='', action='store', diff --git a/deluge/core/preferencesmanager.py b/deluge/core/preferencesmanager.py index d4ba358b7..9d0e9549c 100644 --- a/deluge/core/preferencesmanager.py +++ b/deluge/core/preferencesmanager.py @@ -191,7 +191,11 @@ class PreferencesManager(component.Component): self.__set_listen_on() def _on_set_outgoing_interface(self, key, value): - self.__set_outgoing_on() + """ Set the adapter name for outgoing BitTorrent connections.""" + value = value.strip() + if not value or deluge.common.is_ip(value): + value = '' + self.core.apply_session_settings({'outgoing_interfaces': value}) def _on_set_random_port(self, key, value): self.__set_listen_on() @@ -224,14 +228,6 @@ class PreferencesManager(component.Component): }, ) - def __set_outgoing_on(self): - """ Set the interface address for outgoing BitTorrent connections.""" - outinterface = self.config['outgoing_interface'].strip() - outinterface = outinterface if outinterface else '0.0.0.0' - self.core.apply_session_settings( - {'outgoing_interfaces': outinterface}, - ) - def _on_set_outgoing_ports(self, key, value): self.__set_outgoing_ports() diff --git a/deluge/ui/console/modes/preferences/preference_panes.py b/deluge/ui/console/modes/preferences/preference_panes.py index 731ac067b..fd989fdbd 100644 --- a/deluge/ui/console/modes/preferences/preference_panes.py +++ b/deluge/ui/console/modes/preferences/preference_panes.py @@ -80,13 +80,13 @@ class BasePreferencePane(BaseInputPane, BaseWindow, PopupsHandler): elif ipt.name == 'out_ports_to' or ipt.name == 'out_ports_from': conf_dict['outgoing_ports'] = (self.outfrom.get_value(), self.outto.get_value()) elif ipt.name == 'listen_interface': - interface = ipt.get_value().strip() - if is_ip(interface) or not interface: - conf_dict['listen_interface'] = interface + listen_interface = ipt.get_value().strip() + if is_ip(listen_interface) or not listen_interface: + conf_dict['listen_interface'] = listen_interface elif ipt.name == 'outgoing_interface': - outinterface = ipt.get_value().strip() - if is_ip(outinterface) or not outinterface: - conf_dict['outgoing_interface'] = outinterface + outgoing_interface = ipt.get_value().strip() + if not is_ip(outgoing_interface) or not outgoing_interface: + conf_dict['outgoing_interface'] = outgoing_interface elif ipt.name.startswith('proxy_'): if ipt.name == 'proxy_type': conf_dict.setdefault('proxy', {})['type'] = ipt.get_value() @@ -300,7 +300,7 @@ class NetworkPane(BasePreferencePane): self.add_header(_('Outgoing Interface'), space_above=True) self.add_text_input( 'outgoing_interface', - _('IP address of the interface to open outgoing connections on. (leave empty for default):'), + _('The interface adapter name for outgoing BitTorrent connections. (Leave empty for default.):'), core_conf['outgoing_interface'], ) diff --git a/deluge/ui/gtkui/glade/preferences_dialog.ui b/deluge/ui/gtkui/glade/preferences_dialog.ui index edfeed534..34509a444 100644 --- a/deluge/ui/gtkui/glade/preferences_dialog.ui +++ b/deluge/ui/gtkui/glade/preferences_dialog.ui @@ -2924,7 +2924,9 @@ used sparingly. True True - IP address for outgoing BitTorrent connections. Leave this empty if you want to use the default. + +The interface adapter name for outgoing BitTorrent connections. (Leave empty for default.) + 15 15 @@ -2942,7 +2944,7 @@ used sparingly. True False - Outgoing Address + Outgoing Interface diff --git a/deluge/ui/gtkui/preferences.py b/deluge/ui/gtkui/preferences.py index ab7619c83..a5b49a71f 100644 --- a/deluge/ui/gtkui/preferences.py +++ b/deluge/ui/gtkui/preferences.py @@ -532,11 +532,12 @@ class Preferences(component.Component): 'chk_random_outgoing_ports', ).get_active() incoming_address = self.builder.get_object('entry_interface').get_text().strip() - outgoing_address = self.builder.get_object('entry_outgoing_interface').get_text().strip() if deluge.common.is_ip(incoming_address) or not incoming_address: new_core_config['listen_interface'] = incoming_address - if deluge.common.is_ip(outgoing_address) or not outgoing_address: - new_core_config['outgoing_interface'] = outgoing_address + outgoing_interface = self.builder.get_object( + 'entry_outgoing_interface').get_text().strip() + if not deluge.common.is_ip(outgoing_interface) or not outgoing_interface: + new_core_config['outgoing_interface'] = outgoing_interface 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() diff --git a/deluge/ui/web/js/deluge-all/preferences/NetworkPage.js b/deluge/ui/web/js/deluge-all/preferences/NetworkPage.js index f110be335..167be3b1d 100644 --- a/deluge/ui/web/js/deluge-all/preferences/NetworkPage.js +++ b/deluge/ui/web/js/deluge-all/preferences/NetworkPage.js @@ -93,7 +93,7 @@ Deluge.preferences.Network = Ext.extend(Ext.form.FormPanel, { fieldset = this.add({ xtype: 'fieldset', border: false, - title: _('Outgoing Address'), + title: _('Outgoing Interface'), style: 'margin-bottom: 5px; padding-bottom: 0px;', autoHeight: true, labelWidth: 1, @@ -103,8 +103,7 @@ Deluge.preferences.Network = Ext.extend(Ext.form.FormPanel, { name: 'outgoing_interface', fieldLabel: '', labelSeparator: '', - width: 200, - vtype: 'IPAddress' + width: 40, })); fieldset = this.add({ diff --git a/deluge/ui/web/js/gettext.js b/deluge/ui/web/js/gettext.js index 47f4f85f4..fd4fa4a97 100644 --- a/deluge/ui/web/js/gettext.js +++ b/deluge/ui/web/js/gettext.js @@ -186,7 +186,7 @@ GetText.add('On','${escape(_("On"))}') GetText.add('Online','${escape(_("Online"))}') GetText.add('Options','${escape(_("Options"))}') GetText.add('Other','${escape(_("Other"))}') -GetText.add('Outgoing Address','${escape(_("Outgoing Address"))}') +GetText.add('Outgoing Interface','${escape(_("Outgoing Interface"))}') GetText.add('Outgoing Ports','${escape(_("Outgoing Ports"))}') GetText.add('Outgoing:','${escape(_("Outgoing:"))}') GetText.add('Owner','${escape(_("Owner"))}')