mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
feat: add VNC WebSocket console support for Docker and QEMU nodes
Add VNC console WebSocket endpoints for Docker and QEMU nodes: - Add /console/vnc WebSocket endpoint to compute API (Docker & QEMU) - Add /console/vnc WebSocket endpoint to controller API - Implement start_vnc_websocket_console() in BaseNode - Forward VNC WebSocket traffic between controller and compute layers The implementation provides bidirectional WebSocket to TCP forwarding for VNC protocol (RFB) connections, allowing browser-based VNC console access to containers and VMs. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
09aa12cf94
commit
ba92b405da
4 files changed
+176
-1
No files matched your search
@@ -384,6 +384,21 @@ async def console_ws(
|
||||
await node.start_websocket_console(websocket)
|
||||
|
||||
|
||||
@router.websocket(
|
||||
"/{node_id}/console/vnc"
|
||||
)
|
||||
async def vnc_console_ws(
|
||||
websocket: Union[None, WebSocket] = Depends(ws_compute_authentication),
|
||||
node: DockerVM = Depends(dep_node)
|
||||
) -> None:
|
||||
"""
|
||||
VNC Console WebSocket.
|
||||
"""
|
||||
|
||||
if websocket:
|
||||
await node.start_vnc_websocket_console(websocket)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/{node_id}/console/reset",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
|
||||
@@ -27,6 +27,7 @@ from typing import Union
|
||||
from uuid import UUID
|
||||
|
||||
from gns3server import schemas
|
||||
from gns3server.compute import qemu
|
||||
from gns3server.compute.qemu import Qemu
|
||||
from gns3server.compute.qemu.qemu_vm import QemuVM
|
||||
|
||||
@@ -414,6 +415,21 @@ async def console_ws(
|
||||
await node.start_websocket_console(websocket)
|
||||
|
||||
|
||||
@router.websocket(
|
||||
"/{node_id}/console/vnc"
|
||||
)
|
||||
async def vnc_console_ws(
|
||||
websocket: Union[None, WebSocket] = Depends(ws_compute_authentication),
|
||||
node: QemuVM = Depends(dep_node)
|
||||
) -> None:
|
||||
"""
|
||||
VNC Console WebSocket.
|
||||
"""
|
||||
|
||||
if websocket:
|
||||
await node.start_vnc_websocket_console(websocket)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/{node_id}/console/reset",
|
||||
status_code=status.HTTP_204_NO_CONTENT,
|
||||
|
||||
@@ -646,6 +646,80 @@ async def ws_console(
|
||||
except aiohttp.ClientError as e:
|
||||
log.error(f"Client error received when forwarding to compute console WebSocket: {e}")
|
||||
|
||||
@router.websocket("/{node_id}/console/vnc")
|
||||
async def vnc_console(
|
||||
websocket: WebSocket,
|
||||
current_user: schemas.User = Depends(has_privilege_on_websocket("Node.Console")),
|
||||
node: Node = Depends(dep_node)
|
||||
) -> None:
|
||||
"""
|
||||
VNC WebSocket console.
|
||||
|
||||
Required privilege: Node.Console
|
||||
"""
|
||||
|
||||
if current_user is None:
|
||||
return
|
||||
|
||||
compute = node.compute
|
||||
log.info(
|
||||
f"New client {websocket.client.host}:{websocket.client.port} has connected to controller VNC console WebSocket"
|
||||
)
|
||||
|
||||
compute_host = compute.host
|
||||
try:
|
||||
# handle IPv6 address
|
||||
ip = ipaddress.ip_address(compute_host)
|
||||
if isinstance(ip, ipaddress.IPv6Address):
|
||||
compute_host = '[' + compute_host + ']'
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
vnc_console_compute_url = (
|
||||
f"{websocket.url.scheme}://{compute_host}:{compute.port}/v3/compute/projects/"
|
||||
f"{node.project.id}/{node.node_type}/nodes/{node.id}/console/vnc"
|
||||
)
|
||||
|
||||
async def vnc_receive(vnc_console_compute):
|
||||
"""
|
||||
Receive binary WebSocket data from client and forward to compute VNC console WebSocket.
|
||||
"""
|
||||
|
||||
try:
|
||||
while True:
|
||||
data = await websocket.receive_bytes()
|
||||
if data:
|
||||
await vnc_console_compute.send_bytes(data)
|
||||
except WebSocketDisconnect:
|
||||
await vnc_console_compute.close()
|
||||
log.info(
|
||||
f"Client {websocket.client.host}:{websocket.client.port} has disconnected from controller"
|
||||
f" VNC console WebSocket"
|
||||
)
|
||||
|
||||
try:
|
||||
# receive binary data from compute VNC console WebSocket and forward to client
|
||||
log.info(f"Forwarding VNC console WebSocket to '{vnc_console_compute_url}'")
|
||||
server_config = Config.instance().settings.Server
|
||||
user = server_config.compute_username
|
||||
password = server_config.compute_password
|
||||
if not user:
|
||||
raise ControllerForbiddenError("Compute username is not set")
|
||||
user = user.strip()
|
||||
if user and password:
|
||||
auth = aiohttp.BasicAuth(user, password.get_secret_value(), "utf-8")
|
||||
else:
|
||||
auth = aiohttp.BasicAuth(user, "")
|
||||
ssl_context = Controller.instance().ssl_context()
|
||||
async with HTTPClient.get_client().ws_connect(vnc_console_compute_url, auth=auth, ssl_context=ssl_context) as ws:
|
||||
asyncio.ensure_future(vnc_receive(ws))
|
||||
async for msg in ws:
|
||||
if msg.type == aiohttp.WSMsgType.BINARY:
|
||||
await websocket.send_bytes(msg.data)
|
||||
elif msg.type == aiohttp.WSMsgType.ERROR:
|
||||
break
|
||||
except aiohttp.ClientError as e:
|
||||
log.error(f"Client error received when forwarding to compute VNC console WebSocket: {e}")
|
||||
|
||||
@router.post(
|
||||
"/console/reset",
|
||||
|
||||
Reference in new issue
Block a user