mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-24 17:28:08 +00:00
Migrate PCAP streaming code to work with FastAPI.
This commit is contained in:
parent
bd8565b2b9
commit
be6d4771d0
@ -19,9 +19,12 @@
|
|||||||
API endpoints for links.
|
API endpoints for links.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
|
import multidict
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Request, status
|
from fastapi import APIRouter, Depends, Request, status
|
||||||
from fastapi.encoders import jsonable_encoder
|
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
|
from fastapi.encoders import jsonable_encoder
|
||||||
from typing import List
|
from typing import List
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
@ -181,37 +184,31 @@ async def reset_link(link: Link = Depends(dep_link)):
|
|||||||
return link.__json__()
|
return link.__json__()
|
||||||
|
|
||||||
|
|
||||||
# @router.post("/projects/{project_id}/links/{link_id}/pcap",
|
@router.get("/{link_id}/pcap",
|
||||||
# summary="Stream a packet capture",
|
responses=responses)
|
||||||
# responses={404: {"model": ErrorMessage, "description": "Project or link not found"}})
|
async def pcap(request: Request, link: Link = Depends(dep_link)):
|
||||||
# async def pcap(project_id: UUID, link_id: UUID, request: Request):
|
"""
|
||||||
# """
|
Stream the PCAP capture file from compute.
|
||||||
# Stream the PCAP capture file from compute.
|
"""
|
||||||
# """
|
|
||||||
#
|
if not link.capturing:
|
||||||
# project = await Controller.instance().get_loaded_project(str(project_id))
|
raise ControllerError("This link has no active packet capture")
|
||||||
# link = project.get_link(str(link_id))
|
|
||||||
# if not link.capturing:
|
compute = link.compute
|
||||||
# raise ControllerError("This link has no active packet capture")
|
pcap_streaming_url = link.pcap_streaming_url()
|
||||||
#
|
headers = multidict.MultiDict(request.headers)
|
||||||
# compute = link.compute
|
headers['Host'] = compute.host
|
||||||
# pcap_streaming_url = link.pcap_streaming_url()
|
headers['Router-Host'] = request.client.host
|
||||||
# headers = multidict.MultiDict(request.headers)
|
body = await request.body()
|
||||||
# headers['Host'] = compute.host
|
|
||||||
# headers['Router-Host'] = request.client.host
|
async def compute_pcpa_stream():
|
||||||
# body = await request.body()
|
|
||||||
#
|
connector = aiohttp.TCPConnector(limit=None, force_close=True)
|
||||||
# connector = aiohttp.TCPConnector(limit=None, force_close=True)
|
async with aiohttp.ClientSession(connector=connector, headers=headers) as session:
|
||||||
# async with aiohttp.ClientSession(connector=connector, headers=headers) as session:
|
async with session.request(request.method, pcap_streaming_url, timeout=None, data=body) as compute_response:
|
||||||
# async with session.request(request.method, pcap_streaming_url, timeout=None, data=body) as response:
|
async for data in compute_response.content.iter_any():
|
||||||
# proxied_response = aiohttp.web.Response(headers=response.headers, status=response.status)
|
if not data:
|
||||||
# if response.headers.get('Transfer-Encoding', '').lower() == 'chunked':
|
break
|
||||||
# proxied_response.enable_chunked_encoding()
|
yield data
|
||||||
#
|
|
||||||
# await proxied_response.prepare(request)
|
return StreamingResponse(compute_pcpa_stream(), media_type="application/vnd.tcpdump.pcap")
|
||||||
# async for data in response.content.iter_any():
|
|
||||||
# if not data:
|
|
||||||
# break
|
|
||||||
# await proxied_response.write(data)
|
|
||||||
#
|
|
||||||
# #return StreamingResponse(file_like, media_type="video/mp4"))
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from fastapi import APIRouter, Request, HTTPException
|
from fastapi import APIRouter, Request, HTTPException, status
|
||||||
from fastapi.responses import RedirectResponse, HTMLResponse, FileResponse
|
from fastapi.responses import RedirectResponse, HTMLResponse, FileResponse
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ templates = Jinja2Templates(directory=os.path.join("gns3server", "templates"))
|
|||||||
@router.get("/")
|
@router.get("/")
|
||||||
async def root():
|
async def root():
|
||||||
|
|
||||||
return RedirectResponse("/static/web-ui/bundled", status_code=308)
|
return RedirectResponse("/static/web-ui/bundled", status_code=308) # permanent redirect
|
||||||
|
|
||||||
|
|
||||||
@router.get("/debug",
|
@router.get("/debug",
|
||||||
@ -54,7 +54,7 @@ async def web_ui(file_path: str):
|
|||||||
|
|
||||||
# Raise error if user try to escape
|
# Raise error if user try to escape
|
||||||
if file_path[0] == ".":
|
if file_path[0] == ".":
|
||||||
raise HTTPException(status_code=403)
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
static = get_resource(file_path)
|
static = get_resource(file_path)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user