Skip to content

Commit eff4f54

Browse files
author
Simple-Tracker
committed
Improve
1 parent 2b3408f commit eff4f54

14 files changed

Lines changed: 192 additions & 113 deletions

BTN.go

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ var btnConfig *BTN_ConfigStruct
135135
var btn_isGettingConfig atomic.Bool
136136
var btn_isTaskRunning atomic.Bool
137137

138-
var btnRules BTN_RulesStruct
139-
var btnExceptions BTN_ExceptionStruct
138+
var btnRules *BTN_RulesStruct = &BTN_RulesStruct{}
139+
var btnExceptions *BTN_ExceptionStruct = &BTN_ExceptionStruct{}
140140
var btn_regexCache sync.Map // map[string]*regexp2.Regexp
141141

142142
func BTN_MatchEntry(value string, ruleRaw string) bool {
@@ -182,8 +182,9 @@ func BTN_CheckPeer(peerIP, peerID, peerClient string, peerPort int) (bool, int,
182182
ipObj := net.ParseIP(peerIP)
183183
peerPortStr := strconv.Itoa(peerPort)
184184

185+
currExceptions := btnExceptions
185186
// 1. 检查例外规则 (WhiteList).
186-
for _, rules := range btnExceptions.IP {
187+
for _, rules := range currExceptions.IP {
187188
for _, rule := range rules {
188189
_, subnet, err := net.ParseCIDR(rule)
189190
if err == nil {
@@ -195,31 +196,32 @@ func BTN_CheckPeer(peerIP, peerID, peerClient string, peerPort int) (bool, int,
195196
}
196197
}
197198
}
198-
for _, rules := range btnExceptions.Port {
199+
for _, rules := range currExceptions.Port {
199200
for _, rule := range rules {
200201
if rule == peerPortStr || rule == "ALL" {
201202
return false, 0, ""
202203
}
203204
}
204205
}
205-
for _, rules := range btnExceptions.PeerID {
206+
for _, rules := range currExceptions.PeerID {
206207
for _, rule := range rules {
207208
if BTN_MatchEntry(peerID, rule) {
208209
return false, 0, ""
209210
}
210211
}
211212
}
212-
for _, rules := range btnExceptions.ClientName {
213+
for _, rules := range currExceptions.ClientName {
213214
for _, rule := range rules {
214215
if BTN_MatchEntry(peerClient, rule) {
215216
return false, 0, ""
216217
}
217218
}
218219
}
219220

221+
currRules := btnRules
220222
// 2. 检查封禁规则 (BlockList).
221223
// 处理顺序: IP -> Port -> PeerID -> ClientName.
222-
for reason, rules := range btnRules.IP {
224+
for reason, rules := range currRules.IP {
223225
for _, rule := range rules {
224226
_, subnet, err := net.ParseCIDR(rule)
225227
if err == nil {
@@ -231,21 +233,21 @@ func BTN_CheckPeer(peerIP, peerID, peerClient string, peerPort int) (bool, int,
231233
}
232234
}
233235
}
234-
for reason, rules := range btnRules.Port {
236+
for reason, rules := range currRules.Port {
235237
for _, rule := range rules {
236238
if rule == peerPortStr || rule == "ALL" {
237239
return true, peerPort, "Bad-IP_FromBTN (" + reason + ")"
238240
}
239241
}
240242
}
241-
for reason, rules := range btnRules.PeerID {
243+
for reason, rules := range currRules.PeerID {
242244
for _, rule := range rules {
243245
if BTN_MatchEntry(peerID, rule) {
244246
return true, peerPort, "Bad-IP_FromBTN (" + reason + ")"
245247
}
246248
}
247249
}
248-
for reason, rules := range btnRules.ClientName {
250+
for reason, rules := range currRules.ClientName {
249251
for _, rule := range rules {
250252
if BTN_MatchEntry(peerClient, rule) {
251253
return true, peerPort, "Bad-IP_FromBTN (" + reason + ")"
@@ -257,7 +259,14 @@ func BTN_CheckPeer(peerIP, peerID, peerClient string, peerPort int) (bool, int,
257259
}
258260

259261
func BTN_GetConfig() {
260-
if config.BTNConfigureURL == "" || (atomic.LoadInt64(&btn_lastGetConfig)+int64(btn_configureInterval)) > atomic.LoadInt64(&currentTimestamp) {
262+
if config.BTNConfigureURL == "" {
263+
btnConfig = nil
264+
btnRules = &BTN_RulesStruct{}
265+
btnExceptions = &BTN_ExceptionStruct{}
266+
return
267+
}
268+
269+
if (atomic.LoadInt64(&btn_lastGetConfig)+int64(btn_configureInterval)) > atomic.LoadInt64(&currentTimestamp) {
261270
return
262271
}
263272
if !btn_isGettingConfig.CompareAndSwap(false, true) {
@@ -282,27 +291,31 @@ func BTN_GetConfig() {
282291
return
283292
}
284293

285-
if err := json.Unmarshal(jsonc.ToJSON(btnConfigContent), &btnConfig); err != nil {
294+
var newBtnConfig BTN_ConfigStruct
295+
if err := json.Unmarshal(jsonc.ToJSON(btnConfigContent), &newBtnConfig); err != nil {
286296
Log("BTN_GetConfig", GetLangText("Error-ParseConfig"), true, err.Error())
287297
return
288298
}
289299

290300
// 协议版本校验 (目前我们的实现固定为 3).
291-
if btnConfig.MinMainVersion > 3 || btnConfig.MaxMainVersion < 3 {
292-
Log("BTN_GetConfig", GetLangText("Error-BTNVersionMismatch"), true, btnConfig.MinMainVersion, btnConfig.MaxMainVersion)
301+
if newBtnConfig.MinMainVersion > 3 || newBtnConfig.MaxMainVersion < 3 {
302+
Log("BTN_GetConfig", GetLangText("Error-BTNVersionMismatch"), true, newBtnConfig.MinMainVersion, newBtnConfig.MaxMainVersion)
293303
btnConfig = nil
294304
return
295305
}
296306

307+
btnConfig = &newBtnConfig
308+
297309
Log("BTN_GetConfig", GetLangText("Success-BTNConfigLoaded"), true)
298310
}
299311

300312
func BTN_SubmitPeers(torrentMap map[string]TorrentInfoStruct, currentTimestamp int64) {
301-
if btn_isGettingConfig.Load() || btnConfig == nil {
313+
currConfig := btnConfig
314+
if btn_isGettingConfig.Load() || currConfig == nil {
302315
return
303316
}
304317

305-
ability, _ := btnConfig.Ability["submit_peers"]
318+
ability, _ := currConfig.Ability["submit_peers"]
306319
peers := []BTN_PeerInternalStruct{}
307320
torrentMapMutex.RLock()
308321
for torrentInfoHash, torrentInfo := range torrentMap {
@@ -363,11 +376,12 @@ func BTN_SubmitPeers(torrentMap map[string]TorrentInfoStruct, currentTimestamp i
363376
}
364377

365378
func BTN_SubmitBans(blockPeerMap map[string]BlockPeerInfoStruct, currentTimestamp int64) {
366-
if btn_isGettingConfig.Load() || btnConfig == nil {
379+
currConfig := btnConfig
380+
if btn_isGettingConfig.Load() || currConfig == nil {
367381
return
368382
}
369383

370-
ability, _ := btnConfig.Ability["submit_bans"]
384+
ability, _ := currConfig.Ability["submit_bans"]
371385
bans := []BTN_BanInfo{}
372386
blockPeerMapMutex.RLock()
373387
for peerIP, peerInfo := range blockPeerMap {
@@ -430,11 +444,12 @@ func BTN_SubmitBans(blockPeerMap map[string]BlockPeerInfoStruct, currentTimestam
430444
}
431445

432446
func BTN_SubmitHistories(torrentMap map[string]TorrentInfoStruct, lastTorrentMap map[string]TorrentInfoStruct, currentTimestamp int64) {
433-
if btn_isGettingConfig.Load() || btnConfig == nil {
447+
currConfig := btnConfig
448+
if btn_isGettingConfig.Load() || currConfig == nil {
434449
return
435450
}
436451

437-
ability, _ := btnConfig.Ability["submit_histories"]
452+
ability, _ := currConfig.Ability["submit_histories"]
438453
peers := []BTN_PeerHistoryStruct{}
439454
torrentMapMutex.RLock()
440455
lastTorrentMapMutex.RLock()
@@ -522,13 +537,14 @@ func BTN_SubmitHistories(torrentMap map[string]TorrentInfoStruct, lastTorrentMap
522537
}
523538

524539
func BTN_Reconfigure() {
525-
if btn_isGettingConfig.Load() || btnConfig == nil {
540+
currConfig := btnConfig
541+
if btn_isGettingConfig.Load() || currConfig == nil {
526542
return
527543
}
528544

529-
ability, _ := btnConfig.Ability["reconfigure"]
545+
ability, _ := currConfig.Ability["reconfigure"]
530546
authHeader := getBTNAuthHeader()
531-
statusCode, _, response := Fetch(ability.Endpoint+"?rev="+btnConfig.Ability["reconfigure"].Version, false, false, false, &authHeader)
547+
statusCode, _, response := Fetch(ability.Endpoint+"?rev="+currConfig.Ability["reconfigure"].Version, false, false, false, &authHeader)
532548
if response == nil {
533549
if statusCode == 204 {
534550
Log("BTN_Reconfigure", GetLangText("Debug-BTNConfigNoChange"), false)
@@ -542,18 +558,20 @@ func BTN_Reconfigure() {
542558
}
543559

544560
func BTN_Rules() {
545-
if btn_isGettingConfig.Load() || btnConfig == nil {
561+
currConfig := btnConfig
562+
if btn_isGettingConfig.Load() || currConfig == nil {
546563
return
547564
}
548565

549-
ability, _ := btnConfig.Ability["rules"]
566+
ability, _ := currConfig.Ability["rules"]
550567
authHeader := getBTNAuthHeader()
551568
rulesEndpoint := ability.Endpoint
552-
if btnRules.Version != "" {
569+
currRules := btnRules
570+
if currRules.Version != "" {
553571
if strings.Contains(rulesEndpoint, "?") {
554-
rulesEndpoint += "&rev=" + btnRules.Version
572+
rulesEndpoint += "&rev=" + currRules.Version
555573
} else {
556-
rulesEndpoint += "?rev=" + btnRules.Version
574+
rulesEndpoint += "?rev=" + currRules.Version
557575
}
558576
}
559577

@@ -568,27 +586,31 @@ func BTN_Rules() {
568586
}
569587

570588
// 处理规则数据.
571-
if err := json.Unmarshal(response, &btnRules); err != nil {
589+
var newRules BTN_RulesStruct
590+
if err := json.Unmarshal(response, &newRules); err != nil {
572591
Log("BTN_Rules", GetLangText("Error-Parse"), true, err.Error())
573592
return
574593
}
594+
btnRules = &newRules
575595

576-
Log("BTN_Rules", GetLangText("Success-BTNRegLoaded"), true, btnRules.Version)
596+
Log("BTN_Rules", GetLangText("Success-BTNRegLoaded"), true, currRules.Version)
577597
}
578598

579599
func BTN_Exception() {
580-
if btn_isGettingConfig.Load() || btnConfig == nil {
600+
currConfig := btnConfig
601+
if btn_isGettingConfig.Load() || currConfig == nil {
581602
return
582603
}
583604

584-
ability, _ := btnConfig.Ability["exception"]
605+
ability, _ := currConfig.Ability["exception"]
585606
authHeader := getBTNAuthHeader()
586607
exceptionEndpoint := ability.Endpoint
587-
if btnExceptions.Version != "" {
608+
currExceptions := btnExceptions
609+
if currExceptions.Version != "" {
588610
if strings.Contains(exceptionEndpoint, "?") {
589-
exceptionEndpoint += "&rev=" + btnExceptions.Version
611+
exceptionEndpoint += "&rev=" + currExceptions.Version
590612
} else {
591-
exceptionEndpoint += "?rev=" + btnExceptions.Version
613+
exceptionEndpoint += "?rev=" + currExceptions.Version
592614
}
593615
}
594616

@@ -603,16 +625,18 @@ func BTN_Exception() {
603625
}
604626

605627
// 处理例外规则数据.
606-
if err := json.Unmarshal(response, &btnExceptions); err != nil {
628+
var newExceptions BTN_ExceptionStruct
629+
if err := json.Unmarshal(response, &newExceptions); err != nil {
607630
Log("BTN_Exception", GetLangText("Error-Parse"), true, err.Error())
608631
return
609632
}
633+
btnExceptions = &newExceptions
610634

611-
Log("BTN_Exception", GetLangText("Success-BTNExceptionLoaded"), true, btnExceptions.Version)
635+
Log("BTN_Exception", GetLangText("Success-BTNExceptionLoaded"), true, currExceptions.Version)
612636
}
613637

614638
func BTN_Task() {
615-
if btn_isGettingConfig.Load() || btn_isTaskRunning.Load() {
639+
if config.BTNConfigureURL == "" || btn_isGettingConfig.Load() || btn_isTaskRunning.Load() || btnConfig == nil {
616640
return
617641
}
618642

@@ -622,11 +646,16 @@ func BTN_Task() {
622646
GoWithCrashLog("BTN_Task", func() {
623647
defer btn_isTaskRunning.Store(false)
624648

649+
currConfig := btnConfig
650+
if currConfig == nil {
651+
return
652+
}
653+
625654
btn_taskMutex.Lock()
626655
defer btn_taskMutex.Unlock()
627656

628657
executeTask := func(name string, taskFunc func()) {
629-
ability, exists := btnConfig.Ability[name]
658+
ability, exists := currConfig.Ability[name]
630659
if !exists {
631660
return
632661
}

BTN_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ func TestGetTorrentIdentifierIsStableAndCaseInsensitive(t *testing.T) {
2323

2424
func TestBTNGetConfigLoadsConfiguration(t *testing.T) {
2525
oldClientExternal := httpClientExternal
26-
oldConfig := config
26+
oldConfig := *config
2727
oldBtnConfig := btnConfig
2828
oldLastGetConfig := btn_lastGetConfig
2929
oldCurrentTimestamp := currentTimestamp
3030
oldIsGetting := btn_isGettingConfig.Load()
3131
defer func() {
3232
httpClientExternal = oldClientExternal
33-
config = oldConfig
33+
tmpConf := oldConfig
34+
config = &tmpConf
3435
btnConfig = oldBtnConfig
3536
btn_lastGetConfig = oldLastGetConfig
3637
currentTimestamp = oldCurrentTimestamp
@@ -58,7 +59,8 @@ func TestBTNGetConfigLoadsConfiguration(t *testing.T) {
5859
defer server.Close()
5960

6061
httpClientExternal = *server.Client()
61-
config = oldConfig
62+
tmpConf := oldConfig
63+
config = &tmpConf
6264
config.BTNConfigureURL = server.URL
6365
config.BTNAppID = "app-a"
6466
config.BTNAppSecret = "secret-a"

SyncServer.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type SyncServer_RuleStruct struct {
2727

2828
var syncServer_isSubmiting atomic.Bool
2929
var syncServer_lastSync int64 = 0
30-
var syncServer_syncConfig = SyncServer_ConfigStruct{
30+
var syncServer_syncConfig = &SyncServer_ConfigStruct{
3131
Interval: 60,
3232
Status: "",
3333
BlockIPRule: make(map[string][]string),
@@ -59,19 +59,20 @@ func SyncWithServer_Submit(syncJSON string) bool {
5959
return false
6060
}
6161

62-
if err := json.Unmarshal(jsonc.ToJSON(syncServerContent), &syncServer_syncConfig); err != nil {
62+
var newConfig SyncServer_ConfigStruct
63+
if err := json.Unmarshal(jsonc.ToJSON(syncServerContent), &newConfig); err != nil {
6364
Log("SyncWithServer", GetLangText("Error-ParseConfig"), true, err.Error())
6465
return false
6566
}
6667

67-
if syncServer_syncConfig.Status != "" {
68-
Log("SyncWithServer", GetLangText("Error-SyncWithServer_ServerError"), true, syncServer_syncConfig.Status)
68+
if newConfig.Status != "" {
69+
Log("SyncWithServer", GetLangText("Error-SyncWithServer_ServerError"), true, newConfig.Status)
6970
return false
7071
}
7172

7273
var tmpSyncServerCompiledRules []SyncServer_RuleStruct
7374

74-
for reason, ipArr := range syncServer_syncConfig.BlockIPRule {
75+
for reason, ipArr := range newConfig.BlockIPRule {
7576
logReason := false
7677

7778
for ipBlockListLineNum, ipBlockListLine := range ipArr {
@@ -102,6 +103,8 @@ func SyncWithServer_Submit(syncJSON string) bool {
102103
syncServer_CompiledRules = tmpSyncServerCompiledRules
103104
ipBlockCIDRMapMutex.Unlock()
104105

106+
syncServer_syncConfig = &newConfig
107+
105108
Log("Debug-SyncWithServer", GetLangText("Success-SyncWithServer"), true, len(syncServer_CompiledRules))
106109
return true
107110
}
@@ -128,8 +131,25 @@ func SyncWithServer_FullSubmit(syncJSON string) bool {
128131

129132
return syncStatus
130133
}
134+
131135
func SyncWithServer() bool {
132-
if config.SyncServerURL == "" || (atomic.LoadInt64(&syncServer_lastSync)+int64(syncServer_syncConfig.Interval)) > atomic.LoadInt64(&currentTimestamp) || syncServer_isSubmiting.Load() {
136+
if config.SyncServerURL == "" {
137+
ipBlockCIDRMapMutex.Lock()
138+
if len(syncServer_CompiledRules) > 0 {
139+
syncServer_CompiledRules = nil
140+
}
141+
ipBlockCIDRMapMutex.Unlock()
142+
143+
syncServer_syncConfig = &SyncServer_ConfigStruct{
144+
Interval: 60,
145+
Status: "",
146+
BlockIPRule: make(map[string][]string),
147+
}
148+
return true
149+
}
150+
151+
currSyncConfig := syncServer_syncConfig
152+
if (atomic.LoadInt64(&syncServer_lastSync)+int64(currSyncConfig.Interval)) > atomic.LoadInt64(&currentTimestamp) || syncServer_isSubmiting.Load() {
133153
return true
134154
}
135155

0 commit comments

Comments
 (0)