-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawpile-monitor.py
executable file
·1041 lines (913 loc) · 35.4 KB
/
drawpile-monitor.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# SPDX-License-Identifier: MIT
import argparse
import configparser
import contextlib
import enum
import functools
import logging
import os
import random
import re
import requests
import signal
import sqlite3
import sys
import time
logging.basicConfig(
level=getattr(logging, os.environ.get("DRAWPILE_MONITOR_LOG_LEVEL", "WARNING")),
stream=sys.stdout,
)
class NoWordsError(Exception):
pass
class WordlistChecker:
COMMENT_OR_BLANK_RE = re.compile(r"\A\s*(#.*\s*)?\Z")
ASTERISK_RE = re.compile(r"\A\*+|\*+\Z")
WHITESPACE_RE = re.compile(r"\s+")
def __init__(self, paths):
words = []
for path in paths:
logging.debug("Loading wordlist %s", path)
self.load_words(path, lambda line: self._load_word(words, line))
if not words:
raise NoWordsError()
self._re = re.compile("|".join(words), re.IGNORECASE)
@classmethod
def load_words(cls, path, fn):
with open(path, "r", encoding="UTF-8") as f:
for line in f:
if not cls.COMMENT_OR_BLANK_RE.search(line):
fn(line.strip())
@classmethod
def _load_word(cls, words, line):
prefix = "" if line.startswith("*") else r"\b"
suffix = "" if line.endswith("*") else r"\b"
words.append(
prefix
+ r"[\s\-_\.:;,]*".join(
map(re.escape, cls.WHITESPACE_RE.split(cls.ASTERISK_RE.sub("", line)))
)
+ suffix
)
def check(self, s):
return bool(self._re.search(s))
def init_wordlist_checker(wordlist_path, nsfm_wordlist_path):
global wordlist_checker
paths = [
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"profanity",
"profanity_wordlist.txt",
),
wordlist_path,
nsfm_wordlist_path,
]
wordlist_checker = WordlistChecker(filter(bool, paths))
def init_filter_allowed(allowlist_path):
global filter_allowed
filter_allowed = lambda s: s
if allowlist_path:
logging.debug("Loading allowlist %s", allowlist_path)
allowed_words = []
WordlistChecker.load_words(
allowlist_path, lambda word: allowed_words.append(word)
)
if allowed_words:
escaped_words = "|".join(map(re.escape, allowed_words))
allowed_re = re.compile(
f"(?:\\b|(?<=_))(?:{escaped_words})(?:\\b|(?=_))", re.IGNORECASE
)
logging.debug("Allowed re: /%s/", allowed_re)
def filter_allowed_fn(s):
return re.sub(allowed_re, "", s)
filter_allowed = filter_allowed_fn
else:
logging.warning("No words in allowlist %s", allowlist_path)
def init_is_offensive(min_offensive_probability):
global is_offensive
@functools.lru_cache(maxsize=16384)
def is_offensive_fn(s):
logging.debug("Checking profanity of '%s'", s)
filtered = filter_allowed(s)
return (
is_offensive_wordlist(filtered)
or is_offensive_profanity_check(filtered) >= min_offensive_probability
)
is_offensive = is_offensive_fn
def init_is_offensive_nsfm(nsfm_wordlist_path):
global is_offensive_nsfm
is_offensive_nsfm = lambda s: False
if nsfm_wordlist_path:
logging.debug("Loading nsfm wordlist %s", nsfm_wordlist_path)
try:
nsfm_wordlist_checker = WordlistChecker([nsfm_wordlist_path])
@functools.lru_cache(maxsize=16384)
def is_offensive_nsfm_fn(s):
logging.debug("Checking nsfm profanity of '%s'", s)
filtered = filter_allowed(s)
return nsfm_wordlist_checker.check(s)
is_offensive_nsfm = is_offensive_nsfm_fn
except NoWordsError:
logging.warning("No words in nsfm wordlist %s", nsfm_wordlist_path)
def is_offensive_wordlist(s):
return wordlist_checker.check(s)
def is_offensive_profanity_check(s):
import profanity_check
return profanity_check.predict_prob([s])[0]
def init_is_offensive_silent(silent_wordlist_path):
global is_offensive_silent
is_offensive_silent = lambda s: False
if silent_wordlist_path:
logging.debug("Loading silent wordlist %s", silent_wordlist_path)
try:
silent_wordlist_checker = WordlistChecker([silent_wordlist_path])
@functools.lru_cache(maxsize=16384)
def is_offensive_silent_fn(s):
logging.debug("Checking silent profanity of '%s'", s)
return silent_wordlist_checker.check(s)
is_offensive_silent = is_offensive_silent_fn
return True
except NoWordsError:
logging.warning("No words in silent wordlist %s", silent_wordlist_path)
return False
class HandleNsfm(enum.Enum):
FULL = 1
RELAXED = 2
@classmethod
def convert_from_string(cls, s):
sc = s.casefold()
if sc == "full":
return cls.FULL
elif sc == "relaxed":
return cls.RELAXED
else:
raise ValueError(f"Unknown handle_nsfm value '{s}'")
class NoticeFlags(enum.IntEnum):
OUTDATED = 0x1
def _convert_bool(s):
return s.casefold() not in ["false", "f", "no", "0", ""]
def _convert_discord_ids(s):
if s:
if re.search(r"\A\s*[0-9]+\s*(?:,\s*[0-9]+\s*)*\Z", s):
return [id.strip() for id in s.split(",")]
else:
raise ValueError(f"Invalid id list: {s}")
class Config:
def __init__(self, config_path, test_profanity_only=False):
if not config_path:
config_path = self._relative_to_script("config.ini")
self._has_error = False
parser = configparser.ConfigParser()
if not parser.read(config_path):
raise ValueError(f"Can't parse config '{config_path}'")
if not test_profanity_only:
self._read(parser, "config", "base_url", "base_url")
self._read(
parser, "config", "discord_webhook_url", "discord_webhook_url", None
)
self._read(
parser,
"config",
"database_path",
"database_path",
self._relative_to_script("drawpile-monitor.db"),
)
self._read(
parser,
"config",
"handle_nsfm",
"handle_nsfm",
HandleNsfm.FULL,
lambda s: HandleNsfm.convert_from_string(s),
)
self._read(
parser,
"config",
"nsfm_users_in_passworded_sessions",
"nsfm_users_in_passworded_sessions",
False,
convert=_convert_bool,
)
self._read(
parser,
"config",
"silent_user_mentions",
"silent_user_mentions",
[],
convert=_convert_discord_ids,
)
self._read(
parser,
"config",
"silent_role_mentions",
"silent_role_mentions",
[],
convert=_convert_discord_ids,
)
self._read(
parser,
"config",
"max_error_streak_before_report",
"reportable_error_streak",
convert=int,
)
self._read(
parser,
"config",
"warn_outdated_sessions",
"warn_outdated_sessions",
False,
convert=_convert_bool,
)
message_keys = [
"session_name_first_warning",
"session_name_second_warning",
"session_name_terminate",
"session_alias_terminate",
"session_founder_terminate",
"user_kick",
"session_outdated",
]
for message_key in message_keys:
message_attr = f"message_{message_key}"
self._read(parser, "messages", message_key, message_attr)
self._read(
parser,
"messages",
message_key + "_nsfm",
message_attr + "_nsfm",
getattr(self, message_attr),
)
self._read(parser, "config", "wordlist_path", "wordlist_path", None)
self._read(parser, "config", "nsfm_wordlist_path", "nsfm_wordlist_path", None)
self._read(parser, "config", "allowlist_path", "allowlist_path", None)
self._read(
parser, "config", "silent_wordlist_path", "silent_wordlist_path", None
)
self._read(
parser,
"config",
"min_offensive_probability",
"min_offensive_probability",
0.9,
convert=lambda v: float(v) / 100.0,
)
if self._has_error:
raise ValueError("Invalid configuration")
@staticmethod
def _relative_to_script(file):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), file)
def _read(
self, parser, config_section, config_key, attr_key, fallback=..., convert=...
):
try:
value = parser[config_section][config_key]
if len(value) > 0:
if convert is not Ellipsis:
value = convert(value)
setattr(self, attr_key, value)
else:
raise KeyError()
except KeyError:
if fallback is Ellipsis:
logging.critical(
"[%s][%s] not found in config", config_section, config_key
)
self._has_error = True
else:
logging.debug(
"[%s][%s] not found in config, using fallback %s",
config_section,
config_key,
repr(fallback),
)
setattr(self, attr_key, fallback)
def get_message(self, key, nsfm=False):
return getattr(self, f"message_{key}_nsfm" if nsfm else f"message_{key}")
class Api:
def __init__(self, dry, config, username, password):
self._dry = dry
self._base_url = config.base_url
self._discord_webhook_url = config.discord_webhook_url
self._auth = (username, password)
def _make_url(self, *args):
path = "/".join(requests.utils.quote(str(s)) for s in args)
return f"{self._base_url}/api/{path}"
def get_sessions(self):
return requests.get(self._make_url("sessions"), auth=self._auth).json()
def get_users(self):
return requests.get(self._make_url("users"), auth=self._auth).json()
def update_session(self, session_id, body):
if not self._dry:
return requests.put(
self._make_url("sessions", session_id), auth=self._auth, json=body
).json()
def terminate_session(self, session_id):
if not self._dry:
return requests.delete(
self._make_url("sessions", session_id), auth=self._auth
).json()
def kick_user(self, session_id, user_id):
if not self._dry:
return requests.delete(
self._make_url("sessions", session_id, user_id), auth=self._auth
).json()
def can_send_reports(self):
return bool(self._discord_webhook_url)
def send_report(self, message):
if self.can_send_reports():
self.send_notification(message)
else:
logging.debug("No Discord webhook url, not reporting anything")
def send_notification(self, message, user_mentions=None, role_mentions=None):
requests.post(
self._discord_webhook_url,
json={
"content": message,
"flags": 1 << 2, # suppress embeds
"allowed_mentions": {
"parse": [],
"users": user_mentions if user_mentions else [],
"roles": role_mentions if role_mentions else [],
},
},
)
class Database:
def __init__(self, dry, config):
self._dry = dry
path = config.database_path
logging.debug("Opening database '%s'", path)
self._con = sqlite3.connect(path)
self._create_tables()
self._apply_migrations()
def _create_tables(self):
with self._con as con:
con.execute(
"""
create table if not exists session_offense (
id integer primary key not null,
inserted_at text not null default current_timestamp,
session_id text not null,
offense text not null,
mitigation text not null)
"""
)
con.execute(
"""
create index if not exists
session_offense_session_id_idx
on session_offense (session_id)
"""
)
con.execute(
"""
create table if not exists user_offense (
id integer primary key not null,
inserted_at text not null default current_timestamp,
name text not null,
ip text not null,
offense text not null,
mitigation text not null)
"""
)
con.execute(
"""
create table if not exists session_notices (
session_id text not null,
flags integer not null)
"""
)
con.execute(
"""
create table if not exists session_silent_notification (
id integer primary key not null,
inserted_at text not null default current_timestamp,
session_id text not null,
offense text not null)
"""
)
con.execute(
"""
create unique index if not exists
session_silent_notification_session_id_offense_idx
on session_silent_notification (session_id, offense)
"""
)
con.execute(
"""
create table if not exists migrations (
id integer primary key not null)
"""
)
def _apply_migrations(self):
migrations = [
(
1,
[
"""
alter table user_offense
add column sid text
""",
"""
alter table user_offense
add column is_registered integer not null default 0
""",
],
),
]
for migration_id, sqls in migrations:
with self._con as con:
cur = con.execute("begin")
cur.execute("select 1 from migrations where id = ?", (migration_id,))
if not cur.fetchone():
for sql in sqls:
cur.execute(sql)
cur.execute(
"insert into migrations (id) values (?)", (migration_id,)
)
def count_session_offenses(self):
with contextlib.closing(self._con.cursor()) as cur:
cur.execute("select count (*) from session_offense")
return cur.fetchone()[0]
def count_session_offenses_by_session_id(self, session_id):
with contextlib.closing(self._con.cursor()) as cur:
cur.execute(
"select count (*) from session_offense where session_id = ?",
(session_id,),
)
return cur.fetchone()[0]
def insert_session_offense(self, session_id, offense, mitigation):
if not self._dry:
with self._con as con:
con.execute(
"""
insert into session_offense(session_id, offense, mitigation)
values (?, ?, ?)
""",
(session_id, offense, mitigation),
)
def insert_user_offense(
self, user_name, user_ip, user_sid, user_is_registered, offense, mitigation
):
if not self._dry:
with self._con as con:
con.execute(
"""
insert into user_offense(name, ip, sid, is_registered, offense, mitigation)
values (?, ?, ?, ?, ?, ?)
""",
(
user_name,
user_ip,
user_sid,
1 if user_is_registered else 0,
offense,
mitigation,
),
)
def has_session_silent_notification(self, session_id, offense):
with contextlib.closing(self._con.cursor()) as cur:
cur.execute(
"""
select 1 from session_silent_notification
where session_id = ? and offense = ?
""",
(session_id, offense),
)
return bool(cur.fetchone())
def insert_session_silent_notification(self, session_id, offense):
if not self._dry:
with self._con as con:
con.execute(
"""
insert into session_silent_notification (session_id, offense)
values (?, ?)
""",
(session_id, offense),
)
def get_session_notices(self):
session_notices = {}
with contextlib.closing(self._con.cursor()) as cur:
cur.execute("select session_id, flags from session_notices")
while row := cur.fetchone():
session_notices[row[0]] = row[1]
return session_notices
def replace_session_notices(self, session_notices):
if not self._dry:
params = []
for session_id, notice_flags in session_notices.items():
params.append({"session_id": session_id, "flags": notice_flags})
with self._con as con:
cur = con.execute("begin")
cur.execute("delete from session_notices")
cur.executemany(
"""
insert into session_notices (session_id, flags)
values (:session_id, :flags)
""",
params,
)
class InterruptDisabled:
def __init__(self):
self._received_interrupt = False
def _handle_signal(self, *args):
self._received_interrupt = True
logging.critical("Ate interrupt signal, terminating soon")
def __enter__(self, *args):
signal.signal(signal.SIGINT, self._handle_signal)
return self
def __exit__(self, *args):
signal.signal(signal.SIGINT, signal.SIG_DFL)
if self._received_interrupt:
raise KeyboardInterrupt("Finally interrupting")
class Monitor:
def __init__(self, dry, config, api, db, have_silent_wordlist):
self._dry = dry
self._config = config
self._api = api
self._db = db
self._have_silent_notifications = (
have_silent_wordlist and api.can_send_reports()
)
self._reports = []
self._error_streak = 0
if dry:
self._reports_dedupe = set()
def _append_report(self, message):
if dry:
if message in self._reports_dedupe:
logging.debug("Skipping duplicate notification: %s", message)
return
self._reports_dedupe.add(message)
if self._reports:
new_report = self._reports[-1] + "\n" + message
if len(new_report.encode()) > 1500:
self._reports.append(message)
else:
self._reports[-1] = new_report
else:
self._reports.append(message)
def _get_offensive_fn(self, nsfm):
if nsfm and self._config.handle_nsfm == HandleNsfm.RELAXED:
return is_offensive_nsfm
else:
return is_offensive
# Sessions
def _send_session_silent_notification(self, session_id, offense):
if self._db.has_session_silent_notification(session_id, offense):
logging.debug("Session already has silent notification, skipping")
else:
prefix = ""
if self._dry:
if offense in self._reports_dedupe:
logging.debug("Skipping duplicate silent notification: %s", offense)
return
self._reports_dedupe.add(offense)
prefix += "**DRY RUN** "
user_mentions = self._config.silent_user_mentions
for user_mention in user_mentions:
prefix += f"<@{user_mention}> "
role_mentions = self._config.silent_role_mentions
for role_mention in role_mentions:
prefix += f"<@&{role_mention}> "
self._api.send_notification(
f"{prefix}**Attention required:** {offense}, session id `{session_id}`",
user_mentions=user_mentions,
role_mentions=role_mentions,
)
self._db.insert_session_silent_notification(session_id, offense)
def _check_session_silent_notification(
self, session_id, session_title, session_alias, session_founder
):
if self._have_silent_notifications:
if is_offensive_silent(session_title):
self._send_session_silent_notification(
session_id,
f"title of session '{session_title}'",
)
return True
elif session_alias and is_offensive_silent(session_alias):
self._send_session_silent_notification(
session_id,
f"alias '{session_alias}' of session '{session_title}'",
)
return True
elif session_founder and is_offensive_silent(session_founder):
self._send_session_silent_notification(
session_id,
f"founder '{session_founder}' of session '{session_title}'",
)
return True
return False
def _generate_clean_title(self):
# The number of offenses is a reasonably unique and small number.
return f"Session #{self._db.count_session_offenses()}"
def _manipulate_offensive_session(
self, session_id, offense, mitigation, alert, terminate
):
logging.warning("session %s: %s", session_id, mitigation)
self._db.insert_session_offense(session_id, offense, mitigation)
self._api.update_session(
session_id,
{
"alert": alert,
"title": self._generate_clean_title(),
},
)
if terminate:
self._api.terminate_session(session_id)
self._append_report(f"Session {session_id}: {offense} - {mitigation}")
def _handle_offensive_session_name(self, session_id, offense, nsfm):
past_offenses = self._db.count_session_offenses_by_session_id(session_id)
if past_offenses == 0:
self._manipulate_offensive_session(
session_id,
offense,
"warn nicely, rename session",
self._config.get_message("session_name_first_warning", nsfm),
False,
)
elif past_offenses == 1:
self._manipulate_offensive_session(
session_id,
offense,
"warn threateningly, rename session",
self._config.get_message("session_name_second_warning", nsfm),
False,
)
else:
self._manipulate_offensive_session(
session_id,
offense,
"terminate session",
self._config.get_message("session_name_terminate", nsfm),
True,
)
def _check_session(self, session):
session_id = session["id"]
logging.debug("Check session %s", session_id)
if session.get("protocol") in ("dp:4.21.2", "dp:4.22.2", "dp:4.23.0"):
notice_flags = NoticeFlags.OUTDATED
else:
notice_flags = 0
session_title = session["title"]
session_alias = session.get("alias", "")
session_founder = session.get("founder", "")
nsfm = session.get("nsfm", False)
passworded = session.get("hasPassword", False)
if not self._check_session_silent_notification(
session_id, session_title, session_alias, session_founder
):
offense_suffix = " (nsfm)" if nsfm else ""
check_offensive = self._get_offensive_fn(nsfm)
if session_alias and check_offensive(session_alias):
logging.warning("Session alias is offensive: %s", session)
self._manipulate_offensive_session(
session_id,
f"offensive alias '{session_alias}'{offense_suffix}",
"terminate session",
self._config.get_message("session_alias_terminate", nsfm),
True,
)
elif session_founder and check_offensive(session_founder):
logging.warning("Session founder is offensive: %s", session)
self._manipulate_offensive_session(
session_id,
f"offensive founder '{session_founder}'{offense_suffix}",
"terminate session",
self._config.get_message("session_founder_terminate", nsfm),
True,
)
elif check_offensive(session_title):
logging.warning("Session is offensive: %s", session)
self._handle_offensive_session_name(
session_id,
f"offensive title '{session_title}'{offense_suffix}",
nsfm,
)
else:
logging.debug(
"Session title '%s', alias '%s', founder '%s' are okay%s",
session_title,
session_alias,
session_founder,
offense_suffix,
)
return (session_id, nsfm, passworded, notice_flags)
def _notify_session(self, session_id, notice_flags):
if (notice_flags & int(NoticeFlags.OUTDATED)) != 0:
logging.warning("Notifying outdated session %s", session_id)
self._api.update_session(
session_id, {"alert": self._config.get_message("session_outdated")}
)
self._append_report(f"Session {session_id} on old version, notifying")
# Users
def _handle_offensive_user(self, user, offense, mitigation, nsfm):
user_name = user["name"]
user_ip = user["ip"]
user_id = user["id"]
session_id = user["session"]
logging.warning("user %s at %s: %s", user_name, user_ip, mitigation)
self._db.insert_user_offense(
user_name, user_ip, user.get("s"), user.get("auth"), offense, mitigation
)
self._api.update_session(
session_id,
{"alert": self._config.get_message("user_kick", nsfm)},
)
self._api.kick_user(session_id, user_id)
self._append_report(f"User {user_name}: {offense} - {mitigation}")
def _check_user(self, user, nsfm_sessions):
user_name = user["name"]
if user.get("mod"):
logging.debug("User '%s' is mod, skipping", user_name)
return
session_id = user.get("session")
if not session_id:
logging.debug("User '%s' is not in any session, skipping", user_name)
return
nsfm = nsfm_sessions.get(session_id)
if nsfm is None:
logging.warning(
"User '%s' is in unknown session '%s', treating as NSFM",
user_name,
session_id,
)
nsfm = True
offense_suffix = " (nsfm)" if nsfm else ""
check_offensive = self._get_offensive_fn(nsfm)
if check_offensive(user_name):
logging.warning("User is offensive: %s", user)
self._handle_offensive_user(
user,
f"offensive name '{user_name}'{offense_suffix}",
"warn in session, kick user",
nsfm,
)
else:
logging.debug("User name '%s' is okay%s", user_name, offense_suffix)
# Main entry
def check(self):
have_error = False
report_errors = self._error_streak == self._config.reportable_error_streak
logging.debug(
"Checking; report_errors=%s error_streak=%s reportable_error_streak=%s",
report_errors,
self._error_streak,
self._config.reportable_error_streak,
)
warn_outdated_sessions = self._config.warn_outdated_sessions
if warn_outdated_sessions:
prev_session_notices = self._db.get_session_notices()
else:
prev_session_notices = {}
try:
sessions = self._api.get_sessions()
except Exception:
sessions = []
logging.exception("Error retrieving sessions altogether")
have_error = True
if report_errors:
self._append_report("Error retrieving sessions")
nsfm_sessions = {}
session_notices = {}
for session in sessions:
with InterruptDisabled():
try:
session_id, nsfm, passworded, notice_flags = self._check_session(
session
)
nsfm_sessions[str(session_id)] = nsfm or (
passworded and self._config.nsfm_users_in_passworded_sessions
)
if notice_flags != 0:
session_notices[str(session_id)] = int(notice_flags)
except Exception:
logging.exception("Error checking session %s", session)
have_error = True
if report_errors:
self._append_report(f"Error checking session {session}")
try:
users = self._api.get_users()
except Exception:
users = []
logging.exception("Error retrieving users altogether")
have_error = True
if report_errors:
self._append_report("Error retrieving users")
for user in users:
with InterruptDisabled():
try:
self._check_user(user, nsfm_sessions)
except Exception:
logging.exception("Error checking user %s", user)
have_error = True
if report_errors:
self._append_report(f"Error checking user {user}")
if warn_outdated_sessions and not have_error:
with InterruptDisabled():
for session_id, notice_flags in session_notices.items():
try:
if session_id in prev_session_notices:
logging.debug("Session %s already notified", session_id)
else:
self._notify_session(session_id, notice_flags)
except Exception:
logging.exception("Error notifying session %s", session_id)
have_error = True
if report_errors:
self._append_report(f"Error notifying session {session_id}")
if prev_session_notices or session_notices:
self._db.replace_session_notices(session_notices)
if report_errors and have_error:
self._append_report(
f"There have been {self._error_streak + 1} errors in a row. "
+ "Something might be wrong with your server. "
+ "Will report when getting a good run again."
)
elif (
not have_error and self._error_streak > self._config.reportable_error_streak
):
self._append_report(
"Got a good run, looks like your server is healthy again."
)
if have_error:
self._error_streak += 1
else:
self._error_streak = 0
def report(self):
while self._reports:
with InterruptDisabled():
try:
message = self._reports[0]
if self._dry:
message = "**DRY RUN:** " + message
logging.debug("Send report %s", message)
self._api.send_report(message)
self._reports.pop(0)
except Exception:
logging.exception("Error sending report to webhook")
break
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog="Drawpile Monitor", description="Monitors sessions on a Drawpile server."
)
parser.add_argument(
"-c",
"--config",
help="Where to find the config file",
)
parser.add_argument(
"-n",
"--dryrun",
action=argparse.BooleanOptionalAction,
help="Don't actually change anything, only print logs and report to Discord",
default=False,
)
parser.add_argument(
"-i",
"--interval",
type=int,
help="How many seconds to wait between each request, 0 means to only do one",
default=0,
)
parser.add_argument(
"-j",
"--jitter",
type=int,
help="Up to how many seconds to randomly add to the interval",