-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathSyncServer.go
More file actions
173 lines (141 loc) · 4.85 KB
/
Copy pathSyncServer.go
File metadata and controls
173 lines (141 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"encoding/json"
"net"
"sync"
"sync/atomic"
"github.com/tidwall/jsonc"
)
type SyncServer_ConfigStruct struct {
Interval uint32 `json:"interval"`
Status string `json:"status"`
BlockIPRule map[string][]string `json:"blockIPRule"`
}
type SyncServer_SubmitStruct struct {
Version uint32 `json:"version"`
Timestamp int64 `json:"timestamp"`
Token string `json:"token"`
TorrentMap map[string]TorrentInfoStruct `json:"torrentMap"`
}
type SyncServer_RuleStruct struct {
Net *net.IPNet
Reason string
}
var syncServer_isSubmiting atomic.Bool
var syncServer_lastSync int64 = 0
var syncServer_syncConfig = &SyncServer_ConfigStruct{
Interval: 60,
Status: "",
BlockIPRule: make(map[string][]string),
}
var syncServer_CompiledRules []SyncServer_RuleStruct
var ipBlockCIDRMapMutex sync.RWMutex
func SyncWithServer_PrepareJSON(torrentMap map[string]TorrentInfoStruct) (bool, string) {
torrentMapMutex.RLock()
syncJSON, err := json.Marshal(SyncServer_SubmitStruct{Version: 1, Timestamp: atomic.LoadInt64(¤tTimestamp), Token: config.SyncServerToken, TorrentMap: torrentMap})
torrentMapMutex.RUnlock()
if err != nil {
Log("SyncWithServer_PrepareJSON", GetLangText("Error-GenJSON"), true, err.Error())
return false, ""
}
return true, string(syncJSON)
}
func SyncWithServer_Submit(syncJSON string) bool {
_, _, syncServerContent := Submit(config.SyncServerURL, syncJSON, false, false, nil)
if syncServerContent == nil {
Log("SyncWithServer", GetLangText("Error-FetchResponse2"), true)
return false
}
// 最大 8MB.
if len(syncServerContent) > 8388608 {
Log("SyncWithServer", GetLangText("Error-LargeFile"), true)
return false
}
var newConfig SyncServer_ConfigStruct
if err := json.Unmarshal(jsonc.ToJSON(syncServerContent), &newConfig); err != nil {
Log("SyncWithServer", GetLangText("Error-ParseConfig"), true, err.Error())
return false
}
if newConfig.Status != "" {
Log("SyncWithServer", GetLangText("Error-SyncWithServer_ServerError"), true, newConfig.Status)
return false
}
var tmpSyncServerCompiledRules []SyncServer_RuleStruct
for reason, ipArr := range newConfig.BlockIPRule {
logReason := false
for ipBlockListLineNum, ipBlockListLine := range ipArr {
ipBlockListLine = ProcessRemark(ipBlockListLine)
if ipBlockListLine == "" {
Log("Debug-SyncWithServer_Compile", GetLangText("Error-Debug-EmptyLine"), false, ipBlockListLineNum)
continue
}
Log("Debug-SyncWithServer_Compile", ":%d %s", false, ipBlockListLineNum, ipBlockListLine)
cidr := ParseIPCIDR(ipBlockListLine)
if cidr == nil {
Log("SyncWithServer_Compile", GetLangText("Error-SyncWithServer_Compile"), true, ipBlockListLineNum, ipBlockListLine)
continue
}
if !logReason {
logReason = true
Log("SyncWithServer", GetLangText("SyncWithServer_Compile-BlockByReason"), true, reason)
}
tmpSyncServerCompiledRules = append(tmpSyncServerCompiledRules, SyncServer_RuleStruct{Net: cidr, Reason: reason})
Log("SyncWithServer_BlockCIDR", ":%d %s", false, ipBlockListLineNum, ipBlockListLine)
}
}
ipBlockCIDRMapMutex.Lock()
syncServer_CompiledRules = tmpSyncServerCompiledRules
ipBlockCIDRMapMutex.Unlock()
syncServer_syncConfig = &newConfig
Log("Debug-SyncWithServer", GetLangText("Success-SyncWithServer"), true, len(syncServer_CompiledRules))
return true
}
func SyncServer_CheckPeer(ipObj net.IP) (bool, string) {
if ipObj == nil {
return false, ""
}
ipBlockCIDRMapMutex.RLock()
defer ipBlockCIDRMapMutex.RUnlock()
for _, rule := range syncServer_CompiledRules {
if rule.Net != nil && rule.Net.Contains(ipObj) {
return true, "Bad-IP_FromSyncServer (" + rule.Reason + ")"
}
}
return false, ""
}
func SyncWithServer_FullSubmit(syncJSON string) bool {
syncStatus := SyncWithServer_Submit(syncJSON)
syncServer_isSubmiting.Store(false)
return syncStatus
}
func SyncWithServer() bool {
if config.SyncServerURL == "" {
ipBlockCIDRMapMutex.Lock()
if len(syncServer_CompiledRules) > 0 {
syncServer_CompiledRules = nil
}
ipBlockCIDRMapMutex.Unlock()
syncServer_syncConfig = &SyncServer_ConfigStruct{
Interval: 60,
Status: "",
BlockIPRule: make(map[string][]string),
}
return true
}
currSyncConfig := syncServer_syncConfig
if (atomic.LoadInt64(&syncServer_lastSync)+int64(currSyncConfig.Interval)) > atomic.LoadInt64(¤tTimestamp) || syncServer_isSubmiting.Load() {
return true
}
Log("Debug-SyncWithServer", "In progress..", false)
status, syncJSON := SyncWithServer_PrepareJSON(torrentMap)
if !status {
return false
}
atomic.StoreInt64(&syncServer_lastSync, atomic.LoadInt64(¤tTimestamp))
if syncServer_isSubmiting.CompareAndSwap(false, true) {
GoWithCrashLog("SyncWithServer_FullSubmit", func() {
SyncWithServer_FullSubmit(syncJSON)
})
}
return true
}