-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.py
77 lines (66 loc) · 2.2 KB
/
dns.py
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
from inspect import cleandoc
from time import sleep
from dnslib.intercept import InterceptResolver
from dnslib.server import DNSServer
import netifaces
# Port used by the DNS server
_port = 53
def get_lan_ips():
try:
iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
ipv4 = netifaces.ifaddresses(iface)[netifaces.AF_INET][0]['addr']
except Exception:
ipv4 = None
try:
iface = netifaces.gateways()['default'][netifaces.AF_INET6][1]
ipv6 = netifaces.ifaddresses(iface)[netifaces.AF_INET6][0]['addr']
except Exception:
ipv6 = None
return ipv4, ipv6
def start(port):
lan_ipv4, lan_ipv6 = get_lan_ips()
zone_record = ''
if lan_ipv4:
zone_record += cleandoc(f'''
danmakujp4-local.lcx.tokyo. 60 IN A {lan_ipv4}
danmakujp4-v1.lcx.tokyo. 60 IN A {lan_ipv4}
player-api-local.dena-takasho.com. 60 IN A {lan_ipv4}
''')
zone_record += '\n'
if lan_ipv6:
zone_record += cleandoc(f'''
danmakujp4-local.lcx.tokyo. 60 IN AAAA {lan_ipv6}
danmakujp4-v1.lcx.tokyo. 60 IN AAAA {lan_ipv6}
player-api-local.dena-takasho.com. 60 IN AAAA {lan_ipv6}
''')
zone_record += '\n'
# Uncomment these lines to use local asset server
# if lan_ipv4:
# zone_record += 'dankagu-assets.rainbowunicorn7297.com. 60 IN A ' \
# + '{lan_ipv4}\n'
# if lan_ipv6:
# zone_record += 'dankagu-assets.rainbowunicorn7297.com. 60 IN AAAA ' \
# + '{lan_ipv6}\n'
resolver = InterceptResolver(
'8.8.8.8', #upstream address
53, #upstream port
'60s', #ttl
[zone_record], #incercept
[], #skip
[], #nxdomain
[], #forward
False, #all_qtypes
5 #timeout
)
udp_server = DNSServer(resolver, port=port)
print(f'DNS is running on port {port}...')
print(f'IPv4: {lan_ipv4}')
print(f'IPv6: {lan_ipv6}')
udp_server.start_thread()
if __name__ == '__main__':
start(_port)
try:
while True:
sleep(1)
except KeyboardInterrupt:
pass