Skip to content

Commit

Permalink
allow zeromq to veto uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
9001 committed Jan 25, 2025
1 parent f38c754 commit 3a5c1d9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
7 changes: 6 additions & 1 deletion bin/zmq-recv.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
run this script with "rep" and run copyparty with this:
--xm t3,zmq:req:tcp://localhost:5555
note: to conditionally block uploads based on message contents,
use rep_server to answer with "return 1" and run copyparty with
--xau t3,c,zmq:req:tcp://localhost:5555
"""


Expand Down Expand Up @@ -56,7 +59,9 @@ def rep_server():
sck.bind("tcp://*:5555")
while True:
print("copyparty says %r" % (sck.recv_string(),))
sck.send(b"thx")
reply = b"thx"
# reply = b"return 1" # non-zero to block an upload
sck.send(reply)


mode = sys.argv[1].lower() if len(sys.argv) > 1 else ""
Expand Down
18 changes: 10 additions & 8 deletions copyparty/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3482,7 +3482,7 @@ def _zmq_hook(
msg: str,
wait: float,
sp_ka: dict[str, Any],
) -> str:
) -> tuple[int, str]:
import zmq

try:
Expand All @@ -3493,6 +3493,7 @@ def _zmq_hook(
mtx = ZMQ["mtx"]

ret = ""
nret = 0
t0 = time.time()
if verbose and log:
log("hook(%s) %r entering zmq-main-lock" % (src, cmd), 6)
Expand Down Expand Up @@ -3554,6 +3555,10 @@ def _zmq_hook(
log("hook(%s) %r awaiting ack from req" % (src, cmd), 6)
try:
ret = sck.recv().decode("utf-8", "replace")
if ret.startswith("return "):
m = re.search("^return ([0-9]+)", ret[:12])
if m:
nret = int(m.group(1))
except:
sck.close()
del ZMQ[cmd] # bad state; must reset
Expand All @@ -3567,7 +3572,7 @@ def _zmq_hook(
if wait > 0:
time.sleep(wait)

return ret
return nret, ret


def _runhook(
Expand Down Expand Up @@ -3614,12 +3619,9 @@ def _runhook(
arg = txt or ap

if acmd[0].startswith("zmq:"):
zs = "zmq-error"
try:
zs = _zmq_hook(log, verbose, src, acmd[0][4:].lower(), arg, wait, sp_ka)
except Exception as ex:
if log:
log("zeromq failed: %r" % (ex,))
zi, zs = _zmq_hook(log, verbose, src, acmd[0][4:].lower(), arg, wait, sp_ka)
if zi:
raise Exception("zmq says %d" % (zi,))
return {"rc": 0, "stdout": zs}

acmd += [arg]
Expand Down
2 changes: 1 addition & 1 deletion tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def __init__(self, a=None, v=None, c=None, **ka0):
ex = "hash_mt hsortn safe_dedup srch_time u2abort u2j u2sz"
ka.update(**{k: 1 for k in ex.split()})

ex = "au_vol dl_list mtab_age reg_cap s_thead s_tbody th_convt"
ex = "au_vol dl_list mtab_age reg_cap s_thead s_tbody th_convt ups_who"
ka.update(**{k: 9 for k in ex.split()})

ex = "db_act k304 loris no304 re_maxage rproxy rsp_jtr rsp_slp s_wr_slp snap_wri theme themes turbo"
Expand Down

0 comments on commit 3a5c1d9

Please sign in to comment.