mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Use black with -l 120 param.
This commit is contained in:
1 parent
f928738bd5
commit
c021e21309
194 files changed
+6034
-4564
No files matched your search
@@ -26,9 +26,12 @@ from gns3server.compute.compute_error import ComputeError
|
||||
from gns3server.config import Config
|
||||
|
||||
if psutil.version_info < (3, 0, 0):
|
||||
raise Exception("psutil version should >= 3.0.0. If you are under Ubuntu/Debian install gns3 via apt instead of pip")
|
||||
raise Exception(
|
||||
"psutil version should >= 3.0.0. If you are under Ubuntu/Debian install gns3 via apt instead of pip"
|
||||
)
|
||||
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -45,7 +48,10 @@ def _get_windows_interfaces_from_registry():
|
||||
hkeycard = winreg.OpenKey(hkey, network_card_id)
|
||||
guid, _ = winreg.QueryValueEx(hkeycard, "ServiceName")
|
||||
netcard, _ = winreg.QueryValueEx(hkeycard, "Description")
|
||||
connection = r"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}" + fr"\{guid}\Connection"
|
||||
connection = (
|
||||
r"SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
|
||||
+ fr"\{guid}\Connection"
|
||||
)
|
||||
hkeycon = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, connection)
|
||||
name, _ = winreg.QueryValueEx(hkeycon, "Name")
|
||||
interface = fr"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{guid}"
|
||||
@@ -61,13 +67,17 @@ def _get_windows_interfaces_from_registry():
|
||||
# get the first IPv4 address only
|
||||
ip_address = ip_address[0]
|
||||
npf_interface = "\\Device\\NPF_{guid}".format(guid=guid)
|
||||
interfaces.append({"id": npf_interface,
|
||||
"name": name,
|
||||
"ip_address": ip_address,
|
||||
"mac_address": "", # TODO: find MAC address in registry
|
||||
"netcard": netcard,
|
||||
"netmask": netmask,
|
||||
"type": "ethernet"})
|
||||
interfaces.append(
|
||||
{
|
||||
"id": npf_interface,
|
||||
"name": name,
|
||||
"ip_address": ip_address,
|
||||
"mac_address": "", # TODO: find MAC address in registry
|
||||
"netcard": netcard,
|
||||
"netmask": netmask,
|
||||
"type": "ethernet",
|
||||
}
|
||||
)
|
||||
winreg.CloseKey(hkeyinterface)
|
||||
winreg.CloseKey(hkeycon)
|
||||
winreg.CloseKey(hkeycard)
|
||||
@@ -107,13 +117,17 @@ def get_windows_interfaces():
|
||||
netmask = network_config.IPSubnet[0]
|
||||
break
|
||||
npf_interface = "\\Device\\NPF_{guid}".format(guid=adapter.GUID)
|
||||
interfaces.append({"id": npf_interface,
|
||||
"name": adapter.NetConnectionID,
|
||||
"ip_address": ip_address,
|
||||
"mac_address": adapter.MACAddress,
|
||||
"netcard": adapter.name,
|
||||
"netmask": netmask,
|
||||
"type": "ethernet"})
|
||||
interfaces.append(
|
||||
{
|
||||
"id": npf_interface,
|
||||
"name": adapter.NetConnectionID,
|
||||
"ip_address": ip_address,
|
||||
"mac_address": adapter.MACAddress,
|
||||
"netcard": adapter.name,
|
||||
"netmask": netmask,
|
||||
"type": "ethernet",
|
||||
}
|
||||
)
|
||||
except (AttributeError, pywintypes.com_error):
|
||||
log.warning("Could not use the COM service to retrieve interface info, trying using the registry...")
|
||||
return _get_windows_interfaces_from_registry()
|
||||
@@ -152,11 +166,12 @@ def is_interface_up(interface):
|
||||
return False
|
||||
|
||||
import fcntl
|
||||
|
||||
SIOCGIFFLAGS = 0x8913
|
||||
try:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||
result = fcntl.ioctl(s.fileno(), SIOCGIFFLAGS, interface + '\0' * 256)
|
||||
flags, = struct.unpack('H', result[16:18])
|
||||
result = fcntl.ioctl(s.fileno(), SIOCGIFFLAGS, interface + "\0" * 256)
|
||||
(flags,) = struct.unpack("H", result[16:18])
|
||||
if flags & 1: # check if the up bit is set
|
||||
return True
|
||||
return False
|
||||
@@ -203,12 +218,16 @@ def interfaces():
|
||||
if interface.startswith("tap"):
|
||||
# found no way to reliably detect a TAP interface
|
||||
interface_type = "tap"
|
||||
results.append({"id": interface,
|
||||
"name": interface,
|
||||
"ip_address": ip_address,
|
||||
"netmask": netmask,
|
||||
"mac_address": mac_address,
|
||||
"type": interface_type})
|
||||
results.append(
|
||||
{
|
||||
"id": interface,
|
||||
"name": interface,
|
||||
"ip_address": ip_address,
|
||||
"netmask": netmask,
|
||||
"mac_address": mac_address,
|
||||
"type": interface_type,
|
||||
}
|
||||
)
|
||||
else:
|
||||
try:
|
||||
service_installed = True
|
||||
@@ -217,7 +236,9 @@ def interfaces():
|
||||
else:
|
||||
results = get_windows_interfaces()
|
||||
except ImportError:
|
||||
message = "pywin32 module is not installed, please install it on the server to get the available interface names"
|
||||
message = (
|
||||
"pywin32 module is not installed, please install it on the server to get the available interface names"
|
||||
)
|
||||
raise ComputeError(message)
|
||||
except Exception as e:
|
||||
log.error(f"uncaught exception {type(e)}", exc_info=1)
|
||||
@@ -229,12 +250,25 @@ def interfaces():
|
||||
# This interface have special behavior
|
||||
for result in results:
|
||||
result["special"] = False
|
||||
for special_interface in ("lo", "vmnet", "vboxnet", "docker", "lxcbr",
|
||||
"virbr", "ovs-system", "veth", "fw", "p2p",
|
||||
"bridge", "vmware", "virtualbox", "gns3"):
|
||||
for special_interface in (
|
||||
"lo",
|
||||
"vmnet",
|
||||
"vboxnet",
|
||||
"docker",
|
||||
"lxcbr",
|
||||
"virbr",
|
||||
"ovs-system",
|
||||
"veth",
|
||||
"fw",
|
||||
"p2p",
|
||||
"bridge",
|
||||
"vmware",
|
||||
"virtualbox",
|
||||
"gns3",
|
||||
):
|
||||
if result["name"].lower().startswith(special_interface):
|
||||
result["special"] = True
|
||||
for special_interface in ("-nic"):
|
||||
for special_interface in "-nic":
|
||||
if result["name"].lower().endswith(special_interface):
|
||||
result["special"] = True
|
||||
return results
|
||||
Reference in new issue
Block a user