Skip to content

Commit

Permalink
Add Support for "Nice Names" for Windows Network Interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
doadin committed Jan 30, 2025
1 parent 65d302c commit 0c19b03
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 13 deletions.
39 changes: 37 additions & 2 deletions deluge/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ def is_interface_name(name):
"""Returns True if an interface name exists.
Args:
name (str): The Interface to test. eg. eth0 linux. GUID on Windows.
name (str): The Interface to test. eg. eth0 linux. GUID or "Nice Name" eg.Local Area Connection" on Windows.
Returns:
bool: Whether name is valid or not.
Expand All @@ -1049,6 +1049,8 @@ def is_interface_name(name):
True
>>> is_interface_name("{7A30AE62-23ZA-3744-Z844-A5B042524871}")
True
>>> is_interface_name("Local Area Connection")
True
"""

Expand All @@ -1066,14 +1068,47 @@ def is_interface_name(name):
except OSError:
return True
else:
return any([name == a.name for a in adapters])
return any([name == a.name for a in adapters]) or any(
[name == a.nice_name for a in adapters]
)

if windows_check():
regex = '^{[0-9A-Z]{8}-([0-9A-Z]{4}-){3}[0-9A-Z]{12}}$'
return bool(re.search(regex, str(name)))

return True

def convert_win_ifaddr_nice_name_to_name(interface):
"""Returns name or guid for an interface from ifaddr.
Args:
name (str): The Interface to convert. eg. "Local Area Connection"
Returns:
str: A GUID eg. {EB51199A-G725-5138-3H29-3529J3D419F7}.
Examples:
>>> convert_win_ifaddr_nice_name_to_name("Local Area Connection")
"{EB51199A-G725-5138-3H29-3529J3D419F7}"
"""
if ifaddr:
if windows_check:
if is_interface(interface):
regex = '^{[0-9A-Z]{8}-([0-9A-Z]{4}-){3}[0-9A-Z]{12}}$'
# if is_interface is true and doesnt match what a GUID would look like
# It is a nice name find what GUID belongs to the nice name for use in LT
if not bool(re.search(regex, str(interface))):
try:
adapters = ifaddr.get_adapters()
except OSError:
return interface
else:
for a in adapters:
if a.nice_name == interface:
interface = a.name
return interface


def decode_bytes(byte_str, encoding='utf8'):
"""Decodes a byte string and return unicode.
Expand Down
27 changes: 27 additions & 0 deletions deluge/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import glob
import logging
import os
import re
import shutil
import tempfile
from base64 import b64decode, b64encode
Expand Down Expand Up @@ -166,6 +167,19 @@ def __init__(
if listen_interface:
if deluge.common.is_interface(listen_interface):
self._old_listen_interface = self.config['listen_interface']
if deluge.common.windows_check:
if deluge.common.is_interface(listen_interface):
regex = '^{[0-9A-Z]{8}-([0-9A-Z]{4}-){3}[0-9A-Z]{12}}$'
# if is_interface is true and doesnt match what a GUID would look like
# It is a nice name find what GUID belongs to the nice name for use in LT
if (
not bool(re.search(regex, str(listen_interface)))
):
listen_interface = (
deluge.common.convert_win_ifaddr_nice_name_to_name(
listen_interface
)
)
self.config['listen_interface'] = listen_interface
else:
log.error(
Expand All @@ -177,6 +191,19 @@ def __init__(
if outgoing_interface:
if deluge.common.is_interface(outgoing_interface):
self._old_outgoing_interface = self.config['outgoing_interface']
if deluge.common.windows_check:
if deluge.common.is_interface(outgoing_interface):
regex = '^{[0-9A-Z]{8}-([0-9A-Z]{4}-){3}[0-9A-Z]{12}}$'
# if is_interface is true and doesnt match what a GUID would look like
# It is a nice name find what GUID belongs to the nice name for use in LT
if (
not bool(re.search(regex, str(outgoing_interface)))
):
outgoing_interface = (
deluge.common.convert_win_ifaddr_nice_name_to_name(
outgoing_interface
)
)
self.config['outgoing_interface'] = outgoing_interface
else:
log.error(
Expand Down
12 changes: 4 additions & 8 deletions deluge/ui/console/modes/preferences/preference_panes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#

import logging
import re

from deluge.common import convert_win_ifaddr_nice_name_to_name, is_interface, windows_check
from deluge.decorators import overrides
from deluge.i18n import get_languages
from deluge.ui.client import client
Expand Down Expand Up @@ -90,17 +92,11 @@ def add_config_values(self, conf_dict):
)
elif ipt.name == 'listen_interface':
listen_interface = ipt.get_value().strip()
if (
client.core.is_valid_interface(listen_interface)
or not listen_interface
):
if is_interface(listen_interface) or not listen_interface:
conf_dict['listen_interface'] = listen_interface
elif ipt.name == 'outgoing_interface':
outgoing_interface = ipt.get_value().strip()
if (
client.core.is_valid_interface(outgoing_interface)
or not outgoing_interface
):
if is_interface(outgoing_interface) or not outgoing_interface:
conf_dict['outgoing_interface'] = outgoing_interface
elif ipt.name.startswith('proxy_'):
if ipt.name == 'proxy_type':
Expand Down
35 changes: 32 additions & 3 deletions deluge/ui/gtk3/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import logging
import os
import re
from hashlib import sha1 as sha
from urllib.parse import urlparse

Expand Down Expand Up @@ -686,13 +687,41 @@ def set_config(self, hide=False):
'chk_random_outgoing_ports'
).get_active()
incoming_address = self.builder.get_object('entry_interface').get_text().strip()
if client.core.is_valid_interface(incoming_address) or not incoming_address:
if deluge.common.is_interface(incoming_address) or not incoming_address:
if deluge.common.windows_check:
if deluge.common.is_interface(incoming_address):
regex = '^{[0-9A-Z]{8}-([0-9A-Z]{4}-){3}[0-9A-Z]{12}}$'
# if is_interface is true and doesnt match what a GUID would look like
# It is a nice name find what GUID belongs to the nice name for use in LT
if (
not bool(re.search(regex, str(incoming_address)))
):
incoming_address = (
deluge.common.convert_win_ifaddr_nice_name_to_name(
incoming_address
)
)
new_core_config['listen_interface'] = incoming_address
outgoing_address = (
self.builder.get_object('entry_outgoing_interface').get_text().strip()
)
if client.core.is_valid_interface(outgoing_address) or not outgoing_address:
new_core_config['outgoing_interface'] = outgoing_address
if deluge.common.is_interface(outgoing_address) or not outgoing_address:
if deluge.common.windows_check:
if deluge.common.is_interface(outgoing_address):
regex = '^{[0-9A-Z]{8}-([0-9A-Z]{4}-){3}[0-9A-Z]{12}}$'
# if is_interface is true and doesnt match what a GUID would look like
# It is a nice name find what GUID belongs to the nice name for use in LT
if (
not bool(re.search(regex, str(outgoing_address)))
):
outgoing_address = (
deluge.common.convert_win_ifaddr_nice_name_to_name(
outgoing_address
)
)
new_core_config['outgoing_interface'] = (
self.builder.get_object('entry_outgoing_interface').get_text().strip()
)
new_core_config['peer_tos'] = self.builder.get_object(
'entry_peer_tos'
).get_text()
Expand Down

0 comments on commit 0c19b03

Please sign in to comment.