2016-03-11 15:51:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 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-04-22 14:22:03 +00:00
|
|
|
import aiohttp
|
2018-10-27 07:47:17 +00:00
|
|
|
import multidict
|
2016-04-22 14:22:03 +00:00
|
|
|
|
2016-05-14 00:00:07 +00:00
|
|
|
from gns3server.web.route import Route
|
|
|
|
from gns3server.controller import Controller
|
|
|
|
|
|
|
|
from gns3server.schemas.link import (
|
|
|
|
LINK_OBJECT_SCHEMA,
|
|
|
|
LINK_CAPTURE_SCHEMA
|
|
|
|
)
|
2016-03-11 15:51:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LinkHandler:
|
|
|
|
"""
|
|
|
|
API entry point for Link
|
|
|
|
"""
|
|
|
|
|
2016-05-17 14:10:47 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/links",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "List of links returned",
|
|
|
|
},
|
|
|
|
description="List links of a project")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def list_links(request, response):
|
2016-05-17 14:10:47 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-17 14:10:47 +00:00
|
|
|
response.json([v for v in project.links.values()])
|
|
|
|
|
2016-03-11 15:51:35 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/links",
|
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID"
|
2016-03-11 15:51:35 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "Link created",
|
|
|
|
400: "Invalid request"
|
|
|
|
},
|
|
|
|
description="Create a new link instance",
|
|
|
|
input=LINK_OBJECT_SCHEMA,
|
|
|
|
output=LINK_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def create(request, response):
|
2016-03-11 15:51:35 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
link = await project.add_link()
|
2017-07-19 15:30:25 +00:00
|
|
|
if "filters" in request.json:
|
2018-10-15 10:05:49 +00:00
|
|
|
await link.update_filters(request.json["filters"])
|
2017-07-19 15:30:25 +00:00
|
|
|
if "suspend" in request.json:
|
2018-10-15 10:05:49 +00:00
|
|
|
await link.update_suspend(request.json["suspend"])
|
2016-10-03 10:31:01 +00:00
|
|
|
try:
|
|
|
|
for node in request.json["nodes"]:
|
2018-10-15 10:05:49 +00:00
|
|
|
await link.add_node(project.get_node(node["node_id"]),
|
2016-10-03 10:31:01 +00:00
|
|
|
node.get("adapter_number", 0),
|
|
|
|
node.get("port_number", 0),
|
|
|
|
label=node.get("label"))
|
2018-04-28 09:01:43 +00:00
|
|
|
except aiohttp.web.HTTPException as e:
|
2018-10-15 10:05:49 +00:00
|
|
|
await project.delete_link(link.id)
|
2016-10-03 10:31:01 +00:00
|
|
|
raise e
|
2016-03-11 15:51:35 +00:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(link)
|
2016-03-14 16:40:27 +00:00
|
|
|
|
2017-06-30 08:22:30 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/links/{link_id}/available_filters",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"link_id": "Link UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "List of filters",
|
|
|
|
400: "Invalid request"
|
|
|
|
},
|
|
|
|
description="Return the list of filters available for this link")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def list_filters(request, response):
|
2017-06-30 08:22:30 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2017-06-30 08:22:30 +00:00
|
|
|
link = project.get_link(request.match_info["link_id"])
|
|
|
|
response.set_status(200)
|
|
|
|
response.json(link.available_filters())
|
|
|
|
|
2018-01-05 12:44:46 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/links/{link_id}",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"link_id": "Link UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
200: "Link found",
|
|
|
|
400: "Invalid request",
|
|
|
|
404: "Link doesn't exist"
|
|
|
|
},
|
|
|
|
description="Get a link instance",
|
|
|
|
output=LINK_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def get_link(request, response):
|
2018-01-05 12:44:46 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2018-01-05 12:44:46 +00:00
|
|
|
link = project.get_link(request.match_info["link_id"])
|
|
|
|
response.set_status(200)
|
|
|
|
response.json(link)
|
|
|
|
|
2016-07-01 15:38:32 +00:00
|
|
|
@Route.put(
|
|
|
|
r"/projects/{project_id}/links/{link_id}",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"link_id": "Link UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "Link updated",
|
|
|
|
400: "Invalid request"
|
|
|
|
},
|
|
|
|
description="Update a link instance",
|
|
|
|
input=LINK_OBJECT_SCHEMA,
|
|
|
|
output=LINK_OBJECT_SCHEMA)
|
2018-10-15 10:05:49 +00:00
|
|
|
async def update(request, response):
|
2016-07-01 15:38:32 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-07-01 15:38:32 +00:00
|
|
|
link = project.get_link(request.match_info["link_id"])
|
2017-07-19 15:30:25 +00:00
|
|
|
if "filters" in request.json:
|
2018-10-15 10:05:49 +00:00
|
|
|
await link.update_filters(request.json["filters"])
|
2017-07-19 15:30:25 +00:00
|
|
|
if "suspend" in request.json:
|
2018-10-15 10:05:49 +00:00
|
|
|
await link.update_suspend(request.json["suspend"])
|
2017-06-30 08:22:30 +00:00
|
|
|
if "nodes" in request.json:
|
2018-10-15 10:05:49 +00:00
|
|
|
await link.update_nodes(request.json["nodes"])
|
2016-07-01 15:38:32 +00:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(link)
|
|
|
|
|
2016-04-21 10:14:09 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/links/{link_id}/start_capture",
|
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"link_id": "Link UUID"
|
2016-04-21 10:14:09 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-04-26 16:13:15 +00:00
|
|
|
201: "Capture started",
|
2016-04-21 10:14:09 +00:00
|
|
|
400: "Invalid request"
|
|
|
|
},
|
2016-04-21 11:49:29 +00:00
|
|
|
input=LINK_CAPTURE_SCHEMA,
|
2016-04-26 16:13:15 +00:00
|
|
|
output=LINK_OBJECT_SCHEMA,
|
2016-05-14 00:00:07 +00:00
|
|
|
description="Start capture on a link instance. By default we consider it as an Ethernet link")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def start_capture(request, response):
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-11 17:35:36 +00:00
|
|
|
link = project.get_link(request.match_info["link_id"])
|
2018-10-27 07:47:17 +00:00
|
|
|
await link.start_capture(data_link_type=request.json.get("data_link_type", "DLT_EN10MB"),
|
|
|
|
capture_file_name=request.json.get("capture_file_name"))
|
2016-04-26 16:13:15 +00:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(link)
|
2016-04-21 10:14:09 +00:00
|
|
|
|
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/links/{link_id}/stop_capture",
|
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"link_id": "Link UUID"
|
2016-04-21 10:14:09 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-04-26 16:13:15 +00:00
|
|
|
201: "Capture stopped",
|
2016-04-21 10:14:09 +00:00
|
|
|
400: "Invalid request"
|
|
|
|
},
|
|
|
|
description="Stop capture on a link instance")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def stop_capture(request, response):
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-11 17:35:36 +00:00
|
|
|
link = project.get_link(request.match_info["link_id"])
|
2018-10-15 10:05:49 +00:00
|
|
|
await link.stop_capture()
|
2016-04-26 16:13:15 +00:00
|
|
|
response.set_status(201)
|
|
|
|
response.json(link)
|
2016-04-21 10:14:09 +00:00
|
|
|
|
2016-03-14 16:40:27 +00:00
|
|
|
@Route.delete(
|
|
|
|
r"/projects/{project_id}/links/{link_id}",
|
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"link_id": "Link UUID"
|
2016-03-14 16:40:27 +00:00
|
|
|
},
|
|
|
|
status_codes={
|
2016-03-14 19:54:05 +00:00
|
|
|
204: "Link deleted",
|
2016-03-14 16:40:27 +00:00
|
|
|
400: "Invalid request"
|
|
|
|
},
|
|
|
|
description="Delete a link instance")
|
2018-10-15 10:05:49 +00:00
|
|
|
async def delete(request, response):
|
2016-03-14 16:40:27 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
await project.delete_link(request.match_info["link_id"])
|
2016-03-14 19:54:05 +00:00
|
|
|
response.set_status(204)
|
2016-04-22 14:22:03 +00:00
|
|
|
|
2020-07-24 12:18:49 +00:00
|
|
|
@Route.post(
|
|
|
|
r"/projects/{project_id}/links/{link_id}/reset",
|
|
|
|
parameters={
|
|
|
|
"project_id": "Project UUID",
|
|
|
|
"link_id": "Link UUID"
|
|
|
|
},
|
|
|
|
status_codes={
|
|
|
|
201: "Link reset",
|
|
|
|
400: "Invalid request"
|
|
|
|
},
|
|
|
|
description="Reset link instance",
|
|
|
|
output=LINK_OBJECT_SCHEMA)
|
|
|
|
async def reset(request, response):
|
|
|
|
|
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
|
|
|
link = project.get_link(request.match_info["link_id"])
|
|
|
|
await link.reset()
|
|
|
|
response.set_status(201)
|
|
|
|
response.json(link)
|
|
|
|
|
2016-04-22 14:22:03 +00:00
|
|
|
@Route.get(
|
|
|
|
r"/projects/{project_id}/links/{link_id}/pcap",
|
|
|
|
parameters={
|
2016-05-14 00:00:07 +00:00
|
|
|
"project_id": "Project UUID",
|
|
|
|
"link_id": "Link UUID"
|
2016-04-22 14:22:03 +00:00
|
|
|
},
|
2018-10-27 07:47:17 +00:00
|
|
|
description="Stream the PCAP capture file from compute",
|
2016-04-22 14:22:03 +00:00
|
|
|
status_codes={
|
2016-05-14 00:00:07 +00:00
|
|
|
200: "File returned",
|
2016-04-22 14:22:03 +00:00
|
|
|
403: "Permission denied",
|
|
|
|
404: "The file doesn't exist"
|
|
|
|
})
|
2018-10-15 10:05:49 +00:00
|
|
|
async def pcap(request, response):
|
2016-04-22 14:22:03 +00:00
|
|
|
|
2018-10-15 10:05:49 +00:00
|
|
|
project = await Controller.instance().get_loaded_project(request.match_info["project_id"])
|
2016-05-11 17:35:36 +00:00
|
|
|
link = project.get_link(request.match_info["link_id"])
|
2018-10-27 07:47:17 +00:00
|
|
|
if not link.capturing:
|
|
|
|
raise aiohttp.web.HTTPConflict(text="This link has no active packet capture")
|
|
|
|
|
|
|
|
compute = link.compute
|
|
|
|
pcap_streaming_url = link.pcap_streaming_url()
|
|
|
|
headers = multidict.MultiDict(request.headers)
|
|
|
|
headers['Host'] = compute.host
|
|
|
|
headers['Router-Host'] = request.host
|
|
|
|
body = await request.read()
|
|
|
|
|
|
|
|
connector = aiohttp.TCPConnector(limit=None, force_close=True)
|
|
|
|
async with aiohttp.ClientSession(connector=connector, headers=headers) as session:
|
|
|
|
async with session.request(request.method, pcap_streaming_url, timeout=None, data=body) as response:
|
|
|
|
proxied_response = aiohttp.web.Response(headers=response.headers, status=response.status)
|
|
|
|
if response.headers.get('Transfer-Encoding', '').lower() == 'chunked':
|
|
|
|
proxied_response.enable_chunked_encoding()
|
|
|
|
|
|
|
|
await proxied_response.prepare(request)
|
|
|
|
async for data in response.content.iter_any():
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
await proxied_response.write(data)
|