2015-01-14 01:26:32 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-11-13 09:28:14 +00:00
|
|
|
import sys
|
2015-02-16 05:13:24 +00:00
|
|
|
import os
|
2016-05-31 19:08:41 +00:00
|
|
|
import stat
|
2015-02-16 05:13:24 +00:00
|
|
|
import shutil
|
|
|
|
import asyncio
|
2015-03-17 15:31:45 +00:00
|
|
|
import tempfile
|
2015-10-12 21:57:37 +00:00
|
|
|
import psutil
|
|
|
|
import platform
|
2018-03-12 06:38:50 +00:00
|
|
|
import re
|
2015-02-16 05:13:24 +00:00
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
from fastapi import WebSocketDisconnect
|
2016-11-13 09:28:14 +00:00
|
|
|
from gns3server.utils.interfaces import interfaces
|
2020-10-02 06:37:50 +00:00
|
|
|
from gns3server.compute.compute_error import ComputeError
|
2016-06-23 22:56:06 +00:00
|
|
|
from ..compute.port_manager import PortManager
|
2018-08-25 07:10:47 +00:00
|
|
|
from ..utils.asyncio import wait_run_in_executor, locking
|
2016-11-08 18:44:12 +00:00
|
|
|
from ..utils.asyncio.telnet_server import AsyncioTelnetServer
|
2020-11-19 04:51:03 +00:00
|
|
|
from gns3server.compute.ubridge.hypervisor import Hypervisor
|
|
|
|
from gns3server.compute.ubridge.ubridge_error import UbridgeError
|
2016-06-23 22:56:06 +00:00
|
|
|
from .nios.nio_udp import NIOUDP
|
2016-06-07 13:34:04 +00:00
|
|
|
from .error import NodeError
|
2015-02-16 05:13:24 +00:00
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
import logging
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
log = logging.getLogger(__name__)
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-15 23:50:36 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
class BaseNode:
|
2015-01-18 22:41:53 +00:00
|
|
|
|
2015-02-19 10:33:25 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Base node implementation.
|
2015-02-19 10:33:25 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
:param name: name of this node
|
|
|
|
:param node_id: Node instance identifier
|
2015-02-19 10:33:25 +00:00
|
|
|
:param project: Project instance
|
2016-05-11 17:35:36 +00:00
|
|
|
:param manager: parent node manager
|
2020-07-29 06:53:51 +00:00
|
|
|
:param console: console TCP port
|
|
|
|
:param console_type: console type
|
|
|
|
:param aux: auxiliary console TCP port
|
|
|
|
:param aux_type: auxiliary console type
|
2016-10-24 19:39:35 +00:00
|
|
|
:param linked_clone: The node base image is duplicate/overlay (Each node data are independent)
|
2016-11-08 18:44:12 +00:00
|
|
|
:param wrap_console: The console is wrapped using AsyncioTelnetServer
|
2020-07-29 06:53:51 +00:00
|
|
|
:param wrap_aux: The auxiliary console is wrapped using AsyncioTelnetServer
|
2015-02-19 10:33:25 +00:00
|
|
|
"""
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
name,
|
|
|
|
node_id,
|
|
|
|
project,
|
|
|
|
manager,
|
|
|
|
console=None,
|
|
|
|
console_type="telnet",
|
|
|
|
aux=None,
|
|
|
|
aux_type="none",
|
|
|
|
linked_clone=True,
|
|
|
|
wrap_console=False,
|
|
|
|
wrap_aux=False,
|
|
|
|
):
|
2015-01-16 00:43:06 +00:00
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
self._name = name
|
2015-12-01 09:54:51 +00:00
|
|
|
self._usage = ""
|
2016-05-11 17:35:36 +00:00
|
|
|
self._id = node_id
|
2016-10-24 19:39:35 +00:00
|
|
|
self._linked_clone = linked_clone
|
2015-01-20 11:46:15 +00:00
|
|
|
self._project = project
|
2015-01-19 10:22:24 +00:00
|
|
|
self._manager = manager
|
2015-02-19 10:33:25 +00:00
|
|
|
self._console = console
|
2016-02-29 09:38:30 +00:00
|
|
|
self._aux = aux
|
2015-07-03 22:06:25 +00:00
|
|
|
self._console_type = console_type
|
2020-07-29 06:53:51 +00:00
|
|
|
self._aux_type = aux_type
|
2015-03-17 15:31:45 +00:00
|
|
|
self._temporary_directory = None
|
2015-07-22 04:58:28 +00:00
|
|
|
self._hw_virtualization = False
|
2015-09-13 20:52:25 +00:00
|
|
|
self._ubridge_hypervisor = None
|
2016-02-29 09:38:30 +00:00
|
|
|
self._closed = False
|
2016-05-11 17:35:36 +00:00
|
|
|
self._node_status = "stopped"
|
2016-02-02 17:25:17 +00:00
|
|
|
self._command_line = ""
|
2016-11-08 18:44:12 +00:00
|
|
|
self._wrap_console = wrap_console
|
2020-07-29 06:53:51 +00:00
|
|
|
self._wrap_aux = wrap_aux
|
|
|
|
self._wrapper_telnet_servers = []
|
2022-12-31 01:43:17 +00:00
|
|
|
self._wrap_console_reader = None
|
|
|
|
self._wrap_console_writer = None
|
2018-03-23 08:44:16 +00:00
|
|
|
self._internal_console_port = None
|
2020-07-29 06:53:51 +00:00
|
|
|
self._internal_aux_port = None
|
2018-04-02 15:27:12 +00:00
|
|
|
self._custom_adapters = []
|
2018-11-19 17:22:16 +00:00
|
|
|
self._ubridge_require_privileged_access = False
|
2015-02-19 10:33:25 +00:00
|
|
|
|
|
|
|
if self._console is not None:
|
2020-07-29 06:53:51 +00:00
|
|
|
# use a previously allocated console port
|
2015-12-07 11:26:46 +00:00
|
|
|
if console_type == "vnc":
|
2020-12-03 07:34:42 +00:00
|
|
|
vnc_console_start_port_range, vnc_console_end_port_range = self._get_vnc_console_port_range()
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(
|
|
|
|
self._console,
|
|
|
|
self._project,
|
|
|
|
port_range_start=vnc_console_start_port_range,
|
|
|
|
port_range_end=vnc_console_end_port_range,
|
|
|
|
)
|
2018-03-24 11:11:21 +00:00
|
|
|
elif console_type == "none":
|
|
|
|
self._console = None
|
2015-12-07 11:26:46 +00:00
|
|
|
else:
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(self._console, self._project)
|
2016-02-29 09:38:30 +00:00
|
|
|
|
|
|
|
if self._aux is not None:
|
2020-07-29 06:53:51 +00:00
|
|
|
# use a previously allocated auxiliary console port
|
|
|
|
if aux_type == "vnc":
|
|
|
|
# VNC is a special case and the range must be 5900-6000
|
2021-04-13 09:16:50 +00:00
|
|
|
self._aux = self._manager.port_manager.reserve_tcp_port(
|
|
|
|
self._aux, self._project, port_range_start=5900, port_range_end=6000
|
|
|
|
)
|
2020-07-29 06:53:51 +00:00
|
|
|
elif aux_type == "none":
|
|
|
|
self._aux = None
|
|
|
|
else:
|
|
|
|
self._aux = self._manager.port_manager.reserve_tcp_port(self._aux, self._project)
|
2016-02-29 09:38:30 +00:00
|
|
|
|
2018-03-23 14:29:39 +00:00
|
|
|
if self._console is None:
|
2020-07-29 06:53:51 +00:00
|
|
|
# allocate a new console
|
2018-03-23 14:29:39 +00:00
|
|
|
if console_type == "vnc":
|
2020-12-03 07:34:42 +00:00
|
|
|
vnc_console_start_port_range, vnc_console_end_port_range = self._get_vnc_console_port_range()
|
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port(
|
|
|
|
self._project,
|
|
|
|
port_range_start=vnc_console_start_port_range,
|
2021-04-13 09:16:50 +00:00
|
|
|
port_range_end=vnc_console_end_port_range,
|
|
|
|
)
|
2018-03-23 14:29:39 +00:00
|
|
|
elif console_type != "none":
|
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port(self._project)
|
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
if self._aux is None:
|
|
|
|
# allocate a new auxiliary console
|
|
|
|
if aux_type == "vnc":
|
|
|
|
# VNC is a special case and the range must be 5900-6000
|
2021-04-13 09:16:50 +00:00
|
|
|
self._aux = self._manager.port_manager.get_free_tcp_port(
|
|
|
|
self._project, port_range_start=5900, port_range_end=6000
|
|
|
|
)
|
2020-07-29 06:53:51 +00:00
|
|
|
elif aux_type != "none":
|
|
|
|
self._aux = self._manager.port_manager.get_free_tcp_port(self._project)
|
|
|
|
|
2016-11-08 18:44:12 +00:00
|
|
|
if self._wrap_console:
|
2016-12-01 10:47:05 +00:00
|
|
|
self._internal_console_port = self._manager.port_manager.get_free_tcp_port(self._project)
|
2016-11-08 18:44:12 +00:00
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
if self._wrap_aux:
|
|
|
|
self._internal_aux_port = self._manager.port_manager.get_free_tcp_port(self._project)
|
2016-02-29 09:38:30 +00:00
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
log.debug(
|
|
|
|
"{module}: {name} [{id}] initialized. Console port {console}".format(
|
|
|
|
module=self.manager.module_name, name=self.name, id=self.id, console=self._console
|
|
|
|
)
|
|
|
|
)
|
2015-01-21 22:21:15 +00:00
|
|
|
|
2015-01-22 10:49:22 +00:00
|
|
|
def __del__(self):
|
2015-01-23 02:06:17 +00:00
|
|
|
|
2016-11-03 13:21:28 +00:00
|
|
|
if hasattr(self, "_temporary_directory") and self._temporary_directory is not None:
|
2015-03-17 15:31:45 +00:00
|
|
|
if os.path.exists(self._temporary_directory):
|
2015-04-08 17:17:34 +00:00
|
|
|
shutil.rmtree(self._temporary_directory, ignore_errors=True)
|
2015-01-19 10:22:24 +00:00
|
|
|
|
2016-10-24 19:39:35 +00:00
|
|
|
@property
|
|
|
|
def linked_clone(self):
|
|
|
|
return self._linked_clone
|
|
|
|
|
|
|
|
@linked_clone.setter
|
|
|
|
def linked_clone(self, val):
|
|
|
|
self._linked_clone = val
|
|
|
|
|
2018-04-02 15:27:12 +00:00
|
|
|
@property
|
|
|
|
def custom_adapters(self):
|
|
|
|
return self._custom_adapters
|
|
|
|
|
|
|
|
@custom_adapters.setter
|
|
|
|
def custom_adapters(self, val):
|
|
|
|
self._custom_adapters = val
|
|
|
|
|
2015-03-04 15:01:56 +00:00
|
|
|
@property
|
|
|
|
def status(self):
|
2016-06-24 21:04:58 +00:00
|
|
|
"""
|
|
|
|
Returns current node status
|
|
|
|
"""
|
2015-03-04 15:01:56 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
return self._node_status
|
2015-03-04 15:01:56 +00:00
|
|
|
|
|
|
|
@status.setter
|
|
|
|
def status(self, status):
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
self._node_status = status
|
2016-05-18 09:00:35 +00:00
|
|
|
self.updated()
|
|
|
|
|
|
|
|
def updated(self):
|
|
|
|
"""
|
2016-06-24 21:04:58 +00:00
|
|
|
Sends an updated event
|
2016-05-18 09:00:35 +00:00
|
|
|
"""
|
2016-05-16 19:12:32 +00:00
|
|
|
self.project.emit("node.updated", self)
|
2015-03-04 15:01:56 +00:00
|
|
|
|
2016-02-02 17:25:17 +00:00
|
|
|
@property
|
|
|
|
def command_line(self):
|
2016-06-24 21:04:58 +00:00
|
|
|
"""
|
|
|
|
Returns command used to start the node
|
|
|
|
"""
|
2016-02-02 17:25:17 +00:00
|
|
|
|
|
|
|
return self._command_line
|
|
|
|
|
|
|
|
@command_line.setter
|
|
|
|
def command_line(self, command_line):
|
|
|
|
|
|
|
|
self._command_line = command_line
|
|
|
|
|
2015-01-20 11:46:15 +00:00
|
|
|
@property
|
|
|
|
def project(self):
|
2015-01-21 02:02:22 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the node current project.
|
2015-01-21 02:02:22 +00:00
|
|
|
|
|
|
|
:returns: Project instance.
|
|
|
|
"""
|
|
|
|
|
2015-01-20 11:46:15 +00:00
|
|
|
return self._project
|
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
@property
|
2015-01-20 01:30:57 +00:00
|
|
|
def name(self):
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the name for this node.
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
:returns: name
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@name.setter
|
|
|
|
def name(self, new_name):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Sets the name of this node.
|
2015-01-20 01:30:57 +00:00
|
|
|
|
|
|
|
:param new_name: name
|
|
|
|
"""
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
"{module}: {name} [{id}] renamed to {new_name}".format(
|
|
|
|
module=self.manager.module_name, name=self.name, id=self.id, new_name=new_name
|
|
|
|
)
|
|
|
|
)
|
2015-01-20 01:30:57 +00:00
|
|
|
self._name = new_name
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-12-01 09:54:51 +00:00
|
|
|
@property
|
|
|
|
def usage(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the usage for this node.
|
2015-12-01 09:54:51 +00:00
|
|
|
|
|
|
|
:returns: usage
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._usage
|
|
|
|
|
|
|
|
@usage.setter
|
|
|
|
def usage(self, new_usage):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Sets the usage of this node.
|
2015-12-01 09:54:51 +00:00
|
|
|
|
|
|
|
:param new_usage: usage
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._usage = new_usage
|
|
|
|
|
2015-01-14 01:26:32 +00:00
|
|
|
@property
|
2015-02-04 20:48:29 +00:00
|
|
|
def id(self):
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the ID for this node.
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
:returns: Node identifier (string)
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
|
|
|
|
2015-02-04 20:48:29 +00:00
|
|
|
return self._id
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
@property
|
|
|
|
def manager(self):
|
2015-01-18 22:41:53 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the manager for this node.
|
2015-01-20 01:30:57 +00:00
|
|
|
|
|
|
|
:returns: instance of manager
|
2015-01-18 22:41:53 +00:00
|
|
|
"""
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
return self._manager
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2015-01-20 13:31:47 +00:00
|
|
|
@property
|
|
|
|
def working_dir(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Return the node working directory
|
2015-01-20 13:31:47 +00:00
|
|
|
"""
|
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
return self._project.node_working_directory(self)
|
2015-01-20 13:31:47 +00:00
|
|
|
|
2017-10-02 08:41:57 +00:00
|
|
|
@property
|
|
|
|
def working_path(self):
|
|
|
|
"""
|
|
|
|
Return the node working path. Doesn't create structure of directories when not present.
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._project.node_working_path(self)
|
|
|
|
|
2015-03-17 15:31:45 +00:00
|
|
|
@property
|
|
|
|
def temporary_directory(self):
|
|
|
|
if self._temporary_directory is None:
|
2015-03-17 21:18:55 +00:00
|
|
|
try:
|
|
|
|
self._temporary_directory = tempfile.mkdtemp()
|
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise NodeError(f"Can't create temporary directory: {e}")
|
2015-03-17 15:31:45 +00:00
|
|
|
return self._temporary_directory
|
|
|
|
|
2015-01-20 01:30:57 +00:00
|
|
|
def create(self):
|
2015-01-18 22:41:53 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Creates the node.
|
2015-01-18 22:41:53 +00:00
|
|
|
"""
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info("{module}: {name} [{id}] created".format(module=self.manager.module_name, name=self.name, id=self.id))
|
2015-01-14 01:26:32 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def delete(self):
|
2015-02-16 05:13:24 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Delete the node (including all its files).
|
2015-02-16 05:13:24 +00:00
|
|
|
"""
|
2018-03-15 07:17:39 +00:00
|
|
|
|
2016-05-31 19:08:41 +00:00
|
|
|
def set_rw(operation, name, exc):
|
|
|
|
os.chmod(name, stat.S_IWRITE)
|
2015-02-16 05:13:24 +00:00
|
|
|
|
2016-05-11 17:35:36 +00:00
|
|
|
directory = self.project.node_working_directory(self)
|
2015-02-16 05:13:24 +00:00
|
|
|
if os.path.exists(directory):
|
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await wait_run_in_executor(shutil.rmtree, directory, onerror=set_rw)
|
2015-02-16 05:13:24 +00:00
|
|
|
except OSError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise ComputeError(f"Could not delete the node working directory: {e}")
|
2015-02-16 05:13:24 +00:00
|
|
|
|
2015-01-15 23:50:36 +00:00
|
|
|
def start(self):
|
2015-01-14 17:52:02 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Starts the node process.
|
2015-01-14 17:52:02 +00:00
|
|
|
"""
|
2015-01-18 22:41:53 +00:00
|
|
|
|
2015-01-14 17:52:02 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def stop(self):
|
2015-01-19 12:47:20 +00:00
|
|
|
"""
|
2016-11-08 18:44:12 +00:00
|
|
|
Stop the node process.
|
2015-01-14 01:26:32 +00:00
|
|
|
"""
|
2018-03-15 07:17:39 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
await self.stop_wrap_console()
|
2016-11-08 18:44:12 +00:00
|
|
|
self.status = "stopped"
|
2015-01-22 10:49:22 +00:00
|
|
|
|
2016-05-14 01:26:50 +00:00
|
|
|
def suspend(self):
|
|
|
|
"""
|
|
|
|
Suspends the node process.
|
|
|
|
"""
|
|
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def close(self):
|
2015-01-22 10:49:22 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Close the node process.
|
2015-01-22 10:49:22 +00:00
|
|
|
"""
|
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
if self._closed:
|
|
|
|
return False
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
"{module}: '{name}' [{id}]: is closing".format(module=self.manager.module_name, name=self.name, id=self.id)
|
|
|
|
)
|
2016-02-29 09:38:30 +00:00
|
|
|
|
|
|
|
if self._console:
|
|
|
|
self._manager.port_manager.release_tcp_port(self._console, self._project)
|
|
|
|
self._console = None
|
2016-11-08 18:44:12 +00:00
|
|
|
if self._wrap_console:
|
|
|
|
self._manager.port_manager.release_tcp_port(self._internal_console_port, self._project)
|
|
|
|
self._internal_console_port = None
|
2016-02-29 09:38:30 +00:00
|
|
|
if self._aux:
|
|
|
|
self._manager.port_manager.release_tcp_port(self._aux, self._project)
|
|
|
|
self._aux = None
|
2020-07-29 06:53:51 +00:00
|
|
|
if self._wrap_aux:
|
|
|
|
self._manager.port_manager.release_tcp_port(self._internal_aux_port, self._project)
|
|
|
|
self._internal_aux_port = None
|
2016-02-29 09:38:30 +00:00
|
|
|
|
|
|
|
self._closed = True
|
|
|
|
return True
|
|
|
|
|
2020-12-03 07:34:42 +00:00
|
|
|
def _get_vnc_console_port_range(self):
|
|
|
|
"""
|
|
|
|
Returns the VNC console port range.
|
|
|
|
"""
|
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
vnc_console_start_port_range = self._manager.config.settings.Server.vnc_console_start_port_range
|
|
|
|
vnc_console_end_port_range = self._manager.config.settings.Server.vnc_console_end_port_range
|
2020-12-03 07:34:42 +00:00
|
|
|
|
|
|
|
if not 5900 <= vnc_console_start_port_range <= 65535:
|
|
|
|
raise NodeError("The VNC console start port range must be between 5900 and 65535")
|
|
|
|
if not 5900 <= vnc_console_end_port_range <= 65535:
|
|
|
|
raise NodeError("The VNC console start port range must be between 5900 and 65535")
|
|
|
|
if vnc_console_start_port_range >= vnc_console_end_port_range:
|
2021-04-13 09:16:50 +00:00
|
|
|
raise NodeError(
|
|
|
|
f"The VNC console start port range value ({vnc_console_start_port_range}) "
|
|
|
|
f"cannot be above or equal to the end value ({vnc_console_end_port_range})"
|
|
|
|
)
|
2020-12-03 07:34:42 +00:00
|
|
|
|
|
|
|
return vnc_console_start_port_range, vnc_console_end_port_range
|
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
async def _wrap_telnet_proxy(self, internal_port, external_port):
|
2016-11-08 18:44:12 +00:00
|
|
|
"""
|
2020-01-31 09:31:27 +00:00
|
|
|
Start a telnet proxy for the console allowing multiple telnet clients
|
|
|
|
to be connected at the same time
|
2016-11-08 18:44:12 +00:00
|
|
|
"""
|
2018-03-15 07:17:39 +00:00
|
|
|
|
2016-11-16 12:06:43 +00:00
|
|
|
remaining_trial = 60
|
|
|
|
while True:
|
|
|
|
try:
|
2022-12-31 01:43:17 +00:00
|
|
|
(self._wrap_console_reader, self._wrap_console_writer) = await asyncio.open_connection(
|
|
|
|
host="127.0.0.1",
|
|
|
|
port=self._internal_console_port
|
|
|
|
)
|
2016-11-16 12:06:43 +00:00
|
|
|
break
|
|
|
|
except (OSError, ConnectionRefusedError) as e:
|
|
|
|
if remaining_trial <= 0:
|
|
|
|
raise e
|
2018-10-15 10:05:49 +00:00
|
|
|
await asyncio.sleep(0.1)
|
2016-11-16 12:06:43 +00:00
|
|
|
remaining_trial -= 1
|
2022-12-31 01:43:17 +00:00
|
|
|
await AsyncioTelnetServer.write_client_intro(self._wrap_console_writer, echo=True)
|
|
|
|
server = AsyncioTelnetServer(
|
|
|
|
reader=self._wrap_console_reader,
|
|
|
|
writer=self._wrap_console_writer,
|
|
|
|
binary=True,
|
|
|
|
echo=True
|
|
|
|
)
|
2018-04-16 07:30:06 +00:00
|
|
|
# warning: this will raise OSError exception if there is a problem...
|
2020-07-29 06:53:51 +00:00
|
|
|
telnet_server = await asyncio.start_server(server.run, self._manager.port_manager.console_host, external_port)
|
|
|
|
self._wrapper_telnet_servers.append(telnet_server)
|
|
|
|
|
|
|
|
async def start_wrap_console(self):
|
|
|
|
"""
|
|
|
|
Start a Telnet proxy servers for the console and auxiliary console allowing multiple telnet clients
|
|
|
|
to be connected at the same time
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._wrap_console and self._console_type == "telnet":
|
|
|
|
await self._wrap_telnet_proxy(self._internal_console_port, self.console)
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
f"New Telnet proxy server for console started "
|
|
|
|
f"(internal port = {self._internal_console_port}, external port = {self.console})"
|
|
|
|
)
|
2020-07-29 06:53:51 +00:00
|
|
|
|
|
|
|
if self._wrap_aux and self._aux_type == "telnet":
|
|
|
|
await self._wrap_telnet_proxy(self._internal_aux_port, self.aux)
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
f"New Telnet proxy server for auxiliary console started "
|
|
|
|
f"(internal port = {self._internal_aux_port}, external port = {self.aux})"
|
|
|
|
)
|
2016-11-08 18:44:12 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def stop_wrap_console(self):
|
2018-03-24 11:11:21 +00:00
|
|
|
"""
|
2020-07-29 06:53:51 +00:00
|
|
|
Stops the telnet proxy servers.
|
2018-03-24 11:11:21 +00:00
|
|
|
"""
|
|
|
|
|
2023-01-05 04:38:00 +00:00
|
|
|
if self._wrap_console_writer:
|
2022-12-31 01:43:17 +00:00
|
|
|
self._wrap_console_writer.close()
|
|
|
|
await self._wrap_console_writer.wait_closed()
|
2020-07-29 06:53:51 +00:00
|
|
|
for telnet_proxy_server in self._wrapper_telnet_servers:
|
|
|
|
telnet_proxy_server.close()
|
|
|
|
await telnet_proxy_server.wait_closed()
|
|
|
|
self._wrapper_telnet_servers = []
|
2018-03-24 11:11:21 +00:00
|
|
|
|
2022-12-31 01:43:17 +00:00
|
|
|
async def reset_wrap_console(self):
|
2020-07-26 08:57:18 +00:00
|
|
|
"""
|
2022-12-31 01:43:17 +00:00
|
|
|
Reset the wrap console (restarts the Telnet proxy)
|
2020-07-26 08:57:18 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
await self.stop_wrap_console()
|
|
|
|
await self.start_wrap_console()
|
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
async def start_websocket_console(self, websocket):
|
2020-01-31 09:31:27 +00:00
|
|
|
"""
|
|
|
|
Connect to console using Websocket.
|
|
|
|
|
|
|
|
:param ws: Websocket object
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.status != "started":
|
2021-04-13 09:07:58 +00:00
|
|
|
raise NodeError(f"Node {self.name} is not started")
|
2020-01-31 09:31:27 +00:00
|
|
|
|
|
|
|
if self._console_type != "telnet":
|
2021-04-13 09:07:58 +00:00
|
|
|
raise NodeError(f"Node {self.name} console type is not telnet")
|
2020-01-31 09:31:27 +00:00
|
|
|
|
|
|
|
try:
|
2021-04-13 09:16:50 +00:00
|
|
|
(telnet_reader, telnet_writer) = await asyncio.open_connection(
|
|
|
|
self._manager.port_manager.console_host, self.console
|
|
|
|
)
|
2020-01-31 09:31:27 +00:00
|
|
|
except ConnectionError as e:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise NodeError(f"Cannot connect to node {self.name} telnet server: {e}")
|
2020-01-31 09:31:27 +00:00
|
|
|
|
|
|
|
log.info("Connected to Telnet server")
|
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
await websocket.accept()
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
f"New client {websocket.client.host}:{websocket.client.port} has connected to compute"
|
|
|
|
f" console WebSocket"
|
|
|
|
)
|
2020-01-31 09:31:27 +00:00
|
|
|
|
|
|
|
async def ws_forward(telnet_writer):
|
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
data = await websocket.receive_text()
|
|
|
|
if data:
|
|
|
|
telnet_writer.write(data.encode())
|
|
|
|
await telnet_writer.drain()
|
|
|
|
except WebSocketDisconnect:
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
f"Client {websocket.client.host}:{websocket.client.port} has disconnected from compute"
|
|
|
|
f" console WebSocket"
|
|
|
|
)
|
2020-01-31 09:31:27 +00:00
|
|
|
|
|
|
|
async def telnet_forward(telnet_reader):
|
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
while not telnet_reader.at_eof():
|
2020-01-31 09:31:27 +00:00
|
|
|
data = await telnet_reader.read(1024)
|
|
|
|
if data:
|
2020-10-19 04:30:41 +00:00
|
|
|
await websocket.send_bytes(data)
|
2020-01-31 09:31:27 +00:00
|
|
|
|
2020-10-19 04:30:41 +00:00
|
|
|
# keep forwarding WebSocket data in both direction
|
2021-04-13 09:16:50 +00:00
|
|
|
done, pending = await asyncio.wait(
|
|
|
|
[ws_forward(telnet_writer), telnet_forward(telnet_reader)], return_when=asyncio.FIRST_COMPLETED
|
|
|
|
)
|
2020-10-19 04:30:41 +00:00
|
|
|
for task in done:
|
|
|
|
if task.exception():
|
|
|
|
log.warning(f"Exception while forwarding WebSocket data to Telnet server {task.exception()}")
|
|
|
|
|
|
|
|
for task in pending:
|
|
|
|
task.cancel()
|
2020-01-31 09:31:27 +00:00
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
@property
|
|
|
|
def aux(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the aux console port of this node.
|
2016-02-29 09:38:30 +00:00
|
|
|
|
|
|
|
:returns: aux console port
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._aux
|
|
|
|
|
|
|
|
@aux.setter
|
|
|
|
def aux(self, aux):
|
|
|
|
"""
|
|
|
|
Changes the aux port
|
|
|
|
|
|
|
|
:params aux: Console port (integer) or None to free the port
|
|
|
|
"""
|
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
if aux == self._aux or self._aux_type == "none":
|
2016-02-29 09:38:30 +00:00
|
|
|
return
|
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
if self._aux_type == "vnc" and aux is not None and aux < 5900:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise NodeError(f"VNC auxiliary console require a port superior or equal to 5900, current port is {aux}")
|
2020-07-29 06:53:51 +00:00
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
if self._aux:
|
|
|
|
self._manager.port_manager.release_tcp_port(self._aux, self._project)
|
|
|
|
self._aux = None
|
|
|
|
if aux is not None:
|
2020-07-29 06:53:51 +00:00
|
|
|
if self.aux_type == "vnc":
|
2021-04-13 09:16:50 +00:00
|
|
|
self._aux = self._manager.port_manager.reserve_tcp_port(
|
|
|
|
aux, self._project, port_range_start=5900, port_range_end=6000
|
|
|
|
)
|
2020-07-29 06:53:51 +00:00
|
|
|
else:
|
|
|
|
self._aux = self._manager.port_manager.reserve_tcp_port(aux, self._project)
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
"{module}: '{name}' [{id}]: auxiliary console port set to {port}".format(
|
|
|
|
module=self.manager.module_name, name=self.name, id=self.id, port=aux
|
|
|
|
)
|
|
|
|
)
|
2015-02-19 10:33:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def console(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the console port of this node.
|
2015-02-19 10:33:25 +00:00
|
|
|
|
|
|
|
:returns: console port
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._console
|
|
|
|
|
|
|
|
@console.setter
|
|
|
|
def console(self, console):
|
|
|
|
"""
|
2015-04-08 17:17:34 +00:00
|
|
|
Changes the console port
|
2015-02-19 10:33:25 +00:00
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
:params console: Console port (integer) or None to free the port
|
2015-02-19 10:33:25 +00:00
|
|
|
"""
|
|
|
|
|
2018-03-24 11:11:21 +00:00
|
|
|
if console == self._console or self._console_type == "none":
|
2015-02-19 10:33:25 +00:00
|
|
|
return
|
2015-10-16 16:15:27 +00:00
|
|
|
|
2016-02-29 09:38:30 +00:00
|
|
|
if self._console_type == "vnc" and console is not None and console < 5900:
|
2021-04-13 09:07:58 +00:00
|
|
|
raise NodeError(f"VNC console require a port superior or equal to 5900, current port is {console}")
|
2015-10-16 16:15:27 +00:00
|
|
|
|
2015-02-19 10:33:25 +00:00
|
|
|
if self._console:
|
2015-03-21 23:19:12 +00:00
|
|
|
self._manager.port_manager.release_tcp_port(self._console, self._project)
|
2016-02-29 09:38:30 +00:00
|
|
|
self._console = None
|
|
|
|
if console is not None:
|
2016-04-06 12:57:52 +00:00
|
|
|
if self.console_type == "vnc":
|
2020-12-03 07:34:42 +00:00
|
|
|
vnc_console_start_port_range, vnc_console_end_port_range = self._get_vnc_console_port_range()
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(
|
|
|
|
console,
|
|
|
|
self._project,
|
|
|
|
port_range_start=vnc_console_start_port_range,
|
2021-04-13 09:16:50 +00:00
|
|
|
port_range_end=vnc_console_end_port_range,
|
2020-12-03 07:34:42 +00:00
|
|
|
)
|
2016-04-06 12:57:52 +00:00
|
|
|
else:
|
|
|
|
self._console = self._manager.port_manager.reserve_tcp_port(console, self._project)
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
"{module}: '{name}' [{id}]: console port set to {port}".format(
|
|
|
|
module=self.manager.module_name, name=self.name, id=self.id, port=console
|
|
|
|
)
|
|
|
|
)
|
2015-07-03 22:06:25 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def console_type(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns the console type for this node.
|
2015-07-03 22:06:25 +00:00
|
|
|
|
|
|
|
:returns: console type (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._console_type
|
|
|
|
|
|
|
|
@console_type.setter
|
|
|
|
def console_type(self, console_type):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Sets the console type for this node.
|
2015-07-03 22:06:25 +00:00
|
|
|
|
|
|
|
:param console_type: console type (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if console_type != self._console_type:
|
|
|
|
# get a new port if the console type change
|
2018-03-23 14:29:39 +00:00
|
|
|
if self._console:
|
|
|
|
self._manager.port_manager.release_tcp_port(self._console, self._project)
|
2018-03-23 08:44:16 +00:00
|
|
|
if console_type == "none":
|
|
|
|
# no need to allocate a port when the console type is none
|
|
|
|
self._console = None
|
|
|
|
elif console_type == "vnc":
|
2022-11-06 11:36:31 +00:00
|
|
|
vnc_console_start_port_range, vnc_console_end_port_range = self._get_vnc_console_port_range()
|
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port(
|
|
|
|
self._project,
|
|
|
|
vnc_console_start_port_range,
|
|
|
|
vnc_console_end_port_range
|
|
|
|
)
|
2015-07-03 22:06:25 +00:00
|
|
|
else:
|
|
|
|
self._console = self._manager.port_manager.get_free_tcp_port(self._project)
|
|
|
|
|
|
|
|
self._console_type = console_type
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
"{module}: '{name}' [{id}]: console type set to {console_type} (console port is {console})".format(
|
|
|
|
module=self.manager.module_name,
|
|
|
|
name=self.name,
|
|
|
|
id=self.id,
|
|
|
|
console_type=console_type,
|
|
|
|
console=self.console,
|
|
|
|
)
|
|
|
|
)
|
2015-07-22 04:58:28 +00:00
|
|
|
|
2020-07-29 06:53:51 +00:00
|
|
|
@property
|
|
|
|
def aux_type(self):
|
|
|
|
"""
|
|
|
|
Returns the auxiliary console type for this node.
|
|
|
|
|
|
|
|
:returns: aux type (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._aux_type
|
|
|
|
|
|
|
|
@aux_type.setter
|
|
|
|
def aux_type(self, aux_type):
|
|
|
|
"""
|
|
|
|
Sets the auxiliary console type for this node.
|
|
|
|
|
|
|
|
:param aux_type: console type (string)
|
|
|
|
"""
|
|
|
|
|
|
|
|
if aux_type != self._aux_type:
|
|
|
|
# get a new port if the aux type change
|
|
|
|
if self._aux:
|
|
|
|
self._manager.port_manager.release_tcp_port(self._aux, self._project)
|
|
|
|
if aux_type == "none":
|
|
|
|
# no need to allocate a port when the auxiliary console type is none
|
|
|
|
self._aux = None
|
|
|
|
elif aux_type == "vnc":
|
|
|
|
# VNC is a special case and the range must be 5900-6000
|
|
|
|
self._aux = self._manager.port_manager.get_free_tcp_port(self._project, 5900, 6000)
|
|
|
|
else:
|
|
|
|
self._aux = self._manager.port_manager.get_free_tcp_port(self._project)
|
|
|
|
|
|
|
|
self._aux_type = aux_type
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
"{module}: '{name}' [{id}]: console type set to {aux_type} (auxiliary console port is {aux})".format(
|
|
|
|
module=self.manager.module_name, name=self.name, id=self.id, aux_type=aux_type, aux=self.aux
|
|
|
|
)
|
|
|
|
)
|
2020-07-29 06:53:51 +00:00
|
|
|
|
2016-06-23 01:40:46 +00:00
|
|
|
@property
|
|
|
|
def ubridge(self):
|
|
|
|
"""
|
|
|
|
Returns the uBridge hypervisor.
|
|
|
|
|
2017-07-10 18:38:28 +00:00
|
|
|
:returns: instance of uBridge
|
2016-06-23 01:40:46 +00:00
|
|
|
"""
|
|
|
|
|
2016-12-14 11:01:34 +00:00
|
|
|
if self._ubridge_hypervisor and not self._ubridge_hypervisor.is_running():
|
|
|
|
self._ubridge_hypervisor = None
|
2016-06-23 01:40:46 +00:00
|
|
|
return self._ubridge_hypervisor
|
|
|
|
|
|
|
|
@ubridge.setter
|
|
|
|
def ubridge(self, ubride_hypervisor):
|
|
|
|
"""
|
|
|
|
Set an uBridge hypervisor.
|
|
|
|
|
|
|
|
:param ubride_hypervisor: uBridge hypervisor
|
|
|
|
"""
|
|
|
|
|
|
|
|
self._ubridge_hypervisor = ubride_hypervisor
|
|
|
|
|
2015-09-13 20:52:25 +00:00
|
|
|
@property
|
|
|
|
def ubridge_path(self):
|
|
|
|
"""
|
|
|
|
Returns the uBridge executable path.
|
|
|
|
|
|
|
|
:returns: path to uBridge
|
|
|
|
"""
|
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
path = shutil.which(self._manager.config.settings.Server.ubridge_path)
|
2015-09-13 20:52:25 +00:00
|
|
|
return path
|
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _ubridge_send(self, command):
|
2016-05-29 22:35:07 +00:00
|
|
|
"""
|
|
|
|
Sends a command to uBridge hypervisor.
|
|
|
|
|
|
|
|
:param command: command to send
|
|
|
|
"""
|
|
|
|
|
2017-07-10 18:38:28 +00:00
|
|
|
if not self._ubridge_hypervisor or not self._ubridge_hypervisor.is_running():
|
2018-11-19 17:22:16 +00:00
|
|
|
await self._start_ubridge(self._ubridge_require_privileged_access)
|
2016-05-29 22:35:07 +00:00
|
|
|
if not self._ubridge_hypervisor or not self._ubridge_hypervisor.is_running():
|
2021-04-13 09:07:58 +00:00
|
|
|
raise NodeError(f"Cannot send command '{command}': uBridge is not running")
|
2016-05-31 04:07:37 +00:00
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._ubridge_hypervisor.send(command)
|
2016-05-31 04:07:37 +00:00
|
|
|
except UbridgeError as e:
|
2021-04-13 09:16:50 +00:00
|
|
|
raise UbridgeError(
|
|
|
|
f"Error while sending command '{command}': {e}: {self._ubridge_hypervisor.read_stdout()}"
|
|
|
|
)
|
2016-05-29 22:35:07 +00:00
|
|
|
|
2018-08-25 07:10:47 +00:00
|
|
|
@locking
|
2018-11-19 17:22:16 +00:00
|
|
|
async def _start_ubridge(self, require_privileged_access=False):
|
2015-09-13 20:52:25 +00:00
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Starts uBridge (handles connections to and from this node).
|
2015-09-13 20:52:25 +00:00
|
|
|
"""
|
|
|
|
|
2017-03-28 14:27:09 +00:00
|
|
|
# Prevent us to start multiple ubridge
|
|
|
|
if self._ubridge_hypervisor and self._ubridge_hypervisor.is_running():
|
|
|
|
return
|
|
|
|
|
2016-07-12 15:38:13 +00:00
|
|
|
if self.ubridge_path is None:
|
2021-04-13 09:16:50 +00:00
|
|
|
raise NodeError(
|
|
|
|
"uBridge is not available, path doesn't exist, or you just installed GNS3 and need to restart your user session to refresh user permissions."
|
|
|
|
)
|
2016-07-12 15:38:13 +00:00
|
|
|
|
2018-11-19 17:22:16 +00:00
|
|
|
if require_privileged_access and not self._manager.has_privileged_access(self.ubridge_path):
|
|
|
|
raise NodeError("uBridge requires root access or the capability to interact with network adapters")
|
2015-09-14 21:05:25 +00:00
|
|
|
|
2021-04-12 07:32:23 +00:00
|
|
|
server_host = self._manager.config.settings.Server.host
|
2017-03-03 17:40:26 +00:00
|
|
|
if not self.ubridge:
|
2016-06-23 01:40:46 +00:00
|
|
|
self._ubridge_hypervisor = Hypervisor(self._project, self.ubridge_path, self.working_dir, server_host)
|
2021-04-13 09:07:58 +00:00
|
|
|
log.info(f"Starting new uBridge hypervisor {self._ubridge_hypervisor.host}:{self._ubridge_hypervisor.port}")
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._ubridge_hypervisor.start()
|
2017-05-11 15:26:18 +00:00
|
|
|
if self._ubridge_hypervisor:
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
f"Hypervisor {self._ubridge_hypervisor.host}:{self._ubridge_hypervisor.port} has successfully started"
|
|
|
|
)
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._ubridge_hypervisor.connect()
|
2018-11-19 17:22:16 +00:00
|
|
|
# save if privileged are required in case uBridge needs to be restarted in self._ubridge_send()
|
|
|
|
self._ubridge_require_privileged_access = require_privileged_access
|
2015-09-13 20:52:25 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _stop_ubridge(self):
|
2016-05-29 22:35:07 +00:00
|
|
|
"""
|
|
|
|
Stops uBridge.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self._ubridge_hypervisor and self._ubridge_hypervisor.is_running():
|
2021-04-13 09:07:58 +00:00
|
|
|
log.info(f"Stopping uBridge hypervisor {self._ubridge_hypervisor.host}:{self._ubridge_hypervisor.port}")
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._ubridge_hypervisor.stop()
|
2016-12-14 11:01:34 +00:00
|
|
|
self._ubridge_hypervisor = None
|
2016-05-29 22:35:07 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def add_ubridge_udp_connection(self, bridge_name, source_nio, destination_nio):
|
2016-06-23 22:56:06 +00:00
|
|
|
"""
|
2016-11-13 09:28:14 +00:00
|
|
|
Creates an UDP connection in uBridge.
|
2016-06-23 22:56:06 +00:00
|
|
|
|
|
|
|
:param bridge_name: bridge name in uBridge
|
|
|
|
:param source_nio: source NIO instance
|
|
|
|
:param destination_nio: destination NIO instance
|
|
|
|
"""
|
|
|
|
|
2021-04-13 09:07:58 +00:00
|
|
|
await self._ubridge_send(f"bridge create {bridge_name}")
|
2016-06-23 22:56:06 +00:00
|
|
|
|
|
|
|
if not isinstance(destination_nio, NIOUDP):
|
|
|
|
raise NodeError("Destination NIO is not UDP")
|
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
await self._ubridge_send(
|
|
|
|
"bridge add_nio_udp {name} {lport} {rhost} {rport}".format(
|
|
|
|
name=bridge_name, lport=source_nio.lport, rhost=source_nio.rhost, rport=source_nio.rport
|
|
|
|
)
|
2021-04-13 09:07:58 +00:00
|
|
|
)
|
2016-06-23 22:56:06 +00:00
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
await self._ubridge_send(
|
|
|
|
"bridge add_nio_udp {name} {lport} {rhost} {rport}".format(
|
|
|
|
name=bridge_name, lport=destination_nio.lport, rhost=destination_nio.rhost, rport=destination_nio.rport
|
|
|
|
)
|
2021-04-13 09:07:58 +00:00
|
|
|
)
|
2016-06-23 22:56:06 +00:00
|
|
|
|
|
|
|
if destination_nio.capturing:
|
2021-04-13 09:16:50 +00:00
|
|
|
await self._ubridge_send(
|
|
|
|
'bridge start_capture {name} "{pcap_file}"'.format(
|
|
|
|
name=bridge_name, pcap_file=destination_nio.pcap_output_file
|
|
|
|
)
|
2021-04-13 09:07:58 +00:00
|
|
|
)
|
2016-06-23 22:56:06 +00:00
|
|
|
|
2021-04-13 09:07:58 +00:00
|
|
|
await self._ubridge_send(f"bridge start {bridge_name}")
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._ubridge_apply_filters(bridge_name, destination_nio.filters)
|
2017-06-30 08:22:30 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def update_ubridge_udp_connection(self, bridge_name, source_nio, destination_nio):
|
2017-07-11 11:42:47 +00:00
|
|
|
if destination_nio:
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._ubridge_apply_filters(bridge_name, destination_nio.filters)
|
2017-06-30 08:22:30 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def ubridge_delete_bridge(self, name):
|
2017-07-12 09:42:37 +00:00
|
|
|
"""
|
|
|
|
:params name: Delete the bridge with this name
|
|
|
|
"""
|
2018-03-15 07:17:39 +00:00
|
|
|
|
2017-07-12 09:42:37 +00:00
|
|
|
if self.ubridge:
|
2021-04-13 09:07:58 +00:00
|
|
|
await self._ubridge_send(f"bridge delete {name}")
|
2017-07-12 09:42:37 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _ubridge_apply_filters(self, bridge_name, filters):
|
2017-06-30 08:22:30 +00:00
|
|
|
"""
|
2018-03-12 06:38:50 +00:00
|
|
|
Apply packet filters
|
2017-06-30 08:22:30 +00:00
|
|
|
|
|
|
|
:param bridge_name: bridge name in uBridge
|
2018-03-12 06:38:50 +00:00
|
|
|
:param filters: Array of filter dictionary
|
2017-06-30 08:22:30 +00:00
|
|
|
"""
|
2018-03-15 07:17:39 +00:00
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
await self._ubridge_send("bridge reset_packet_filters " + bridge_name)
|
2018-03-12 06:38:50 +00:00
|
|
|
for packet_filter in self._build_filter_list(filters):
|
2021-04-13 09:07:58 +00:00
|
|
|
cmd = f"bridge add_packet_filter {bridge_name} {packet_filter}"
|
2018-03-12 06:38:50 +00:00
|
|
|
try:
|
2018-10-15 10:05:49 +00:00
|
|
|
await self._ubridge_send(cmd)
|
2018-03-12 06:38:50 +00:00
|
|
|
except UbridgeError as e:
|
2019-01-17 11:01:58 +00:00
|
|
|
match = re.search(r"Cannot compile filter '(.*)': syntax error", str(e))
|
2018-03-12 06:38:50 +00:00
|
|
|
if match:
|
2021-04-13 09:07:58 +00:00
|
|
|
message = f"Warning: ignoring BPF packet filter '{self.name}' due to syntax error: {match.group(1)}"
|
2018-03-12 06:38:50 +00:00
|
|
|
log.warning(message)
|
|
|
|
self.project.emit("log.warning", {"message": message})
|
|
|
|
else:
|
|
|
|
raise
|
2017-07-17 09:21:54 +00:00
|
|
|
|
|
|
|
def _build_filter_list(self, filters):
|
|
|
|
"""
|
|
|
|
:returns: Iterator building a list of filter
|
|
|
|
"""
|
2018-03-15 07:17:39 +00:00
|
|
|
|
2017-06-30 08:22:30 +00:00
|
|
|
i = 0
|
2017-07-05 14:36:39 +00:00
|
|
|
for (filter_type, values) in filters.items():
|
2017-07-11 15:30:29 +00:00
|
|
|
if isinstance(values[0], str):
|
2021-04-13 09:16:50 +00:00
|
|
|
for line in values[0].split("\n"):
|
2017-07-11 15:30:29 +00:00
|
|
|
line = line.strip()
|
2017-07-17 09:21:54 +00:00
|
|
|
yield "{filter_name} {filter_type} {filter_value}".format(
|
2017-07-11 15:30:29 +00:00
|
|
|
filter_name="filter" + str(i),
|
|
|
|
filter_type=filter_type,
|
2021-04-13 09:16:50 +00:00
|
|
|
filter_value='"{}" {}'.format(line, " ".join([str(v) for v in values[1:]])),
|
|
|
|
).strip()
|
2017-07-11 15:30:29 +00:00
|
|
|
i += 1
|
|
|
|
else:
|
2017-07-17 09:21:54 +00:00
|
|
|
yield "{filter_name} {filter_type} {filter_value}".format(
|
2017-07-11 15:30:29 +00:00
|
|
|
filter_name="filter" + str(i),
|
|
|
|
filter_type=filter_type,
|
2021-04-13 09:16:50 +00:00
|
|
|
filter_value=" ".join([str(v) for v in values]),
|
|
|
|
)
|
2017-07-11 15:30:29 +00:00
|
|
|
i += 1
|
2016-06-23 22:56:06 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
async def _add_ubridge_ethernet_connection(self, bridge_name, ethernet_interface, block_host_traffic=False):
|
2016-11-13 09:28:14 +00:00
|
|
|
"""
|
|
|
|
Creates a connection with an Ethernet interface in uBridge.
|
|
|
|
|
|
|
|
:param bridge_name: bridge name in uBridge
|
|
|
|
:param ethernet_interface: Ethernet interface name
|
|
|
|
:param block_host_traffic: block network traffic originating from the host OS (Windows only)
|
|
|
|
"""
|
|
|
|
|
2017-07-19 09:56:24 +00:00
|
|
|
if sys.platform.startswith("linux") and block_host_traffic is False:
|
|
|
|
# on Linux we use RAW sockets by default excepting if host traffic must be blocked
|
2021-04-13 09:16:50 +00:00
|
|
|
await self._ubridge_send(
|
|
|
|
'bridge add_nio_linux_raw {name} "{interface}"'.format(name=bridge_name, interface=ethernet_interface)
|
2021-04-13 09:07:58 +00:00
|
|
|
)
|
2016-11-13 09:28:14 +00:00
|
|
|
else:
|
|
|
|
# on other platforms we just rely on the pcap library
|
2021-04-13 09:16:50 +00:00
|
|
|
await self._ubridge_send(
|
|
|
|
'bridge add_nio_ethernet {name} "{interface}"'.format(name=bridge_name, interface=ethernet_interface)
|
2021-04-13 09:07:58 +00:00
|
|
|
)
|
2017-07-19 09:56:24 +00:00
|
|
|
source_mac = None
|
|
|
|
for interface in interfaces():
|
|
|
|
if interface["name"] == ethernet_interface:
|
|
|
|
source_mac = interface["mac_address"]
|
|
|
|
if source_mac:
|
2021-04-13 09:16:50 +00:00
|
|
|
await self._ubridge_send(
|
|
|
|
'bridge set_pcap_filter {name} "not ether src {mac}"'.format(name=bridge_name, mac=source_mac)
|
2021-04-13 09:07:58 +00:00
|
|
|
)
|
|
|
|
log.info(f"PCAP filter applied on '{ethernet_interface}' for source MAC {source_mac}")
|
2016-11-13 09:28:14 +00:00
|
|
|
|
2016-06-23 22:56:06 +00:00
|
|
|
def _create_local_udp_tunnel(self):
|
|
|
|
"""
|
|
|
|
Creates a local UDP tunnel (pair of 2 NIOs, one for each direction)
|
|
|
|
|
|
|
|
:returns: source NIO and destination NIO.
|
|
|
|
"""
|
|
|
|
|
|
|
|
m = PortManager.instance()
|
|
|
|
lport = m.get_free_udp_port(self.project)
|
|
|
|
rport = m.get_free_udp_port(self.project)
|
2021-04-13 09:16:50 +00:00
|
|
|
source_nio_settings = {"lport": lport, "rhost": "127.0.0.1", "rport": rport, "type": "nio_udp"}
|
|
|
|
destination_nio_settings = {"lport": rport, "rhost": "127.0.0.1", "rport": lport, "type": "nio_udp"}
|
2016-06-25 00:35:39 +00:00
|
|
|
source_nio = self.manager.create_nio(source_nio_settings)
|
|
|
|
destination_nio = self.manager.create_nio(destination_nio_settings)
|
2021-04-13 09:16:50 +00:00
|
|
|
log.info(
|
|
|
|
"{module}: '{name}' [{id}]:local UDP tunnel created between port {port1} and {port2}".format(
|
|
|
|
module=self.manager.module_name, name=self.name, id=self.id, port1=lport, port2=rport
|
|
|
|
)
|
2021-04-13 09:07:58 +00:00
|
|
|
)
|
2016-06-23 22:56:06 +00:00
|
|
|
return source_nio, destination_nio
|
|
|
|
|
2015-07-22 04:58:28 +00:00
|
|
|
@property
|
|
|
|
def hw_virtualization(self):
|
|
|
|
"""
|
2016-05-11 17:35:36 +00:00
|
|
|
Returns either the node is using hardware virtualization or not.
|
2015-07-22 04:58:28 +00:00
|
|
|
|
|
|
|
:return: boolean
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self._hw_virtualization
|
2015-10-12 21:57:37 +00:00
|
|
|
|
|
|
|
def check_available_ram(self, requested_ram):
|
|
|
|
"""
|
|
|
|
Sends a warning notification if there is not enough RAM on the system to allocate requested RAM.
|
|
|
|
|
|
|
|
:param requested_ram: requested amount of RAM in MB
|
|
|
|
"""
|
|
|
|
|
|
|
|
available_ram = int(psutil.virtual_memory().available / (1024 * 1024))
|
2021-09-20 08:38:02 +00:00
|
|
|
percentage_left = 100 - psutil.virtual_memory().percent
|
2015-10-12 21:57:37 +00:00
|
|
|
if requested_ram > available_ram:
|
2021-04-13 09:07:58 +00:00
|
|
|
message = '"{}" requires {}MB of RAM to run but there is only {}MB - {}% of RAM left on "{}"'.format(
|
2021-04-13 09:16:50 +00:00
|
|
|
self.name, requested_ram, available_ram, percentage_left, platform.node()
|
2021-04-13 09:07:58 +00:00
|
|
|
)
|
2015-10-12 21:57:37 +00:00
|
|
|
self.project.emit("log.warning", {"message": message})
|
2018-04-02 15:27:12 +00:00
|
|
|
|
|
|
|
def _get_custom_adapter_settings(self, adapter_number):
|
|
|
|
|
|
|
|
for custom_adapter in self.custom_adapters:
|
|
|
|
if custom_adapter["adapter_number"] == adapter_number:
|
|
|
|
return custom_adapter
|
|
|
|
return {}
|