-
Notifications
You must be signed in to change notification settings - Fork 15
/
SyncServer.go
105 lines (85 loc) · 3.27 KB
/
SyncServer.go
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
package main
import (
"net"
"strings"
"encoding/json"
"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"`
}
var lastSync int64 = 0
var syncConfig = SyncServer_ConfigStruct {
Interval: 60,
Status: "",
BlockIPRule: make(map[string][]string),
}
var ipBlockCIDRMapFromSyncServerCompiled = make(map[string]*net.IPNet)
func SyncWithServer() bool {
if config.SyncServerURL == "" || (lastSync + int64(syncConfig.Interval)) > currentTimestamp {
return true
}
Log("Debug-SyncWithServer", "In progress..", false)
syncJSON, err := json.Marshal(SyncServer_SubmitStruct { Version: 1, Timestamp: currentTimestamp, Token: config.SyncServerToken, TorrentMap: torrentMap })
if err != nil {
Log("SyncWithServer", GetLangText("Error-GenJSON"), true, err.Error())
return false
}
lastSync = currentTimestamp
_, _, syncServerContent := Submit(config.SyncServerURL, string(syncJSON), false, false, nil)
if syncServerContent == nil {
Log("SyncWithServer", GetLangText("Error-FetchResponse"), true)
return false
}
// Max 8MB.
if len(syncServerContent) > 8388608 {
Log("SyncWithServer", GetLangText("Error-LargeFile"), true)
return false
}
if err := json.Unmarshal(jsonc.ToJSON(syncServerContent), &syncConfig); err != nil {
Log("SyncWithServer", GetLangText("Error-ParseConfig"), true, err.Error())
return false
}
if syncConfig.Status != "" {
Log("SyncWithServer", GetLangText("Error-SyncWithServer_ServerError"), true, syncConfig.Status)
return false
}
tmpIPBlockCIDRMapFromSyncServerCompiled := make(map[string]*net.IPNet)
for reason, ipArr := range syncConfig.BlockIPRule {
logReason := false
for ipBlockListLineNum, ipBlockListLine := range ipArr {
ipBlockListLine = StrTrim(strings.SplitN(ipBlockListLine, "#", 2)[0])
if ipBlockListLine == "" {
Log("Debug-SyncWithServer_Compile", GetLangText("Error-Debug-EmptyLine"), false, ipBlockListLineNum)
continue
}
if cidr, exists := ipBlockCIDRMapFromSyncServerCompiled[ipBlockListLine]; exists {
tmpIPBlockCIDRMapFromSyncServerCompiled[ipBlockListLine] = cidr
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)
}
tmpIPBlockCIDRMapFromSyncServerCompiled[ipBlockListLine] = cidr
Log("SyncWithServer_BlockCIDR", ":%d %s", false, ipBlockListLineNum, ipBlockListLine)
}
}
ipBlockCIDRMapFromSyncServerCompiled = tmpIPBlockCIDRMapFromSyncServerCompiled
Log("Debug-SyncWithServer", GetLangText("Success-SyncWithServer"), true, len(ipBlockCIDRMapFromSyncServerCompiled))
return true
}