Skip to content

Commit c1fb9f0

Browse files
author
Simple-Tracker
committed
Improve
1 parent 63cf225 commit c1fb9f0

8 files changed

Lines changed: 319 additions & 165 deletions

File tree

BTN.go

Lines changed: 258 additions & 149 deletions
Large diffs are not rendered by default.

README.en.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,17 @@ Docker version is configured through the same name variable configuration, which
170170
| 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 |
171171
| execCommand_Run | string | Empty | Execute external command (Run). First parameter is regarded as an external program path, execute the command when program starts |
172172
| execCommand_Ban | string | Empty | Execute external command (Ban). First parameter is regarded as an external program path, and each parameter should be separated by ```\|```, command can use ```{peerIP}```/```{peerPort}```/```{torrentInfoHash}``` to use related info (peerPort=-1 means ban all port) |
173-
| execCommand_Ban | string | Empty | Execute external command (Ban). First parameter is regarded as an external program path, and each parameter should be separated by ```\|```, command can use ```{peerIP}```/```{peerPort}```/```{torrentInfoHash}``` to use related info (peerPort=-1 means ban all port) |
174173
| execCommand_Unban | string | Empty | Execute external command (Unban). First parameter is regarded as an external program path, and each parameter should be separated by ```\|```, command can use ```{peerIP}```/```{peerPort}```/```{torrentInfoHash}``` to use related info (peerPort=-1 means ban all port) |
175174
| syncServerURL | string | Empty | Sync server URL. Sync server will submit TorrentMap to server and receive BlockIPCIDR from server |
176175
| syncServerToken | string | Empty | Sync server Token. Some sync server may require authentication |
176+
| btnConfigureURL | string | Empty | BTN server URL. Currently not available, [What is BTN?](https://docs.pbh-btn.com/en/docs/btn/intro/) |
177+
| btnAppID | string | Empty | BTN AppID. Some BTN servers may require authentication |
178+
| btnAppSecret | string | Empty | BTN AppSecret. Some BTN servers may require authentication |
179+
| btnSyncRules | bool | true (Enable) | Sync BTN server cloud ban rules |
180+
| btnSyncException | bool | true (Enable) | Sync BTN server cloud exception rules (whitelist) |
181+
| btnSubmitPeers | bool | false (Disable) | Submit Peer snapshots to BTN server |
182+
| btnSubmitBans | bool | false (Disable) | Submit local ban records to BTN server |
183+
| btnSubmitHistories | bool | false (Disable) | Submit Peer traffic delta history snapshots to BTN server |
177184
| blockList | []string | Empty (Included in config.json) | Block client list. Judge PeerID or ClientName at the same time, case-insensitive, support regular expression |
178185
| blockListURL | []string | Empty | Block client list URL. Support format is same as blockList, one rule per line |
179186
| blockListFile | []string | Empty | Block client list File. Support format is same as blockList, one rule per line |

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ Docker 版本通过相同名称的环境变量配置, 通过自动转换环境
173173
| execCommand_Unban | string | 空 | 执行外部命令 (Unban). 首个参数被视作外部程序路径, 各参数均应使用 ```\|``` 分割, 命令可以使用 ```{peerIP}```/```{peerPort}```/```{torrentInfoHash}``` 来使用相关信息 (peerPort=-1 意味着全端口封禁) |
174174
| syncServerURL | string | 空 | 同步服务器 URL. 同步服务器会将 TorrentMap 提交至服务器, 并从服务器接收屏蔽 IPCIDR 列表 |
175175
| syncServerToken | string | 空 | 同步服务器 Token. 部分同步服务器可能需要认证 |
176+
| btnConfigureURL | string | 空 | BTN 服务器 URL. 目前尚不可用, [什么是 BTN?](https://docs.pbh-btn.com/docs/btn/intro/) |
177+
| btnAppID | string | 空 | BTN AppID. 部分 BTN 服务器可能需要认证 |
178+
| btnAppSecret | string | 空 | BTN AppSecret. 部分 BTN 服务器可能需要认证 |
179+
| btnSyncRules | bool | true (启用) | 同步 BTN 服务器的云端封禁规则 |
180+
| btnSyncException | bool | true (启用) | 同步 BTN 服务器的云端例外规则 (白名单) |
181+
| btnSubmitPeers | bool | false (禁用) | 上报 Peer 快照至 BTN 服务器 |
182+
| btnSubmitBans | bool | false (禁用) | 上报本地封禁记录至 BTN 服务器 |
183+
| btnSubmitHistories | bool | false (禁用) | 上报 Peer 流量增量历史至 BTN 服务器 |
176184
| blockList | []string | 空 (于 config.json 附带) | 屏蔽客户端列表. 同时判断 PeerID 及 ClientName, 不区分大小写, 支持正则表达式 |
177185
| blockListURL | []string | 空 | 屏蔽客户端列表 URL. 支持格式同 blockList, 一行一条 |
178186
| blockListFile | []string | 空 | 屏蔽客户端列表文件. 支持格式同 blockList, 一行一条 |

config.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"crypto/tls"
55
"encoding/json"
66
"flag"
7+
"fmt"
78
"log"
89
"net/http"
910
"net/http/cookiejar"
1011
"os"
1112
"path/filepath"
1213
"reflect"
14+
"runtime"
1315
"strings"
1416
"sync"
1517
"time"
@@ -45,6 +47,10 @@ type ConfigStruct struct {
4547
LogToFile bool
4648
LogDebug bool
4749
Listen string
50+
WebUI bool
51+
WebUIListen string
52+
WebUIUsername string
53+
WebUIPassword string
4854
ClientType string
4955
ClientURL string
5056
ClientUsername string
@@ -62,6 +68,11 @@ type ConfigStruct struct {
6268
BTNConfigureURL string
6369
BTNAppID string
6470
BTNAppSecret string
71+
BTNSyncRules bool
72+
BTNSyncException bool
73+
BTNSubmitPeers bool
74+
BTNSubmitBans bool
75+
BTNSubmitHistories bool
6576
BlockList []string
6677
BlockListURL []string
6778
BlockListFile []string
@@ -84,15 +95,11 @@ type ConfigStruct struct {
8495
BanByRelativePUStartMB uint32
8596
BanByRelativePUStartPrecent float64
8697
BanByRelativePUAntiErrorRatio float64
87-
WebUI bool
88-
WebUIListen string
89-
WebUIUsername string
90-
WebUIPassword string
9198
}
9299

93100
var programName = "qBittorrent-ClientBlocker"
94101
var programVersion = "Unknown"
95-
var programUserAgent = programName + "/" + programVersion
102+
var programUserAgent = fmt.Sprintf("%s/%s (%s, %s)", programName, programVersion, runtime.GOOS, runtime.GOARCH)
96103

97104
var shortFlag_ShowVersion bool
98105
var longFlag_ShowVersion bool
@@ -119,6 +126,7 @@ var lastModMutex sync.RWMutex
119126

120127
// currentTimestamp 记录当前的 UNIX 时间戳.
121128
var currentTimestamp int64 = 0
129+
var programStartTimestamp = time.Now().Unix()
122130

123131
// configLastFetch 记录上次加载配置的具体时间.
124132
var configLastFetch int64 = 0
@@ -181,6 +189,10 @@ var config = ConfigStruct{
181189
LogToFile: true,
182190
LogDebug: false,
183191
Listen: "127.0.0.1:26262",
192+
WebUI: false,
193+
WebUIListen: "127.0.0.1:7222",
194+
WebUIUsername: "",
195+
WebUIPassword: "",
184196
ClientType: "",
185197
ClientURL: "",
186198
ClientUsername: "",
@@ -196,6 +208,13 @@ var config = ConfigStruct{
196208
SyncServerURL: "",
197209
SyncServerToken: "",
198210
BTNConfigureURL: "",
211+
BTNAppID: "",
212+
BTNAppSecret: "",
213+
BTNSyncRules: true,
214+
BTNSyncException: true,
215+
BTNSubmitPeers: false,
216+
BTNSubmitBans: false,
217+
BTNSubmitHistories: false,
199218
BlockList: []string{},
200219
BlockListURL: []string{},
201220
BlockListFile: []string{},
@@ -218,10 +237,6 @@ var config = ConfigStruct{
218237
BanByRelativePUStartMB: 20,
219238
BanByRelativePUStartPrecent: 2,
220239
BanByRelativePUAntiErrorRatio: 3,
221-
WebUI: false,
222-
WebUIListen: "127.0.0.1:7222",
223-
WebUIUsername: "",
224-
WebUIPassword: "",
225240
}
226241

227242
func SetBlockListFromContent(blockListContent []string, blockListSource string) int {

i18n.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,20 @@ var defaultLangContent = map[string]string{
120120
"Success-SyncWithServer": "成功与同步服务器同步",
121121
"StartServer_ListeningOn": "监听于: %s",
122122

123+
"Failed-GetQBPreferences": "获取 qBittorrent 偏好设置失败",
124+
123125
// ShadowBan 部分.
124126
"Warning-ShadowBanAPINotExist": "未检测到 ShadowBan API, 将使用常规方法",
125127
"Warning-ShadowBanAPIDisabled": "未启用 ShadowBan API, 将使用常规方法",
126128
"Failed-UnknownShadowBanAPI": "检测到未知 Shadow Ban API, 将使用常规方法",
127-
"Failed-GetQBPreferences": "获取 qBittorrent 偏好设置失败",
129+
130+
// BTN 部分.
131+
"Success-BTNRegLoaded": "BTN 规则加载成功, 版本: %s",
132+
"Success-BTNExceptionLoaded": "BTN 例外规则加载成功, 版本: %s",
133+
"Success-BTNConfigLoaded": "BTN 配置加载成功",
134+
"Debug-BTNNoChange": "BTN 规则/例外无变化",
135+
"Debug-BTNConfigNoChange": "BTN 配置无变化",
136+
"Error-BTNVersionMismatch": "BTN 协议版本不匹配: 服务器要求 [%d, %d], 但我们支持 3。BTN 模块已禁用。",
128137
}
129138

130139
func LoadLang(langCode string) bool {

lang/en.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,16 @@
106106
"Success-SyncWithServer": "Sync with server success",
107107
"StartServer_ListeningOn": "Listening on: %s",
108108

109-
// Part ShadowBan.
109+
"Failed-GetQBPreferences": "Failed to get qBittorrent preferences",
110+
110111
"Warning-ShadowBanAPINotExist": "ShadowBan API not detected, and the normal method will be used",
111112
"Warning-ShadowBanAPIDisabled": "ShadowBan API is disabled, and the normal method will be used",
112113
"Failed-UnknownShadowBanAPI": "Detected unknown ShadowBan API, and the normal method will be used",
113-
"Failed-GetQBPreferences": "Failed to get qBittorrent preferences"
114+
115+
"Success-BTNRegLoaded": "BTN rules loaded successfully, version: %s",
116+
"Success-BTNExceptionLoaded": "BTN exception rules loaded successfully, version: %s",
117+
"Success-BTNConfigLoaded": "BTN configuration loaded successfully",
118+
"Debug-BTNNoChange": "BTN rules/exceptions have not changed",
119+
"Debug-BTNConfigNoChange": "BTN configuration has not changed",
120+
"Error-BTNVersionMismatch": "BTN protocol version mismatch: server requires [%d, %d], but we support 3. BTN module disabled."
114121
}

torrent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func IsProgressNotMatchUploaded_Relative(torrentTotalSize int64, peerInfo PeerIn
103103

104104
// CheckAllTorrent 对所有种子和 Peer 进行分析.
105105
func CheckAllTorrent(torrentMap map[string]TorrentInfoStruct, lastTorrentMap map[string]TorrentInfoStruct) (int, int) {
106-
if ((config.IPUploadedCheck && config.IPUpCheckPerTorrentRatio > 0) || config.BanByRelativeProgressUploaded) && len(lastTorrentMap) > 0 && currentTimestamp > (lastTorrentCleanTimestamp+int64(config.TorrentMapCleanInterval)) {
106+
if ((config.IPUploadedCheck && config.IPUpCheckPerTorrentRatio > 0) || config.BanByRelativeProgressUploaded || config.BTNSubmitHistories) && (currentTimestamp > (lastTorrentCleanTimestamp + int64(config.TorrentMapCleanInterval))) {
107107
blockCount := 0
108108
ipBlockCount := 0
109109

webui.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ type WebUIBlockPeer struct {
5050
Uploaded int64 `json:"uploaded"`
5151
}
5252

53-
var startTimestamp = time.Now().Unix()
5453

5554
func WebUI_IsPath(path string) bool {
5655
return path == "/" || path == "/api/status" || path == "/api/peers" || path == "/api/logs"
@@ -150,7 +149,7 @@ func WebUI_GetStatus(w http.ResponseWriter, r *http.Request) {
150149
resp := StatusResponse{
151150
ProgramName: programName,
152151
ProgramVersion: programVersion,
153-
UptimeSeconds: time.Now().Unix() - startTimestamp,
152+
UptimeSeconds: time.Now().Unix() - programStartTimestamp,
154153
ClientType: currentClientType,
155154
ClientURL: config.ClientURL,
156155
LoadedExtensions: loadedExtensions,

0 commit comments

Comments
 (0)