2020-10-02 09:37:50 +03:00
|
|
|
#
|
|
|
|
# Copyright (C) 2020 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/>.
|
|
|
|
|
|
|
|
"""
|
2020-11-19 06:51:03 +02:00
|
|
|
API routes for controller notifications.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
2022-09-13 23:10:01 +03:00
|
|
|
from fastapi import APIRouter, Request, Depends, WebSocket, WebSocketDisconnect
|
2020-10-19 07:30:41 +03:00
|
|
|
from fastapi.responses import StreamingResponse
|
2020-11-11 08:48:41 +02:00
|
|
|
from websockets.exceptions import ConnectionClosed, WebSocketException
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2020-10-19 07:30:41 +03:00
|
|
|
from gns3server.controller import Controller
|
2021-11-01 08:15:14 +02:00
|
|
|
from gns3server import schemas
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-11-01 08:15:14 +02:00
|
|
|
from .dependencies.authentication import get_current_active_user, get_current_active_user_from_websocket
|
2021-04-26 09:48:18 +03:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
import logging
|
2021-04-13 12:16:50 +03:00
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2020-10-19 07:30:41 +03:00
|
|
|
router = APIRouter()
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2020-10-19 07:30:41 +03:00
|
|
|
|
2021-04-26 09:48:18 +03:00
|
|
|
@router.get("", dependencies=[Depends(get_current_active_user)])
|
2022-09-13 23:10:01 +03:00
|
|
|
async def controller_http_notifications(request: Request) -> StreamingResponse:
|
2020-10-19 07:30:41 +03:00
|
|
|
"""
|
|
|
|
Receive controller notifications about the controller from HTTP stream.
|
|
|
|
"""
|
|
|
|
|
2022-09-13 23:10:01 +03:00
|
|
|
from gns3server.api.server import app
|
|
|
|
log.info(f"New client {request.client.host}:{request.client.port} has connected to controller HTTP "
|
|
|
|
f"notification stream")
|
2020-10-19 07:30:41 +03:00
|
|
|
|
2022-09-13 23:10:01 +03:00
|
|
|
async def event_stream():
|
|
|
|
try:
|
|
|
|
with Controller.instance().notification.controller_queue() as queue:
|
|
|
|
while not app.state.exiting:
|
|
|
|
msg = await queue.get_json(5)
|
|
|
|
yield f"{msg}\n".encode("utf-8")
|
|
|
|
finally:
|
|
|
|
log.info(f"Client {request.client.host}:{request.client.port} has disconnected from controller HTTP "
|
|
|
|
f"notification stream")
|
2020-10-19 07:30:41 +03:00
|
|
|
return StreamingResponse(event_stream(), media_type="application/json")
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2020-11-11 08:48:41 +02:00
|
|
|
@router.websocket("/ws")
|
2021-11-01 08:15:14 +02:00
|
|
|
async def controller_ws_notifications(
|
|
|
|
websocket: WebSocket,
|
|
|
|
current_user: schemas.User = Depends(get_current_active_user_from_websocket)
|
|
|
|
) -> None:
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-11-11 08:48:41 +02:00
|
|
|
Receive project notifications about the controller from WebSocket.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2021-04-26 09:48:18 +03:00
|
|
|
|
2021-11-01 08:15:14 +02:00
|
|
|
if current_user is None:
|
|
|
|
return
|
2021-04-26 09:48:18 +03:00
|
|
|
|
2020-11-11 08:48:41 +02:00
|
|
|
log.info(f"New client {websocket.client.host}:{websocket.client.port} has connected to controller WebSocket")
|
|
|
|
try:
|
|
|
|
with Controller.instance().notification.controller_queue() as queue:
|
2020-10-02 09:37:50 +03:00
|
|
|
while True:
|
|
|
|
notification = await queue.get_json(5)
|
|
|
|
await websocket.send_text(notification)
|
2020-11-11 08:48:41 +02:00
|
|
|
except (ConnectionClosed, WebSocketDisconnect):
|
|
|
|
log.info(f"Client {websocket.client.host}:{websocket.client.port} has disconnected from controller WebSocket")
|
|
|
|
except WebSocketException as e:
|
2021-04-13 12:07:58 +03:00
|
|
|
log.warning(f"Error while sending to controller event to WebSocket client: {e}")
|
2020-11-11 08:48:41 +02:00
|
|
|
finally:
|
2021-12-07 15:30:54 +02:00
|
|
|
try:
|
|
|
|
await websocket.close()
|
|
|
|
except OSError:
|
|
|
|
pass # ignore OSError: [Errno 107] Transport endpoint is not connected
|