This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
dns_conf.go
182 lines (142 loc) · 4.35 KB
/
dns_conf.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
package netLayer
import (
"net"
"net/netip"
"github.com/e1732a364fed/v2ray_simple/utils"
"go.uber.org/zap"
)
type DnsConf struct {
Strategy int64 `toml:"strategy"` //0表示默认(和4含义相同), 4表示先查ip4后查ip6, 6表示先查6后查4; 40表示只查ipv4, 60 表示只查ipv6
Hosts map[string]any `toml:"hosts"` //用于强制指定哪些域名会被解析为哪些具体的ip;可以为一个ip字符串,or a []string, 内可以是A,AAAA或CNAME
Servers []any `toml:"servers"` //可以为一个地址url字符串,or a SpecialDnsServerConf; 如果第一个元素是url字符串形式,则此第一个元素将会被用作默认dns服务器
}
type SpecialDnsServerConf struct {
AddrUrlStr string `toml:"addr"` //必须为 udp://1.1.1.1:53 这种格式
Domains []string `toml:"domain"` //指定哪些域名需要通过 该dns服务器进行查询
}
func loadSpecialDnsServerConf_fromTomlUnmarshalledMap(m map[string]any) *SpecialDnsServerConf {
addr := m["addr"]
if addr == nil {
if ce := utils.CanLogErr("LoadDnsMachine, addr required"); ce != nil {
ce.Write()
}
return nil
}
addrStr, ok := addr.(string)
if !ok {
if ce := utils.CanLogErr("LoadDnsMachine, addr not a string"); ce != nil {
ce.Write()
}
return nil
}
domains := m["domain"]
if domains == nil {
if ce := utils.CanLogErr("LoadDnsMachine, domain required"); ce != nil {
ce.Write()
}
return nil
}
domainsAnySlice, ok := domains.([]any)
if !ok {
if ce := utils.CanLogErr("LoadDnsMachine, domain not a list"); ce != nil {
ce.Write()
}
return nil
}
domainsSlice := []string{}
for _, anyD := range domainsAnySlice {
dstr, ok := anyD.(string)
if !ok {
if ce := utils.CanLogErr("LoadDnsMachine, domain list contains non-string item"); ce != nil {
ce.Write()
}
return nil
}
domainsSlice = append(domainsSlice, dstr)
}
return &SpecialDnsServerConf{
Domains: domainsSlice,
AddrUrlStr: addrStr,
}
}
func LoadDnsMachine(conf *DnsConf) *DNSMachine {
var dm = &DNSMachine{TypeStrategy: conf.Strategy}
var ok = false
if len(conf.Servers) > 0 {
ok = true
servers := conf.Servers
dm.SpecialServerPollicy = make(map[string]string)
for _, ser := range servers {
switch server := ser.(type) {
case string:
ad, e := NewAddrByURL(server)
if e != nil {
if ce := utils.CanLogErr("LoadDnsMachine parse server url failed"); ce != nil {
ce.Write(zap.Error(e))
}
continue
}
if err := dm.AddNewServer(server, &ad); err != nil {
if ce := utils.CanLogErr("LoadDnsMachine, AddNewServer by string failed"); ce != nil {
ce.Write(zap.Error(err))
}
continue
}
case map[string]any:
realServer := loadSpecialDnsServerConf_fromTomlUnmarshalledMap(server)
if realServer == nil {
continue
}
if len(realServer.Domains) <= 0 { //既然是特殊dns服务器, 那么就必须指定哪些域名要使用该dns服务器进行查询
if ce := utils.CanLogErr("LoadDnsMachine, special domain list required"); ce != nil {
ce.Write()
}
continue
}
addr, e := NewAddrByURL(realServer.AddrUrlStr)
if e != nil {
if ce := utils.CanLogErr("LoadDnsMachine, server url invalid"); ce != nil {
ce.Write(zap.Error(e))
}
continue
}
if err := dm.AddNewServer(realServer.AddrUrlStr, &addr); err != nil {
if ce := utils.CanLogErr("LoadDnsMachine, AddNewServer by map failed"); ce != nil {
ce.Write(zap.Error(err))
}
continue
}
for _, thisdomain := range realServer.Domains {
dm.SpecialServerPollicy[thisdomain] = realServer.AddrUrlStr
}
}
}
}
if conf.Hosts != nil {
ok = true
dm.SpecialIPPollicy = make(map[string][]netip.Addr)
for thishost, things := range conf.Hosts {
switch value := things.(type) {
case string:
ip := net.ParseIP(value)
ad, _ := netip.AddrFromSlice(ip)
dm.SpecialIPPollicy[thishost] = []netip.Addr{ad}
case []string:
for _, str := range value {
ad, err := NewAddrFromAny(str)
if err != nil {
if ce := utils.CanLogErr("LoadDnsMachine loading SpecialIP from list failed"); ce != nil {
ce.Write(zap.Error(err))
}
return nil
}
dm.SpecialIPPollicy[thishost] = append(dm.SpecialIPPollicy[thishost], ad.GetHashable().Addr())
}
}
}
}
if !ok {
return nil
}
return dm
}