Skip to content

Commit 3af63ca

Browse files
author
Simple-Tracker
committed
Improve
1 parent 213ab19 commit 3af63ca

File tree

7 files changed

+65
-30
lines changed

7 files changed

+65
-30
lines changed

SyncServer.go

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,27 @@ type SyncServer_SubmitStruct struct {
1818
TorrentMap map[string]TorrentInfoStruct `json:"torrentMap"`
1919
}
2020

21-
var lastSync int64 = 0
22-
var syncConfig = SyncServer_ConfigStruct{
21+
var syncServer_isSubmiting bool = false
22+
var syncServer_lastSync int64 = 0
23+
var syncServer_syncConfig = SyncServer_ConfigStruct{
2324
Interval: 60,
2425
Status: "",
2526
BlockIPRule: make(map[string][]string),
2627
}
2728
var ipBlockCIDRMapFromSyncServerCompiled = make(map[string]*net.IPNet)
2829

29-
func SyncWithServer() bool {
30-
if config.SyncServerURL == "" || (lastSync+int64(syncConfig.Interval)) > currentTimestamp {
31-
return true
32-
}
33-
34-
Log("Debug-SyncWithServer", "In progress..", false)
35-
30+
func SyncWithServer_PrepareJSON(torrentMap map[string]TorrentInfoStruct) (bool, string) {
3631
syncJSON, err := json.Marshal(SyncServer_SubmitStruct{Version: 1, Timestamp: currentTimestamp, Token: config.SyncServerToken, TorrentMap: torrentMap})
3732
if err != nil {
38-
Log("SyncWithServer", GetLangText("Error-GenJSON"), true, err.Error())
39-
return false
33+
Log("SyncWithServer_PrepareJSON", GetLangText("Error-GenJSON"), true, err.Error())
34+
return false, ""
4035
}
4136

42-
lastSync = currentTimestamp
43-
44-
_, _, syncServerContent := Submit(config.SyncServerURL, string(syncJSON), false, false, nil)
37+
return true, string(syncJSON)
38+
}
39+
func SyncWithServer_Submit(syncJSON string) bool {
40+
_, _, syncServerContent := Submit(config.SyncServerURL, syncJSON, false, false, nil)
41+
syncServer_isSubmiting = false
4542
if syncServerContent == nil {
4643
Log("SyncWithServer", GetLangText("Error-FetchResponse2"), true)
4744
return false
@@ -53,19 +50,19 @@ func SyncWithServer() bool {
5350
return false
5451
}
5552

56-
if err := json.Unmarshal(jsonc.ToJSON(syncServerContent), &syncConfig); err != nil {
53+
if err := json.Unmarshal(jsonc.ToJSON(syncServerContent), &syncServer_syncConfig); err != nil {
5754
Log("SyncWithServer", GetLangText("Error-ParseConfig"), true, err.Error())
5855
return false
5956
}
6057

61-
if syncConfig.Status != "" {
62-
Log("SyncWithServer", GetLangText("Error-SyncWithServer_ServerError"), true, syncConfig.Status)
58+
if syncServer_syncConfig.Status != "" {
59+
Log("SyncWithServer", GetLangText("Error-SyncWithServer_ServerError"), true, syncServer_syncConfig.Status)
6360
return false
6461
}
6562

6663
tmpIPBlockCIDRMapFromSyncServerCompiled := make(map[string]*net.IPNet)
6764

68-
for reason, ipArr := range syncConfig.BlockIPRule {
65+
for reason, ipArr := range syncServer_syncConfig.BlockIPRule {
6966
logReason := false
7067

7168
for ipBlockListLineNum, ipBlockListLine := range ipArr {
@@ -102,3 +99,28 @@ func SyncWithServer() bool {
10299
Log("Debug-SyncWithServer", GetLangText("Success-SyncWithServer"), true, len(ipBlockCIDRMapFromSyncServerCompiled))
103100
return true
104101
}
102+
func SyncWithServer_FullSubmit(syncJSON string) bool {
103+
syncServer_isSubmiting = true
104+
syncStatus := SyncWithServer_Submit(syncJSON)
105+
syncServer_isSubmiting = false
106+
107+
return syncStatus
108+
}
109+
func SyncWithServer() bool {
110+
if config.SyncServerURL == "" || (syncServer_lastSync+int64(syncServer_syncConfig.Interval)) > currentTimestamp || syncServer_isSubmiting {
111+
return true
112+
}
113+
114+
Log("Debug-SyncWithServer", "In progress..", false)
115+
116+
status, syncJSON := SyncWithServer_PrepareJSON(torrentMap)
117+
if !status {
118+
return false
119+
}
120+
121+
syncServer_lastSync = currentTimestamp
122+
123+
go SyncWithServer_FullSubmit(syncJSON)
124+
125+
return true
126+
}

i18n.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var defaultLangContent = map[string]string{
4545
"ClientQB_Detect-OldClientURL": "检测到 ClientURL (Web UI), 已自动修改至 ClientURL (Web API): %s",
4646
"Debug-LoadConfig_HotReload": "发现配置文件 (%s) 更改, 正在进行热重载",
4747
"Debug-Request_NoChange": "请求的 URL (%s) 没有发生改变",
48+
"Debug-Request_NoContent": "请求的 URL (%s) 没有返回内容",
4849
"Debug-SetBlockListFromFile_HotReload": "发现 BlockListFile (%s) 更改, 正在进行热重载",
4950
"Debug-SetIPBlockListFromFile_HotReload": "发现 IPBlockListFile (%s) 更改, 正在进行热重载",
5051
"Debug-ShowOrHiddenWindow_HideWindow": "窗口隐藏",

ip.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func CheckAllIP(ipMap map[string]IPInfoStruct, lastIPMap map[string]IPInfoStruct
8888
if len(ipInfo.Port) > int(config.MaxIPPortCount) {
8989
Log("CheckAllIP_AddBlockPeer (Too many ports)", "%s:%d", true, ip, -1)
9090
ipBlockCount++
91-
AddBlockPeer("Too many ports", ip, -1, "")
91+
AddBlockPeer("CheckAllIP", "Too many ports", ip, -1, "")
9292
AddBlockCIDR(ip, ipInfo.Net)
9393
continue
9494
}
@@ -98,7 +98,7 @@ func CheckAllIP(ipMap map[string]IPInfoStruct, lastIPMap map[string]IPInfoStruct
9898
if uploadDuring := IsIPTooHighUploaded(ipInfo, lastIPInfo); uploadDuring > 0 {
9999
Log("CheckAllIP_AddBlockPeer (Global-Too high uploaded)", "%s:%d (UploadDuring: %.2f MB)", true, ip, -1, uploadDuring)
100100
ipBlockCount++
101-
AddBlockPeer("Global-Too high uploaded", ip, -1, "")
101+
AddBlockPeer("CheckAllIP", "Global-Too high uploaded", ip, -1, "")
102102
AddBlockCIDR(ip, ipInfo.Net)
103103
}
104104
}

lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"ClientQB_Detect-OldClientURL": "Detected ClientURL (Web UI), automatically changed to ClientURL (Web API): %s",
3434
"Debug-LoadConfig_HotReload": "Config (%s) change found, hot reload in progress",
3535
"Debug-Request_NoChange": "The requested URL (%s) has not changed",
36+
"Debug-Request_NoContent": "The requested URL (%s) did not return any content",
3637
"Debug-SetBlockListFromFile_HotReload": "BlockListFile (%s) change found, hot reload in progress",
3738
"Debug-SetIPBlockListFromFile_HotReload": "IPBlockListFile (%s) change found, hot reload in progress",
3839
"Debug-ShowOrHiddenWindow_HideWindow": "Hide window",

peer.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type BlockPeerInfoStruct struct {
1212
Timestamp int64
13+
Module string
1314
Reason string
1415
Port map[int]bool
1516
InfoHash string
@@ -24,7 +25,7 @@ var lastCleanTimestamp int64 = 0
2425
var blockPeerMap = make(map[string]BlockPeerInfoStruct)
2526
var blockCIDRMap = make(map[string]BlockCIDRInfoStruct)
2627

27-
func AddBlockPeer(reason string, peerIP string, peerPort int, torrentInfoHash string) {
28+
func AddBlockPeer(module string, reason string, peerIP string, peerPort int, torrentInfoHash string) {
2829
var blockPeerPortMap map[int]bool
2930
if blockPeer, exist := blockPeerMap[peerIP]; !exist {
3031
blockPeerPortMap = make(map[int]bool)
@@ -33,7 +34,7 @@ func AddBlockPeer(reason string, peerIP string, peerPort int, torrentInfoHash st
3334
}
3435

3536
blockPeerPortMap[peerPort] = true
36-
blockPeerMap[peerIP] = BlockPeerInfoStruct{Timestamp: currentTimestamp, Reason: reason, Port: blockPeerPortMap, InfoHash: torrentInfoHash}
37+
blockPeerMap[peerIP] = BlockPeerInfoStruct{Timestamp: currentTimestamp, Module: module, Reason: reason, Port: blockPeerPortMap, InfoHash: torrentInfoHash}
3738

3839
AddBlockCIDR(peerIP, ParseIPCIDRByConfig(peerIP))
3940

@@ -194,7 +195,7 @@ func CheckPeer(peerIP string, peerPort int, peerID, peerClient string, peerDlSpe
194195
blockListCompiled.Range(func(key, val any) bool {
195196
if MatchBlockList(val.(*regexp2.Regexp), peerIP, peerPort, peerID, peerClient) {
196197
Log("CheckPeer_AddBlockPeer (Bad-Client_Normal)", "%s:%d %s|%s (TorrentInfoHash: %s)", true, peerIP, peerPort, strconv.QuoteToASCII(peerID), strconv.QuoteToASCII(peerClient), torrentInfoHash)
197-
AddBlockPeer("Bad-Client_Normal", peerIP, peerPort, torrentInfoHash)
198+
AddBlockPeer("CheckPeer", "Bad-Client_Normal", peerIP, peerPort, torrentInfoHash)
198199
earlyStop = true
199200
return false
200201
}
@@ -209,7 +210,7 @@ func CheckPeer(peerIP string, peerPort int, peerID, peerClient string, peerDlSpe
209210
for port := range config.PortBlockList {
210211
if port == peerPort {
211212
Log("CheckPeer_AddBlockPeer (Bad-Port)", "%s:%d %s|%s (TorrentInfoHash: %s)", true, peerIP, peerPort, strconv.QuoteToASCII(peerID), strconv.QuoteToASCII(peerClient), torrentInfoHash)
212-
AddBlockPeer("Bad-Port", peerIP, peerPort, torrentInfoHash)
213+
AddBlockPeer("CheckPeer", "Bad-Port", peerIP, peerPort, torrentInfoHash)
213214
return 1, peerNet
214215
}
215216
}
@@ -230,7 +231,7 @@ func CheckPeer(peerIP string, peerPort int, peerID, peerClient string, peerDlSpe
230231
}
231232
if ipNet.Contains(ip) {
232233
Log("CheckPeer_AddBlockPeer (Bad-IP_Normal)", "%s:%d %s|%s (TorrentInfoHash: %s)", true, peerIP, -1, strconv.QuoteToASCII(peerID), strconv.QuoteToASCII(peerClient), torrentInfoHash)
233-
AddBlockPeer("Bad-IP_Normal", peerIP, -1, torrentInfoHash)
234+
AddBlockPeer("CheckPeer", "Bad-IP_Normal", peerIP, -1, torrentInfoHash)
234235
earlyStop = true
235236
return false
236237
}
@@ -247,15 +248,15 @@ func CheckPeer(peerIP string, peerPort int, peerID, peerClient string, peerDlSpe
247248
}
248249
if v.Contains(ip) {
249250
Log("CheckPeer_AddBlockPeer (Bad-IP_FromSyncServer)", "%s:%d %s|%s (TorrentInfoHash: %s)", true, peerIP, -1, strconv.QuoteToASCII(peerID), strconv.QuoteToASCII(peerClient), torrentInfoHash)
250-
AddBlockPeer("Bad-IP_FromSyncServer", peerIP, -1, torrentInfoHash)
251+
AddBlockPeer("CheckPeer", "Bad-IP_FromSyncServer", peerIP, -1, torrentInfoHash)
251252
return 3, peerNet
252253
}
253254
}
254255
}
255256

256257
if IsMatchCIDR(peerNet) {
257258
Log("CheckPeer_AddBlockPeer (Bad-CIDR)", "%s:%d %s|%s (TorrentInfoHash: %s, PeerNet: %s)", true, peerIP, peerPort, strconv.QuoteToASCII(peerID), strconv.QuoteToASCII(peerClient), torrentInfoHash, peerNet.String())
258-
AddBlockPeer("Bad-CIDR", peerIP, peerPort, torrentInfoHash)
259+
AddBlockPeer("CheckPeer", "Bad-CIDR", peerIP, peerPort, torrentInfoHash)
259260
return 1, peerNet
260261
}
261262

@@ -271,7 +272,7 @@ func CheckPeer(peerIP string, peerPort int, peerID, peerClient string, peerDlSpe
271272
}
272273
if !ignoreByDownloaded && IsProgressNotMatchUploaded(torrentTotalSize, peerProgress, peerUploaded) {
273274
Log("CheckPeer_AddBlockPeer (Bad-Progress_Uploaded)", "%s:%d %s|%s (TorrentInfoHash: %s, TorrentTotalSize: %.2f MB, PeerDlSpeed: %.2f MB/s, PeerUpSpeed: %.2f MB/s, Progress: %.2f%%, Downloaded: %.2f MB, Uploaded: %.2f MB)", true, peerIP, peerPort, strconv.QuoteToASCII(peerID), strconv.QuoteToASCII(peerClient), torrentInfoHash, (float64(torrentTotalSize) / 1024 / 1024), (float64(peerDlSpeed) / 1024 / 1024), (float64(peerUpSpeed) / 1024 / 1024), (peerProgress * 100), (float64(peerDownloaded) / 1024 / 1024), (float64(peerUploaded) / 1024 / 1024))
274-
AddBlockPeer("Bad-Progress_Uploaded", peerIP, peerPort, torrentInfoHash)
275+
AddBlockPeer("CheckPeer", "Bad-Progress_Uploaded", peerIP, peerPort, torrentInfoHash)
275276
return 1, peerNet
276277
}
277278
}

request.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ func Fetch(url string, tryLogin bool, clientReq bool, allowCache bool, withHeade
110110
return -3, nil, nil
111111
}
112112

113+
if response.StatusCode == 204 {
114+
Log("Fetch", GetLangText("Debug-Request_NoContent"), false, url)
115+
return 204, response.Header, nil
116+
}
117+
113118
if response.StatusCode == 401 {
114119
Log("Fetch", GetLangText("Error-NoAuth"), true)
115120
return 401, response.Header, nil
@@ -199,6 +204,11 @@ func Submit(url string, postdata string, tryLogin bool, clientReq bool, withHead
199204
return -3, nil, nil
200205
}
201206

207+
if response.StatusCode == 204 {
208+
Log("Fetch", GetLangText("Debug-Request_NoContent"), false, url)
209+
return 204, response.Header, nil
210+
}
211+
202212
if response.StatusCode == 401 {
203213
Log("Submit", GetLangText("Error-NoAuth"), true)
204214
return 401, response.Header, nil

torrent.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func CheckAllTorrent(torrentMap map[string]TorrentInfoStruct, lastTorrentMap map
109109
if float64(peerInfo.Uploaded) > (float64(torrentInfo.Size) * peerInfo.Progress * config.IPUpCheckPerTorrentRatio) {
110110
Log("CheckAllTorrent_AddBlockPeer (Torrent-Too high uploaded)", "%s (Uploaded: %.2f MB)", true, peerIP, (float64(peerInfo.Uploaded) / 1024 / 1024))
111111
ipBlockCount++
112-
AddBlockPeer("Torrent-Too high uploaded", peerIP, -1, torrentInfoHash)
112+
AddBlockPeer("CheckAllTorrent", "Torrent-Too high uploaded", peerIP, -1, torrentInfoHash)
113113
AddBlockCIDR(peerIP, peerInfo.Net)
114114
continue
115115
}
@@ -124,7 +124,7 @@ func CheckAllTorrent(torrentMap map[string]TorrentInfoStruct, lastTorrentMap map
124124
}
125125
Log("CheckAllTorrent_AddBlockPeer (Bad-Relative_Progress_Uploaded)", "%s:%d (UploadDuring: %.2f MB)", true, peerIP, port, uploadDuring)
126126
blockCount++
127-
AddBlockPeer("Bad-Relative_Progress_Uploaded", peerIP, port, torrentInfoHash)
127+
AddBlockPeer("CheckAllTorrent", "Bad-Relative_Progress_Uploaded", peerIP, port, torrentInfoHash)
128128
AddBlockCIDR(peerIP, peerInfo.Net)
129129
}
130130
continue

0 commit comments

Comments
 (0)