Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c6a1e0

Browse files
committed
minor fixes
1 parent 4a7371a commit 9c6a1e0

10 files changed

Lines changed: 40 additions & 17 deletions

File tree

misc/r8.service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[Unit]
22
Description=r8 ctf system
3+
# ln -s /root/r8/misc/r8.service /etc/systemd/system/r8.servicen
34

45
[Service]
56
Type=simple

r8/challenge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def active(self) -> bool:
6666
t_start, t_stop = self._active_times()
6767
return t_start <= time.time() <= t_stop
6868

69-
@functools.lru_cache(maxsize=None)
69+
@functools.cache
7070
def _active_times(self) -> tuple[int, int]:
7171
with r8.db:
7272
return r8.db.execute("""

r8/challenge_mixins/docker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class DockerChallenge(r8.Challenge):
4242
"--cap-drop", "all",
4343
"--user", "nobody",
4444
)
45+
docker_started: bool = False
4546

4647
max_concurrent: ClassVar[asyncio.Semaphore] = asyncio.Semaphore(r8.settings.get("docker_max_concurrent", 5))
4748
timeout = r8.settings.get("docker_timeout", 10)
@@ -111,9 +112,12 @@ async def start(self):
111112
await self._exec("docker", "pull", self.docker_tag)
112113
self.echo(f"Docker: {self.docker_tag} pulled.")
113114
await self._exec("docker", "inspect", self.docker_tag)
115+
self.docker_started = True
114116

115117
async def docker_run_unlimited(self, *args) -> str:
116118
"""`docker run` without rate limits"""
119+
if not self.docker_started:
120+
raise DockerError("Docker service not started.")
117121
self.echo(f"Docker: run {' '.join(args)}")
118122
name = "r8_" + secrets.token_hex(8)
119123
start = time.time()

r8/cli/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def cli(rows, watch, query):
5454
time.sleep(0.5)
5555

5656

57-
@functools.lru_cache()
57+
@functools.cache
5858
def get_widths():
5959
time_w = 19
6060
ip_w = 15

r8/cli/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def cli(debug) -> None:
3131
server.start(),
3232
r8.challenges.start()
3333
))
34+
r8.echo("r8", "Started.")
3435

3536
if os.name != "nt":
3637
loop.add_signal_handler(signal.SIGTERM, loop.stop)

r8/rest_api/scoreboard.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import time
3-
import weakref
43

54
import aiohttp
65
from aiohttp import web
@@ -10,7 +9,7 @@
109
from ..scoring import Scoreboard
1110

1211
scoreboards: list[Scoreboard] = [Scoreboard()]
13-
ws_connections = weakref.WeakSet()
12+
ws_connections: set[web.WebSocketResponse] = set()
1413

1514

1615
async def on_startup(app):
@@ -40,16 +39,31 @@ def on_solve(sender, user, cid):
4039
if team.startswith("_"):
4140
return
4241
scoreboards.append(scoreboards[-1].solve(team, r8.challenges[cid], time.time()))
42+
43+
data = scoreboards[-1].to_json()
4344
for ws in ws_connections:
44-
try:
45-
asyncio.create_task(ws.send_json(scoreboards[-1].to_json()))
46-
except ConnectionError:
47-
pass
45+
asyncio.create_task(send_task(ws, data))
46+
47+
48+
async def send_task(ws: web.WebSocketResponse, data) -> None:
49+
try:
50+
await ws.send_json(data)
51+
except ConnectionError:
52+
pass
4853

4954

5055
async def on_shutdown(app):
51-
for ws in ws_connections:
52-
await ws.close(code=aiohttp.WSCloseCode.GOING_AWAY, message='Server shutdown')
56+
await asyncio.gather(*[
57+
shutdown_task(ws)
58+
for ws in ws_connections
59+
])
60+
61+
62+
async def shutdown_task(ws: web.WebSocketResponse) -> None:
63+
try:
64+
await ws.close(code=aiohttp.WSCloseCode.GOING_AWAY, message=b'server shutdown')
65+
except ConnectionError:
66+
pass
5367

5468

5569
routes = web.RouteTableDef()
@@ -91,8 +105,7 @@ async def get_updates(user: str, request: web.Request):
91105
elif msg.type == aiohttp.WSMsgType.ERROR:
92106
r8.echo("scoreboard", f'ws connection closed with exception {ws.exception()}')
93107
finally:
94-
ws_connections.discard(ws)
95-
# r8.echo('scoreboard', 'websocket connection closed')
108+
ws_connections.remove(ws)
96109
return ws
97110

98111

r8/scoring.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __repr__(self):
7777
)
7878
return f"Scoreboard[{leaders}]"
7979

80-
@functools.lru_cache(maxsize=None)
80+
@functools.cache
8181
def to_json(self) -> dict:
8282
return {
8383
"timestamp": self.timestamp,

r8/static/_index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@
173173
return <form className="col-md-5 col-lg-4 text-center" onSubmit={this.onSubmit}>
174174

175175
<div className="form-group">
176-
<input name="username" type="email" placeholder="foo@example.com"
176+
<input name="username" type="email" placeholder="foo@uibk.ac.at"
177177
className="form-control" value={this.state.username}
178178
required autoComplete="username"
179179
onChange={this.onChange}/>
180180
</div>
181181
<div className="form-group">
182-
<input name="nickname" type="text" placeholder="Display Name (public)"
182+
<input name="nickname" type="text" placeholder="Team Name (public, SFW)"
183183
className="form-control" value={this.state.nickname}
184184
required autoComplete="nickname"
185185
onChange={this.onChange}/>
@@ -469,8 +469,12 @@ <h2 className="text-center m-5">Challenges</h2>
469469
break;
470470
case "login":
471471
body = <div className="container p-5">
472+
{% block login %}{% endblock %}
472473
<div className="row p-5 justify-content-md-center">
473474
<Login onLogin={this.onLogin}/>
475+
{% if r8.settings.register %}
476+
<Register onLogin={this.onLogin}/>
477+
{% endif %}
474478
</div>
475479
</div>;
476480
break;

r8/static/scoreboard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110

111111
window.ws = new WebSocket(location.origin.replace(/^http/, "ws") + "/api/scoreboard/updates");
112112
window.ws.onopen = () => console.log("WebSocket connection opened.");
113-
window.ws.onerror = (e) => {
113+
window.ws.onclose = (e) => {
114114
console.error(e);
115115
window.setTimeout(() => location.reload(), 5000);
116116
};

r8/static/scoretable.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
this.refresh();
113113
this.ws = new WebSocket(location.origin.replace(/^http/, "ws") + "/api/scoreboard/updates");
114114
this.ws.onopen = () => console.log("WebSocket connection opened.");
115-
this.ws.onerror = (e) => {
115+
this.ws.onclose = (e) => {
116116
console.error(e);
117117
window.setTimeout(() => location.reload(), 5000);
118118
};

0 commit comments

Comments
 (0)