Skip to content

Commit

Permalink
Bug fix & Add ETag and Last-Modified support
Browse files Browse the repository at this point in the history
  • Loading branch information
Simple-Tracker committed Jan 6, 2025
1 parent bce92cf commit 48c9d49
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 24 deletions.
2 changes: 1 addition & 1 deletion SyncServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func SyncWithServer() bool {

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

Expand Down
8 changes: 4 additions & 4 deletions client_BitComet.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,16 @@ func BC_ParseIP(ipStr string) (string, int) {
return ipWithoutPortStr, port
}
func BC_DetectClient() bool {
apiResponseStatusCode, apiResponseHeaders, _ := Fetch(config.ClientURL+"/panel/", false, false, nil)
apiResponseStatusCode, apiResponseHeaders, _ := Fetch(config.ClientURL+"/panel/", false, false, false, nil)
return (apiResponseStatusCode == 401 && strings.Contains(apiResponseHeaders.Get("WWW-Authenticate"), "BitComet"))
}
func BC_Login() bool {
// BitComet 通过 Basic Auth 进行认证, 因此此处只进行验证.
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/panel/", false, true, nil)
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/panel/", false, true, false, nil)
return (apiResponseStatusCode == 200)
}
func BC_FetchTorrents() *map[int]BC_TorrentStruct {
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/panel/task_list?group=active", true, true, nil)
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/panel/task_list?group=active", true, true, false, nil)
if torrentsResponseBody == nil {
Log("FetchTorrents", GetLangText("Error"), true)
return nil
Expand Down Expand Up @@ -198,7 +198,7 @@ func BC_FetchTorrents() *map[int]BC_TorrentStruct {
return &torrentsMap
}
func BC_FetchTorrentPeers(infoHash string) *[]BC_PeerStruct {
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/panel/task_detail?id="+infoHash+"&show=peers", true, true, nil)
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/panel/task_detail?id="+infoHash+"&show=peers", true, true, false, nil)
if torrentPeersResponseBody == nil {
Log("FetchTorrentPeers", GetLangText("Error"), true)
return nil
Expand Down
8 changes: 4 additions & 4 deletions client_qBittorrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ func qB_SetURL() bool {
}
func qB_GetAPIVersion() bool {
if !strings.HasSuffix(config.ClientURL, "/api") {
apiResponseStatusCodeWithSuffix, _, _ := Fetch(config.ClientURL+"/api/v2/app/webapiVersion", false, false, nil)
apiResponseStatusCodeWithSuffix, _, _ := Fetch(config.ClientURL+"/api/v2/app/webapiVersion", false, false, false, nil)
if apiResponseStatusCodeWithSuffix == 200 || apiResponseStatusCodeWithSuffix == 403 {
config.ClientURL += "/api"
Log("qB_GetAPIVersion", GetLangText("ClientQB_Detect-OldClientURL"), true, config.ClientURL)
return true
}
}

apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/v2/app/webapiVersion", false, false, nil)
apiResponseStatusCode, _, _ := Fetch(config.ClientURL+"/v2/app/webapiVersion", false, false, false, nil)
return (apiResponseStatusCode == 200 || apiResponseStatusCode == 403)
}
func qB_Login() bool {
Expand All @@ -175,7 +175,7 @@ func qB_Login() bool {
return false
}
func qB_FetchTorrents() *[]qB_TorrentStruct {
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/v2/torrents/info?filter=active", true, true, nil)
_, _, torrentsResponseBody := Fetch(config.ClientURL+"/v2/torrents/info?filter=active", true, true, false, nil)
if torrentsResponseBody == nil {
Log("FetchTorrents", GetLangText("Error"), true)
return nil
Expand All @@ -190,7 +190,7 @@ func qB_FetchTorrents() *[]qB_TorrentStruct {
return &torrentsResult
}
func qB_FetchTorrentPeers(infoHash string) *qB_TorrentPeersStruct {
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/v2/sync/torrentPeers?rid=0&hash="+infoHash, true, true, nil)
_, _, torrentPeersResponseBody := Fetch(config.ClientURL+"/v2/sync/torrentPeers?rid=0&hash="+infoHash, true, true, false, nil)
if torrentPeersResponseBody == nil {
Log("FetchTorrentPeers", GetLangText("Error"), true)
return nil
Expand Down
13 changes: 11 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,11 @@ func SetBlockListFromURL() bool {
setCount := 0

for _, blockListURL := range config.BlockListURL {
_, httpHeader, blockListContent := Fetch(blockListURL, false, false, nil)
httpStatusCode, httpHeader, blockListContent := Fetch(blockListURL, false, false, true, nil)
if httpStatusCode == 304 {
continue
}

if blockListContent == nil {
blockListURLLastFetch -= (int64(config.UpdateInterval) + 900)
Log("SetBlockListFromURL", GetLangText("Error-FetchResponse2"), true)
Expand Down Expand Up @@ -403,12 +407,17 @@ func SetIPBlockListFromURL() bool {
setCount := 0

for _, ipBlockListURL := range config.IPBlockListURL {
_, httpHeader, ipBlockListContent := Fetch(ipBlockListURL, false, false, nil)
httpStatusCode, httpHeader, ipBlockListContent := Fetch(ipBlockListURL, false, false, true, nil)
if httpStatusCode == 304 {
continue
}

if ipBlockListContent == nil {
ipBlockListURLLastFetch -= (int64(config.UpdateInterval) + 900)
Log("SetIPBlockListFromURL", GetLangText("Error-FetchResponse2"), true)
continue
}

if len(ipBlockListContent) > 8388608 {
Log("SetIPBlockListFromURL", GetLangText("Error-LargeFile"), true)
continue
Expand Down
6 changes: 5 additions & 1 deletion console.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ func CheckUpdate() {
return
}

_, _, listReleaseContent := Fetch("https://api.github.com/repos/Simple-Tracker/qBittorrent-ClientBlocker/releases?per_page=5", false, false, &githubAPIHeader)
listResponseCode, _, listReleaseContent := Fetch("https://api.github.com/repos/Simple-Tracker/qBittorrent-ClientBlocker/releases?per_page=5", false, false, true, &githubAPIHeader)
if listResponseCode == 304 {
return
}

if listReleaseContent == nil {
Log("CheckUpdate", GetLangText("Error-FetchUpdate"), true)
return
Expand Down
9 changes: 5 additions & 4 deletions i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var defaultLangContent = map[string]string{
"GetProxy_UseEnvVar": "当前平台通过环境变量设置代理, 若要使用, 请确保已正确设置环境变量",
"ClientQB_Detect-OldClientURL": "检测到 ClientURL (Web UI), 已自动修改至 ClientURL (Web API): %s",
"Debug-LoadConfig_HotReload": "发现配置文件 (%s) 更改, 正在进行热重载",
"Debug-Request_NoChange": "请求的 URL (%s) 没有发生改变",
"Debug-SetBlockListFromFile_HotReload": "发现 BlockListFile (%s) 更改, 正在进行热重载",
"Debug-SetIPBlockListFromFile_HotReload": "发现 IPBlockListFile (%s) 更改, 正在进行热重载",
"Debug-ShowOrHiddenWindow_HideWindow": "窗口隐藏",
Expand Down Expand Up @@ -102,10 +103,10 @@ var defaultLangContent = map[string]string{
"Success-SetCSRFToken": "设置 CSRF Token 成功: %s",
"Success-SetURL": "读取客户端配置文件成功 (WebUIEnabled: %t, URL: %s, Username: %s)",
"Success-GenIPFilter": "生成了 %d 条 IP 规则",
"Success-SetBlockListFromURL": "设置了 %d 条 表达式 规则 (来源: BlockListURL)",
"Success-SetIPBlockListFromURL": "设置了 %d 条 IP 规则 (来源: IPBlockListURL)",
"Success-SetBlockListFromFile": "设置了 %d 条 表达式 规则 (来源: BlockListFile)",
"Success-SetIPBlockListFromFile": "设置了 %d 条 IP 规则 (来源: IPBlockListFile)",
"Success-SetBlockListFromURL": "本次设置了 %d 条 表达式 规则 (来源: BlockListURL)",
"Success-SetIPBlockListFromURL": "本次设置了 %d 条 IP 规则 (来源: IPBlockListURL)",
"Success-SetBlockListFromFile": "本次设置了 %d 条 表达式 规则 (来源: BlockListFile)",
"Success-SetIPBlockListFromFile": "本次设置了 %d 条 IP 规则 (来源: IPBlockListFile)",
"Success-DetectClient": "检测客户端类型成功: %s",
"Success-Login": "登录成功",
"Success-ClearBlockPeer": "已清理过期客户端: %d 个",
Expand Down
9 changes: 5 additions & 4 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"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",
"ClientQB_Detect-OldClientURL": "Detected ClientURL (Web UI), automatically changed to ClientURL (Web API): %s",
"Debug-LoadConfig_HotReload": "Config (%s) change found, hot reload in progress",
"Debug-Request_NoChange": "The requested URL (%s) has not changed",
"Debug-SetBlockListFromFile_HotReload": "BlockListFile (%s) change found, hot reload in progress",
"Debug-SetIPBlockListFromFile_HotReload": "IPBlockListFile (%s) change found, hot reload in progress",
"Debug-ShowOrHiddenWindow_HideWindow": "Hide window",
Expand Down Expand Up @@ -90,10 +91,10 @@
"Success-SetCSRFToken": "Set CSRF Token successfully: %s",
"Success-SetURL": "Read client config file successfully (WebUIEnabled: %t, URL: %s, Username: %s)",
"Success-GenIPFilter": "%d IP rules are generate",
"Success-SetBlocklistFromURL": "%d regexp rules are set (Source: BlockListURL)",
"Success-SetIPBlockListFromURL": "%d IP rules are set (Source: IPBlockListURL)",
"Success-SetBlockListFromFile": "%d regexp rules are set (Source: BlockListFile)",
"Success-SetIPBlockListFromFile": "%d IP rules are set (Source: IPBlockListFile)",
"Success-SetBlocklistFromURL": "This time %d regexp rules are set (Source: BlockListURL)",
"Success-SetIPBlockListFromURL": "This time %d IP rules are set (Source: IPBlockListURL)",
"Success-SetBlockListFromFile": "This time %d regexp rules are set (Source: BlockListFile)",
"Success-SetIPBlockListFromFile": "This time %d IP rules are set (Source: IPBlockListFile)",
"Success-DetectClient": "Detect client type successful: %s",
"Success-Login": "Login successful",
"Success-ClearBlockPeer": "Cleaned up expired client: %d",
Expand Down
36 changes: 32 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
)

var fetchFailedCount = 0
var urlETagCache = make(map[string]string)
var urlLastModCache = make(map[string]string)

func NewRequest(isPOST bool, url string, postdata string, clientReq bool, withHeader *map[string]string) *http.Request {
func NewRequest(isPOST bool, url string, postdata string, clientReq bool, allowCache bool, withHeader *map[string]string) *http.Request {
var request *http.Request
var err error

Expand Down Expand Up @@ -55,12 +57,20 @@ func NewRequest(isPOST bool, url string, postdata string, clientReq bool, withHe
if config.UseBasicAuth && config.ClientUsername != "" {
request.SetBasicAuth(config.ClientUsername, config.ClientPassword)
}
} else if !isPOST && allowCache {
if etag, exist := urlETagCache[url]; exist {
request.Header.Set("If-None-Match", etag)
}

if lastMod, exist := urlLastModCache[url]; exist {
request.Header.Set("If-Modified-Since", lastMod)
}
}

return request
}
func Fetch(url string, tryLogin bool, clientReq bool, withHeader *map[string]string) (int, http.Header, []byte) {
request := NewRequest(false, url, "", clientReq, withHeader)
func Fetch(url string, tryLogin bool, clientReq bool, allowCache bool, withHeader *map[string]string) (int, http.Header, []byte) {
request := NewRequest(false, url, "", clientReq, allowCache, withHeader)
if request == nil {
return -1, nil, nil
}
Expand Down Expand Up @@ -136,15 +146,33 @@ func Fetch(url string, tryLogin bool, clientReq bool, withHeader *map[string]str
return 404, response.Header, nil
}

if allowCache && response.StatusCode == 304 {
Log("Fetch", GetLangText("Debug-Request_NoChange"), false, url)
return 304, response.Header, nil
}

if response.StatusCode != 200 {
Log("Fetch", GetLangText("Error-UnknownStatusCode"), true, response.StatusCode)
return response.StatusCode, response.Header, nil
}

if allowCache {
etag := response.Header.Get("ETag")

if etag != "" {
urlETagCache[url] = etag
}

lastMod := response.Header.Get("Last-Modified")
if lastMod != "" {
urlLastModCache[url] = lastMod
}
}

return response.StatusCode, response.Header, responseBody
}
func Submit(url string, postdata string, tryLogin bool, clientReq bool, withHeader *map[string]string) (int, http.Header, []byte) {
request := NewRequest(true, url, postdata, clientReq, withHeader)
request := NewRequest(true, url, postdata, clientReq, false, withHeader)
if request == nil {
return -1, nil, nil
}
Expand Down

0 comments on commit 48c9d49

Please sign in to comment.