@@ -135,8 +135,8 @@ var btnConfig *BTN_ConfigStruct
135135var btn_isGettingConfig atomic.Bool
136136var 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 {}
140140var btn_regexCache sync.Map // map[string]*regexp2.Regexp
141141
142142func 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
259261func 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
300312func 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
365378func 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
432446func 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
524539func 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
544560func 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
579599func 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
614638func 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 }
0 commit comments