forked from gh-tt/cloudflare-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
118 lines (113 loc) · 2.91 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
package main
import (
"fmt"
"github.com/spf13/viper"
)
type Config struct {
selectCountEveryIp int
ipFilename string
pingRoutine int
pingCount int
speedTestCount int
downloadSecond int
downloadRoutine int
downloadUrl string
rttLimit float64
recvRateLimit float64
isOutputTxt bool
outputCount int
}
type DnsConfig struct {
modifyEnable bool
dnspodToken,
domain,
subDomain,
recordId,
recordType,
recordLine string
speedLimit float64
}
var Conf *Config
var DnsConf *DnsConfig
func initConfig() {
viper.AddConfigPath(".")
viper.SetConfigType("yaml")
viper.SetConfigName("config")
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Sprintf("读取配置文件失败,请检查 config.yaml 配置文件是否存在: %v", err))
}
Conf = newConfig()
DnsConf = newDnsConfig()
}
func newConfig() *Config {
pingRoutine := viper.GetInt("pingRoutine")
if pingRoutine <= 0 {
pingRoutine = 100
}
pingCount := viper.GetInt("pingCount")
if pingCount <= 0 {
pingCount = 10
}
speedTestCount := viper.GetInt("downloadTestCount")
if speedTestCount <= 0 {
speedTestCount = 10
}
downloadSecond := viper.GetInt("downloadSecond")
if downloadSecond <= 0 {
downloadSecond = 10
}
downloadRoutine := viper.GetInt("downloadRoutine")
if downloadRoutine <= 0 {
downloadRoutine = 1
}
downloadUrl := viper.GetString("downloadUrl")
if downloadUrl == "" {
downloadUrl = "https://storage.idx0.workers.dev/Images/public-notion-06b4a73f-0d4e-4b8f-b273-77becf84a0b3.png"
}
rttLimit := viper.GetFloat64("rttLimit")
if rttLimit <= 0 {
rttLimit = 200
}
recvRateLimit := viper.GetFloat64("recvRateLimit")
if recvRateLimit <= 0 || recvRateLimit > 100 {
recvRateLimit = 0
}
ipFilename := viper.GetString("ipFilename")
if ipFilename == "" {
ipFilename = "ip.txt"
}
outputCount := viper.GetInt("outputCount")
if outputCount <= 0 {
outputCount = 5
}
return &Config{
selectCountEveryIp: viper.GetInt("selectCountEveryIp"),
ipFilename: ipFilename,
pingRoutine: pingRoutine,
pingCount: pingCount,
speedTestCount: speedTestCount,
downloadSecond: downloadSecond,
downloadRoutine: downloadRoutine,
downloadUrl: downloadUrl,
rttLimit: rttLimit,
recvRateLimit: recvRateLimit,
isOutputTxt: viper.GetBool("isOutputTxt"),
outputCount: outputCount,
}
}
func newDnsConfig() *DnsConfig {
speedLimit := viper.GetFloat64("dns.speedLimit")
if speedLimit <= 0 {
speedLimit = 0
}
return &DnsConfig{
modifyEnable: viper.GetBool("dns.modifyEnable"),
dnspodToken: viper.GetString("dns.dnspodToken"),
domain: viper.GetString("dns.domain"),
subDomain: viper.GetString("dns.subDomain"),
recordId: viper.GetString("dns.recordId"),
recordType: viper.GetString("dns.recordType"),
recordLine: viper.GetString("dns.recordLine"),
speedLimit: speedLimit,
}
}