mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-06 07:11:13 +02:00
Stream pcap from compute to controller to client
This commit is contained in:
15 files changed
+172
-124
No files matched your search
@@ -27,7 +27,6 @@ from .virtualbox_handler import VirtualBoxHandler
|
||||
from .vpcs_handler import VPCSHandler
|
||||
from .vmware_handler import VMwareHandler
|
||||
from .config_handler import ConfigHandler
|
||||
from .file_handler import FileHandler
|
||||
from .version_handler import VersionHandler
|
||||
from .notification_handler import NotificationHandler
|
||||
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
#
|
||||
# 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/>.
|
||||
|
||||
import asyncio
|
||||
import aiohttp
|
||||
|
||||
from ....web.route import Route
|
||||
from ....schemas.file import FILE_STREAM_SCHEMA
|
||||
|
||||
|
||||
class FileHandler:
|
||||
|
||||
@classmethod
|
||||
@Route.get(
|
||||
r"/files/stream",
|
||||
description="Stream a file from the server",
|
||||
status_codes={
|
||||
200: "File retrieved",
|
||||
404: "File doesn't exist",
|
||||
409: "Can't access to file"
|
||||
},
|
||||
input=FILE_STREAM_SCHEMA
|
||||
)
|
||||
def read(request, response):
|
||||
response.enable_chunked_encoding()
|
||||
|
||||
try:
|
||||
with open(request.json.get("location"), "rb") as f:
|
||||
loop = asyncio.get_event_loop()
|
||||
response.content_type = "application/octet-stream"
|
||||
response.set_status(200)
|
||||
# Very important: do not send a content lenght otherwise QT close the connection but curl can consume the Feed
|
||||
response.content_length = None
|
||||
|
||||
response.start(request)
|
||||
|
||||
while True:
|
||||
data = yield from loop.run_in_executor(None, f.read, 16)
|
||||
if len(data) == 0:
|
||||
yield from asyncio.sleep(0.1)
|
||||
else:
|
||||
response.write(data)
|
||||
except FileNotFoundError:
|
||||
raise aiohttp.web.HTTPNotFound()
|
||||
except OSError as e:
|
||||
raise aiohttp.web.HTTPConflict(text=str(e))
|
||||
@@ -305,6 +305,50 @@ class ProjectHandler:
|
||||
except PermissionError:
|
||||
raise aiohttp.web.HTTPForbidden()
|
||||
|
||||
@classmethod
|
||||
@Route.get(
|
||||
r"/projects/{project_id}/stream/{path:.+}",
|
||||
description="Stream a file from a project",
|
||||
parameters={
|
||||
"project_id": "The UUID of the project",
|
||||
},
|
||||
status_codes={
|
||||
200: "Return the file",
|
||||
403: "Permission denied",
|
||||
404: "The file doesn't exist"
|
||||
})
|
||||
def stream_file(request, response):
|
||||
|
||||
pm = ProjectManager.instance()
|
||||
project = pm.get_project(request.match_info["project_id"])
|
||||
path = request.match_info["path"]
|
||||
path = os.path.normpath(path)
|
||||
|
||||
# Raise error if user try to escape
|
||||
if path[0] == ".":
|
||||
raise aiohttp.web.HTTPForbidden
|
||||
path = os.path.join(project.path, path)
|
||||
|
||||
response.content_type = "application/octet-stream"
|
||||
response.set_status(200)
|
||||
response.enable_chunked_encoding()
|
||||
# Very important: do not send a content length otherwise QT close the connection but curl can consume the Feed
|
||||
response.content_length = None
|
||||
|
||||
try:
|
||||
with open(path, "rb") as f:
|
||||
response.start(request)
|
||||
while True:
|
||||
data = f.read(4096)
|
||||
if not data:
|
||||
yield from asyncio.sleep(0.1)
|
||||
yield from response.write(data)
|
||||
|
||||
except FileNotFoundError:
|
||||
raise aiohttp.web.HTTPNotFound()
|
||||
except PermissionError:
|
||||
raise aiohttp.web.HTTPForbidden()
|
||||
|
||||
@classmethod
|
||||
@Route.post(
|
||||
r"/projects/{project_id}/files/{path:.+}",
|
||||
|
||||
Reference in new issue
Block a user