add event-hook for banning users

This commit is contained in:
ed 2023-07-13 22:29:32 +00:00
parent 50c7bba6ea
commit 22fc4bb938
5 changed files with 49 additions and 9 deletions

View file

@ -587,6 +587,7 @@ def get_sects():
\033[36mxbd\033[35m executes CMD before a file delete \033[36mxbd\033[35m executes CMD before a file delete
\033[36mxad\033[35m executes CMD after a file delete \033[36mxad\033[35m executes CMD after a file delete
\033[36mxm\033[35m executes CMD on message \033[36mxm\033[35m executes CMD on message
\033[36mxban\033[35m executes CMD if someone gets banned
\033[0m \033[0m
can be defined as --args or volflags; for example \033[36m can be defined as --args or volflags; for example \033[36m
--xau notify-send --xau notify-send
@ -622,6 +623,9 @@ def get_sects():
executed program on STDIN instead of as argv arguments, and executed program on STDIN instead of as argv arguments, and
it also includes the wark (file-id/hash) as a json property 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, 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 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 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("--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("--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("--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): def add_yolo(ap):

View file

@ -1047,7 +1047,8 @@ class AuthSrv(object):
flags[name] = True flags[name] = True
return 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: if value is True:
t = "└─add volflag [{}] = {} ({})" t = "└─add volflag [{}] = {} ({})"
else: else:
@ -1457,7 +1458,7 @@ class AuthSrv(object):
vol.flags["mth"] = self.args.mth vol.flags["mth"] = self.args.mth
# append additive args from argv to volflags # 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: for name in "mtp on404 on403".split() + hooks:
self._read_volflag(vol.flags, name, getattr(self.args, name), True) 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"] hfs = [x for x in hfs if x != "f"]
ocmd = ",".join(hfs + [cmd]) 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) ncmds.append(ocmd)
vol.flags[hn] = ncmds vol.flags[hn] = ncmds
@ -1857,7 +1862,8 @@ class AuthSrv(object):
] ]
csv = set("i p".split()) 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()) askip = set("a v c vc cgen theme".split())
# keymap from argv to vflag # keymap from argv to vflag

View file

@ -142,6 +142,7 @@ flagcats = {
"xbd=CMD": "execute CMD before a file delete", "xbd=CMD": "execute CMD before a file delete",
"xad=CMD": "execute CMD after a file delete", "xad=CMD": "execute CMD after a file delete",
"xm=CMD": "execute CMD on message", "xm=CMD": "execute CMD on message",
"xban=CMD": "execute CMD if someone gets banned",
}, },
"client and ux": { "client and ux": {
"grid": "show grid/thumbnails by default", "grid": "show grid/thumbnails by default",

View file

@ -602,8 +602,22 @@ class HttpCli(object):
if g.lim: if g.lim:
bonk, ip = g.bonk(self.ip, self.vpath) bonk, ip = g.bonk(self.ip, self.vpath)
if bonk: if bonk:
self.log("client banned: 404s", 1) xban = self.vn.flags.get("xban")
self.conn.hsrv.bans[ip] = bonk 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: if volsan:
vols = list(self.asrv.vfs.all_vols.values()) vols = list(self.asrv.vfs.all_vols.values())
@ -1324,7 +1338,7 @@ class HttpCli(object):
self.host, self.host,
self.uname, self.uname,
time.time(), time.time(),
len(xm), len(buf),
self.ip, self.ip,
time.time(), time.time(),
plain, plain,
@ -1996,8 +2010,22 @@ class HttpCli(object):
if g.lim: if g.lim:
bonk, ip = g.bonk(self.ip, pwd) bonk, ip = g.bonk(self.ip, pwd)
if bonk: if bonk:
self.log("client banned: invalid passwords", 1) xban = self.vn.flags.get("xban")
self.conn.hsrv.bans[ip] = bonk 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" msg = "naw dude"
pwd = "x" # nosec pwd = "x" # nosec

View file

@ -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" 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()}) 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()}) ka.update(**{k: [] for k in ex.split()})
super(Cfg, self).__init__( super(Cfg, self).__init__(