-
Notifications
You must be signed in to change notification settings - Fork 15
/
config.go
518 lines (444 loc) · 15.4 KB
/
config.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package main
import (
"os"
"net"
"time"
"flag"
"reflect"
"strings"
"crypto/tls"
"encoding/json"
"path/filepath"
"net/http"
"net/http/cookiejar"
"github.com/tidwall/jsonc"
"github.com/dlclark/regexp2"
)
type ConfigStruct struct {
CheckUpdate bool
Debug bool
Debug_CheckTorrent bool
Debug_CheckPeer bool
Interval uint32
CleanInterval uint32
UpdateInterval uint32
TorrentMapCleanInterval uint32
BanTime uint32
BanAllPort bool
BanIPCIDR string
BanIP6CIDR string
IgnoreEmptyPeer bool
IgnoreNoLeechersTorrent bool
IgnorePTTorrent bool
SleepTime uint32
Timeout uint32
LongConnection bool
LogPath string
LogToFile bool
LogDebug bool
Listen string
ClientType string
ClientURL string
ClientUsername string
ClientPassword string
UseBasicAuth bool
SkipCertVerification bool
FetchFailedThreshold int
ExecCommand_FetchFailed string
ExecCommand_Run string
ExecCommand_Ban string
ExecCommand_Unban string
SyncServerURL string
SyncServerToken string
BlockList []string
BlockListURL string
PortBlockList []uint32
IPBlockList []string
IPBlockListURL string
IgnoreByDownloaded uint32
GenIPDat uint32
IPUploadedCheck bool
IPUpCheckInterval uint32
IPUpCheckIncrementMB uint32
IPUpCheckPerTorrentRatio float64
MaxIPPortCount uint32
BanByProgressUploaded bool
BanByPUStartMB uint32
BanByPUStartPrecent float64
BanByPUAntiErrorRatio float64
BanByRelativeProgressUploaded bool
BanByRelativePUStartMB uint32
BanByRelativePUStartPrecent float64
BanByRelativePUAntiErrorRatio float64
}
var programName = "qBittorrent-ClientBlocker"
var programVersion = "Unknown"
var programUserAgent = programName + "/" + programVersion
var shortFlag_ShowVersion bool
var longFlag_ShowVersion bool
var startDelay uint
var noChdir bool
var needHideWindow bool
var needHideSystray bool
var randomStrRegexp = regexp2.MustCompile("[a-zA-Z0-9]{32}", 0)
var blockListCompiled []*regexp2.Regexp
var blockListFromURLCompiled = make(map[string]*regexp2.Regexp)
var ipBlockListCompiled []*net.IPNet
var ipBlockListFromURLCompiled = make(map[string]*net.IPNet)
var cookieJar, _ = cookiejar.New(nil)
var lastURL = ""
var configLastMod = make(map[string]int64)
var configFilename string = "config.json"
var shortFlag_configFilename string
var longFlag_configFilename string
var additionConfigFilename string = "config_additional.json"
var shortFlag_additionConfigFilename string
var longFlag_additionConfigFilename string
var blockListLastFetch int64 = 0
var ipBlockListLastFetch int64 = 0
var httpTransport = &http.Transport {
DisableKeepAlives: true,
ForceAttemptHTTP2: false,
MaxConnsPerHost: 32,
MaxIdleConns: 32,
MaxIdleConnsPerHost: 32,
TLSClientConfig: &tls.Config { InsecureSkipVerify: false },
}
var httpClient http.Client
var httpClientWithoutCookie http.Client
var httpServer = http.Server {
ReadTimeout: 30,
WriteTimeout: 30,
Handler: &httpServerHandler {},
}
var config = ConfigStruct {
CheckUpdate: true,
Debug: false,
Debug_CheckTorrent: false,
Debug_CheckPeer: false,
Interval: 6,
CleanInterval: 3600,
UpdateInterval: 86400,
TorrentMapCleanInterval: 60,
BanTime: 86400,
BanAllPort: false,
BanIPCIDR: "/32",
BanIP6CIDR: "/128",
IgnoreEmptyPeer: true,
IgnoreNoLeechersTorrent: true,
IgnorePTTorrent: true,
SleepTime: 20,
Timeout: 6,
LongConnection: true,
LogPath: "logs",
LogToFile: true,
LogDebug: false,
Listen: ":26262",
ClientType: "",
ClientURL: "",
ClientUsername: "",
ClientPassword: "",
UseBasicAuth: false,
SkipCertVerification: false,
FetchFailedThreshold: 0,
ExecCommand_FetchFailed: "",
ExecCommand_Run: "",
ExecCommand_Ban: "",
ExecCommand_Unban: "",
SyncServerURL: "",
SyncServerToken: "",
BlockList: []string {},
BlockListURL: "",
PortBlockList: []uint32 {},
IPBlockList: []string {},
IPBlockListURL: "",
IgnoreByDownloaded: 100,
GenIPDat: 0,
IPUploadedCheck: false,
IPUpCheckInterval: 300,
IPUpCheckIncrementMB: 38000,
IPUpCheckPerTorrentRatio: 3,
MaxIPPortCount: 0,
BanByProgressUploaded: false,
BanByPUStartMB: 20,
BanByPUStartPrecent: 2,
BanByPUAntiErrorRatio: 3,
BanByRelativeProgressUploaded: false,
BanByRelativePUStartMB: 20,
BanByRelativePUStartPrecent: 2,
BanByRelativePUAntiErrorRatio: 3,
}
func SetBlockListFromContent(blockListContent []byte, blockListCompiled map[string]*regexp2.Regexp) int {
// Max 8MB.
if len(blockListContent) > 8388608 {
Log("SetBlockList", GetLangText("Error-LargeFile"), true)
return 0
}
blockListArr := strings.Split(string(blockListContent), "\n")
tmpBlockListCompiled := make(map[string]*regexp2.Regexp)
for blockListLineNum, blockListLine := range blockListArr {
blockListLine = StrTrim(strings.SplitN(blockListLine, "#", 2)[0])
if blockListLine == "" {
Log("Debug-SetBlockList_Compile", GetLangText("Error-Debug-EmptyLine"), false, blockListLineNum)
continue
}
if reg, exists := blockListCompiled[blockListLine]; exists {
tmpBlockListCompiled[blockListLine] = reg
continue
}
Log("Debug-SetBlockList_Compile", ":%d %s", false, blockListLineNum, blockListLine)
reg, err := regexp2.Compile("(?i)" + blockListLine, 0)
if err != nil {
Log("SetBlockList_Compile", GetLangText("Error-SetBlockList_Compile"), true, blockListLineNum, blockListLine)
continue
}
reg.MatchTimeout = 50 * time.Millisecond
tmpBlockListCompiled[blockListLine] = reg
}
blockListCompiled = tmpBlockListCompiled
return len(blockListCompiled)
}
func SetIPBlockListFromContent(ipBlockListContent []byte, ipBlockListCompiled map[string]*net.IPNet) int {
// Max 8MB.
if len(ipBlockListContent) > 8388608 {
Log("SetIPBlockList", GetLangText("Error-LargeFile"), true)
return 0
}
ipBlockListArr := strings.Split(string(ipBlockListContent), "\n")
tmpIPBlockListCompiled := make(map[string]*net.IPNet)
for ipBlockListLineNum, ipBlockListLine := range ipBlockListArr {
ipBlockListLine = StrTrim(strings.SplitN(ipBlockListLine, "#", 2)[0])
if ipBlockListLine == "" {
Log("Debug-SetIPBlockList_Compile", GetLangText("Error-Debug-EmptyLine"), false, ipBlockListLineNum)
continue
}
if cidr, exists := ipBlockListCompiled[ipBlockListLine]; exists {
tmpIPBlockListCompiled[ipBlockListLine] = cidr
continue
}
Log("Debug-SetIPBlockList_Compile", ":%d %s", false, ipBlockListLineNum, ipBlockListLine)
cidr := ParseIPCIDR(ipBlockListLine)
if cidr == nil {
Log("SetIPBlockList_Compile", GetLangText("Error-SetIPBlockList_Compile"), true, ipBlockListLineNum, ipBlockListLine)
continue
}
tmpIPBlockListCompiled[ipBlockListLine] = cidr
}
ipBlockListCompiled = tmpIPBlockListCompiled
return len(ipBlockListCompiled)
}
func SetBlockListFromURL() bool {
if config.BlockListURL == "" || (blockListLastFetch + int64(config.UpdateInterval)) > currentTimestamp {
return true
}
blockListLastFetch = currentTimestamp
_, _, blockListContent := Fetch(config.BlockListURL, false, false, nil)
if blockListContent == nil {
Log("SetBlockListFromURL", GetLangText("Error-FetchResponse"), true)
return false
}
ruleCount := SetBlockListFromContent(blockListContent, blockListFromURLCompiled)
Log("SetBlockListFromURL", GetLangText("Success-SetBlockListFromURL"), true, ruleCount)
return true
}
func SetIPBlockListFromURL() bool {
if config.IPBlockListURL == "" || (ipBlockListLastFetch + int64(config.UpdateInterval)) > currentTimestamp {
return true
}
ipBlockListLastFetch = currentTimestamp
_, _, ipBlockListContent := Fetch(config.IPBlockListURL, false, false, nil)
if ipBlockListContent == nil {
Log("SetIPBlockListFromURL", GetLangText("Error-FetchResponse"), true)
return false
}
ruleCount := SetIPBlockListFromContent(ipBlockListContent, ipBlockListFromURLCompiled)
Log("SetIPBlockListFromURL", GetLangText("Success-SetIPBlockListFromURL"), true, ruleCount)
return true
}
func LoadConfig(filename string, notExistErr bool) int {
configFileStat, err := os.Stat(filename)
if err != nil {
notExist := os.IsNotExist(err)
if notExistErr || !notExist {
Log("Debug-LoadConfig", GetLangText("Error-LoadConfigMeta"), false, filename, err.Error())
}
if notExist {
return -5
}
return -2
}
tmpConfigLastMod := configFileStat.ModTime().Unix()
if tmpConfigLastMod <= configLastMod[filename] {
return -1
}
if configLastMod[filename] != 0 {
Log("Debug-LoadConfig", GetLangText("Debug-LoadConfig_HotReload"), false, filename)
}
configFile, err := os.ReadFile(filename)
if err != nil {
Log("LoadConfig", GetLangText("Error-LoadConfig"), true, filename, err.Error())
return -3
}
configLastMod[filename] = tmpConfigLastMod
if err := json.Unmarshal(jsonc.ToJSON(configFile), &config); err != nil {
Log("LoadConfig", GetLangText("Error-ParseConfig"), true, filename, err.Error())
return -4
}
Log("LoadConfig", GetLangText("Success-LoadConfig"), true, filename)
return 0
}
func InitConfig() {
if config.Interval < 1 {
config.Interval = 1
}
if config.Timeout < 1 {
config.Timeout = 1
}
if config.ClientURL != "" {
config.ClientURL = strings.TrimRight(config.ClientURL, "/")
}
if config.SkipCertVerification {
httpTransport.TLSClientConfig = &tls.Config { InsecureSkipVerify: true }
} else {
httpTransport.TLSClientConfig = &tls.Config { InsecureSkipVerify: false }
}
httpTransportWithoutCookie := httpTransport.Clone()
if config.LongConnection {
httpTransport.DisableKeepAlives = false
}
currentTimeout := time.Duration(config.Timeout) * time.Second
httpClient = http.Client {
Timeout: currentTimeout,
Jar: cookieJar,
Transport: httpTransport,
CheckRedirect: func (req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
httpClientWithoutCookie = http.Client {
Timeout: currentTimeout,
Transport: httpTransportWithoutCookie,
CheckRedirect: func (req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
httpServer.ReadTimeout = currentTimeout
httpServer.WriteTimeout = currentTimeout
t := reflect.TypeOf(config)
v := reflect.ValueOf(config)
for k := 0; k < t.NumField(); k++ {
Log("LoadConfig_Current", "%v: %v", false, t.Field(k).Name, v.Field(k).Interface())
}
blockListCompiled = make([]*regexp2.Regexp, len(config.BlockList))
for k, v := range config.BlockList {
Log("Debug-LoadConfig_CompileBlockList", "%s", false, v)
reg, err := regexp2.Compile("(?i)" + v, 0)
if err != nil {
Log("LoadConfig_CompileBlockList", GetLangText("Error-CompileBlockList"), false, v)
continue
}
reg.MatchTimeout = 50 * time.Millisecond
blockListCompiled[k] = reg
}
ipBlockListCompiled = make([]*net.IPNet, len(config.IPBlockList))
for k, v := range config.IPBlockList {
Log("Debug-LoadConfig_CompileIPBlockList", "%s", false, v)
cidr := ParseIPCIDR(v)
if cidr == nil {
Log("LoadConfig_CompileIPBlockList", GetLangText("Error-CompileIPBlockList"), false, v)
continue
}
ipBlockListCompiled[k] = cidr
}
}
func LoadInitConfig(firstLoad bool) bool {
lastURL = config.ClientURL
loadConfigStatus := LoadConfig(configFilename, true)
if loadConfigStatus < -1 {
Log("LoadInitConfig", GetLangText("Failed-LoadInitConfig"), true)
} else {
loadAdditionalConfigStatus := LoadConfig(additionConfigFilename, false)
if loadAdditionalConfigStatus == -5 && additionConfigFilename == "config_additional.json" {
loadAdditionalConfigStatus = LoadConfig("config/" + additionConfigFilename, false)
}
if loadConfigStatus == 0 || loadAdditionalConfigStatus == 0 {
InitConfig()
}
}
if !LoadLog() && logFile != nil {
logFile.Close()
logFile = nil
}
if firstLoad {
SetURLFromClient()
}
if config.ClientURL != "" {
if lastURL != config.ClientURL {
DetectClient()
InitClient()
if firstLoad && !Login() {
return false
}
SubmitBlockPeer(nil)
lastURL = config.ClientURL
}
} else {
// 重置为上次使用的 URL, 主要目的是防止热重载配置文件可能破坏首次启动后从 qBittorrent 配置文件读取的 URL.
config.ClientURL = lastURL
}
if !firstLoad {
SetIPBlockListFromURL()
SetBlockListFromURL()
}
return true
}
func RegFlag() {
flag.BoolVar(&shortFlag_ShowVersion, "v", false, GetLangText("ProgramVersion"))
flag.BoolVar(&longFlag_ShowVersion, "version", false, GetLangText("ProgramVersion"))
flag.StringVar(&shortFlag_configFilename, "c", "", GetLangText("ConfigPath"))
flag.StringVar(&longFlag_configFilename, "config", "", GetLangText("ConfigPath"))
flag.StringVar(&shortFlag_additionConfigFilename, "ca", "", GetLangText("AdditionalConfigPath"))
flag.StringVar(&longFlag_additionConfigFilename, "config_additional", "", GetLangText("AdditionalConfigPath"))
flag.BoolVar(&config.Debug, "debug", false, GetLangText("DebugMode"))
flag.UintVar(&startDelay, "startdelay", 0, GetLangText("StartDelay"))
flag.BoolVar(&noChdir, "nochdir", false, GetLangText("NoChdir"))
flag.BoolVar(&needHideWindow, "hidewindow", false, GetLangText("HideWindow"))
flag.BoolVar(&needHideSystray, "hidesystray", false, GetLangText("HideSystray"))
flag.Parse()
}
func ShowVersion() {
Log("ShowVersion", "%s %s", false, programName, programVersion)
}
func PrepareEnv() bool {
LoadLang(GetLangCode())
RegFlag()
ShowVersion()
if shortFlag_ShowVersion || longFlag_ShowVersion {
return false
}
if longFlag_configFilename != "" {
configFilename = longFlag_configFilename
} else if shortFlag_configFilename != "" {
configFilename = shortFlag_configFilename
}
if longFlag_additionConfigFilename != "" {
additionConfigFilename = longFlag_additionConfigFilename
} else if shortFlag_additionConfigFilename != "" {
additionConfigFilename = shortFlag_additionConfigFilename
}
path, err := os.Executable()
if err != nil {
Log("PrepareEnv", GetLangText("Error-DetectProgramPath"), false, err.Error())
return false
}
if !noChdir {
programDir := filepath.Dir(path)
if os.Chdir(programDir) == nil {
Log("PrepareEnv", GetLangText("Success-ChangeWorkingDir"), false, programDir)
LoadLang(GetLangCode())
} else {
Log("PrepareEnv", GetLangText("Failed-ChangeWorkingDir"), false, programDir)
}
}
return true
}