Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/pwncore/models/ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Problem(BaseProblem):
hints: fields.ReverseRelation[Hint]

class PydanticMeta:
exclude = ["image_name", "static_files", "mi", "ma", "visible"]
exclude = ["image_name", "static_files", "static_flag", "mi", "ma", "visible"]

async def _solves(self) -> int:
return await SolvedProblem.filter(problem=self).count()
Expand Down
13 changes: 8 additions & 5 deletions src/pwncore/routes/ctf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,14 @@ async def flag_post(
f"{config.staticfs_data_dir}/{team_id}/{team_container.docker_id}"
)
else:
container = await containerASD.docker_client.containers.get(
team_container.docker_id
)
await container.kill()
await container.delete()
try:
container = await containerASD.docker_client.containers.get(
team_container.docker_id
)
await container.kill()
await container.delete()
except Exception:
pass

await SolvedProblem.create(team_id=team_id, problem_id=ctf_id, penalty=pnlt)
create_task(update_points(req, ctf_id))
Expand Down
45 changes: 31 additions & 14 deletions src/pwncore/routes/ctf/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pwncore.containerASD as containerASD
from pwncore.config import config
from pwncore.models import Container, Ports, Problem
from pwncore.models import Container, Ports, Problem, SolvedProblem
from pwncore.routes.auth import RequireJwt

router = APIRouter(tags=["ctf"])
Expand Down Expand Up @@ -40,6 +40,12 @@ async def start_docker_container(ctf_id: int, response: Response, jwt: RequireJw
response.status_code = 404
return {"msg_code": config.msg_codes["ctf_not_found"]}

team_id = jwt["team_id"]
solved = await SolvedProblem.filter(team=team_id, problem=ctf_id).exists()
if solved:
response.status_code = 400
return {"msg_code": config.msg_codes["ctf_solved"]}

if ctf.static_flag:
existing_container = await Container.get_or_none(problem=ctf_id)
if existing_container:
Expand All @@ -57,7 +63,6 @@ async def start_docker_container(ctf_id: int, response: Response, jwt: RequireJw
"ctf_id": ctf_id,
}

team_id = jwt["team_id"] # From JWT
team_container = await Container.filter(team=team_id, problem=ctf_id)
if team_container:
a, b = team_container[0], team_container[1:]
Expand Down Expand Up @@ -242,11 +247,14 @@ async def stopall_docker_container(response: Response, jwt: RequireJwt):
shutil.rmtree(team_path)

for db_container in containers:
container = await containerASD.docker_client.containers.get(
db_container["docker_id"]
)
await container.kill()
await container.delete()
try:
container = await containerASD.docker_client.containers.get(
db_container["docker_id"]
)
await container.kill()
await container.delete()
except Exception:
pass

return {"msg_code": config.msg_codes["containers_team_stop"]}

Expand Down Expand Up @@ -274,16 +282,25 @@ async def stop_docker_container(ctf_id: int, response: Response, jwt: RequireJwt
response.status_code = 500
return {"msg_code": config.msg_codes["db_error"]}

if ctf.static_files:
shutil.rmtree(
f"{config.staticfs_data_dir}/{team_id}/{team_container.docker_id}"
)
return {"msg_code": config.msg_codes["container_stop"]}
try:
if ctf.static_files:
shutil.rmtree(
f"{config.staticfs_data_dir}/{team_id}/{team_container.docker_id}"
)
return {"msg_code": config.msg_codes["container_stop"]}
except:
pass

container = await containerASD.docker_client.containers.get(
team_container.docker_id
)
await container.kill()
await container.delete()
try:
container = await containerASD.docker_client.containers.get(
team_container.docker_id
)
await container.kill()
await container.delete()
except Exception:
pass

return {"msg_code": config.msg_codes["container_stop"]}