Skip to content

Commit

Permalink
enable target machine file streaming via API (kevoreilly#2263)
Browse files Browse the repository at this point in the history
  • Loading branch information
azurda authored Aug 7, 2024
1 parent 2e0b106 commit 5e001dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions web/apiv2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
re_path(r"^tasks/get/config/(?P<task_id>\d+)/(?P<cape_name>\w+)/$", views.tasks_config),
re_path(r"^tasks/get/screenshot/(?P<task_id>\d+)/$", views.tasks_screenshot),
re_path(r"^tasks/get/screenshot/(?P<task_id>\d+)/(?P<screenshot>\d{1,4})/$", views.tasks_screenshot),
re_path(r"^tasks/get/stream/(?P<task_id>\d+)/$", views.tasks_file_stream),
re_path(r"^tasks/get/procmemory/(?P<task_id>\d+)/$", views.tasks_procmemory),
re_path(r"^tasks/get/procmemory/(?P<task_id>\d+)/(?P<pid>\d{1,5})/$", views.tasks_procmemory),
re_path(r"^tasks/get/fullmemory/(?P<task_id>\d+)/$", views.tasks_fullmemory),
Expand Down
37 changes: 37 additions & 0 deletions web/apiv2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,43 @@ def common_download_func(service, request):
return Response(resp)


@csrf_exempt
@api_view(["POST"])
def tasks_file_stream(request, task_id):
"""Streams a file from the running machine with matching task_id."""
if not apiconf.taskstatus.get("enabled"):
resp = {"error": True, "error_value": "Task status API is disabled"}
return Response(resp)
filepath = request.data.get("filepath")
if not filepath:
resp = {"error": True, "error_value": "filepath not set"}
return Response(resp)
resp = {}
task = db.view_task(task_id)
if not task:
resp = {"error": True, "error_value": "Task does not exist"}
return Response(resp)
machine = db.view_machine(task.guest.name)
if machine.status != "running":
resp = {"error": True, "error_value": "Machine is not running", "errors": machine.status}
return Response(resp)
try:
r = requests.post(
f"http://{machine.ip}:8000/retrieve",
stream=True,
data={"filepath": filepath, "streaming": "1"})
if r.status_code >= 400:
resp = {"error": True, "error_value": f"{filepath} does not exist"}
return Response(resp)
return StreamingHttpResponse(
streaming_content=r.iter_content(chunk_size=1024),
content_type="application/octet-stream")
except requests.exceptions.RequestException as ex:
log.error(ex, exc_info=True)
resp = {"error": True, "error_value": f"Requests exception: {ex}"}
return Response(resp)


@csrf_exempt
@api_view(["POST"])
def tasks_vtdl(request):
Expand Down

0 comments on commit 5e001dd

Please sign in to comment.