feat: Add tcp proxy v1

This commit is contained in:
Nathan Woodburn 2024-05-30 00:09:34 +10:00
parent d89dd3e0ca
commit 2cdc96f4b1
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 50 additions and 1 deletions

View File

@ -4,7 +4,7 @@ on:
push: push:
jobs: jobs:
Build Docker: Build_Docker:
runs-on: [ubuntu-latest, amd] # Add amd to require amd64 runs-on: [ubuntu-latest, amd] # Add amd to require amd64
steps: steps:
- name: Checkout - name: Checkout

49
proxy.py Normal file
View File

@ -0,0 +1,49 @@
import socket
from dnslib.server import DNSHandler, BaseResolver, DNSServer, DNSLogger
from dnslib import DNSRecord
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:
data = self.request.recv(1024).strip()
dns_request = DNSRecord.parse(data)
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)
dns_response = DNSRecord.parse(response_data)
self.request.sendall(response_data)
except Exception as e:
print(e)
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()
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_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()
if __name__ == "__main__":
start_tcp_server()
start_udp_server()
print("Started")