Skip to content

Commit 297439d

Browse files
authored
Merge branch 'lugvitc:master' into tests
2 parents 508ea0f + 0a6e3b3 commit 297439d

File tree

8 files changed

+18
-314
lines changed

8 files changed

+18
-314
lines changed

src/pwncore/models/__init__.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,15 @@
2222
from pwncore.models.user import (
2323
User,
2424
Team,
25-
MetaTeam,
2625
Team_Pydantic,
2726
User_Pydantic,
28-
MetaTeam_Pydantic,
2927
)
3028
from pwncore.models.pre_event import (
3129
PreEventProblem,
3230
PreEventSolvedProblem,
3331
PreEventUser,
3432
PreEventProblem_Pydantic,
3533
)
36-
from pwncore.models.round2 import (
37-
R2Problem,
38-
R2Ports,
39-
R2Container,
40-
R2AttackRecord,
41-
)
4234

4335

4436
__all__ = (
@@ -57,16 +49,9 @@
5749
"User_Pydantic",
5850
"Team",
5951
"Team_Pydantic",
60-
"MetaTeam",
61-
"MetaTeam_Pydantic",
6252
"PreEventProblem_Pydantic",
6353
"Problem_Pydantic",
6454
"BaseProblem",
65-
"R2Problem",
66-
"R2Ports",
67-
"R2Container",
68-
"R2Container_Pydantic",
69-
"R2AttackRecord",
7055
)
7156

7257

@@ -76,7 +61,3 @@ def get_annotations(cls, method=None):
7661

7762
tortoise.contrib.pydantic.utils.get_annotations = get_annotations # type: ignore[unused-ignore]
7863
tortoise.contrib.pydantic.creator.get_annotations = get_annotations # type: ignore[unused-ignore]
79-
80-
81-
tortoise.Tortoise.init_models(["pwncore.models.round2"], "models")
82-
R2Container_Pydantic = pydantic_model_creator(R2Container)

src/pwncore/models/round2.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/pwncore/models/user.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
__all__ = (
1313
"User",
1414
"Team",
15-
"MetaTeam",
16-
"MetaTeam_Pydantic",
1715
"User_Pydantic",
1816
"Team_Pydantic",
1917
)
@@ -57,22 +55,9 @@ class Team(Model):
5755
members: fields.ReverseRelation[User]
5856
containers: fields.ReverseRelation[Container]
5957

60-
meta_team: fields.ForeignKeyNullableRelation[MetaTeam] = fields.ForeignKeyField(
61-
"models.MetaTeam", "teams", null=True, on_delete=fields.OnDelete.SET_NULL
62-
)
63-
6458
class PydanticMeta:
6559
exclude = ["secret_hash"]
6660

6761

68-
class MetaTeam(Model):
69-
id = fields.IntField(pk=True)
70-
name = fields.CharField(255, unique=True)
71-
points = fields.IntField(default=0)
72-
73-
teams: fields.ReverseRelation[Team]
74-
75-
7662
Team_Pydantic = pydantic_model_creator(Team)
7763
User_Pydantic = pydantic_model_creator(User)
78-
MetaTeam_Pydantic = pydantic_model_creator(MetaTeam)

src/pwncore/routes/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from fastapi import APIRouter
22

3-
from pwncore.routes import ctf, team, auth, admin, leaderboard, round2
3+
from pwncore.routes import ctf, team, auth, admin, leaderboard
44

55
# from pwncore.config import config
66

@@ -12,7 +12,6 @@
1212
router.include_router(ctf.router)
1313
router.include_router(team.router)
1414
router.include_router(leaderboard.router)
15-
router.include_router(round2.router)
1615
router.include_router(admin.router)
1716
# if config.development:
1817
# router.include_router(admin.router)

src/pwncore/routes/admin.py

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import asyncio
21
from datetime import date
3-
import itertools
42
import logging
5-
import uuid
63

74
from fastapi import APIRouter, Request, Response
85
from passlib.hash import bcrypt
96
from tortoise.transactions import atomic, in_transaction
10-
from tortoise.expressions import RawSQL, Q
11-
from tortoise.functions import Sum
127

138
from pwncore.models import (
149
Team,
@@ -19,12 +14,9 @@
1914
PreEventProblem,
2015
)
2116
from pwncore.config import config
22-
from pwncore.models.container import Container
2317
from pwncore.models.ctf import SolvedProblem
2418
from pwncore.models.pre_event import PreEventUser
2519
from pwncore.container import docker_client
26-
from pwncore.models.round2 import R2Container, R2Ports, R2Problem
27-
from pwncore.models.user import MetaTeam
2820

2921
metadata = {
3022
"name": "admin",
@@ -61,108 +53,6 @@ async def _del_cont(id: str):
6153

6254

6355
@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-
16656
@router.get("/union")
16757
async def calculate_team_coins(
16858
response: Response, req: Request

src/pwncore/routes/leaderboard.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,23 @@ def __init__(self, period: float) -> None:
2626
self.data = b"[]"
2727

2828
async def _do_update(self):
29-
self.data = dumps((
30-
await Team.all()
31-
.filter(Q(solved_problem__problem__visible=True) | Q(points__gte=0))
32-
.annotate(
33-
tpoints=RawSQL(
34-
'COALESCE((SUM("solvedproblem"."penalty" * '
35-
'"solvedproblem__problem"."points")'
36-
' + "team"."points"), 0)'
29+
self.data = dumps(
30+
(
31+
await Team.all()
32+
.filter(Q(solved_problem__problem__visible=True) | Q(points__gte=0))
33+
.annotate(
34+
tpoints=RawSQL(
35+
'COALESCE((SUM("solvedproblem"."penalty" * '
36+
'"solvedproblem__problem"."points")'
37+
' + "team"."points"), 0)'
38+
)
3739
)
38-
)
39-
.group_by("id", "meta_team__name")
40-
.order_by("-tpoints")
41-
.values("name", "tpoints", "meta_team__name")
42-
), separators=(",", ":")).encode("utf-8")
40+
.group_by("id")
41+
.order_by("-tpoints")
42+
.values("name", "tpoints")
43+
),
44+
separators=(",", ":"),
45+
).encode("utf-8")
4346
self.last_update = monotonic()
4447

4548
async def get_lb(self, req: Request):

0 commit comments

Comments
 (0)