Created
August 4, 2023 15:06
-
-
Save joshfinley/f4d9ad9ea9d8b6586ccbd40aaaaf4ab2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 http.client | |
import socket | |
import select | |
import sys | |
import base64 | |
import argparse | |
def main(proxy_host, proxy_port, dest_host, dest_port, auth_file=None): | |
# Set up authentication | |
auth = '' | |
if auth_file: | |
with open(auth_file, 'r') as file: | |
auth = file.readline().strip() | |
auth = base64.b64encode(auth.encode()).decode() | |
# Set up HTTP CONNECT tunnel through proxy | |
conn = http.client.HTTPConnection(proxy_host, proxy_port) | |
conn.set_tunnel(dest_host, dest_port) | |
headers = {} | |
if auth: | |
headers['Proxy-Authorization'] = f'Basic {auth}' | |
conn.connect() | |
# Establish a raw socket connection for data transfer | |
sock = conn.sock | |
while True: | |
# Wait for input from user or server | |
ready_to_read, ready_to_write, in_error = select.select([sock, sys.stdin,], [], []) | |
# User has typed something | |
if sys.stdin in ready_to_read: | |
data = sys.stdin.readline() | |
if data: | |
sock.sendall(data.encode()) | |
else: | |
break | |
# Data has been received from the server | |
if sock in ready_to_read: | |
data = sock.recv(4096) | |
if data: | |
sys.stdout.write(data.decode()) | |
else: | |
break | |
if __name__ == "__main__": | |
# Create the parser | |
parser = argparse.ArgumentParser(description="Python Corkscrew") | |
# Define the command line arguments | |
parser.add_argument("proxy_host", help="The proxy server host name") | |
parser.add_argument("proxy_port", type=int, help="The proxy server port number") | |
parser.add_argument("dest_host", help="The destination host name") | |
parser.add_argument("dest_port", type=int, help="The destination port number") | |
parser.add_argument("auth_file", nargs='?', help="The file containing the username and password for authentication") | |
# Parse the command line arguments | |
args = parser.parse_args() | |
# Run the main function with the command line arguments | |
main(args.proxy_host, args.proxy_port, args.dest_host, args.dest_port, args.auth_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment