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.
This commit is contained in:
Calum Lind 2018-06-02 19:03:12 +01:00
parent edd431a304
commit 21b5a15e5d
9 changed files with 44 additions and 38 deletions

View File

@ -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()

View File

@ -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.

View File

@ -36,8 +36,12 @@ def add_daemon_options(parser):
help=_('IP address to listen for BitTorrent connections'),
)
group.add_argument(
'-o', '--outinterface', metavar='<ip-addr>', dest='outgoing_interface',
action='store', help=_('The IP address for outgoing BitTorrent connections.'),
'-o',
'--outgoing-interface',
metavar='<adapter-name>',
dest='outgoing_interface',
action='store',
help=_('The interface adapter name for outgoing BitTorrent connections.'),
)
group.add_argument(
'--read-only-config-keys', metavar='<comma-separated-keys>', action='store',

View File

@ -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()

View File

@ -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'],
)

View File

@ -2924,7 +2924,9 @@ used sparingly.</property>
<object class="GtkEntry" id="entry_outgoing_interface">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="tooltip_text" translatable="yes">IP address for outgoing BitTorrent connections. Leave this empty if you want to use the default.</property>
<property name="tooltip_text" translatable="yes">
The interface adapter name for outgoing BitTorrent connections. (Leave empty for default.)
</property>
<property name="max_length">15</property>
<property name="invisible_char">●</property>
<property name="width_chars">15</property>
@ -2942,7 +2944,7 @@ used sparingly.</property>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Outgoing Address</property>
<property name="label" translatable="yes">Outgoing Interface</property>
<attributes>
<attribute name="weight" value="bold"/>
</attributes>

View File

@ -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()

View File

@ -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({

View File

@ -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"))}')