-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
173 lines (161 loc) · 5.01 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
package main
import (
"fmt"
"gopkg.in/yaml.v3"
"os"
"regexp"
"strings"
)
var (
ExecConfig = ConfigModel{
Dns53Config: Dns53ConfigModel{
Enabled: false,
Listen: "tcp://:53,udp://53",
Upstream: "https://dns.google/dns-query",
UpstreamProto: "doh",
EcsIP2nd: "",
},
DohConfig: DohConfigModel{
Enabled: false,
Listen: DefaultDohListen,
Upstream: "tcp://8.8.8.8:53,tcp://9.9.9.9:53",
UpstreamProto: "dns53",
Path: "/dns-query",
EcsIP2nd: "",
UseTls: false,
TLSCertFile: "",
TLSKeyFile: "",
},
CacheEnabled: false,
CacheBackend: CacheTypeInternal,
RedisURI: "",
GeoIPCityDBPath: "",
LogLevel: "info",
IPv6Answer: false,
NamesInJail: []NameInJailConfigModel{},
}
NamesInJailConfig = map[string][]*regexp.Regexp{}
)
const (
RelayUpstreamProtoDoh = "doh"
RelayUpstreamProtoJson = "doh_json"
RelayUpstreamProtoDns53 = "dns53"
)
type UpstreamType string
type NameInJailConfigModel struct {
NameRegex string `yaml:"name_regex"`
CountryCodes string `yaml:"country_codes"`
}
type FixedResolvingConfigModel struct {
NameRegex string `yaml:"name_regex"`
Server string `yaml:"server"`
}
type Dns53ConfigModel struct {
Enabled bool `yaml:"enabled"`
Listen string `yaml:"listen"`
Upstream string `yaml:"upstream"`
UpstreamFallback string `yaml:"upstream_fallback"`
UpstreamProto string `yaml:"upstream_proto"`
EcsIP1st string `yaml:"1st_ecs_ip"`
EcsIP2nd string `yaml:"2nd_ecs_ip"`
UseClientIP bool `yaml:"use_client_ip"`
FixedResolving []FixedResolvingConfigModel `yaml:"fixed_resolving"`
}
type DohConfigModel struct {
Enabled bool `yaml:"enabled"`
Listen string `yaml:"listen"`
Upstream string `yaml:"upstream"`
UpstreamFallback string `yaml:"upstream_fallback"`
UpstreamProto string `yaml:"upstream_proto"`
Path string `yaml:"path"`
EcsIP1st string `yaml:"1st_ecs_ip"`
EcsIP2nd string `yaml:"2nd_ecs_ip"`
UseTls bool `yaml:"use_tls"`
TLSCertFile string `yaml:"tls_cert_file"`
TLSKeyFile string `yaml:"tls_key_file"`
UseClientIP bool `yaml:"use_client_ip"`
FixedResolving []FixedResolvingConfigModel `yaml:"fixed_resolving"`
}
type ConfigModel struct {
Dns53Config Dns53ConfigModel `yaml:"dns53"`
DohConfig DohConfigModel `yaml:"doh"`
CacheEnabled bool `yaml:"cache_enabled"`
CacheBackend string `yaml:"cache_backend"`
RedisURI string `yaml:"redis_uri"`
GeoIPCityDBPath string `yaml:"geoip_city_db_path"`
LogLevel string `yaml:"log_level"`
IPv6Answer bool `yaml:"ipv6_answer"`
NamesInJail []NameInJailConfigModel `yaml:"names_in_jail"`
UpstreamHostResolver string `yaml:"upstream_host_resolver"`
}
func ReadConfigFromFile(path string) (config ConfigModel) {
file, err := os.ReadFile(path)
if err != nil {
fmt.Println("Read file error:", err)
panic(err)
}
err = yaml.Unmarshal(file, &config)
if err != nil {
fmt.Println("Unmarshal config file error:", err)
panic(err)
}
ExecConfig = config
for _, nameInJail := range ExecConfig.NamesInJail {
regexp_, err := regexp.Compile(nameInJail.NameRegex)
if err != nil {
fmt.Println("Compile regex error:", err)
continue
}
countryCodes_ := strings.Split(nameInJail.CountryCodes, ",")
for _, countryCode := range countryCodes_ {
countryCode = strings.TrimSpace(countryCode)
if countryCode == "" {
continue
}
if _, ok := NamesInJailConfig[countryCode]; !ok {
NamesInJailConfig[countryCode] = []*regexp.Regexp{}
}
NamesInJailConfig[countryCode] = append(NamesInJailConfig[countryCode], regexp_)
}
}
return
}
func IsNameInJailOfCountry(name, countryCode string) bool {
regexps_, ok := NamesInJailConfig[countryCode]
if !ok {
return false
}
chRet_, chFinal_, ret_ := make(chan bool, len(regexps_)), make(chan bool), false
go func() {
for i := 0; i < len(regexps_); i++ {
if <-chRet_ {
ret_ = true
drainChannelLoop:
for {
select {
case _ = <-chRet_:
default:
break drainChannelLoop
}
}
break
}
}
chFinal_ <- true
}()
for _, regexp_ := range regexps_ {
go func(re *regexp.Regexp) {
matched_ := re.MatchString(name)
if ret_ {
return
}
if matched_ {
chRet_ <- true
} else {
chRet_ <- false
}
}(regexp_)
}
<-chFinal_
return ret_
}