Skip to content

Commit

Permalink
feat: Close docker container on flag submit
Browse files Browse the repository at this point in the history
  • Loading branch information
mradigen committed Jan 28, 2024
1 parent ba630ef commit f174533
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/pwncore/routes/ctf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pwncore.routes.ctf.start import router as start_router
from pwncore.routes.ctf.pre_event import router as pre_event_router
from pwncore.routes.auth import RequireJwt
from pwncore.container import docker_client

# Metadata at the top for instant accessibility
metadata = {
Expand Down Expand Up @@ -92,17 +93,30 @@ async def flag_post(
response.status_code = 401
return {"msg_code": config.msg_codes["ctf_solved"]}

check_solved = await Container.exists(
team_id=team_id, flag=flag.flag, problem_id=ctf_id
)
if check_solved:
team_container = await Container.get_or_none(team_id=team_id, problem_id=ctf_id)
if not team_container:
return {"msg_code": config.msg_codes["container_not_found"]}

if team_container.flag == flag.flag:
hints = await Hint.filter(
problem_id=ctf_id,
viewedhints__team_id=team_id,
viewedhints__with_points=True,
)
pnlt = (100 - sum(map(lambda h: HINTPENALTY[h.order], hints))) / 100

# Stop container after submitting
try:
await Container.filter(team_id=team_id, problem_id=ctf_id).delete()
except Exception:
response.status_code = 500
return {"msg_code": config.msg_codes["db_error"]}

container = await docker_client.containers.get(team_container.docker_id)
await container.stop()
await container.delete()
#

await SolvedProblem.create(team_id=team_id, problem_id=ctf_id, penalty=pnlt)
create_task(update_points(req, ctf_id))
return {"status": True}
Expand Down

0 comments on commit f174533

Please sign in to comment.