-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·381 lines (299 loc) · 13.2 KB
/
main.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-
# ██████╗██╗██████╗ ██████╗██╗ ███████╗███████╗
# ██╔════╝██║██╔══██╗██╔════╝██║ ██╔════╝██╔════╝
# ██║ ██║██████╔╝██║ ██║ █████╗ ███████╗
# ██║ ██║██╔══██╗██║ ██║ ██╔══╝ ╚════██║
# ╚██████╗██║██║ ██║╚██████╗███████╗███████╗███████║
# ╚═════╝╚═╝╚═╝ ╚═╝ ╚═════╝╚══════╝╚══════╝╚══════╝
import asyncio
import io
import os
import subprocess
import sys
from datetime import datetime
from pathlib import Path
import aiohttp
import aiomysql
import cmyui
import datadog
import geoip2.database
import orjson
from cmyui.logging import Ansi, log
import bg_loops
import utils.misc
from constants.privileges import Privileges
from objects.achievement import Achievement
from objects.collections import Channels, Clans, MapPools, Matches, Players
from objects.player import Player
from utils.updater import Updater
__all__ = ()
# we print utf-8 content quite often
if isinstance(sys.stdout, io.TextIOWrapper):
sys.stdout.reconfigure(encoding='utf-8')
# set cwd to /circles
os.chdir(os.path.dirname(os.path.realpath(__file__)))
try:
from objects import glob
except ModuleNotFoundError as exc:
if exc.name == 'config':
# config file doesn't exist; create it from the default.
import shutil
shutil.copy('ext/config.sample.py', 'config.py')
log('A config file has been generated, '
'please configure it to your needs.', Ansi.LRED)
raise SystemExit(1)
else:
raise
utils.misc.install_excepthook()
# current version of circles
# NOTE: this is used internally for the updater, it may be
# worth reading through it's code before playing with it.
glob.version = cmyui.Version(3, 5, 4)
OPPAI_PATH = Path.cwd() / 'oppai-ng'
GEOLOC_DB_FILE = Path.cwd() / 'ext/GeoLite2-City.mmdb'
DEBUG_HOOKS_PATH = Path.cwd() / '_testing/runtime.py'
DATA_PATH = Path.cwd() / '.data'
ACHIEVEMENTS_ASSETS_PATH = DATA_PATH / 'assets/medals/client'
async def setup_collections(db_cursor: aiomysql.DictCursor) -> None:
"""Setup & cache many global collections."""
# dynamic (active) sets, only in ram
glob.players = Players()
glob.matches = Matches()
# static (inactive) sets, in ram & sql
glob.channels = await Channels.prepare(db_cursor)
glob.clans = await Clans.prepare(db_cursor)
glob.pools = await MapPools.prepare(db_cursor)
# create bot & add it to online players
glob.bot = Player(
id=1,
name=await utils.misc.fetch_bot_name(db_cursor),
login_time=float(0x7fffffff), # (never auto-dc)
priv=Privileges.Normal,
bot_client=True
)
glob.players.append(glob.bot)
# global achievements (sorted by vn gamemodes)
glob.achievements = []
await db_cursor.execute('SELECT * FROM achievements')
async for row in db_cursor:
# NOTE: achievement conditions are stored as stringified python
# expressions in the database to allow for extensive customizability.
condition = eval(f'lambda score, mode_vn: {row.pop("cond")}')
achievement = Achievement(**row, cond=condition)
glob.achievements.append(achievement)
# static api keys
await db_cursor.execute(
'SELECT id, api_key FROM users '
'WHERE api_key IS NOT NULL'
)
glob.api_keys = {
row['api_key']: row['id']
async for row in db_cursor
}
async def before_serving() -> None:
"""Called before the server begins serving connections."""
glob.loop = asyncio.get_running_loop()
if glob.has_internet:
# retrieve a client session to use for http connections.
glob.http = aiohttp.ClientSession(
json_serialize=orjson.dumps) # type: ignore
else:
glob.http = None
# retrieve a pool of connections to use for mysql interaction.
glob.db = cmyui.AsyncSQLPool()
await glob.db.connect(glob.config.mysql)
# run the sql & submodule updater (uses http & db).
# TODO: updating cmyui_pkg should run before it's imported.
updater = Updater(glob.version)
await updater.run()
await updater.log_startup()
# open a connection to our local geoloc database,
# if the database file is present.
if GEOLOC_DB_FILE.exists():
glob.geoloc_db = geoip2.database.Reader(GEOLOC_DB_FILE)
else:
glob.geoloc_db = None
# support for https://datadoghq.com
if all(glob.config.datadog.values()):
datadog.initialize(**glob.config.datadog)
glob.datadog = datadog.ThreadStats()
glob.datadog.start(flush_in_thread=True,
flush_interval=15)
# wipe any previous stats from the page.
glob.datadog.gauge('circles.online_players', 0)
else:
glob.datadog = None
new_coros = []
# cache many global collections/objects from sql,
# such as channels, mappools, clans, bot, etc.
async with glob.db.pool.acquire() as conn:
async with conn.cursor(aiomysql.DictCursor) as db_cursor:
await setup_collections(db_cursor)
# create a task for each donor expiring in 30d.
new_coros.extend(await bg_loops.donor_expiry(db_cursor))
# setup a loop to kick inactive ghosted players.
new_coros.append(bg_loops.disconnect_ghosts())
'''
# if the surveillance webhook has a value, run
# automatic (still very primitive) detections on
# replays deemed by the server's configurable values.
if glob.config.webhooks['surveillance']:
new_coros.append(bg_loops.replay_detections())
'''
# reroll the bot's random status every `interval` sec.
new_coros.append(bg_loops.reroll_bot_status(interval=300))
for coro in new_coros:
glob.app.add_pending_task(coro)
async def after_serving() -> None:
"""Called after the server stops serving connections."""
if hasattr(glob, 'http') and glob.http is not None:
await glob.http.close()
if hasattr(glob, 'db') and glob.db.pool is not None:
await glob.db.close()
if hasattr(glob, 'geoloc_db') and glob.geoloc_db is not None:
glob.geoloc_db.close()
if hasattr(glob, 'datadog') and glob.datadog is not None:
glob.datadog.stop()
glob.datadog.flush()
def ensure_supported_platform() -> int:
"""Ensure we're running on an appropriate platform for circles."""
if sys.platform != 'linux':
log('circles currently only supports linux', Ansi.LRED)
if sys.platform == 'win32':
log("you could also try wsl(2), i'd recommend ubuntu 18.04 "
"(i use it to test circles)", Ansi.LBLUE)
return 1
if sys.version_info < (3, 9):
log('circles uses many modern python features, '
'and the minimum python version is 3.9.', Ansi.LRED)
return 1
return 0
def ensure_local_services_are_running() -> int:
"""Ensure all required services (mysql) are running."""
# NOTE: if you have any problems with this, please contact me
# @cmyui#0425/[email protected]. i'm interested in knowing
# how people are using the software so that i can keep it
# in mind while developing new features & refactoring.
if glob.config.mysql['host'] in ('localhost', '127.0.0.1', None):
# sql server running locally, make sure it's running
for service in ('mysqld', 'mariadb'):
if os.path.exists(f'/var/run/{service}/{service}.pid'):
break
else:
# not found, try pgrep
pgrep_exit_code = os.system('pgrep mysqld')
if pgrep_exit_code != 0:
log('Please start your mysqld server.', Ansi.LRED)
return 1
return 0
def ensure_directory_structure() -> int:
"""Ensure the .data directory and git submodules are ready."""
# create /.data and its subdirectories.
DATA_PATH.mkdir(exist_ok=True)
for sub_dir in ('avatars', 'logs', 'osu', 'osr', 'ss'):
subdir = DATA_PATH / sub_dir
subdir.mkdir(exist_ok=True)
if not ACHIEVEMENTS_ASSETS_PATH.exists():
if not glob.has_internet:
# TODO: make it safe to run without achievements
return 1
ACHIEVEMENTS_ASSETS_PATH.mkdir(parents=True)
utils.misc.download_achievement_images(ACHIEVEMENTS_ASSETS_PATH)
return 0
def ensure_dependencies_and_requirements() -> int:
"""Make sure all of circles's dependencies are ready."""
if not OPPAI_PATH.exists():
log('No oppai-ng submodule found, attempting to clone.', Ansi.LMAGENTA)
p = subprocess.Popen(args=['git', 'submodule', 'init'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
if exit_code := p.wait():
log('Failed to initialize git submodules.', Ansi.LRED)
return exit_code
p = subprocess.Popen(args=['git', 'submodule', 'update'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
if exit_code := p.wait():
log('Failed to update git submodules.', Ansi.LRED)
return exit_code
if not (OPPAI_PATH / 'liboppai.so').exists():
log('No oppai-ng library found, attempting to build.', Ansi.LMAGENTA)
p = subprocess.Popen(args=['./libbuild'], cwd='oppai-ng',
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
if exit_code := p.wait():
log('Failed to build oppai-ng automatically.', Ansi.LRED)
return exit_code
return 0
def __install_debugging_hooks() -> None:
"""Change internals to help with debugging & active development."""
if DEBUG_HOOKS_PATH.exists():
from _testing import runtime # type: ignore
runtime.setup()
def display_startup_dialog() -> None:
"""Print any general information or warnings to the console."""
if glob.config.advanced:
log('running in advanced mode', Ansi.LRED)
# running on root grants the software potentally dangerous and
# unnecessary power over the operating system and is not advised.
if os.geteuid() == 0:
log('It is not recommended to run circles as root, '
'especially in production..', Ansi.LYELLOW)
if glob.config.advanced:
log('The risk is even greater with features '
'such as config.advanced enabled.', Ansi.LRED)
if not glob.has_internet:
log('Running in offline mode, some features '
'will not be available.', Ansi.LRED)
def main() -> int:
for safety_check in (
ensure_supported_platform, # linux only at the moment
ensure_local_services_are_running, # mysql (if local)
ensure_directory_structure, # .data/ & achievements/ dir structure
ensure_dependencies_and_requirements # submodules & oppai-ng built
):
if (exit_code := safety_check()) != 0:
return exit_code
'''Server is safe to start up'''
glob.boot_time = datetime.now()
# install any debugging hooks from
# _testing/runtime.py, if present
__install_debugging_hooks()
# check our internet connection status
glob.has_internet = utils.misc.check_connection(timeout=1.5)
# show info & any contextual warnings.
display_startup_dialog()
# create the server object; this will handle http connections
# for us via the transport (tcp/ip) socket interface, and will
# handle housekeeping (setup, cleanup) for us automatically.
glob.app = cmyui.Server(
name=f'circles v{glob.version}',
gzip=4, debug=glob.config.debug
)
# add the domains and their respective endpoints to our server object
from domains.ava import domain as ava_domain # a.ppy.sh
from domains.cho import domain as cho_domain # c[e4-6]?.ppy.sh
from domains.map import domain as map_domain # b.ppy.sh
from domains.osu import domain as osu_domain # osu.ppy.sh
glob.app.add_domains({cho_domain, osu_domain,
ava_domain, map_domain})
# attach housekeeping tasks (setup, cleanup)
glob.app.before_serving = before_serving
glob.app.after_serving = after_serving
# run the server (this is a blocking call)
glob.app.run(addr=glob.config.server_addr,
handle_restart=True) # (using SIGUSR1)
return 0
if __name__ == '__main__':
raise SystemExit(main())
elif __name__ == 'main':
# check specifically for asgi servers since many related projects
# (such as circles-web) use them, so people may assume we do as well.
if utils.misc.running_via_asgi_webserver(sys.argv[0]):
raise RuntimeError(
"circles does not use an ASGI framework, and uses it's own custom "
"web framework implementation; please run it directly (./main.py)."
)
else:
raise RuntimeError('circles should only be run directly (./main.py).')