-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforwarder.py
executable file
·107 lines (91 loc) · 3.63 KB
/
forwarder.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
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
#!/usr/bin/python3
# Copyright (c) 2020,2023 by Fred Morris Tacoma WA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""TCP-only DNS Forwarder.
Please read the README. Usage:
forwarder.py {--tls} <loopback-address> <dns-server-address> &
The above will run the script in the background. dns-server-address should be one of your
configured local caching resolvers (I don't recommend 8.8.8.8).
After running the script edit your network settings and change your resolver to loopback-address.
loopback-address will typically be 127.0.0.1 for IP4 or ::1 for IP6.
Specifying "--tls" establishes the connection with TLS, contacting the server on
port 853. (Also known as "DoT".)
"""
import sys
import asyncio
import ssl
class UDPListener(asyncio.DatagramProtocol):
def connection_made(self, transport):
self.transport = transport
if not hasattr(self, 'requests'):
self.requests = set()
return
async def handle_request(self, request, addr, task_promise):
reader, writer = await asyncio.open_connection(self.remote_address, self.ssl and 853 or 53, ssl=self.ssl)
# NOTE: When using TCP the request and response are prepended with
# the length of the request/response.
writer.write(len(request).to_bytes(2, byteorder='big')+request)
await writer.drain()
response_length = int.from_bytes(await reader.read(2), byteorder='big')
response = b''
while response_length:
resp = await reader.read(response_length)
if not len(resp):
break
response += resp
response_length -= len(resp)
writer.close()
self.transport.sendto(response, addr)
self.requests.remove(task_promise[0])
return
def datagram_received(self, request, addr):
promise = []
task = self.event_loop.create_task( self.handle_request( request, addr, promise ) )
promise.append(task)
self.requests.add(task)
return
def main():
try:
tls = sys.argv[1] == '--tls'
if tls:
listen_address, remote_address = sys.argv[2:4]
else:
listen_address, remote_address = sys.argv[1:3]
except:
print('Usage: forwarder.py {--tls} <udp-listen-address> <remote-server-address>', file=sys.stderr)
sys.exit(1)
event_loop = asyncio.get_event_loop()
listener = event_loop.create_datagram_endpoint(UDPListener, local_addr=(listen_address, 53))
try:
transport, service = event_loop.run_until_complete(listener)
except PermissionError:
print('Permission Denied! (are you root?)', file=sys.stderr)
sys.exit(1)
except OSError as e:
print('{} (did you supply a loopback address?)'.format(e), file=sys.stderr)
sys.exit(1)
service.remote_address = remote_address
service.event_loop = event_loop
if tls:
service.ssl = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
else:
service.ssl = None
try:
event_loop.run_forever()
except KeyboardInterrupt:
pass
transport.close()
event_loop.close()
if __name__ == "__main__":
main()