Simple-Tracker / qBittorrent-ClientBlocker

一款适用于 qBittorrent/Transmission (Beta)/BitComet (Beta, Partial) 的客户端屏蔽器, 默认屏蔽包括但不限于迅雷等客户端. A client blocker compatible with qBittorrent/Transmission (Beta)/BitComet (Beta, Partial) which is prohibited to include but not limited to clients such as Xunlei.
MIT License
840 stars 20 forks source link

systemd sdnotify的支持 #63

Closed soiamsoNG closed 3 months ago

soiamsoNG commented 3 months ago
  1. 增加readyCommand 运行于 [RunConsole] 程序已启动 之后
  2. 增加clientLostCommand 运行于 [Fetch] 获取时发生了错误 之后
  3. 增加clientLostThreshold 发生多少次后触发 clientLostCommand
  4. image里面添加 socat (image里面的nc 没有 -U参数,不能处理socket)

例子: readyCommand=echo "READY=1" | socat - UNIX-SENDTO:/run/notify/notify.sock clientLostCommand=echo "STOPPING=1" | socat - UNIX-SENDTO:/run/notify/notify.sock

Simple-Tracker commented 3 months ago

感谢反馈!

  1. readyCommand 可通过程序运行状态进行判定, 因此是没有必要的;
  2. 相比于 clientLost, 我更倾向于 fetchFailed, Fetch 不止用于 Client 交互, 还用于 IPBlockListURL 等功能;
  3. 加入 Threshold 很好, 不好的是如何确定次数重置的间隔. 暂时倾向于 10 倍 Interval (改为次数);
  4. 可以添加 socat;
soiamsoNG commented 3 months ago

systemd service Type=notify 的情况下 ,会一直等待 READY=1,未收到前会一直处于启动未完成状态(activating),需要一个位点发出 READY=1。

soiamsoNG commented 3 months ago

ExecCommand 没有 | 的escape, 导致 echo "STOPPING=1" | socat - UNIX-SENDTO:/run/notify/notify.sock 这种代码不能使用

ExecCommand 是否可以添加array类型支持,而不是只有string类型用 | 分割,

例子: execCommand_Run = ["sh","-c","echo \"READY=1\" | socat - UNIX-SENDTO:/run/notify/notify.sock"]

Simple-Tracker commented 3 months ago

因为 Golang 的原因, 管道需要套娃: https://studygolang.com/topics/10056 不过, 不清楚用什么符号分割命令及参数较好. 理论上, 用空格最为直观, 但也需要处理空格转义的问题

soiamsoNG commented 3 months ago

方向一,如下可以前面两个参数硬编码,这样就不用考虑现在的分割问题或转义问题, 也可以用分号输入多行命令。 exec.Command("/bin/sh", "-c", "这部分是execCommand设定的字符串")

方向二,也可以继续现在的分割方法, 见到 “ \| ” 不分割

方向三,ExecCommand识别json的type,string用旧方法,array就按顺序放到 exec.Command的arg里面。

Simple-Tracker commented 3 months ago

可测试此版本: https://github.com/Simple-Tracker/qBittorrent-ClientBlocker/actions/runs/8813068013

具体而言: 通过改为对字符串的逐字读取, 现在可以以更接近实际命令使用的空格作为分割符, 且若遇到 '" 时, 内部的空格不会被识别为分割符, 管道现也不会因为和分割符一样而被忽略. 但是, 出于上述提到的原因, 可能仍然需要以 sh 来套娃.

对转义做了简单的处理, 并在 RunConsole 开头插入一个具体的测试, 测试代码及结果如下:

    config.Debug = true
    _, out, _ := ExecCommand("/bin/sh -c 'echo Testing'")
    Log("Test", "%s", false, string(out))
    _, out, _ = ExecCommand("/bin/sh -c 'echo \"'Testing'\"'")
    Log("Test", "%s", false, string(out))
    _, out, _ = ExecCommand("/bin/sh -c 'echo '\\Testing...\\''")
    Log("Test", "%s", false, string(out))
    _, out, _ = ExecCommand("/bin/bash -c 'echo '\\Testing...\\''")
    Log("Test", "%s", false, string(out))
    _, out, _ = ExecCommand("/bin/bash -c 'echo '\\Testing...\\'|cat'")
    Log("Test", "%s", false, string(out))
    return
[2024-04-24 15:57:25][Debug-ExecCommand] Split (|): /bin/sh|-c|echo Testing.
[2024-04-24 15:57:25][Test] Testing
.
[2024-04-24 15:57:25][Debug-ExecCommand] Split (|): /bin/sh|-c|echo "Testing".
[2024-04-24 15:57:25][Test] Testing
.
[2024-04-24 15:57:25][Debug-ExecCommand] Split (|): /bin/sh|-c|echo \Testing...\'.
[2024-04-24 15:57:25][Test] Testing...'
.
[2024-04-24 15:57:25][Debug-ExecCommand] Split (|): /bin/bash|-c|echo \Testing...\'.
[2024-04-24 15:57:25][Test] Testing...'
.
[2024-04-24 15:57:25][Debug-ExecCommand] Split (|): /bin/bash|-c|echo \Testing...\'|cat.
[2024-04-24 15:57:25][Test] Testing...'
.
soiamsoNG commented 3 months ago

测试版本可用,要等docker版本再测试了

Simple-Tracker commented 3 months ago

3.3b6 已发布!

soiamsoNG commented 3 months ago

在3.3b6 container下测试以下配置可用

"execCommand_Run": "sh -c 'echo \"READY=1\" | socat - UNIX-SENDTO:/run/notify/notify.sock'",
"fetchFailedThreshold": 2,
"execCommand_FetchFailed": "sh -c 'echo \"STOPPING=1\" | socat - UNIX-SENDTO:/run/notify/notify.sock'"
Simple-Tracker commented 3 months ago

视作完成而关闭!