Skip to content

Commit 48c9d49

Browse files
author
Simple-Tracker
committed
Bug fix & Add ETag and Last-Modified support
1 parent bce92cf commit 48c9d49

File tree

8 files changed

+67
-24
lines changed

8 files changed

+67
-24
lines changed

SyncServer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func SyncWithServer() bool {
4343

4444
_, _, syncServerContent := Submit(config.SyncServerURL, string(syncJSON), false, false, nil)
4545
if syncServerContent == nil {
46-
Log("SyncWithServer", GetLangText("Error-FetchResponse"), true)
46+
Log("SyncWithServer", GetLangText("Error-FetchResponse2"), true)
4747
return false
4848
}
4949

client_BitComet.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,16 @@ func BC_ParseIP(ipStr string) (string, int) {
133133
return ipWithoutPortStr, port
134134
}
135135
func BC_DetectClient() bool {
136-
apiResponseStatusCode, apiResponseHeaders, _ := Fetch(config.ClientURL+"/panel/", false, false, nil)
136+
apiResponseStatusCode, apiResponseHeaders, _ := Fetch(config.ClientURL+"/panel/", false, false, false, nil)
137137
return (apiResponseStatusCode == 401 && strings.Contains(apiResponseHeaders.Get("WWW-Authenticate"), "BitComet"))
138138
}
139139
func BC_Login() bool {
140140
// BitComet 通过 Basic Auth 进行认证, 因此此处只进行验证.
141-
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/panel/", false, true, nil)
141+
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/panel/", false, true, false, nil)
142142
return (apiResponseStatusCode == 200)
143143
}
144144
func BC_FetchTorrents() *map[int]BC_TorrentStruct {
145-
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/panel/task_list?group=active", true, true, nil)
145+
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/panel/task_list?group=active", true, true, false, nil)
146146
if torrentsResponseBody == nil {
147147
Log("FetchTorrents", GetLangText("Error"), true)
148148
return nil
@@ -198,7 +198,7 @@ func BC_FetchTorrents() *map[int]BC_TorrentStruct {
198198
return &torrentsMap
199199
}
200200
func BC_FetchTorrentPeers(infoHash string) *[]BC_PeerStruct {
201-
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/panel/task_detail?id="+infoHash+"&show=peers", true, true, nil)
201+
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/panel/task_detail?id="+infoHash+"&show=peers", true, true, false, nil)
202202
if torrentPeersResponseBody == nil {
203203
Log("FetchTorrentPeers", GetLangText("Error"), true)
204204
return nil

client_qBittorrent.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ func qB_SetURL() bool {
142142
}
143143
func qB_GetAPIVersion() bool {
144144
if !strings.HasSuffix(config.ClientURL, "/api") {
145-
apiResponseStatusCodeWithSuffix, _, _ := Fetch(config.ClientURL+"/api/v2/app/webapiVersion", false, false, nil)
145+
apiResponseStatusCodeWithSuffix, _, _ := Fetch(config.ClientURL+"/api/v2/app/webapiVersion", false, false, false, nil)
146146
if apiResponseStatusCodeWithSuffix == 200 || apiResponseStatusCodeWithSuffix == 403 {
147147
config.ClientURL += "/api"
148148
Log("qB_GetAPIVersion", GetLangText("ClientQB_Detect-OldClientURL"), true, config.ClientURL)
149149
return true
150150
}
151151
}
152152

153-
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/v2/app/webapiVersion", false, false, nil)
153+
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/v2/app/webapiVersion", false, false, false, nil)
154154
return (apiResponseStatusCode == 200 || apiResponseStatusCode == 403)
155155
}
156156
func qB_Login() bool {
@@ -175,7 +175,7 @@ func qB_Login() bool {
175175
return false
176176
}
177177
func qB_FetchTorrents() *[]qB_TorrentStruct {
178-
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/v2/torrents/info?filter=active", true, true, nil)
178+
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/v2/torrents/info?filter=active", true, true, false, nil)
179179
if torrentsResponseBody == nil {
180180
Log("FetchTorrents", GetLangText("Error"), true)
181181
return nil
@@ -190,7 +190,7 @@ func qB_FetchTorrents() *[]qB_TorrentStruct {
190190
return &torrentsResult
191191
}
192192
func qB_FetchTorrentPeers(infoHash string) *qB_TorrentPeersStruct {
193-
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/v2/sync/torrentPeers?rid=0&hash="+infoHash, true, true, nil)
193+
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/v2/sync/torrentPeers?rid=0&hash="+infoHash, true, true, false, nil)
194194
if torrentPeersResponseBody == nil {
195195
Log("FetchTorrentPeers", GetLangText("Error"), true)
196196
return nil

config.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,11 @@ func SetBlockListFromURL() bool {
290290
setCount := 0
291291

292292
for _, blockListURL := range config.BlockListURL {
293-
_, httpHeader, blockListContent := Fetch(blockListURL, false, false, nil)
293+
httpStatusCode, httpHeader, blockListContent := Fetch(blockListURL, false, false, true, nil)
294+
if httpStatusCode == 304 {
295+
continue
296+
}
297+
294298
if blockListContent == nil {
295299
blockListURLLastFetch -= (int64(config.UpdateInterval) + 900)
296300
Log("SetBlockListFromURL", GetLangText("Error-FetchResponse2"), true)
@@ -403,12 +407,17 @@ func SetIPBlockListFromURL() bool {
403407
setCount := 0
404408

405409
for _, ipBlockListURL := range config.IPBlockListURL {
406-
_, httpHeader, ipBlockListContent := Fetch(ipBlockListURL, false, false, nil)
410+
httpStatusCode, httpHeader, ipBlockListContent := Fetch(ipBlockListURL, false, false, true, nil)
411+
if httpStatusCode == 304 {
412+
continue
413+
}
414+
407415
if ipBlockListContent == nil {
408416
ipBlockListURLLastFetch -= (int64(config.UpdateInterval) + 900)
409417
Log("SetIPBlockListFromURL", GetLangText("Error-FetchResponse2"), true)
410418
continue
411419
}
420+
412421
if len(ipBlockListContent) > 8388608 {
413422
Log("SetIPBlockListFromURL", GetLangText("Error-LargeFile"), true)
414423
continue

console.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ func CheckUpdate() {
9999
return
100100
}
101101

102-
_, _, listReleaseContent := Fetch("https://api.github.com/repos/Simple-Tracker/qBittorrent-ClientBlocker/releases?per_page=5", false, false, &githubAPIHeader)
102+
listResponseCode, _, listReleaseContent := Fetch("https://api.github.com/repos/Simple-Tracker/qBittorrent-ClientBlocker/releases?per_page=5", false, false, true, &githubAPIHeader)
103+
if listResponseCode == 304 {
104+
return
105+
}
106+
103107
if listReleaseContent == nil {
104108
Log("CheckUpdate", GetLangText("Error-FetchUpdate"), true)
105109
return

i18n.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var defaultLangContent = map[string]string{
4343
"GetProxy_UseEnvVar": "当前平台通过环境变量设置代理, 若要使用, 请确保已正确设置环境变量",
4444
"ClientQB_Detect-OldClientURL": "检测到 ClientURL (Web UI), 已自动修改至 ClientURL (Web API): %s",
4545
"Debug-LoadConfig_HotReload": "发现配置文件 (%s) 更改, 正在进行热重载",
46+
"Debug-Request_NoChange": "请求的 URL (%s) 没有发生改变",
4647
"Debug-SetBlockListFromFile_HotReload": "发现 BlockListFile (%s) 更改, 正在进行热重载",
4748
"Debug-SetIPBlockListFromFile_HotReload": "发现 IPBlockListFile (%s) 更改, 正在进行热重载",
4849
"Debug-ShowOrHiddenWindow_HideWindow": "窗口隐藏",
@@ -102,10 +103,10 @@ var defaultLangContent = map[string]string{
102103
"Success-SetCSRFToken": "设置 CSRF Token 成功: %s",
103104
"Success-SetURL": "读取客户端配置文件成功 (WebUIEnabled: %t, URL: %s, Username: %s)",
104105
"Success-GenIPFilter": "生成了 %d 条 IP 规则",
105-
"Success-SetBlockListFromURL": "设置了 %d 条 表达式 规则 (来源: BlockListURL)",
106-
"Success-SetIPBlockListFromURL": "设置了 %d 条 IP 规则 (来源: IPBlockListURL)",
107-
"Success-SetBlockListFromFile": "设置了 %d 条 表达式 规则 (来源: BlockListFile)",
108-
"Success-SetIPBlockListFromFile": "设置了 %d 条 IP 规则 (来源: IPBlockListFile)",
106+
"Success-SetBlockListFromURL": "本次设置了 %d 条 表达式 规则 (来源: BlockListURL)",
107+
"Success-SetIPBlockListFromURL": "本次设置了 %d 条 IP 规则 (来源: IPBlockListURL)",
108+
"Success-SetBlockListFromFile": "本次设置了 %d 条 表达式 规则 (来源: BlockListFile)",
109+
"Success-SetIPBlockListFromFile": "本次设置了 %d 条 IP 规则 (来源: IPBlockListFile)",
109110
"Success-DetectClient": "检测客户端类型成功: %s",
110111
"Success-Login": "登录成功",
111112
"Success-ClearBlockPeer": "已清理过期客户端: %d 个",

lang/en.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"GetProxy_UseEnvVar": "The current platform sets the proxy through environment variables. If you want to use it, please make sure that the environment variables are set correctly",
3232
"ClientQB_Detect-OldClientURL": "Detected ClientURL (Web UI), automatically changed to ClientURL (Web API): %s",
3333
"Debug-LoadConfig_HotReload": "Config (%s) change found, hot reload in progress",
34+
"Debug-Request_NoChange": "The requested URL (%s) has not changed",
3435
"Debug-SetBlockListFromFile_HotReload": "BlockListFile (%s) change found, hot reload in progress",
3536
"Debug-SetIPBlockListFromFile_HotReload": "IPBlockListFile (%s) change found, hot reload in progress",
3637
"Debug-ShowOrHiddenWindow_HideWindow": "Hide window",
@@ -90,10 +91,10 @@
9091
"Success-SetCSRFToken": "Set CSRF Token successfully: %s",
9192
"Success-SetURL": "Read client config file successfully (WebUIEnabled: %t, URL: %s, Username: %s)",
9293
"Success-GenIPFilter": "%d IP rules are generate",
93-
"Success-SetBlocklistFromURL": "%d regexp rules are set (Source: BlockListURL)",
94-
"Success-SetIPBlockListFromURL": "%d IP rules are set (Source: IPBlockListURL)",
95-
"Success-SetBlockListFromFile": "%d regexp rules are set (Source: BlockListFile)",
96-
"Success-SetIPBlockListFromFile": "%d IP rules are set (Source: IPBlockListFile)",
94+
"Success-SetBlocklistFromURL": "This time %d regexp rules are set (Source: BlockListURL)",
95+
"Success-SetIPBlockListFromURL": "This time %d IP rules are set (Source: IPBlockListURL)",
96+
"Success-SetBlockListFromFile": "This time %d regexp rules are set (Source: BlockListFile)",
97+
"Success-SetIPBlockListFromFile": "This time %d IP rules are set (Source: IPBlockListFile)",
9798
"Success-DetectClient": "Detect client type successful: %s",
9899
"Success-Login": "Login successful",
99100
"Success-ClearBlockPeer": "Cleaned up expired client: %d",

request.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import (
77
)
88

99
var fetchFailedCount = 0
10+
var urlETagCache = make(map[string]string)
11+
var urlLastModCache = make(map[string]string)
1012

11-
func NewRequest(isPOST bool, url string, postdata string, clientReq bool, withHeader *map[string]string) *http.Request {
13+
func NewRequest(isPOST bool, url string, postdata string, clientReq bool, allowCache bool, withHeader *map[string]string) *http.Request {
1214
var request *http.Request
1315
var err error
1416

@@ -55,12 +57,20 @@ func NewRequest(isPOST bool, url string, postdata string, clientReq bool, withHe
5557
if config.UseBasicAuth && config.ClientUsername != "" {
5658
request.SetBasicAuth(config.ClientUsername, config.ClientPassword)
5759
}
60+
} else if !isPOST && allowCache {
61+
if etag, exist := urlETagCache[url]; exist {
62+
request.Header.Set("If-None-Match", etag)
63+
}
64+
65+
if lastMod, exist := urlLastModCache[url]; exist {
66+
request.Header.Set("If-Modified-Since", lastMod)
67+
}
5868
}
5969

6070
return request
6171
}
62-
func Fetch(url string, tryLogin bool, clientReq bool, withHeader *map[string]string) (int, http.Header, []byte) {
63-
request := NewRequest(false, url, "", clientReq, withHeader)
72+
func Fetch(url string, tryLogin bool, clientReq bool, allowCache bool, withHeader *map[string]string) (int, http.Header, []byte) {
73+
request := NewRequest(false, url, "", clientReq, allowCache, withHeader)
6474
if request == nil {
6575
return -1, nil, nil
6676
}
@@ -136,15 +146,33 @@ func Fetch(url string, tryLogin bool, clientReq bool, withHeader *map[string]str
136146
return 404, response.Header, nil
137147
}
138148

149+
if allowCache && response.StatusCode == 304 {
150+
Log("Fetch", GetLangText("Debug-Request_NoChange"), false, url)
151+
return 304, response.Header, nil
152+
}
153+
139154
if response.StatusCode != 200 {
140155
Log("Fetch", GetLangText("Error-UnknownStatusCode"), true, response.StatusCode)
141156
return response.StatusCode, response.Header, nil
142157
}
143158

159+
if allowCache {
160+
etag := response.Header.Get("ETag")
161+
162+
if etag != "" {
163+
urlETagCache[url] = etag
164+
}
165+
166+
lastMod := response.Header.Get("Last-Modified")
167+
if lastMod != "" {
168+
urlLastModCache[url] = lastMod
169+
}
170+
}
171+
144172
return response.StatusCode, response.Header, responseBody
145173
}
146174
func Submit(url string, postdata string, tryLogin bool, clientReq bool, withHeader *map[string]string) (int, http.Header, []byte) {
147-
request := NewRequest(true, url, postdata, clientReq, withHeader)
175+
request := NewRequest(true, url, postdata, clientReq, false, withHeader)
148176
if request == nil {
149177
return -1, nil, nil
150178
}

0 commit comments

Comments
 (0)