yacscript 0.3.2__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {yacscript-0.3.2 → yacscript-0.5.0}/PKG-INFO +70 -7
- {yacscript-0.3.2 → yacscript-0.5.0}/README.md +69 -6
- yacscript-0.5.0/app/__init__.py +267 -0
- yacscript-0.5.0/app/app.py +933 -0
- yacscript-0.5.0/app/db.py +82 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/definitions.py +12 -7
- yacscript-0.5.0/app/static/admin.js +116 -0
- yacscript-0.5.0/app/static/dialog.js +178 -0
- yacscript-0.5.0/app/static/handoff.js +14 -0
- yacscript-0.5.0/app/static/room.js +629 -0
- yacscript-0.5.0/app/static/style.css +302 -0
- yacscript-0.5.0/app/static/vendor/98.css +2 -0
- yacscript-0.5.0/app/static/vendor/ms_sans_serif.woff +0 -0
- yacscript-0.5.0/app/static/vendor/ms_sans_serif.woff2 +0 -0
- yacscript-0.5.0/app/static/vendor/ms_sans_serif_bold.woff +0 -0
- yacscript-0.5.0/app/static/vendor/ms_sans_serif_bold.woff2 +0 -0
- yacscript-0.5.0/app/static/vendor/socket.io.min.js +7 -0
- yacscript-0.5.0/app/templates/handoff.jinja +12 -0
- yacscript-0.5.0/app/templates/index.jinja +50 -0
- yacscript-0.5.0/app/templates/room.jinja +99 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/pyproject.toml +30 -30
- yacscript-0.3.2/app/__init__.py +0 -113
- yacscript-0.3.2/app/app.py +0 -656
- yacscript-0.3.2/app/db.py +0 -24
- yacscript-0.3.2/app/static/admin.js +0 -110
- yacscript-0.3.2/app/static/room.js +0 -369
- yacscript-0.3.2/app/static/style.css +0 -64
- yacscript-0.3.2/app/templates/index.jinja +0 -47
- yacscript-0.3.2/app/templates/room.jinja +0 -107
- {yacscript-0.3.2 → yacscript-0.5.0}/COPYING +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/file.mp3 +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/icon.png +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/join.mp3 +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/leave.mp3 +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/msg.mp3 +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/send.mp3 +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/tile.gif +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/tile.jpg +0 -0
- {yacscript-0.3.2 → yacscript-0.5.0}/app/static/welcome.mp3 +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: yacscript
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: yet another chatroom script.
|
|
5
5
|
Author: Valine0x
|
|
6
6
|
Author-email: Valine0x <me@valine0x.icu>
|
|
@@ -32,6 +32,33 @@ Inspired by [LE-CHAT-PHP](https://github.com/DanWin/le-chat-php)
|
|
|
32
32
|
- Vanilla HTML & CSS & JS.
|
|
33
33
|
- No Cookies, it's bad for your teeth.
|
|
34
34
|
|
|
35
|
+
## Migrating from Older Versions (IMPORTANT)
|
|
36
|
+
|
|
37
|
+
**Migrating from 0.3.x**
|
|
38
|
+
|
|
39
|
+
If you are upgrading from YACS 0.3.x or earlier, your existing database and resource files may not be compatible with the new release. Use the `migrate` command to fix things up **before** starting the server:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
# From venv
|
|
43
|
+
(.venv) yacscript migrate --config config.toml
|
|
44
|
+
|
|
45
|
+
# From source
|
|
46
|
+
$ uv run yacscript migrate --config config.toml
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The `migrate` command will fix the database schema as well as the resource naming.
|
|
50
|
+
|
|
51
|
+
It is safe to run `migrate` multiple times — if there is nothing to migrate, no changes will be made anyway.
|
|
52
|
+
|
|
53
|
+
**Migrating from 0.4.0**
|
|
54
|
+
|
|
55
|
+
We're currently introducing a lot of breaking update since 0.5.0 regarding certain security issues. Manual interventions are minimal but required. Check lastest `README.md`, especially the <a href="#configuration">configuration section</a>.
|
|
56
|
+
|
|
57
|
+
- Inline attachments are now controlled by an allowlist of extensions. Other file types are sanitized.
|
|
58
|
+
- A strong admin passphrase is now required to start the server.
|
|
59
|
+
- `/auth` is now rate limited.
|
|
60
|
+
- `/room-preview` now requires explicit opt-in in config.
|
|
61
|
+
|
|
35
62
|
## Try It Out Right Now!
|
|
36
63
|
|
|
37
64
|
Join [The YACS Demo Server](https://chat.valine0x.icu) right now with passphrase **`the-yacs`** to see YACS in action!
|
|
@@ -71,7 +98,11 @@ For reference, see the <a href="#configuration">configuration section</a>.
|
|
|
71
98
|
(.venv) vi ./config.toml
|
|
72
99
|
```
|
|
73
100
|
|
|
74
|
-
Finally,
|
|
101
|
+
Finally, start the server within the venv:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
(.venv) yacscript start --config config.toml
|
|
105
|
+
```
|
|
75
106
|
|
|
76
107
|
In addition to that, you might want to deploy the program as a daemon. Here's an example of a possible simple unit file for systemd.
|
|
77
108
|
|
|
@@ -86,7 +117,7 @@ After=network.target
|
|
|
86
117
|
Type=simple
|
|
87
118
|
WorkingDirectory=/path/to
|
|
88
119
|
Environment="PATH=/path/to/.venv/bin"
|
|
89
|
-
ExecStart=/path/to/.venv/bin/
|
|
120
|
+
ExecStart=/path/to/.venv/bin/yacscript start --config /path/to/config.toml
|
|
90
121
|
|
|
91
122
|
[Install]
|
|
92
123
|
WantedBy=multi-user.target
|
|
@@ -127,7 +158,7 @@ $ vi ./config.toml
|
|
|
127
158
|
Finally, run the server, dependencies and venv will be installed along the way.
|
|
128
159
|
|
|
129
160
|
```bash
|
|
130
|
-
$ uv run
|
|
161
|
+
$ uv run yacscript start --config config.toml
|
|
131
162
|
```
|
|
132
163
|
|
|
133
164
|
Additionally, it's also possible to make a daemon service out of deployment from source.
|
|
@@ -144,19 +175,29 @@ Type=simple
|
|
|
144
175
|
WorkingDirectory=/path/to/yacs
|
|
145
176
|
Environment="PATH=/path/to/yacs/.venv/bin"
|
|
146
177
|
# Notice the difference to deploying from package!
|
|
147
|
-
ExecStart=/path/to/uv run
|
|
178
|
+
ExecStart=/path/to/uv run yacscript start --config /path/to/config.toml
|
|
148
179
|
|
|
149
180
|
[Install]
|
|
150
181
|
WantedBy=multi-user.target
|
|
151
182
|
```
|
|
152
183
|
|
|
184
|
+
## CLI Reference
|
|
185
|
+
|
|
186
|
+
The `yacscript` command ships with the following subcommands:
|
|
187
|
+
|
|
188
|
+
- `yacscript start --config config.toml` — Start the YACS server.
|
|
189
|
+
- `yacscript gc --config config.toml` — Clean expired resources from disk.
|
|
190
|
+
- `yacscript migrate --config config.toml` — Migrate your data from older versions.
|
|
191
|
+
|
|
192
|
+
In which gc and migrate are mostly safe to run.
|
|
193
|
+
|
|
153
194
|
## User Manual
|
|
154
195
|
|
|
155
196
|
Consult <a href="docs/manual.md">User Manual</a> for guidance.
|
|
156
197
|
|
|
157
198
|
## Configuration
|
|
158
199
|
|
|
159
|
-
|
|
200
|
+
You should at least set a unique `app.admin_phrase` (at least 8 characters, not among some of the well-known placeholder) to start the server!
|
|
160
201
|
|
|
161
202
|
All possible fields are shown below.
|
|
162
203
|
|
|
@@ -171,12 +212,24 @@ ip = "0.0.0.0"
|
|
|
171
212
|
port = 8080
|
|
172
213
|
|
|
173
214
|
admin_phrase = "CHANGE_ME_ADMIN"
|
|
215
|
+
# admin_phrase must be at least 8 characters long and must not be a
|
|
216
|
+
# well-known/placeholder value ("admin", "password", "CHANGE_ME_ADMIN", ...).
|
|
217
|
+
# The server refuses to start otherwise.
|
|
174
218
|
# set user_phrase to empty will expose chatroom to the public
|
|
175
219
|
user_phrase = "CHANGE_ME"
|
|
176
220
|
|
|
177
221
|
log_level = "INFO"
|
|
178
222
|
timeout = 5000 # in milisecond, use for heartbeat
|
|
179
223
|
|
|
224
|
+
# Failed-login throttling, applied per IP and per nickname.
|
|
225
|
+
# The counters are kept in memory and reset when the process restarts.
|
|
226
|
+
auth_fail_limit = 20
|
|
227
|
+
auth_fail_window = 300 # in seconds
|
|
228
|
+
|
|
229
|
+
# Development only: lets /room-preview create an admin session without any
|
|
230
|
+
# credentials. NEVER enable this on a public server.
|
|
231
|
+
enable_room_preview = false
|
|
232
|
+
|
|
180
233
|
# Set to true only when the app is behind a reverse proxy!
|
|
181
234
|
# Make sure X-Forwarded-For and X-Forwarded-Host are properly set!
|
|
182
235
|
proxy_fix = false
|
|
@@ -202,6 +255,10 @@ emoticons = [
|
|
|
202
255
|
path = "./resource" # Relative to project's root
|
|
203
256
|
size_max = 1024 # In Megabytes
|
|
204
257
|
|
|
258
|
+
# Per-user quota for files staged before they are attached to a message.
|
|
259
|
+
max_pending_files = 10
|
|
260
|
+
max_pending_mb = 0 # Total staged bytes per user, in Megabytes; 0 = 2 * size_max
|
|
261
|
+
|
|
205
262
|
|
|
206
263
|
[captcha]
|
|
207
264
|
length = 4
|
|
@@ -214,6 +271,12 @@ numbers = false
|
|
|
214
271
|
lowercase = true
|
|
215
272
|
uppercase = false
|
|
216
273
|
|
|
274
|
+
[reserved]
|
|
275
|
+
# Having regulars? Don't want your name get stolen? Reserve your name right now! It's free!
|
|
276
|
+
# A reserved nickname only accepts its own passphrase (or the admin phrase), so
|
|
277
|
+
# the shared user_phrase cannot be used to take it. Nicknames are case-sensitive.
|
|
278
|
+
"User01" = "their_passphrase"
|
|
279
|
+
|
|
217
280
|
[db]
|
|
218
281
|
path = "./yacs.db"
|
|
219
282
|
```
|
|
@@ -251,4 +314,4 @@ YACS is licensed under WTFPL with NO WARRANTY.
|
|
|
251
314
|
|
|
252
315
|
<a href="http://www.wtfpl.net/"><img
|
|
253
316
|
src="http://www.wtfpl.net/wp-content/uploads/2012/12/wtfpl-badge-4.png"
|
|
254
|
-
width="80" height="15" alt="WTFPL" /></a>
|
|
317
|
+
width="80" height="15" alt="WTFPL" /></a>
|
|
@@ -15,6 +15,33 @@ Inspired by [LE-CHAT-PHP](https://github.com/DanWin/le-chat-php)
|
|
|
15
15
|
- Vanilla HTML & CSS & JS.
|
|
16
16
|
- No Cookies, it's bad for your teeth.
|
|
17
17
|
|
|
18
|
+
## Migrating from Older Versions (IMPORTANT)
|
|
19
|
+
|
|
20
|
+
**Migrating from 0.3.x**
|
|
21
|
+
|
|
22
|
+
If you are upgrading from YACS 0.3.x or earlier, your existing database and resource files may not be compatible with the new release. Use the `migrate` command to fix things up **before** starting the server:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# From venv
|
|
26
|
+
(.venv) yacscript migrate --config config.toml
|
|
27
|
+
|
|
28
|
+
# From source
|
|
29
|
+
$ uv run yacscript migrate --config config.toml
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The `migrate` command will fix the database schema as well as the resource naming.
|
|
33
|
+
|
|
34
|
+
It is safe to run `migrate` multiple times — if there is nothing to migrate, no changes will be made anyway.
|
|
35
|
+
|
|
36
|
+
**Migrating from 0.4.0**
|
|
37
|
+
|
|
38
|
+
We're currently introducing a lot of breaking update since 0.5.0 regarding certain security issues. Manual interventions are minimal but required. Check lastest `README.md`, especially the <a href="#configuration">configuration section</a>.
|
|
39
|
+
|
|
40
|
+
- Inline attachments are now controlled by an allowlist of extensions. Other file types are sanitized.
|
|
41
|
+
- A strong admin passphrase is now required to start the server.
|
|
42
|
+
- `/auth` is now rate limited.
|
|
43
|
+
- `/room-preview` now requires explicit opt-in in config.
|
|
44
|
+
|
|
18
45
|
## Try It Out Right Now!
|
|
19
46
|
|
|
20
47
|
Join [The YACS Demo Server](https://chat.valine0x.icu) right now with passphrase **`the-yacs`** to see YACS in action!
|
|
@@ -54,7 +81,11 @@ For reference, see the <a href="#configuration">configuration section</a>.
|
|
|
54
81
|
(.venv) vi ./config.toml
|
|
55
82
|
```
|
|
56
83
|
|
|
57
|
-
Finally,
|
|
84
|
+
Finally, start the server within the venv:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
(.venv) yacscript start --config config.toml
|
|
88
|
+
```
|
|
58
89
|
|
|
59
90
|
In addition to that, you might want to deploy the program as a daemon. Here's an example of a possible simple unit file for systemd.
|
|
60
91
|
|
|
@@ -69,7 +100,7 @@ After=network.target
|
|
|
69
100
|
Type=simple
|
|
70
101
|
WorkingDirectory=/path/to
|
|
71
102
|
Environment="PATH=/path/to/.venv/bin"
|
|
72
|
-
ExecStart=/path/to/.venv/bin/
|
|
103
|
+
ExecStart=/path/to/.venv/bin/yacscript start --config /path/to/config.toml
|
|
73
104
|
|
|
74
105
|
[Install]
|
|
75
106
|
WantedBy=multi-user.target
|
|
@@ -110,7 +141,7 @@ $ vi ./config.toml
|
|
|
110
141
|
Finally, run the server, dependencies and venv will be installed along the way.
|
|
111
142
|
|
|
112
143
|
```bash
|
|
113
|
-
$ uv run
|
|
144
|
+
$ uv run yacscript start --config config.toml
|
|
114
145
|
```
|
|
115
146
|
|
|
116
147
|
Additionally, it's also possible to make a daemon service out of deployment from source.
|
|
@@ -127,19 +158,29 @@ Type=simple
|
|
|
127
158
|
WorkingDirectory=/path/to/yacs
|
|
128
159
|
Environment="PATH=/path/to/yacs/.venv/bin"
|
|
129
160
|
# Notice the difference to deploying from package!
|
|
130
|
-
ExecStart=/path/to/uv run
|
|
161
|
+
ExecStart=/path/to/uv run yacscript start --config /path/to/config.toml
|
|
131
162
|
|
|
132
163
|
[Install]
|
|
133
164
|
WantedBy=multi-user.target
|
|
134
165
|
```
|
|
135
166
|
|
|
167
|
+
## CLI Reference
|
|
168
|
+
|
|
169
|
+
The `yacscript` command ships with the following subcommands:
|
|
170
|
+
|
|
171
|
+
- `yacscript start --config config.toml` — Start the YACS server.
|
|
172
|
+
- `yacscript gc --config config.toml` — Clean expired resources from disk.
|
|
173
|
+
- `yacscript migrate --config config.toml` — Migrate your data from older versions.
|
|
174
|
+
|
|
175
|
+
In which gc and migrate are mostly safe to run.
|
|
176
|
+
|
|
136
177
|
## User Manual
|
|
137
178
|
|
|
138
179
|
Consult <a href="docs/manual.md">User Manual</a> for guidance.
|
|
139
180
|
|
|
140
181
|
## Configuration
|
|
141
182
|
|
|
142
|
-
|
|
183
|
+
You should at least set a unique `app.admin_phrase` (at least 8 characters, not among some of the well-known placeholder) to start the server!
|
|
143
184
|
|
|
144
185
|
All possible fields are shown below.
|
|
145
186
|
|
|
@@ -154,12 +195,24 @@ ip = "0.0.0.0"
|
|
|
154
195
|
port = 8080
|
|
155
196
|
|
|
156
197
|
admin_phrase = "CHANGE_ME_ADMIN"
|
|
198
|
+
# admin_phrase must be at least 8 characters long and must not be a
|
|
199
|
+
# well-known/placeholder value ("admin", "password", "CHANGE_ME_ADMIN", ...).
|
|
200
|
+
# The server refuses to start otherwise.
|
|
157
201
|
# set user_phrase to empty will expose chatroom to the public
|
|
158
202
|
user_phrase = "CHANGE_ME"
|
|
159
203
|
|
|
160
204
|
log_level = "INFO"
|
|
161
205
|
timeout = 5000 # in milisecond, use for heartbeat
|
|
162
206
|
|
|
207
|
+
# Failed-login throttling, applied per IP and per nickname.
|
|
208
|
+
# The counters are kept in memory and reset when the process restarts.
|
|
209
|
+
auth_fail_limit = 20
|
|
210
|
+
auth_fail_window = 300 # in seconds
|
|
211
|
+
|
|
212
|
+
# Development only: lets /room-preview create an admin session without any
|
|
213
|
+
# credentials. NEVER enable this on a public server.
|
|
214
|
+
enable_room_preview = false
|
|
215
|
+
|
|
163
216
|
# Set to true only when the app is behind a reverse proxy!
|
|
164
217
|
# Make sure X-Forwarded-For and X-Forwarded-Host are properly set!
|
|
165
218
|
proxy_fix = false
|
|
@@ -185,6 +238,10 @@ emoticons = [
|
|
|
185
238
|
path = "./resource" # Relative to project's root
|
|
186
239
|
size_max = 1024 # In Megabytes
|
|
187
240
|
|
|
241
|
+
# Per-user quota for files staged before they are attached to a message.
|
|
242
|
+
max_pending_files = 10
|
|
243
|
+
max_pending_mb = 0 # Total staged bytes per user, in Megabytes; 0 = 2 * size_max
|
|
244
|
+
|
|
188
245
|
|
|
189
246
|
[captcha]
|
|
190
247
|
length = 4
|
|
@@ -197,6 +254,12 @@ numbers = false
|
|
|
197
254
|
lowercase = true
|
|
198
255
|
uppercase = false
|
|
199
256
|
|
|
257
|
+
[reserved]
|
|
258
|
+
# Having regulars? Don't want your name get stolen? Reserve your name right now! It's free!
|
|
259
|
+
# A reserved nickname only accepts its own passphrase (or the admin phrase), so
|
|
260
|
+
# the shared user_phrase cannot be used to take it. Nicknames are case-sensitive.
|
|
261
|
+
"User01" = "their_passphrase"
|
|
262
|
+
|
|
200
263
|
[db]
|
|
201
264
|
path = "./yacs.db"
|
|
202
265
|
```
|
|
@@ -234,4 +297,4 @@ YACS is licensed under WTFPL with NO WARRANTY.
|
|
|
234
297
|
|
|
235
298
|
<a href="http://www.wtfpl.net/"><img
|
|
236
299
|
src="http://www.wtfpl.net/wp-content/uploads/2012/12/wtfpl-badge-4.png"
|
|
237
|
-
width="80" height="15" alt="WTFPL" /></a>
|
|
300
|
+
width="80" height="15" alt="WTFPL" /></a>
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
from .app import (routes, MessageNamespace, garbage_collect,
|
|
2
|
+
MAX_UPLOAD_OVERHEAD, PENDING_SUBDIR)
|
|
3
|
+
from copy import deepcopy
|
|
4
|
+
from os import makedirs, path, listdir, remove, rename
|
|
5
|
+
import re
|
|
6
|
+
import click
|
|
7
|
+
from flask import Flask
|
|
8
|
+
from .definitions import CONFIG_DEFAULT
|
|
9
|
+
import tomllib
|
|
10
|
+
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
11
|
+
from string import ascii_lowercase, ascii_uppercase
|
|
12
|
+
from flask_socketio import SocketIO
|
|
13
|
+
from .db import init_db, close_db, migrate_db, clean_resources
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def deep_update(dst: dict, src: dict) -> None:
|
|
17
|
+
for k, v in src.items():
|
|
18
|
+
if isinstance(v, dict) and isinstance(dst.get(k), dict):
|
|
19
|
+
deep_update(dst[k], v)
|
|
20
|
+
else:
|
|
21
|
+
dst[k] = v
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
MIN_ADMIN_PHRASE_LENGTH = 8
|
|
25
|
+
WEAK_ADMIN_PHRASES = frozenset({
|
|
26
|
+
'admin', 'administrator', 'password', 'passphrase', 'secret',
|
|
27
|
+
'changeme', 'change_me', 'change_me_admin', 'letmein', 'yacs', '12345678',
|
|
28
|
+
})
|
|
29
|
+
# Same nickname grammar as LOGIN_FORM in app.py (3-16 ASCII letters, digits
|
|
30
|
+
# or underscores). A reserved key that fails it could never be used to log in.
|
|
31
|
+
RESERVED_NICK_RE = re.compile(r'[A-Za-z0-9_]{3,16}')
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _require_int(value, name: str, minimum: int) -> None:
|
|
35
|
+
if isinstance(value, bool) or not isinstance(value, int) or value < minimum:
|
|
36
|
+
raise RuntimeError(f'{name} must be an integer >= {minimum}.')
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _validate_config(app) -> None:
|
|
40
|
+
'''Refuse to start with a configuration that would be unsafe or crash.'''
|
|
41
|
+
cfg = app.config
|
|
42
|
+
app_cfg = cfg.get('app')
|
|
43
|
+
res_cfg = cfg.get('res')
|
|
44
|
+
captcha_cfg = cfg.get('captcha')
|
|
45
|
+
db_cfg = cfg.get('db')
|
|
46
|
+
reserved_cfg = cfg.get('reserved')
|
|
47
|
+
if not all(isinstance(section, dict)
|
|
48
|
+
for section in (app_cfg, res_cfg, captcha_cfg, db_cfg, reserved_cfg)):
|
|
49
|
+
raise RuntimeError(
|
|
50
|
+
'Malformed configuration: [app], [res], [captcha], [db] and [reserved] must be tables.')
|
|
51
|
+
|
|
52
|
+
admin_phrase = app_cfg.get('admin_phrase')
|
|
53
|
+
user_phrase = app_cfg.get('user_phrase')
|
|
54
|
+
if not isinstance(admin_phrase, str) or len(admin_phrase) < MIN_ADMIN_PHRASE_LENGTH:
|
|
55
|
+
raise RuntimeError(
|
|
56
|
+
f'app.admin_phrase must be a string of at least {MIN_ADMIN_PHRASE_LENGTH} characters.')
|
|
57
|
+
if admin_phrase.strip().lower() in WEAK_ADMIN_PHRASES:
|
|
58
|
+
raise RuntimeError(
|
|
59
|
+
'app.admin_phrase is a well-known or placeholder passphrase; choose a unique one.')
|
|
60
|
+
if not isinstance(user_phrase, str):
|
|
61
|
+
raise RuntimeError('app.user_phrase must be a string (empty string = public room).')
|
|
62
|
+
if admin_phrase == user_phrase:
|
|
63
|
+
raise RuntimeError('app.user_phrase and app.admin_phrase must not be identical.')
|
|
64
|
+
if not isinstance(cfg.get('DEBUG'), bool):
|
|
65
|
+
raise RuntimeError('flask.DEBUG must be a boolean (true/false).')
|
|
66
|
+
if not isinstance(app_cfg.get('enable_room_preview'), bool):
|
|
67
|
+
raise RuntimeError('app.enable_room_preview must be a boolean (true/false).')
|
|
68
|
+
|
|
69
|
+
_require_int(app_cfg.get('timeout'), 'app.timeout', 1000)
|
|
70
|
+
_require_int(app_cfg.get('auth_fail_limit'), 'app.auth_fail_limit', 1)
|
|
71
|
+
_require_int(app_cfg.get('auth_fail_window'), 'app.auth_fail_window', 1)
|
|
72
|
+
_require_int(res_cfg.get('size_max'), 'res.size_max', 1)
|
|
73
|
+
_require_int(res_cfg.get('max_pending_files'), 'res.max_pending_files', 1)
|
|
74
|
+
_require_int(res_cfg.get('max_pending_mb'), 'res.max_pending_mb', 0)
|
|
75
|
+
if not isinstance(res_cfg.get('path'), str) or not res_cfg['path']:
|
|
76
|
+
raise RuntimeError('res.path must be a non-empty string.')
|
|
77
|
+
if not isinstance(db_cfg.get('path'), str) or not db_cfg['path']:
|
|
78
|
+
raise RuntimeError('db.path must be a non-empty string.')
|
|
79
|
+
|
|
80
|
+
for nick, phrase in reserved_cfg.items():
|
|
81
|
+
if not isinstance(nick, str) or not RESERVED_NICK_RE.fullmatch(nick):
|
|
82
|
+
raise RuntimeError(
|
|
83
|
+
f'reserved nickname {nick!r} is invalid: '
|
|
84
|
+
'use 3-16 letters, digits or underscores.')
|
|
85
|
+
if not isinstance(phrase, str) or not phrase:
|
|
86
|
+
raise RuntimeError(
|
|
87
|
+
f'reserved passphrase for {nick!r} must be a non-empty string.')
|
|
88
|
+
if phrase == user_phrase:
|
|
89
|
+
raise RuntimeError(
|
|
90
|
+
f'reserved passphrase for {nick!r} must differ from app.user_phrase, '
|
|
91
|
+
'otherwise anyone with the shared phrase can take the name.')
|
|
92
|
+
|
|
93
|
+
captcha_length = captcha_cfg.get('length')
|
|
94
|
+
_require_int(captcha_length, 'captcha.length', 1)
|
|
95
|
+
if captcha_length > 16:
|
|
96
|
+
raise RuntimeError('captcha.length must be <= 16.')
|
|
97
|
+
_require_int(captcha_cfg.get('max_cache'), 'captcha.max_cache', 1)
|
|
98
|
+
_require_int(captcha_cfg.get('expire'), 'captcha.expire', 1)
|
|
99
|
+
options = captcha_cfg.get('options')
|
|
100
|
+
if not isinstance(options, dict) or not any(options.values()):
|
|
101
|
+
raise RuntimeError('captcha.options must enable at least one character set.')
|
|
102
|
+
|
|
103
|
+
try:
|
|
104
|
+
app.logger.setLevel(app_cfg['log_level'])
|
|
105
|
+
except (KeyError, TypeError, ValueError) as e:
|
|
106
|
+
raise RuntimeError(f'app.log_level is invalid: {e}') from e
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def create_app(config=None) -> tuple[Flask, SocketIO]:
|
|
110
|
+
app = Flask(__name__)
|
|
111
|
+
|
|
112
|
+
# Load config. Nested tables must be copied, otherwise deep_update() below
|
|
113
|
+
# mutates CONFIG_DEFAULT and leaks one instance's settings into the next.
|
|
114
|
+
app.config.update(deepcopy(CONFIG_DEFAULT))
|
|
115
|
+
if config is not None:
|
|
116
|
+
if path.isfile(path.abspath(config)):
|
|
117
|
+
with open(path.abspath(config), 'rb') as file:
|
|
118
|
+
try:
|
|
119
|
+
conf = tomllib.load(file)
|
|
120
|
+
except tomllib.TOMLDecodeError as e:
|
|
121
|
+
app.logger.critical(
|
|
122
|
+
f'Initialization failed while parsing config file: {e}')
|
|
123
|
+
app.logger.warning('Starting YACS without config file')
|
|
124
|
+
else:
|
|
125
|
+
top = conf.get('flask', None)
|
|
126
|
+
if top:
|
|
127
|
+
app.config.update(conf.pop('flask'))
|
|
128
|
+
deep_update(app.config, conf)
|
|
129
|
+
else:
|
|
130
|
+
app.logger.warning('Starting YACS without config file')
|
|
131
|
+
|
|
132
|
+
_validate_config(app)
|
|
133
|
+
|
|
134
|
+
if not path.isdir(app.config['res']['path']):
|
|
135
|
+
makedirs(app.config['res']['path'], exist_ok=True)
|
|
136
|
+
|
|
137
|
+
# Set pre-calculated values
|
|
138
|
+
app.config.update({'runtime': {}})
|
|
139
|
+
size_max_byte = app.config['res']['size_max'] * 1000000
|
|
140
|
+
pending_mb = app.config['res']['max_pending_mb']
|
|
141
|
+
pending_max_byte = (pending_mb if pending_mb > 0 else
|
|
142
|
+
app.config['res']['size_max'] * 2) * 1000000
|
|
143
|
+
app.config['runtime']['SIZE_MAX_BYTE'] = size_max_byte
|
|
144
|
+
app.config['runtime']['PENDING_MAX_BYTE'] = pending_max_byte
|
|
145
|
+
options = app.config['captcha']['options']
|
|
146
|
+
app.config['runtime']['challenge_set'] = ['', ascii_lowercase][options['lowercase']] + \
|
|
147
|
+
['', ascii_uppercase][options['uppercase']] + \
|
|
148
|
+
['', '0123456789'][options['numbers']]
|
|
149
|
+
|
|
150
|
+
# Bound the whole request body (file limit plus multipart overhead).
|
|
151
|
+
app.config['MAX_CONTENT_LENGTH'] = size_max_byte + MAX_UPLOAD_OVERHEAD
|
|
152
|
+
|
|
153
|
+
# Prepare the upload staging area and drop files left behind by a crash.
|
|
154
|
+
pending_path = path.join(app.config['res']['path'], PENDING_SUBDIR)
|
|
155
|
+
makedirs(pending_path, exist_ok=True)
|
|
156
|
+
for stale in listdir(pending_path):
|
|
157
|
+
if stale.endswith('.part'):
|
|
158
|
+
try:
|
|
159
|
+
remove(path.join(pending_path, stale))
|
|
160
|
+
except OSError:
|
|
161
|
+
pass
|
|
162
|
+
|
|
163
|
+
# The .jinja suffix is not in Flask's default autoescape list, so every
|
|
164
|
+
# template variable must be escaped explicitly.
|
|
165
|
+
app.jinja_env.autoescape = True
|
|
166
|
+
|
|
167
|
+
# Configure app instance
|
|
168
|
+
app.logger.setLevel(app.config['app']['log_level'])
|
|
169
|
+
if pending_mb and pending_mb < app.config['res']['size_max']:
|
|
170
|
+
app.logger.warning(
|
|
171
|
+
'res.max_pending_mb is smaller than res.size_max; large uploads will be rejected.')
|
|
172
|
+
if app.config['DEBUG']:
|
|
173
|
+
app.logger.warning('DEBUG mode is enabled; never expose this instance to the public.')
|
|
174
|
+
if app.config['app'].get('enable_room_preview', False):
|
|
175
|
+
app.logger.warning(
|
|
176
|
+
'app.enable_room_preview is enabled: /room-preview grants an admin session '
|
|
177
|
+
'without authentication. Development use only!')
|
|
178
|
+
if app.config['app']['proxy_fix']:
|
|
179
|
+
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_host=1)
|
|
180
|
+
|
|
181
|
+
# Create socket.io instance
|
|
182
|
+
socketio = SocketIO(app)
|
|
183
|
+
|
|
184
|
+
# Attach routes
|
|
185
|
+
app.register_blueprint(routes)
|
|
186
|
+
socketio.on_namespace(MessageNamespace('/'))
|
|
187
|
+
app.teardown_appcontext(close_db)
|
|
188
|
+
|
|
189
|
+
# Making sure that db is propperly initialized
|
|
190
|
+
with app.app_context():
|
|
191
|
+
init_db(app.config['db']['path'])
|
|
192
|
+
|
|
193
|
+
def gc():
|
|
194
|
+
timeout = int(app.config['app']['timeout'] / 1000)
|
|
195
|
+
while True:
|
|
196
|
+
with app.app_context():
|
|
197
|
+
garbage_collect()
|
|
198
|
+
socketio.sleep(timeout)
|
|
199
|
+
|
|
200
|
+
socketio.start_background_task(gc)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
return (app, socketio)
|
|
204
|
+
|
|
205
|
+
@click.group()
|
|
206
|
+
def main():
|
|
207
|
+
'''Anonymous Internet Chatroom in Y2K'''
|
|
208
|
+
pass
|
|
209
|
+
|
|
210
|
+
def _create_app_or_fail(config):
|
|
211
|
+
'''Turn configuration errors into a clean CLI error instead of a traceback.'''
|
|
212
|
+
try:
|
|
213
|
+
return create_app(config)
|
|
214
|
+
except RuntimeError as e:
|
|
215
|
+
raise click.ClickException(str(e)) from e
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
@main.command()
|
|
219
|
+
@click.option('-c', '--config', default=None, help='Path to the config file.')
|
|
220
|
+
def start(config):
|
|
221
|
+
'''Starting YACS'''
|
|
222
|
+
|
|
223
|
+
# Validate given path
|
|
224
|
+
if config is not None and not path.isfile(config):
|
|
225
|
+
raise click.ClickException('Config file does not exist!')
|
|
226
|
+
|
|
227
|
+
(app, socketio) = _create_app_or_fail(config)
|
|
228
|
+
|
|
229
|
+
app.logger.info(
|
|
230
|
+
f'YACS is now running on http://{app.config["app"]["ip"]}:{app.config["app"]["port"]}{' with DEBUG MODE ON' if app.config['DEBUG'] else ''}.')
|
|
231
|
+
|
|
232
|
+
if app.config['app']['user_phrase'] == '':
|
|
233
|
+
app.logger.warning(
|
|
234
|
+
'app.user_phrase is empty: anyone who can reach this server can join as a guest.')
|
|
235
|
+
|
|
236
|
+
socketio.run(
|
|
237
|
+
app,
|
|
238
|
+
host=app.config["app"]["ip"],
|
|
239
|
+
port=app.config["app"]["port"],
|
|
240
|
+
debug=app.config["DEBUG"],
|
|
241
|
+
use_reloader=False,
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
@main.command()
|
|
245
|
+
@click.option('-c', '--config', default=None, help='Path to the config file.')
|
|
246
|
+
def gc(config):
|
|
247
|
+
'''Clean not needed resources from disk'''
|
|
248
|
+
(app, _) = _create_app_or_fail(config)
|
|
249
|
+
with app.app_context():
|
|
250
|
+
clean_resources(
|
|
251
|
+
app.config['db']['path'],
|
|
252
|
+
app.config['res']['path'],
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
@main.command()
|
|
256
|
+
@click.option('-c', '--config', default=None, help='Path to the config file.')
|
|
257
|
+
def migrate(config):
|
|
258
|
+
'''Migrate your YACServer from 0.3.x to 0.4'''
|
|
259
|
+
(app, _) = _create_app_or_fail(config)
|
|
260
|
+
with app.app_context():
|
|
261
|
+
if migrate_db(app.config['db']['path']):
|
|
262
|
+
click.echo("Database fixed!")
|
|
263
|
+
res_path = path.abspath(app.config['res']['path'])
|
|
264
|
+
for file in [x for x in listdir(res_path) if path.isfile(path.join(res_path, x))]:
|
|
265
|
+
corrected = file.replace('..', '.')
|
|
266
|
+
rename(path.join(res_path, file), path.join(res_path, corrected))
|
|
267
|
+
click.echo("Resource naming fixed!")
|