mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 14:01:15 +02:00
Method for start / stop capture on a link
Ref https://github.com/GNS3/gns3-gui/issues/1117
This commit is contained in:
9 files changed
+230
-18
No files matched your search
@@ -15,16 +15,18 @@
|
||||
# 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 re
|
||||
import uuid
|
||||
import asyncio
|
||||
|
||||
|
||||
class Link:
|
||||
|
||||
def __init__(self, project):
|
||||
def __init__(self, project, data_link_type="DLT_EN10MB"):
|
||||
self._id = str(uuid.uuid4())
|
||||
self._vms = []
|
||||
self._project = project
|
||||
self._data_link_type = data_link_type
|
||||
|
||||
@asyncio.coroutine
|
||||
def addVM(self, vm, adapter_number, port_number):
|
||||
@@ -51,6 +53,35 @@ class Link:
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@asyncio.coroutine
|
||||
def start_capture(self):
|
||||
"""
|
||||
Start capture on the link
|
||||
|
||||
:returns: Capture object
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@asyncio.coroutine
|
||||
def stop_capture(self):
|
||||
"""
|
||||
Stop capture on the link
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def capture_file_name(self):
|
||||
"""
|
||||
:returns: File name for a capture on this link
|
||||
"""
|
||||
capture_file_name = "{}_{}-{}_to_{}_{}-{}".format(
|
||||
self._vms[0]["vm"].name,
|
||||
self._vms[0]["adapter_number"],
|
||||
self._vms[0]["port_number"],
|
||||
self._vms[1]["vm"].name,
|
||||
self._vms[1]["adapter_number"],
|
||||
self._vms[1]["port_number"])
|
||||
return re.sub("[^0-9A-Za-z_-]", "", capture_file_name) + ".pcap"
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self._id
|
||||
@@ -63,4 +94,4 @@ class Link:
|
||||
"adapter_number": side["adapter_number"],
|
||||
"port_number": side["port_number"]
|
||||
})
|
||||
return {"vms": res, "link_id": self._id}
|
||||
return {"vms": res, "link_id": self._id, "data_link_type": self._data_link_type}
|
||||
@@ -16,6 +16,7 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import asyncio
|
||||
import aiohttp
|
||||
|
||||
|
||||
from .link import Link
|
||||
@@ -23,8 +24,9 @@ from .link import Link
|
||||
|
||||
class UDPLink(Link):
|
||||
|
||||
def __init__(self, project):
|
||||
super().__init__(project)
|
||||
def __init__(self, project, data_link_type="DLT_EN10MB"):
|
||||
super().__init__(project, data_link_type)
|
||||
self._capture_vm = None
|
||||
|
||||
@asyncio.coroutine
|
||||
def create(self):
|
||||
@@ -76,3 +78,45 @@ class UDPLink(Link):
|
||||
|
||||
yield from vm1.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number1, port_number=port_number1))
|
||||
yield from vm2.delete("/adapters/{adapter_number}/ports/{port_number}/nio".format(adapter_number=adapter_number2, port_number=port_number2))
|
||||
|
||||
@asyncio.coroutine
|
||||
def start_capture(self):
|
||||
"""
|
||||
Start capture on a link
|
||||
"""
|
||||
self._capture_vm = self._choose_capture_side()
|
||||
data = {
|
||||
"capture_file_name": self.capture_file_name(),
|
||||
"data_link_type": self._data_link_type
|
||||
}
|
||||
yield from self._capture_vm["vm"].post("/adapters/{adapter_number}/ports/{port_number}/start_capture".format(adapter_number=self._capture_vm["adapter_number"], port_number=self._capture_vm["port_number"]), data=data)
|
||||
|
||||
@asyncio.coroutine
|
||||
def stop_capture(self):
|
||||
"""
|
||||
Stop capture on a link
|
||||
"""
|
||||
if self._capture_vm:
|
||||
yield from self._capture_vm["vm"].post("/adapters/{adapter_number}/ports/{port_number}/stop_capture".format(adapter_number=self._capture_vm["adapter_number"], port_number=self._capture_vm["port_number"]))
|
||||
self._capture_vm = None
|
||||
|
||||
def _choose_capture_side(self):
|
||||
"""
|
||||
Run capture on the best candidate.
|
||||
|
||||
The ideal candidate is a node who support capture on controller
|
||||
server
|
||||
|
||||
:returns: VM where the capture should run
|
||||
"""
|
||||
|
||||
# For saving bandwith we use the local node first
|
||||
for vm in self._vms:
|
||||
if vm["vm"].compute.id == "local" and vm["vm"].vm_type not in ["qemu", "vpcs"]:
|
||||
return vm
|
||||
|
||||
for vm in self._vms:
|
||||
if vm["vm"].vm_type not in ["qemu", "vpcs"]:
|
||||
return vm
|
||||
|
||||
raise aiohttp.web.HTTPConflict(text="Capture is not supported for this link")
|
||||
@@ -216,6 +216,9 @@ class VM:
|
||||
else:
|
||||
return (yield from self._compute.delete("/projects/{}/{}/vms/{}{}".format(self._project.id, self._vm_type, self._id, path)))
|
||||
|
||||
def __repr__(self):
|
||||
return "<gns3server.controller.VM {} {}>".format(self._vm_type, self._name)
|
||||
|
||||
def __json__(self):
|
||||
return {
|
||||
"compute_id": self._compute.id,
|
||||
|
||||
@@ -52,6 +52,46 @@ class LinkHandler:
|
||||
response.set_status(201)
|
||||
response.json(link)
|
||||
|
||||
@classmethod
|
||||
@Route.post(
|
||||
r"/projects/{project_id}/links/{link_id}/start_capture",
|
||||
parameters={
|
||||
"project_id": "UUID for the project",
|
||||
"link_id": "UUID of the link"
|
||||
},
|
||||
status_codes={
|
||||
204: "Capture started",
|
||||
400: "Invalid request"
|
||||
},
|
||||
description="Start capture on a link instance")
|
||||
def start_capture(request, response):
|
||||
|
||||
controller = Controller.instance()
|
||||
project = controller.getProject(request.match_info["project_id"])
|
||||
link = project.getLink(request.match_info["link_id"])
|
||||
yield from link.start_capture()
|
||||
response.set_status(204)
|
||||
|
||||
@classmethod
|
||||
@Route.post(
|
||||
r"/projects/{project_id}/links/{link_id}/stop_capture",
|
||||
parameters={
|
||||
"project_id": "UUID for the project",
|
||||
"link_id": "UUID of the link"
|
||||
},
|
||||
status_codes={
|
||||
204: "Capture stopped",
|
||||
400: "Invalid request"
|
||||
},
|
||||
description="Stop capture on a link instance")
|
||||
def stop_capture(request, response):
|
||||
|
||||
controller = Controller.instance()
|
||||
project = controller.getProject(request.match_info["project_id"])
|
||||
link = project.getLink(request.match_info["link_id"])
|
||||
yield from link.stop_capture()
|
||||
response.set_status(204)
|
||||
|
||||
@classmethod
|
||||
@Route.delete(
|
||||
r"/projects/{project_id}/links/{link_id}",
|
||||
|
||||
@@ -28,6 +28,10 @@ LINK_OBJECT_SCHEMA = {
|
||||
"maxLength": 36,
|
||||
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
|
||||
},
|
||||
"data_link_type": {
|
||||
"description": "PCAP data link type (http://www.tcpdump.org/linktypes.html)",
|
||||
"enum": ["DLT_ATM_RFC1483", "DLT_EN10MB", "DLT_FRELAY", "DLT_C_HDLC"]
|
||||
},
|
||||
"vms": {
|
||||
"description": "List of the VMS",
|
||||
"type": "array",
|
||||
|
||||
@@ -54,9 +54,8 @@ VM_CAPTURE_SCHEMA = {
|
||||
"minLength": 1,
|
||||
},
|
||||
"data_link_type": {
|
||||
"description": "PCAP data link type",
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"description": "PCAP data link type (http://www.tcpdump.org/linktypes.html)",
|
||||
"enum": ["DLT_ATM_RFC1483", "DLT_EN10MB", "DLT_FRELAY", "DLT_C_HDLC"]
|
||||
}
|
||||
},
|
||||
"additionalProperties": False,
|
||||
|
||||
Reference in new issue
Block a user