Skip to content

Commit 2760ef4

Browse files
committed
feat(server): add wildcard domain name resolution
1 parent 9095778 commit 2760ef4

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

smartdns/server.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,31 @@ def query(self, query, timeout=None, addr=None, edns=None):
7272
return defer.fail(failure.Failure(NotImplementedError(str(self.__class__) + " " + str(query.type))))
7373

7474
def lookupAddress(self, name, timeout=None, addr=None, edns=None):
75-
if name in self.a_mapping:
76-
ttl = self.a_mapping[name]['ttl']
7775

78-
def packResult(value):
79-
ret = []
80-
add = []
81-
for x in value:
82-
ret.append(dns.RRHeader(name, dns.A, dns.IN, ttl, dns.Record_A(x, ttl), True))
76+
def packResult(value, ttl):
77+
ret = []
78+
add = []
79+
for x in value:
80+
ret.append(dns.RRHeader(name, dns.A, dns.IN, ttl, dns.Record_A(x, ttl), True))
8381

84-
if edns is not None:
85-
if edns.rdlength > 8:
86-
add.append(dns.RRHeader('', sdns.EDNS, 4096, edns.ttl, edns.payload, True))
87-
else:
88-
add.append(dns.RRHeader('', sdns.EDNS, 4096, 0, sdns.Record_EDNS(None, 0), True))
89-
return [ret, (), add]
82+
if edns is not None:
83+
if edns.rdlength > 8:
84+
add.append(dns.RRHeader('', sdns.EDNS, 4096, edns.ttl, edns.payload, True))
85+
else:
86+
add.append(dns.RRHeader('', sdns.EDNS, 4096, 0, sdns.Record_EDNS(None, 0), True))
87+
return [ret, (), add]
9088

89+
wildcard = name[name.index("."):]
90+
if name in self.a_mapping:
91+
ttl = self.a_mapping[name]['ttl']
9192
result = self.finder.findIP(str(addr[0]), name)
92-
# 返回的IP数组乱序
93-
random.shuffle(result)
94-
return packResult(result)
93+
random.shuffle(result) # 返回的IP数组乱序
94+
return packResult(result, ttl)
95+
elif wildcard in self.a_mapping:
96+
ttl = self.a_mapping[wildcard]['ttl']
97+
result = self.finder.findIP(str(addr[0]), wildcard)
98+
random.shuffle(result) # 返回的IP数组乱序
99+
return packResult(result, ttl)
95100
else:
96101
return self._lookup(name, dns.IN, dns.A, timeout)
97102

templates/a.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
---
2+
.test.com:
3+
ttl: 3600
4+
default: 3.3.3.7 1.1.1.1
5+
中国,广东,,联通: 1.1.1.1 3.3.3.1
6+
中国,广东,,电信: 1.1.1.2 3.3.3.2
7+
28
test.test.com:
39
ttl: 3600
410
default: 3.3.3.7 1.1.1.1
@@ -12,4 +18,4 @@ test.test.com:
1218
中国,内蒙古,,: 1.1.1.8 3.3.3.8
1319
中国,,,: 1.1.1.9 3.3.3.9
1420
美国,,,: 1.1.1.10 3.3.3.10
15-
马来西亚,,,: 1.1.1.11 3.3.3.11
21+
马来西亚,,,: 1.1.1.11 3.3.3.11

0 commit comments

Comments
 (0)