From c7d90a376350a10fe297c0c390d9155767eed393 Mon Sep 17 00:00:00 2001 From: mradigen <55953083+mradigen@users.noreply.github.com> Date: Tue, 30 Jan 2024 00:06:44 +0530 Subject: [PATCH 1/3] fix: stop -> kill --- src/pwncore/__init__.py | 2 +- src/pwncore/routes/admin.py | 4 ++-- src/pwncore/routes/ctf/__init__.py | 5 +++-- src/pwncore/routes/ctf/start.py | 9 +++++---- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/pwncore/__init__.py b/src/pwncore/__init__.py index 7d3eae1..c9b103e 100644 --- a/src/pwncore/__init__.py +++ b/src/pwncore/__init__.py @@ -26,7 +26,7 @@ async def app_lifespan(app: FastAPI): for db_container in containers: try: container = await docker_client.containers.get(db_container["docker_id"]) - await container.stop() + await container.kill() await container.delete() except ( Exception diff --git a/src/pwncore/routes/admin.py b/src/pwncore/routes/admin.py index c42ab47..1e93159 100644 --- a/src/pwncore/routes/admin.py +++ b/src/pwncore/routes/admin.py @@ -56,7 +56,7 @@ async def _del_cont(id: str): container = await docker_client.containers.get(id) - await container.stop() + await container.kill() await container.delete() @@ -95,7 +95,7 @@ async def _create_container(prob: R2Problem, mteam: MetaTeam): await R2Ports.bulk_create(ports) except Exception as err: try: - await container.stop() + await container.kill() await container.delete() except Exception: pass diff --git a/src/pwncore/routes/ctf/__init__.py b/src/pwncore/routes/ctf/__init__.py index cb44d86..98d2180 100644 --- a/src/pwncore/routes/ctf/__init__.py +++ b/src/pwncore/routes/ctf/__init__.py @@ -69,7 +69,8 @@ async def ctf_list(jwt: RequireJwt): problems = await Problem_Pydantic.from_queryset(Problem.filter(visible=True)) acc: dict[int, float] = defaultdict(lambda: 1.0) for k, v in map( - lambda x: (x.hint.problem_id, HINTPENALTY[x.hint.order]), # type: ignore[attr-defined] + # type: ignore[attr-defined] + lambda x: (x.hint.problem_id, HINTPENALTY[x.hint.order]), await ViewedHint.filter(team_id=team_id, with_points=True).prefetch_related( "hint" ), @@ -125,7 +126,7 @@ async def flag_post( return {"msg_code": config.msg_codes["db_error"]} container = await docker_client.containers.get(team_container.docker_id) - await container.stop() + await container.kill() await container.delete() # diff --git a/src/pwncore/routes/ctf/start.py b/src/pwncore/routes/ctf/start.py index 7785840..5c0e2bc 100644 --- a/src/pwncore/routes/ctf/start.py +++ b/src/pwncore/routes/ctf/start.py @@ -39,7 +39,8 @@ async def start_docker_container(ctf_id: int, response: Response, jwt: RequireJw db_ports = await team_container.ports.all().values( "port" ) # Get ports from DB - ports = [db_port["port"] for db_port in db_ports] # Create a list out of it + ports = [db_port["port"] + for db_port in db_ports] # Create a list out of it return { "msg_code": config.msg_codes["container_already_running"], "ports": ports, @@ -96,7 +97,7 @@ async def start_docker_container(ctf_id: int, response: Response, jwt: RequireJw await Ports.create(port=port, container=db_container) except Exception as err: # Stop the container if failed to make a DB record - await container.stop() + await container.kill() await container.delete() logger.exception("Error while starting", exc_info=err) @@ -127,7 +128,7 @@ async def stopall_docker_container(response: Response, jwt: RequireJwt): for db_container in containers: container = await docker_client.containers.get(db_container["docker_id"]) - await container.stop() + await container.kill() await container.delete() return {"msg_code": config.msg_codes["containers_team_stop"]} @@ -157,7 +158,7 @@ async def stop_docker_container(ctf_id: int, response: Response, jwt: RequireJwt return {"msg_code": config.msg_codes["db_error"]} container = await docker_client.containers.get(team_container.docker_id) - await container.stop() + await container.kill() await container.delete() return {"msg_code": config.msg_codes["container_stop"]} From bd471bd1919aae1bafff0ebdc338d2d40708f2de Mon Sep 17 00:00:00 2001 From: mradigen <55953083+mradigen@users.noreply.github.com> Date: Tue, 30 Jan 2024 00:21:35 +0530 Subject: [PATCH 2/3] fix: shit --- src/pwncore/routes/ctf/__init__.py | 3 +-- src/pwncore/routes/ctf/start.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/pwncore/routes/ctf/__init__.py b/src/pwncore/routes/ctf/__init__.py index 98d2180..84c08c6 100644 --- a/src/pwncore/routes/ctf/__init__.py +++ b/src/pwncore/routes/ctf/__init__.py @@ -69,8 +69,7 @@ async def ctf_list(jwt: RequireJwt): problems = await Problem_Pydantic.from_queryset(Problem.filter(visible=True)) acc: dict[int, float] = defaultdict(lambda: 1.0) for k, v in map( - # type: ignore[attr-defined] - lambda x: (x.hint.problem_id, HINTPENALTY[x.hint.order]), + lambda x: (x.hint.problem_id, HINTPENALTY[x.hint.order]), # type: ignore[attr-defined] await ViewedHint.filter(team_id=team_id, with_points=True).prefetch_related( "hint" ), diff --git a/src/pwncore/routes/ctf/start.py b/src/pwncore/routes/ctf/start.py index 5c0e2bc..0da50ce 100644 --- a/src/pwncore/routes/ctf/start.py +++ b/src/pwncore/routes/ctf/start.py @@ -34,13 +34,25 @@ async def start_docker_container(ctf_id: int, response: Response, jwt: RequireJw return {"msg_code": config.msg_codes["ctf_not_found"]} team_id = jwt["team_id"] # From JWT - team_container = await Container.get_or_none(team=team_id, problem=ctf_id) + team_container = await Container.filter(team=team_id, problem=ctf_id) if team_container: + team_container, b = team_container[0], team_container[1:] db_ports = await team_container.ports.all().values( "port" ) # Get ports from DB ports = [db_port["port"] for db_port in db_ports] # Create a list out of it + + for db_container in b: + try: + await db_container.delete() + except Exception: + pass + + container = await docker_client.containers.get(db_container.docker_id) + await container.kill() + await container.delete() + return { "msg_code": config.msg_codes["container_already_running"], "ports": ports, From 8483f4a51e8a1a8b7590bdfb225baf4717ff6870 Mon Sep 17 00:00:00 2001 From: mradigen <55953083+mradigen@users.noreply.github.com> Date: Tue, 30 Jan 2024 00:28:37 +0530 Subject: [PATCH 3/3] fix: shit --- src/pwncore/routes/ctf/start.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pwncore/routes/ctf/start.py b/src/pwncore/routes/ctf/start.py index 0da50ce..d22c260 100644 --- a/src/pwncore/routes/ctf/start.py +++ b/src/pwncore/routes/ctf/start.py @@ -36,8 +36,8 @@ async def start_docker_container(ctf_id: int, response: Response, jwt: RequireJw team_id = jwt["team_id"] # From JWT team_container = await Container.filter(team=team_id, problem=ctf_id) if team_container: - team_container, b = team_container[0], team_container[1:] - db_ports = await team_container.ports.all().values( + a, b = team_container[0], team_container[1:] + db_ports = await a.ports.all().values( "port" ) # Get ports from DB ports = [db_port["port"]