This repository has been archived by the owner on Oct 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
daemon.py
286 lines (227 loc) · 10.9 KB
/
daemon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os, string, random, json, hashlib, uuid, datetime, shutil, collections
from pathlib import Path
from typing import Dict, Any, List, Optional
from jinja2 import Environment
from jinja2 import FileSystemLoader
from werkzeug.exceptions import HTTPException
from werkzeug.exceptions import NotFound
from werkzeug.middleware.shared_data import SharedDataMiddleware
from werkzeug.routing import Map
from werkzeug.routing import Rule
from werkzeug.urls import url_parse
from werkzeug.utils import redirect
from werkzeug.wrappers import Request
from werkzeug.wrappers import Response
import logging
if not os.path.isdir('logs'):
os.makedirs('logs')
logging.basicConfig(filename='logs/daemon.log',
filemode='w',
format='%(asctime)s [%(levelname)-8s] @ (%(name)s): %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p',
#encoding='utf-8', 3.9
level=logging.INFO)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
from ruamel.yaml import YAML
yaml = YAML(typ='safe')
from ss13vox.runtime import VOXRuntime
from ss13vox.phrase import Phrase
from ss13vox.utils import generate_preshared_key
from ss13vox.daemon.config import DaemonConfig
from ss13vox.daemon.gameserver import VOXGameServer
from ss13vox.daemon.phraseref import PhraseRef
DAEMON_YML = Path.cwd() / 'daemon.yml'
OTF_DIR = Path('.otf') / 'tmp' / 'sounds'
OTF_TMP_DIR = OTF_DIR / 'tmp'
GENERATED_SOUND_DIR = Path('.otf') / 'tmp' / 'sounds'
# Order of operations:
# Auth:
# 1. GET /auth/server, response is random challenge token
# 2. POST /auth/server with response and gsid, response is session key and limits
# New Announcement:
# 1. GS: POST /announcement/new with gsid, auth, ckey and phrase
# 2. OTF responds with CDN URI
class WZService:
def __init__(self) -> None:
self.url_map: Map = None
def render_template(self, template_name, **context):
t = self.jinja_env.get_template(template_name)
return Response(t.render(context), mimetype="text/html")
def dispatch_request(self, request):
adapter = self.url_map.bind_to_environ(request.environ)
try:
endpoint, values = adapter.match()
return getattr(self, f"handle_{endpoint}")(request, **values)
except NotFound:
return self.error_404()
except HTTPException as e:
return e
def wsgi_app(self, environ, start_response):
request = Request(environ)
response = self.dispatch_request(request)
return response(environ, start_response)
def __call__(self, environ, start_response):
return self.wsgi_app(environ, start_response)
class JinjaMixin:
def __init__(self) -> None:
self.template_path: Path = None
self.jinja_env: Environment = None
def setupJinja(self, tmpl_dir) -> None:
self.template_path = tmpl_dir
self.jinja_env = Environment(
loader=FileSystemLoader(self.template_path), autoescape=True
)
class VoxRESTService(WZService, JinjaMixin):
def __init__(self, config: Dict[str, Any]):
super().__init__()
self.config: Dict[str, Any] = config
self.gameservers: Dict[str, VOXGameServer] = {}
self.setupJinja(Path.cwd() / "templates")
#self.jinja_env.filters["hostname"] = get_hostname
self.url_map = Map(
[
Rule("/", endpoint="index"),
Rule("/auth/server", endpoint="auth_server"),
Rule("/announcement/new", endpoint="announcement_new"),
Rule("/announcement/listen/<id>", endpoint="announcement_listen"),
]
)
self.runtime = VOXRuntime()
self.runtime.loadConfig()
self.runtime.initialize()
self.baseurl = Path(self.config['http']['baseurl'])
self.basepath = Path(self.config['storage']['sounds'])
if self.basepath.is_dir():
log.info('Clearing sound storage at %s...', self.basepath)
def handle_index(self) -> Response:
return Response('This endpoint is not intended for human consumption')
MSG_403=json.dumps({
'error': True,
'source': 'server',
'message': 'Access Denied'
})
def make_403(self) -> Response:
return Response(self.MSG_403, status=403)
MSG_400=json.dumps({
'error': True,
'source': 'server',
'message': 'Bad Request'
})
def make_400(self) -> Response:
return Response(self.MSG_400, status=400)
def jsonify(self, data: dict, **kwargs) -> Response:
return Response(json.dumps(data, indent=None), mimetype='application/json', **kwargs)
def handle_auth_server(self, request: Request) -> Response:
if request.method == 'GET' and 'gsid' in request.args:
return self.jsonify({'challenge': generate_preshared_key()})
if request.method == 'POST' and 'response' in request.form and 'gsid' in request.form and 'challenge' in request.form:
challenge = request.form['challenge']
gsid = request.form['gsid']
response = request.form['response']
if gsid not in self.config['gameservers']:
log.info('Failed GS %r login from address %s', gsid, request.remote_addr)
return self.make_403()
gss = VOXGameServer(gsid)
gss.loadFrom(self.config)
expected = hashlib.md5((challenge+gss.secret_key).encode('utf-8')).hexdigest()
if response != expected:
log.info('Failed GS %r login from address %s (%s != %s)', gsid, request.remote_addr, response, expected)
return self.make_403()
gss.session_key = generate_preshared_key()
gss.remote_addr = request.remote_addr
self.gameservers[gss.id] = gss
gss.baseurl = f'{self.baseurl}/{gss.id}'
gss.basepath = self.basepath / gss.id
if gss.basepath.is_dir():
shutil.rmtree(gss.basepath)
gss.basepath.mkdir(parents=True)
log.info('[%s] Successfully logged in from address %s', gss.id, request.remote_addr)
return self.jsonify({
'ip': request.remote_addr,
'session': gss.session_key,
'limits': self.config['limits']
})
return self.make_400()
def handle_announcement_new(self, request: Request) -> Response:
if request.method != 'POST':
log.error('Not a POST request!')
return self.make_400()
for k in ['auth', 'phrase', 'voice', 'ckey', 'gsid']:
if k not in request.form:
log.error('%s missing from request.form (%r)', k, dict(request.form))
return self.make_400()
authkey = request.form['auth']
phrase = request.form['phrase']
voice = request.form['voice']
ckey = request.form['ckey']
srvid = request.form['gsid']
if srvid not in self.gameservers:
log.error('FAILED AUTH: %s tried to request an announcement as GSS %r, which doesn\'t exist.', request.remote_addr, srvid)
return self.make_403()
gss = self.gameservers[srvid]
if authkey != hashlib.md5((gss.session_key+phrase).encode('utf-8')).hexdigest():
log.error('FAILED AUTH: %r via %r requested phrase %r with voice %r', ckey, request.remote_addr, phrase, voice)
return self.make_403()
log.info('[%s]: %s requested phrase %r with voice %r', srvid, ckey, phrase, voice)
if voice not in ('mas', 'fem'):
return self.jsonify({'error': True, 'source': 'user', 'message': f'Incorrect voice.'})
phraselen = len(phrase)
limit = self.config['limits']['phraselen']['min']
if phraselen < limit:
return self.jsonify({'error': True, 'source': 'user', 'message': f'Too few characters. (your phraselen={phraselen}, limits.phraselen.min={limit})'})
limit = self.config['limits']['phraselen']['max']
if phraselen > limit:
return self.jsonify({'error': True, 'source': 'user', 'message': f'Too many characters. (your phraselen={phraselen}, limits.phraselen.max={limit})'})
p = Phrase()
p.phrase = phrase
p.parsed_phrase = phrase.split(' ')
p.wordlen = len(p.parsed_phrase)
limit = self.config['limits']['nwords']['min']
if p.wordlen < limit:
return self.jsonify({'error': True, 'source': 'user', 'message': f'Too few words. (your wordlen={phrase.wordlen}, limits.nwords.min={limit})'})
limit = self.config['limits']['nwords']['max']
if p.wordlen > limit:
return self.jsonify({'error': True, 'source': 'user', 'message': f'Too many words. (your wordlen={phrase.wordlen}, limits.nwords.max={limit})'})
wordlens = [len(w) for w in p.parsed_phrase]
maxwordlen = max(wordlens)
limit = self.config['limits']['wordlen']['max']
if maxwordlen > limit:
return self.jsonify({'error': True, 'source': 'user', 'message': f'A word was too big. (max(len(word), ...)={maxwordlen}, limits.wordlen.max={limit})'})
pr: PhraseRef = self.gameservers[srvid].getPhrase(voice, phrase)
if pr is None:
pr = gss.addPhrase(voice, p)
self.runtime.createSoundFromPhrase(p, self.runtime.getVoiceByGCode(voice), str(pr.path))
return self.jsonify({
'error': False,
'id': pr.id,
'url': pr.url,
})
def main():
global OTF_DIR, OTF_TMP_DIR#, OTF_SOUNDS_DIR
import argparse
argp = argparse.ArgumentParser()
argp.add_argument('--config', '-c', type=str, default=str(Path.cwd() / 'daemon.yml'), help='Path of daemon.yml')
argp.add_argument('--level', '-l', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='INFO', help='Log level to display')
argp.add_argument('--quiet', '-q', action='store_true', default=False, help='Silence console output')
args = argp.parse_args()
# Console logging
if not args.quiet:
ch = logging.StreamHandler()
ch.setLevel(getattr(logging,args.level))
ch.setFormatter(logging.Formatter('%(asctime)s [%(levelname)-8s] @ (%(name)s): %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p'))
logging.getLogger().addHandler(ch)
daemon_yml = Path(args.config)
if not os.path.isfile('daemon.yml'):
print('Run `python tools/otftool.py init` first, then edit daemon.yml.')
return
with open('daemon.yml', 'r') as f:
cfg = yaml.load(f)
OTF_DIR = cfg['storage']['work']
OTF_TMP_DIR = cfg['storage']['tmp']
GENERATED_SOUND_DIR = cfg['storage']['sounds']
app = VoxRESTService(cfg)
from werkzeug.serving import run_simple
run_simple(cfg['http']['address'], cfg['http']['port'], app, use_debugger=cfg['http'].get('debug', False), use_reloader=False)
if __name__ == '__main__':
main()