Last active
April 6, 2021 00:22
-
-
Save wsandin/4576b0e76c9254b2e8606dbbd1e00da1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import socket | |
import ssl | |
class DNSproxy(): | |
def __init__(self): | |
self.listen_addr = '127.0.0.1' | |
self.listen_port = 1053 | |
self.upstream_dns = '1.1.1.1' | |
self.upstream_port = 853 | |
def port_listener(self): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
try: | |
s.bind((self.listen_addr, self.listen_port)) | |
print('Succcess!') | |
except socket.error as e: | |
# Replace with syslog | |
print('[ERROR]: Socket bind failed. Err code: {} Message: {}').format(e[0], e[1]) | |
sys.exit() | |
s.listen(10) | |
while True: | |
conn, addr = s.accept() | |
print("{} {}").format(conn, addr) | |
self.handle_conn(conn=conn) | |
def handle_conn(self, conn): | |
while True: | |
data = conn.recv(1024) | |
if data: | |
resp = self.send_message(query=data) | |
conn.sendall(resp) | |
data = None | |
else: | |
break | |
conn.close() | |
def send_message(self, query, dns='1.1.1.1', ca_path='/etc/ssl/cert.pem'): | |
server = (dns, 853) | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.settimeout(20) | |
ctx = ssl.create_default_context() | |
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) | |
ctx.verify_mode = ssl.CERT_REQUIRED | |
ctx.check_hostname = False | |
ctx.load_verify_locations(ca_path) | |
wrapped_socket = ctx.wrap_socket(sock, server_hostname=dns) | |
wrapped_socket.connect(server) | |
print("Server peer certificate: %s", str(wrapped_socket.getpeercert())) | |
tcp_msg = query | |
print("Client request: %s", str(tcp_msg)) | |
wrapped_socket.send(tcp_msg) | |
data = wrapped_socket.recv(1024) | |
#print('Debug: {}').format(data) | |
return data | |
d = DNSproxy() | |
d.port_listener() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment