A Simple HTTP Proxy
A very simple proof-of-concept local HTTP proxy using Python’s builtin
http.server module, and external
from http.server import HTTPServer, BaseHTTPRequestHandler import requests class SimpleHTTPRequestHandler(BaseHTTPRequestHandler): def do_GET(self): req = requests.get(self.path) self.send_response(req.status_code) self.end_headers() self.wfile.write(req.content) httpd = HTTPServer(('localhost', 1080), SimpleHTTPRequestHandler) httpd.serve_forever()
The proxy server can be configured in system or browser settings, for example on Firefox:
This serves only HTTP (not HTTPS) resources through GET requests. Requests from an HTTP client (e.g. browser) are sent to the proxy server. The path in the HTTP request line is the absolute path of the of the resource to be served from the proxy-server:
127.0.0.1 - - [18/Feb/2023 11:21:27] "GET http://example.com/ HTTP/1.1" 200 -
Note that here the other headers from the webserver are stripped (e.g. cookies), although could be easily relayed. To add support from HTTPS webservers is slightly less trivial. To ensure end-to-end encryption the proxy server should not be able to intercept unencrypted communications between the client and target web server. This is achieved using the CONNECT HTTP method, which opens a TCP tunnel to the destination with the proxy server simply forwarding traffic.