|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +NuGet Proxy Forwarder for Claude Code |
| 4 | +
|
| 5 | +.NET's HttpClient doesn't properly handle proxy credentials from environment variables. |
| 6 | +This forwarder accepts unauthenticated connections on localhost:8888 and forwards them |
| 7 | +to the authenticated Claude Code proxy with proper authentication headers. |
| 8 | +
|
| 9 | +Started automatically by install-dotnet.sh as a background daemon. |
| 10 | +""" |
| 11 | +import socket |
| 12 | +import threading |
| 13 | +import os |
| 14 | +import base64 |
| 15 | +import sys |
| 16 | +from urllib.parse import urlparse |
| 17 | + |
| 18 | +def forward_data(source, destination): |
| 19 | + """Forward data from source socket to destination socket""" |
| 20 | + try: |
| 21 | + while True: |
| 22 | + data = source.recv(4096) |
| 23 | + if not data: |
| 24 | + break |
| 25 | + destination.sendall(data) |
| 26 | + except: |
| 27 | + pass |
| 28 | + |
| 29 | +def handle_client(client_socket, client_addr, proxy_host, proxy_port, proxy_auth_header): |
| 30 | + """Handle a client connection""" |
| 31 | + try: |
| 32 | + # Read the CONNECT request |
| 33 | + request = b"" |
| 34 | + while b"\r\n\r\n" not in request: |
| 35 | + chunk = client_socket.recv(1) |
| 36 | + if not chunk: |
| 37 | + return |
| 38 | + request += chunk |
| 39 | + |
| 40 | + request_str = request.decode('utf-8', errors='ignore') |
| 41 | + lines = request_str.split('\r\n') |
| 42 | + |
| 43 | + if not lines[0].startswith('CONNECT'): |
| 44 | + client_socket.close() |
| 45 | + return |
| 46 | + |
| 47 | + # Parse CONNECT target |
| 48 | + target = lines[0].split(' ')[1] |
| 49 | + |
| 50 | + # Connect to real proxy |
| 51 | + proxy_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 52 | + proxy_socket.connect((proxy_host, proxy_port)) |
| 53 | + |
| 54 | + # Forward CONNECT request with authentication |
| 55 | + connect_request = f"CONNECT {target} HTTP/1.1\r\n" |
| 56 | + connect_request += f"Host: {target}\r\n" |
| 57 | + if proxy_auth_header: |
| 58 | + connect_request += proxy_auth_header |
| 59 | + connect_request += "\r\n" |
| 60 | + |
| 61 | + proxy_socket.sendall(connect_request.encode()) |
| 62 | + |
| 63 | + # Read proxy response |
| 64 | + response = b"" |
| 65 | + while b"\r\n\r\n" not in response: |
| 66 | + chunk = proxy_socket.recv(1) |
| 67 | + if not chunk: |
| 68 | + client_socket.close() |
| 69 | + proxy_socket.close() |
| 70 | + return |
| 71 | + response += chunk |
| 72 | + |
| 73 | + # Forward response to client |
| 74 | + client_socket.sendall(response) |
| 75 | + |
| 76 | + response_str = response.decode('utf-8', errors='ignore') |
| 77 | + status_line = response_str.split('\r\n')[0] |
| 78 | + |
| 79 | + # If successful, start bidirectional forwarding |
| 80 | + if "200" in status_line: |
| 81 | + # Start forwarding threads |
| 82 | + client_to_proxy = threading.Thread(target=forward_data, args=(client_socket, proxy_socket)) |
| 83 | + proxy_to_client = threading.Thread(target=forward_data, args=(proxy_socket, client_socket)) |
| 84 | + |
| 85 | + client_to_proxy.daemon = True |
| 86 | + proxy_to_client.daemon = True |
| 87 | + |
| 88 | + client_to_proxy.start() |
| 89 | + proxy_to_client.start() |
| 90 | + |
| 91 | + # Wait for either thread to finish |
| 92 | + client_to_proxy.join() |
| 93 | + proxy_to_client.join() |
| 94 | + |
| 95 | + except Exception as e: |
| 96 | + pass # Silently handle errors to avoid log spam |
| 97 | + finally: |
| 98 | + try: |
| 99 | + client_socket.close() |
| 100 | + except: |
| 101 | + pass |
| 102 | + try: |
| 103 | + proxy_socket.close() |
| 104 | + except: |
| 105 | + pass |
| 106 | + |
| 107 | +def main(): |
| 108 | + # Parse the real proxy from environment |
| 109 | + proxy_url = os.environ.get('HTTPS_PROXY') or os.environ.get('HTTP_PROXY') |
| 110 | + if not proxy_url: |
| 111 | + print("Error: No HTTPS_PROXY or HTTP_PROXY environment variable set", file=sys.stderr) |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + proxy_uri = urlparse(proxy_url) |
| 115 | + proxy_host = proxy_uri.hostname |
| 116 | + proxy_port = proxy_uri.port or 80 |
| 117 | + proxy_auth = proxy_uri.username and proxy_uri.password |
| 118 | + |
| 119 | + if proxy_auth: |
| 120 | + auth_string = f"{proxy_uri.username}:{proxy_uri.password}" |
| 121 | + proxy_auth_header = "Proxy-Authorization: Basic " + base64.b64encode(auth_string.encode()).decode() + "\r\n" |
| 122 | + else: |
| 123 | + proxy_auth_header = "" |
| 124 | + |
| 125 | + # Create server socket |
| 126 | + server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 127 | + server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 128 | + |
| 129 | + try: |
| 130 | + server.bind(('127.0.0.1', 8888)) |
| 131 | + server.listen(5) |
| 132 | + except OSError as e: |
| 133 | + # Port already in use - another forwarder is running |
| 134 | + sys.exit(0) |
| 135 | + |
| 136 | + # Daemonize - close stdout/stderr to run silently in background |
| 137 | + sys.stdout.close() |
| 138 | + sys.stderr.close() |
| 139 | + |
| 140 | + try: |
| 141 | + while True: |
| 142 | + client_sock, client_addr = server.accept() |
| 143 | + client_thread = threading.Thread( |
| 144 | + target=handle_client, |
| 145 | + args=(client_sock, client_addr, proxy_host, proxy_port, proxy_auth_header) |
| 146 | + ) |
| 147 | + client_thread.daemon = True |
| 148 | + client_thread.start() |
| 149 | + except KeyboardInterrupt: |
| 150 | + server.close() |
| 151 | + |
| 152 | +if __name__ == '__main__': |
| 153 | + main() |
0 commit comments