hns_doh_loadbalancer/proxy.py

48 lines
1.4 KiB
Python
Raw Permalink Normal View History

2024-05-30 00:09:34 +10:00
import socket
from dnslib.server import DNSHandler, BaseResolver, DNSServer, DNSLogger
from dnslib import DNSRecord
2024-05-30 21:12:31 +10:00
import time
2024-05-30 00:09:34 +10:00
UDP_IP = '127.0.0.1'
UDP_PORT = 5350
TCP_IP = '0.0.0.0'
TCP_PORT = 5351
logger = DNSLogger(log="none")
class TCPHandler(DNSHandler):
def handle(self):
try:
2024-05-30 21:12:31 +10:00
while True:
data = self.request.recv(1024).strip()
if not data:
break
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as udp_socket:
udp_socket.sendto(data, (UDP_IP, UDP_PORT))
response_data, _ = udp_socket.recvfrom(1024)
self.request.sendall(response_data)
2024-05-30 00:09:34 +10:00
except Exception as e:
print(e)
2024-05-30 21:12:31 +10:00
2024-05-30 00:09:34 +10:00
class SimpleResolver(BaseResolver):
def resolve(self, request, handler):
try:
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as udp_socket:
udp_socket.sendto(request.pack(), (UDP_IP, UDP_PORT))
response_data, _ = udp_socket.recvfrom(1024)
dns_response = DNSRecord.parse(response_data)
return dns_response
except Exception as e:
return request.reply()
2024-05-30 21:12:31 +10:00
2024-05-30 00:09:34 +10:00
def start_tcp_server():
server = DNSServer(SimpleResolver(), address=TCP_IP, port=TCP_PORT, tcp=True, logger=logger)
print(f"TCP server listening on {TCP_IP}:{TCP_PORT}")
server.start()
if __name__ == "__main__":
start_tcp_server()
print("Started")