mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Captures written in the captures directory on the controller
This commit is contained in:
9 files changed
+155
-40
No files matched your search
@@ -15,19 +15,24 @@
|
||||
# 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 os
|
||||
import re
|
||||
import uuid
|
||||
import asyncio
|
||||
|
||||
|
||||
import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Link:
|
||||
|
||||
def __init__(self, project, data_link_type="DLT_EN10MB"):
|
||||
def __init__(self, project):
|
||||
self._id = str(uuid.uuid4())
|
||||
self._vms = []
|
||||
self._project = project
|
||||
self._data_link_type = data_link_type
|
||||
self._capturing = False
|
||||
self._capture_file_name = None
|
||||
|
||||
@asyncio.coroutine
|
||||
def addVM(self, vm, adapter_number, port_number):
|
||||
@@ -55,29 +60,50 @@ class Link:
|
||||
raise NotImplementedError
|
||||
|
||||
@asyncio.coroutine
|
||||
def start_capture(self):
|
||||
def start_capture(self, data_link_type="DLT_EN10MB", capture_file_name=None):
|
||||
"""
|
||||
Start capture on the link
|
||||
|
||||
:returns: Capture object
|
||||
"""
|
||||
raise NotImplementedError
|
||||
self._capturing = True
|
||||
self._capture_file_name = capture_file_name
|
||||
self._streaming_pcap = asyncio.async(self._start_streaming_pcap())
|
||||
|
||||
@asyncio.coroutine
|
||||
def _start_streaming_pcap(self):
|
||||
"""
|
||||
Dump the pcap file on disk
|
||||
"""
|
||||
stream = yield from self.read_pcap_from_source()
|
||||
with open(self.capture_file_path, "wb+") as f:
|
||||
while self._capturing:
|
||||
# We read 1 bytes by 1 otherwise if the traffic stop the remaining data is not read
|
||||
# this is slow
|
||||
data = yield from stream.read(1)
|
||||
if data:
|
||||
f.write(data)
|
||||
# Flush to disk otherwise the live is not really live
|
||||
f.flush()
|
||||
else:
|
||||
break
|
||||
yield from stream.close()
|
||||
|
||||
@asyncio.coroutine
|
||||
def stop_capture(self):
|
||||
"""
|
||||
Stop capture on the link
|
||||
"""
|
||||
raise NotImplementedError
|
||||
self._capturing = False
|
||||
|
||||
@asyncio.coroutine
|
||||
def read_pcap(self):
|
||||
def read_pcap_from_source(self):
|
||||
"""
|
||||
Return a FileStream of the Pcap from the compute node
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
def capture_file_name(self):
|
||||
def default_capture_file_name(self):
|
||||
"""
|
||||
:returns: File name for a capture on this link
|
||||
"""
|
||||
@@ -98,6 +124,16 @@ class Link:
|
||||
def capturing(self):
|
||||
return self._capturing
|
||||
|
||||
@property
|
||||
def capture_file_path(self):
|
||||
"""
|
||||
Get the path of the capture
|
||||
"""
|
||||
if self._capture_file_name:
|
||||
return os.path.join(self._project.captures_directory, self._capture_file_name)
|
||||
else:
|
||||
return None
|
||||
|
||||
def __json__(self):
|
||||
res = []
|
||||
for side in self._vms:
|
||||
@@ -106,4 +142,8 @@ class Link:
|
||||
"adapter_number": side["adapter_number"],
|
||||
"port_number": side["port_number"]
|
||||
})
|
||||
return {"vms": res, "link_id": self._id, "capturing": self._capturing}
|
||||
return {
|
||||
"vms": res, "link_id": self._id,
|
||||
"capturing": self._capturing,
|
||||
"capture_file_name": self._capture_file_name
|
||||
}
|
||||
@@ -80,17 +80,19 @@ class UDPLink(Link):
|
||||
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, data_link_type="DLT_EN10MB"):
|
||||
def start_capture(self, data_link_type="DLT_EN10MB", capture_file_name=None):
|
||||
"""
|
||||
Start capture on a link
|
||||
"""
|
||||
if not capture_file_name:
|
||||
capture_file_name = self.default_capture_file_name()
|
||||
self._capture_vm = self._choose_capture_side()
|
||||
data = {
|
||||
"capture_file_name": self.capture_file_name(),
|
||||
"capture_file_name": capture_file_name,
|
||||
"data_link_type": 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)
|
||||
self._capturing = True
|
||||
yield from super().start_capture(data_link_type=data_link_type, capture_file_name=capture_file_name)
|
||||
|
||||
@asyncio.coroutine
|
||||
def stop_capture(self):
|
||||
@@ -100,7 +102,7 @@ class UDPLink(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
|
||||
self._capturing = False
|
||||
yield from super().stop_capture()
|
||||
|
||||
def _choose_capture_side(self):
|
||||
"""
|
||||
@@ -124,10 +126,10 @@ class UDPLink(Link):
|
||||
raise aiohttp.web.HTTPConflict(text="Capture is not supported for this link")
|
||||
|
||||
@asyncio.coroutine
|
||||
def read_pcap(self):
|
||||
def read_pcap_from_source(self):
|
||||
"""
|
||||
Return a FileStream of the Pcap from the compute node
|
||||
"""
|
||||
if self._capture_vm:
|
||||
compute = self._capture_vm["vm"].compute
|
||||
return compute.streamFile(self._project, "tmp/captures/" + self.capture_file_name())
|
||||
return compute.streamFile(self._project, "tmp/captures/" + self._capture_file_name)
|
||||
@@ -73,7 +73,7 @@ class LinkHandler:
|
||||
controller = Controller.instance()
|
||||
project = controller.getProject(request.match_info["project_id"])
|
||||
link = project.getLink(request.match_info["link_id"])
|
||||
yield from link.start_capture(request.json.get("data_link_type", "DLT_EN10MB"))
|
||||
yield from link.start_capture(data_link_type=request.json.get("data_link_type", "DLT_EN10MB"), capture_file_name=request.json.get("capture_file_name"))
|
||||
response.set_status(204)
|
||||
|
||||
@classmethod
|
||||
@@ -136,20 +136,24 @@ class LinkHandler:
|
||||
project = controller.getProject(request.match_info["project_id"])
|
||||
link = project.getLink(request.match_info["link_id"])
|
||||
|
||||
content = yield from link.read_pcap()
|
||||
if content is None:
|
||||
if link.capture_file_path is None:
|
||||
raise aiohttp.web.HTTPNotFound(text="pcap file not found")
|
||||
|
||||
response.content_type = "application/vnd.tcpdump.pcap"
|
||||
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:
|
||||
print(link.capture_file_path)
|
||||
with open(link.capture_file_path, "rb") as f:
|
||||
|
||||
response.start(request)
|
||||
response.content_type = "application/vnd.tcpdump.pcap"
|
||||
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
|
||||
response.start(request)
|
||||
|
||||
while True:
|
||||
chunk = yield from content.read(4096)
|
||||
if not chunk:
|
||||
yield from asyncio.sleep(0.1)
|
||||
yield from response.write(chunk)
|
||||
while True:
|
||||
chunk = f.read(4096)
|
||||
if not chunk:
|
||||
break
|
||||
yield from response.write(chunk)
|
||||
except OSError:
|
||||
raise aiohttp.web.HTTPNotFound(text="pcap file {} not found or not accessible".format(link.capture_file_path))
|
||||
@@ -55,9 +55,13 @@ LINK_OBJECT_SCHEMA = {
|
||||
}
|
||||
},
|
||||
"capturing": {
|
||||
"description": "Read only propertie. Is a capture running on the link",
|
||||
"description": "Read only propertie. True if a capture running on the link",
|
||||
"type": "boolean"
|
||||
},
|
||||
"capture_file_name": {
|
||||
"description": "Read only propertie. The name of the capture file if capture is running",
|
||||
"type": ["string", "null"]
|
||||
}
|
||||
},
|
||||
"required": ["vms"],
|
||||
"additionalProperties": False
|
||||
@@ -72,6 +76,10 @@ LINK_CAPTURE_SCHEMA = {
|
||||
"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"]
|
||||
},
|
||||
"capture_file_name": {
|
||||
"description": "Read only propertie. The name of the capture file if capture is running",
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"additionalProperties": False
|
||||
|
||||
Reference in new issue
Block a user