Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for "Nice Names" for Windows Network Interfaces #473

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 28 additions & 1 deletion deluge/ui/console/modes/preferences/preference_panes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#

import logging
import re

from deluge.common import is_interface
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 @@ -92,10 +93,36 @@ def add_config_values(self, conf_dict):
elif ipt.name == 'listen_interface':
listen_interface = ipt.get_value().strip()
if is_interface(listen_interface) or not listen_interface:
if windows_check:
if 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 = (
convert_win_ifaddr_nice_name_to_name(
listen_interface
)
)
conf_dict['listen_interface'] = listen_interface
elif ipt.name == 'outgoing_interface':
outgoing_interface = ipt.get_value().strip()
if is_interface(outgoing_interface) or not outgoing_interface:
if windows_check:
if 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 = (
convert_win_ifaddr_nice_name_to_name(
outgoing_interface
)
)
conf_dict['outgoing_interface'] = outgoing_interface
elif ipt.name.startswith('proxy_'):
if ipt.name == 'proxy_type':
Expand Down
27 changes: 27 additions & 0 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 @@ -687,11 +688,37 @@ def set_config(self, hide=False):
).get_active()
incoming_address = self.builder.get_object('entry_interface').get_text().strip()
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 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()
)
Expand Down
Loading