|
1 |
| -import asyncio |
2 | 1 | from datetime import date
|
3 |
| -import itertools |
4 | 2 | import logging
|
5 |
| -import uuid |
6 | 3 |
|
7 | 4 | from fastapi import APIRouter, Request, Response
|
8 | 5 | from passlib.hash import bcrypt
|
9 | 6 | from tortoise.transactions import atomic, in_transaction
|
10 |
| -from tortoise.expressions import RawSQL, Q |
11 |
| -from tortoise.functions import Sum |
12 | 7 |
|
13 | 8 | from pwncore.models import (
|
14 | 9 | Team,
|
|
19 | 14 | PreEventProblem,
|
20 | 15 | )
|
21 | 16 | from pwncore.config import config
|
22 |
| -from pwncore.models.container import Container |
23 | 17 | from pwncore.models.ctf import SolvedProblem
|
24 | 18 | from pwncore.models.pre_event import PreEventUser
|
25 | 19 | from pwncore.container import docker_client
|
26 |
| -from pwncore.models.round2 import R2Container, R2Ports, R2Problem |
27 |
| -from pwncore.models.user import MetaTeam |
28 | 20 |
|
29 | 21 | metadata = {
|
30 | 22 | "name": "admin",
|
@@ -61,108 +53,6 @@ async def _del_cont(id: str):
|
61 | 53 |
|
62 | 54 |
|
63 | 55 | @atomic()
|
64 |
| -async def _create_container(prob: R2Problem, mteam: MetaTeam): |
65 |
| - try: |
66 |
| - container = await docker_client.containers.run( |
67 |
| - name=f"{mteam.name}_{prob.pk}_{uuid.uuid4().hex}", |
68 |
| - config={ |
69 |
| - "Image": prob.image_name, |
70 |
| - # Detach stuff |
71 |
| - "AttachStdin": False, |
72 |
| - "AttachStdout": False, |
73 |
| - "AttachStderr": False, |
74 |
| - "Tty": False, |
75 |
| - "OpenStdin": False, |
76 |
| - **prob.image_config, |
77 |
| - }, |
78 |
| - ) |
79 |
| - |
80 |
| - container_flag = f"{config.flag}{{{uuid.uuid4().hex}}}" |
81 |
| - |
82 |
| - await ( |
83 |
| - await container.exec(["/bin/bash", "/root/gen_flag", container_flag]) |
84 |
| - ).start(detach=True) |
85 |
| - |
86 |
| - db_container = await R2Container.create( |
87 |
| - docker_id=container.id, problem=prob, meta_tam=mteam, flag=container_flag |
88 |
| - ) |
89 |
| - |
90 |
| - ports = [] |
91 |
| - for guest_port in prob.image_config["PortBindings"]: |
92 |
| - port = int((await container.port(guest_port))[0]["HostPort"]) |
93 |
| - ports.append(R2Ports(port=port, container=db_container)) |
94 |
| - |
95 |
| - await R2Ports.bulk_create(ports) |
96 |
| - except Exception as err: |
97 |
| - try: |
98 |
| - await container.kill() |
99 |
| - await container.delete() |
100 |
| - except Exception: |
101 |
| - pass |
102 |
| - logging.exception("Error while starting", exc_info=err) |
103 |
| - |
104 |
| - |
105 |
| -@router.get("/round2") |
106 |
| -async def round2(response: Response, req: Request): |
107 |
| - if not bcrypt.verify((await req.body()).strip(), ADMIN_HASH): |
108 |
| - response.status_code = 401 |
109 |
| - return |
110 |
| - |
111 |
| - containers = await Container.all() |
112 |
| - |
113 |
| - async with in_transaction(): |
114 |
| - async with asyncio.TaskGroup() as tg: |
115 |
| - for container in containers: |
116 |
| - tg.create_task(_del_cont(container.docker_id)) |
117 |
| - |
118 |
| - try: |
119 |
| - await Container.all().delete() |
120 |
| - except Exception: |
121 |
| - response.status_code = 500 |
122 |
| - logging.exception("Error while initing round2") |
123 |
| - return {"msg_code": config.msg_codes["db_error"]} |
124 |
| - |
125 |
| - await MetaTeam.all().delete() |
126 |
| - await MetaTeam.bulk_create( |
127 |
| - [MetaTeam(name=NAMES[i], id=i + 1) for i in range(12)] |
128 |
| - ) |
129 |
| - |
130 |
| - teams = ( |
131 |
| - await Team.all() |
132 |
| - .filter(Q(solved_problem__problem__visible=True) | Q(points__gte=0)) |
133 |
| - .annotate( |
134 |
| - tpoints=RawSQL( |
135 |
| - 'COALESCE((SUM("solvedproblem"."penalty" * ' |
136 |
| - '"solvedproblem__problem"."points")' |
137 |
| - ' + "team"."points"), 0)' |
138 |
| - ) |
139 |
| - ) |
140 |
| - .annotate( |
141 |
| - tpoints2=Sum( |
142 |
| - RawSQL( |
143 |
| - '"solvedproblem"."penalty" * "solvedproblem__problem"."points"' |
144 |
| - ) |
145 |
| - ) |
146 |
| - ) |
147 |
| - .order_by("-tpoints") |
148 |
| - ) |
149 |
| - |
150 |
| - for i in range(12): |
151 |
| - for team in teams[i::12]: |
152 |
| - team.meta_team_id = i + 1 # type: ignore[attr-defined] |
153 |
| - # print(mts[i].pk, mts) |
154 |
| - await team.save(update_fields=["meta_team_id"]) |
155 |
| - |
156 |
| - # await Team.bulk_update(teams, fields=["meta_team_id"]) |
157 |
| - |
158 |
| - problems = await R2Problem.all() |
159 |
| - mteams = await MetaTeam.all() |
160 |
| - |
161 |
| - async with asyncio.TaskGroup() as tg: |
162 |
| - for pm in itertools.product(problems, mteams): |
163 |
| - tg.create_task(_create_container(*pm)) |
164 |
| - |
165 |
| - |
166 | 56 | @router.get("/union")
|
167 | 57 | async def calculate_team_coins(
|
168 | 58 | response: Response, req: Request
|
|
0 commit comments