From 22fc4bb9382fe045859c1742d11e05da8abf970c Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 13 Jul 2023 22:29:32 +0000 Subject: [PATCH] add event-hook for banning users --- copyparty/__main__.py | 5 +++++ copyparty/authsrv.py | 12 +++++++++--- copyparty/cfg.py | 1 + copyparty/httpcli.py | 38 +++++++++++++++++++++++++++++++++----- tests/util.py | 2 +- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/copyparty/__main__.py b/copyparty/__main__.py index 2a76aea5..d2195e71 100755 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -587,6 +587,7 @@ def get_sects(): \033[36mxbd\033[35m executes CMD before a file delete \033[36mxad\033[35m executes CMD after a file delete \033[36mxm\033[35m executes CMD on message + \033[36mxban\033[35m executes CMD if someone gets banned \033[0m can be defined as --args or volflags; for example \033[36m --xau notify-send @@ -622,6 +623,9 @@ def get_sects(): executed program on STDIN instead of as argv arguments, and it also includes the wark (file-id/hash) as a json property + \033[36mxban\033[0m can be used to overrule / cancel a user ban event; + if the program returns 0 (true/OK) then the ban will NOT happen + except for \033[36mxm\033[0m, only one hook / one action can run at a time, so it's recommended to use the \033[36mf\033[0m flag unless you really need to wait for the hook to finish before continuing (without \033[36mf\033[0m @@ -920,6 +924,7 @@ def add_hooks(ap): ap2.add_argument("--xbd", metavar="CMD", type=u, action="append", help="execute CMD before a file delete") ap2.add_argument("--xad", metavar="CMD", type=u, action="append", help="execute CMD after a file delete") ap2.add_argument("--xm", metavar="CMD", type=u, action="append", help="execute CMD on message") + ap2.add_argument("--xban", metavar="CMD", type=u, action="append", help="execute CMD if someone gets banned (pw/404)") def add_yolo(ap): diff --git a/copyparty/authsrv.py b/copyparty/authsrv.py index ca42ba40..64904831 100644 --- a/copyparty/authsrv.py +++ b/copyparty/authsrv.py @@ -1047,7 +1047,8 @@ class AuthSrv(object): flags[name] = True return - if name not in "mtp xbu xau xiu xbr xar xbd xad xm on404 on403".split(): + zs = "mtp on403 on404 xbu xau xiu xbr xar xbd xad xm xban" + if name not in zs.split(): if value is True: t = "└─add volflag [{}] = {} ({})" else: @@ -1457,7 +1458,7 @@ class AuthSrv(object): vol.flags["mth"] = self.args.mth # append additive args from argv to volflags - hooks = "xbu xau xiu xbr xar xbd xad xm".split() + hooks = "xbu xau xiu xbr xar xbd xad xm xban".split() for name in "mtp on404 on403".split() + hooks: self._read_volflag(vol.flags, name, getattr(self.args, name), True) @@ -1480,6 +1481,10 @@ class AuthSrv(object): hfs = [x for x in hfs if x != "f"] ocmd = ",".join(hfs + [cmd]) + if "c" not in hfs and "f" not in hfs and hn == "xban": + hfs = ["c"] + hfs + ocmd = ",".join(hfs + [cmd]) + ncmds.append(ocmd) vol.flags[hn] = ncmds @@ -1857,7 +1862,8 @@ class AuthSrv(object): ] csv = set("i p".split()) - lst = set("c ihead mtm mtp xad xar xau xiu xbd xbr xbu xm on404 on403".split()) + zs = "c ihead mtm mtp on403 on404 xad xar xau xiu xban xbd xbr xbu xm" + lst = set(zs.split()) askip = set("a v c vc cgen theme".split()) # keymap from argv to vflag diff --git a/copyparty/cfg.py b/copyparty/cfg.py index d541dbcb..818955fd 100644 --- a/copyparty/cfg.py +++ b/copyparty/cfg.py @@ -142,6 +142,7 @@ flagcats = { "xbd=CMD": "execute CMD before a file delete", "xad=CMD": "execute CMD after a file delete", "xm=CMD": "execute CMD on message", + "xban=CMD": "execute CMD if someone gets banned", }, "client and ux": { "grid": "show grid/thumbnails by default", diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index bd74f5e0..5de3da00 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -602,8 +602,22 @@ class HttpCli(object): if g.lim: bonk, ip = g.bonk(self.ip, self.vpath) if bonk: - self.log("client banned: 404s", 1) - self.conn.hsrv.bans[ip] = bonk + xban = self.vn.flags.get("xban") + if not xban or not runhook( + self.log, + xban, + self.vn.canonical(self.rem), + self.vpath, + self.host, + self.uname, + time.time(), + 0, + self.ip, + time.time(), + "404", + ): + self.log("client banned: 404s", 1) + self.conn.hsrv.bans[ip] = bonk if volsan: vols = list(self.asrv.vfs.all_vols.values()) @@ -1324,7 +1338,7 @@ class HttpCli(object): self.host, self.uname, time.time(), - len(xm), + len(buf), self.ip, time.time(), plain, @@ -1996,8 +2010,22 @@ class HttpCli(object): if g.lim: bonk, ip = g.bonk(self.ip, pwd) if bonk: - self.log("client banned: invalid passwords", 1) - self.conn.hsrv.bans[ip] = bonk + xban = self.vn.flags.get("xban") + if not xban or not runhook( + self.log, + xban, + self.vn.canonical(self.rem), + self.vpath, + self.host, + self.uname, + time.time(), + 0, + self.ip, + time.time(), + "pw", + ): + self.log("client banned: invalid passwords", 1) + self.conn.hsrv.bans[ip] = bonk msg = "naw dude" pwd = "x" # nosec diff --git a/tests/util.py b/tests/util.py index 2dc313ce..3aa53ab4 100644 --- a/tests/util.py +++ b/tests/util.py @@ -116,7 +116,7 @@ class Cfg(Namespace): ex = "ah_alg doctitle favico html_head lg_sbf log_fk md_sbf mth textfiles unlist R RS SR" ka.update(**{k: "" for k in ex.split()}) - ex = "on403 on404 xad xar xau xbd xbr xbu xiu xm" + ex = "on403 on404 xad xar xau xban xbd xbr xbu xiu xm" ka.update(**{k: [] for k in ex.split()}) super(Cfg, self).__init__(