Improve ShadowBan

This commit is contained in:
Simple-Tracker 2024-09-09 14:55:40 +08:00
parent 7a3242f1c3
commit 097ce30db5
8 changed files with 48 additions and 37 deletions

View File

@ -156,6 +156,7 @@ Docker version is configured through the same name variable configuration, which
| clientUsername | string | Empty | Client username. Leaving it blank will skip authentication. If you enable client "Skip local client authentication", you can leave it blank by default, because the client config file can be automatically read and set |
| clientPassword | string | Empty | Client password. If client "Skip local client authentication" is enabled, it can be left blank by default |
| useBasicAuth | bool | false | Also authenticates via HxTTP Basic Auth. Suitable for backend that only support Basic Auth (such as Transmission/BitComet) or add/change authentication methods via reverse proxy, etc |
| useShadowBan | bool | true | Use ShadowBan API to ban peer. Only works with clients that support ShadowBan API (such as qBittorrent). If the relevant API is not available when loading the configuration, it will be automatically disabled |
| skipCertVerification | bool | false | Skip Web API certificate verification. Suitable for self-signed and expired certificates |
| fetchFailedThreshold | int | 0 (Disable) | Maximum number of fetch failures. When set number is exceeded, execCommand_FetchFailed will be executed |
| execCommand_FetchFailed | string | Empty | Execute external command (FetchFailed). First parameter is regarded as an external program path, execute the command when number of fetch failures exceeds set threshold |
@ -187,7 +188,6 @@ Docker version is configured through the same name variable configuration, which
| banByRelativePUStartPrecent | float64 | 2 (%) | Enhanced automatic blocking_Relative/Start progress. If the relative upload progress of the client is greater than the set start progress, Peer will be automatically block |
| banByRelativePUAntiErrorRatio | float64 | 3 (X) | Enhanced automatic blocking_Relative/Lag anti-misjudgment ratio. If the relative download progress obtained by the product of the relative download progress reported by the peer and the set ratio is lower than the relative upload progress of the client, Peer will be automatically block |
| ignoreByDownloaded | uint32 | 100 (MB) | Enhanced automatic blocking*/Max downloaded. If downloaded from Peer is greater than this value, enhanced automatic blocking will be skipped |
| ShadowBan | bool | false | Use Shadow Ban API block peers。This setting only works with client which has ShadowBan API.And it will check if Shadow Ban API available while loading config, with ignoring this setting if unavailabled.
## 反馈 Feedback
User and developer can report bug through [Issue](https://github.com/Simple-Tracker/qBittorrent-ClientBlocker/issues), ask/discuss/share usage through [Discussion](https://github.com/Simple-Tracker/qBittorrent-ClientBlocker/discussions), contribute code improvement to blocker through [Pull Request](https://github.com/Simple-Tracker/qBittorrent-ClientBlocker/pulls).

View File

@ -156,6 +156,7 @@ Docker 版本通过相同名称的环境变量配置, 通过自动转换环境
| clientUsername | string | 空 | 客户端账号. 留空会跳过认证. 若启用客户端内 "跳过本机客户端认证" 可默认留空, 因可自动读取客户端配置文件并设置 |
| clientPassword | string | 空 | 客户端密码. 若启用客户端内 "跳过本机客户端认证" 可默认留空 |
| useBasicAuth | bool | false (禁用) | 同时通过 HTTP Basic Auth 进行认证. 适合只支持 Basic Auth (如 Transmission/BitComet) 或通过反向代理等方式 增加/换用 认证方式的后端 |
| useShadowBan | bool | true (启用) | 使用 ShadowBan API 进行封禁. 仅适用于支持 ShadowBan API 的客户端 (如 qBittorrent), 若载入配置时检测相关 API 不可用, 将自动禁用 |
| skipCertVerification | bool | false (禁用) | 跳过 Web API 证书校验. 适合自签及过期证书 |
| fetchFailedThreshold | int | 0 (禁用) | 最大获取失败次数. 当超过设定次数, 将执行设置的外部命令 |
| execCommand_FetchFailed | string | 空 | 执行外部命令 (FetchFailed). 首个参数被视作外部程序路径, 当获取失败次数超过设定次数后执行 |
@ -186,7 +187,6 @@ Docker 版本通过相同名称的环境变量配置, 通过自动转换环境
| banByRelativePUStartPrecent | float64 | 2 (%) | 增强自动屏蔽_相对/起始进度. 若客户端相对上传进度大于设置起始进度, 则允许屏蔽 Peer |
| banByRelativePUAntiErrorRatio | float64 | 3 (X) | 增强自动屏蔽_相对/滞后防误判倍率. 若 Peer 报告相对下载进度与设置倍率之乘积得到之相对下载进度 比 客户端相对上传进度 还低, 则允许屏蔽 Peer |
| ignoreByDownloaded | uint32 | 100 (MB) | 增强自动屏蔽*/最高下载量. 若从 Peer 下载量大于此项, 则跳过增强自动屏蔽 |
| ShadowBan | bool | false | 尝试使用Shadow Ban API进行封禁。仅对部分具有ShadowBan API的客户端生效并会在载入配置时检测客户端相关API是否可用若不可用则会忽略该参数。
## 反馈 Feedback
用户及开发者可以通过 [Issue](https://github.com/Simple-Tracker/qBittorrent-ClientBlocker/issues) 反馈 bug, 通过 [Discussion](https://github.com/Simple-Tracker/qBittorrent-ClientBlocker/discussions) 提问/讨论/分享 使用方法, 通过 [Pull Request](https://github.com/Simple-Tracker/qBittorrent-ClientBlocker/pulls) 向客户端屏蔽器贡献代码改进.

View File

@ -126,7 +126,7 @@ func SubmitBlockPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
switch currentClientType {
case "qBittorrent":
if config.ShadowBan {
if config.UseShadowBan {
return qB_SubmitShadowBanPeer(blockPeerMap)
} else {
return qB_SubmitBlockPeer(blockPeerMap)
@ -137,3 +137,15 @@ func SubmitBlockPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
return false
}
func TestShadowBanAPI() int {
// -1: Unsupported (Error), 0: Unsupported (Silent), 1: Supported.
switch currentClientType {
case "qBittorrent":
if qB_TestShadowBanAPI() {
return 1
}
return -1
}
return 0
}

View File

@ -262,64 +262,64 @@ func qB_SubmitBlockPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
return true
}
func qb_GetPreferences() map[string]interface{} {
func qB_GetPreferences() map[string]interface{} {
_, _, responseBody := Submit(config.ClientURL+"/v2/app/preferences", "", true, true, nil)
if responseBody == nil {
Log("Fail-GetQBPreferences", GetLangText("Fail-GetQBPreferences"), true)
Log("GetPreferences", GetLangText("Failed-GetQBPreferences"), true)
return nil
}
var preferences map[string]interface{}
if err := json.Unmarshal(responseBody, &preferences); err != nil {
Log("Error-ParseQBPreferences", GetLangText("Error-Parse"), true, err.Error())
Log("GetPreferences", GetLangText("Error-Parse"), true, err.Error())
return nil
}
return preferences
}
func qb_TestShadowbanAPI() bool {
func qB_TestShadowBanAPI() bool {
// 1. Check if enable_shadowban is true;
// enable_shadowban may be not exist in the preferences.
pref := qb_GetPreferences()
pref := qB_GetPreferences()
if pref == nil {
return false
}
enableShadowban, ok := pref["shadow_ban_enabled"]
if !ok {
Log("Warn-ShadowbanAPINotExist", GetLangText("Warn-ShadowbanAPINotExist"), true)
enableShadowBan, exist := pref["shadow_ban_enabled"]
if !exist {
Log("TestShadowBanAPI", GetLangText("Warning-ShadowBanAPINotExist"), true)
return false
}
if bEnableShadowban, ok := enableShadowban.(bool); ok {
if !bEnableShadowban {
if bEnableShadowBan, ok := enableShadowBan.(bool); ok {
if !bEnableShadowBan {
return false
}
} else {
Log("Fail-UnknownShadowbanAPI", GetLangText("Fail-UnknownShadowbanAPI"), true)
Log("TestShadowBanAPI", GetLangText("Failed-UnknownShadowBanAPI"), true)
return false
}
// 2. Check if the API is available
// 2. Check if the API is available;
code, _, _ := Submit(config.ClientURL+"/v2/transfer/shadowbanPeers", "peers=", true, true, nil)
if code != 200 {
Log("Warn-ShadowbanAPINotExist", GetLangText("Warn-ShadowbanAPINotExist"), true)
Log("TestShadowBanAPI", GetLangText("Warning-ShadowBanAPINotExist"), true)
return false
}
return true
}
func qB_SubmitShadowBanPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
shadowBanIPPortsList := []string{}
for peerIP, peerInfo := range blockPeerMap {
for port := range peerInfo.Port {
if port <=0 || port > 65535 {
if port <= 0 || port > 65535 {
port = 1 // Seems qBittorrent will ignore the invalid port number, so we just set it to 1.
}
shadowBanIPPortsList = append(shadowBanIPPortsList, peerIP + ":" + strconv.Itoa(port))
}
}
banIPPortsStr := strings.Join(shadowBanIPPortsList, "|")
Log("Debug-SubmitShadowBanPeer", "%s", false, banIPPortsStr)
@ -338,4 +338,4 @@ func qB_SubmitShadowBanPeer(blockPeerMap map[string]BlockPeerInfoStruct) bool {
}
return true
}
}

View File

@ -50,6 +50,7 @@ type ConfigStruct struct {
ClientUsername string
ClientPassword string
UseBasicAuth bool
UseShadowBan bool
SkipCertVerification bool
FetchFailedThreshold int
ExecCommand_FetchFailed string
@ -80,7 +81,6 @@ type ConfigStruct struct {
BanByRelativePUStartMB uint32
BanByRelativePUStartPrecent float64
BanByRelativePUAntiErrorRatio float64
ShadowBan bool
}
var programName = "qBittorrent-ClientBlocker"
@ -165,6 +165,7 @@ var config = ConfigStruct{
ClientUsername: "",
ClientPassword: "",
UseBasicAuth: false,
UseShadowBan: true,
SkipCertVerification: false,
FetchFailedThreshold: 0,
ExecCommand_FetchFailed: "",
@ -195,7 +196,6 @@ var config = ConfigStruct{
BanByRelativePUStartMB: 20,
BanByRelativePUStartPrecent: 2,
BanByRelativePUAntiErrorRatio: 3,
ShadowBan: false,
}
func SetBlockListFromContent(blockListContent []string, blockListSource string) int {
@ -591,11 +591,9 @@ func LoadInitConfig(firstLoad bool) bool {
// 重置为上次使用的 URL, 主要目的是防止热重载配置文件可能破坏首次启动后从 qBittorrent 配置文件读取的 URL.
config.ClientURL = lastURL
}
if currentClientType == "qBittorrent" && config.ShadowBan {
if !qb_TestShadowbanAPI() {
Log("LoadInitConfig", GetLangText("Warn-EnableShadowbanReset"), true)
config.ShadowBan = false
}
if config.UseShadowBan && TestShadowBanAPI() <= 0 {
config.UseShadowBan = false
}
if !firstLoad {

View File

@ -11,6 +11,7 @@
"clientUsername": "",
"clientPassword": "",
"useBasicAuth": false,
"useShadowBan": true,
"skipCertVerification": false,
"blockList": [],
"blockListFile": [
@ -73,6 +74,5 @@
"banByRelativePUStartPrecent": 2.0,
"banByRelativePUAntiErrorRatio": 3.0,
"ignoreByDownloaded": 100,
"ShadowBan": false,
*/
}

10
i18n.go
View File

@ -107,11 +107,11 @@ var defaultLangContent = map[string]string{
"Success-ExecCommand": "执行命令成功, 输出: %s",
"Success-SyncWithServer": "成功与同步服务器同步",
// Shadowban Related
"Warn-ShadowbanAPINotExist": "Shadow Ban API 不存在,请确认使用的是具有 Shadow Ban 功能的客户端",
"Warn-EnableShadowbanReset": "Shadow Ban API 未启用,已忽略 Shadow Ban配置",
"Fail-GetQBPreferences": "获取qB偏好设置失败",
"Fail-UnknownShadowbanAPI": "Shadow Ban API 的类型超出预期",
// Part ShadowBan.
"Warning-ShadowBanAPINotExist": "未检测到 ShadowBan API, 将使用常规方法",
"Warning-ShadowBanAPIDisabled": "未启用 ShadowBan API, 将使用常规方法",
"Failed-UnknownShadowBanAPI": "检测到未知 Shadow Ban API, 将使用常规方法",
"Failed-GetQBPreferences": "获取 qBittorrent 偏好设置失败",
}

View File

@ -96,8 +96,9 @@
"Success-ExecCommand": "Exec command success, output: %s",
"Success-SyncWithServer": "Sync with server success",
"Warn-ShadowbanAPINotExist": "Shadow Ban API is invalid, please double check if client has Shadow Ban feature",
"Warn-EnableShadowbanReset": "Shadow Ban API is reset, ignore Shadow Ban setting",
"Fail-GetQBPreferences": "Failed to get qBittorrent preferences",
"Fail-UnknownShadowbanAPI": "Unknown Shadow Ban API value type"
// Part ShadowBan.
"Warning-ShadowBanAPINotExist": "ShadowBan API not detected, and the normal method will be used",
"Warning-ShadowBanAPIDisabled": "ShadowBan API is disabled, and the normal method will be used",
"Failed-UnknownShadowBanAPI": "Detected unknown ShadowBan API, and the normal method will be used",
"Failed-GetQBPreferences": "Failed to get qBittorrent preferences"
}