feat: Add proxy v2
All checks were successful
Build Docker / Build_Docker (push) Successful in 1m10s

This commit is contained in:
Nathan Woodburn 2024-05-30 21:12:31 +10:00
parent 2cdc96f4b1
commit 3c47a6c114
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -1,6 +1,7 @@
import socket import socket
from dnslib.server import DNSHandler, BaseResolver, DNSServer, DNSLogger from dnslib.server import DNSHandler, BaseResolver, DNSServer, DNSLogger
from dnslib import DNSRecord from dnslib import DNSRecord
import time
UDP_IP = '127.0.0.1' UDP_IP = '127.0.0.1'
UDP_PORT = 5350 UDP_PORT = 5350
@ -12,16 +13,18 @@ logger = DNSLogger(log="none")
class TCPHandler(DNSHandler): class TCPHandler(DNSHandler):
def handle(self): def handle(self):
try: try:
while True:
data = self.request.recv(1024).strip() data = self.request.recv(1024).strip()
dns_request = DNSRecord.parse(data) if not data:
break
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as udp_socket: with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as udp_socket:
udp_socket.sendto(data, (UDP_IP, UDP_PORT)) udp_socket.sendto(data, (UDP_IP, UDP_PORT))
response_data, _ = udp_socket.recvfrom(1024) response_data, _ = udp_socket.recvfrom(1024)
dns_response = DNSRecord.parse(response_data)
self.request.sendall(response_data) self.request.sendall(response_data)
except Exception as e: except Exception as e:
print(e) print(e)
class SimpleResolver(BaseResolver): class SimpleResolver(BaseResolver):
def resolve(self, request, handler): def resolve(self, request, handler):
try: try:
@ -33,17 +36,12 @@ class SimpleResolver(BaseResolver):
except Exception as e: except Exception as e:
return request.reply() return request.reply()
def start_tcp_server(): def start_tcp_server():
server = DNSServer(SimpleResolver(), address=TCP_IP, port=TCP_PORT, tcp=True, logger=logger) server = DNSServer(SimpleResolver(), address=TCP_IP, port=TCP_PORT, tcp=True, logger=logger)
print(f"TCP server listening on {TCP_IP}:{TCP_PORT}") print(f"TCP server listening on {TCP_IP}:{TCP_PORT}")
server.start_thread()
def start_udp_server():
server = DNSServer(SimpleResolver(), address=TCP_IP, port=TCP_PORT, logger=logger)
print(f"UDP server listening on {TCP_IP}:{TCP_PORT}")
server.start() server.start()
if __name__ == "__main__": if __name__ == "__main__":
start_tcp_server() start_tcp_server()
start_udp_server()
print("Started") print("Started")