Created
January 24, 2023 05:17
-
-
Save autosquid/38df328c1b0639268a7dfab986e1c26b to your computer and use it in GitHub Desktop.
A mini web proxy to accelerate github access.
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
# Author: Jay | |
# Date: 2023-01-24 | |
# Description: A mini web proxy to accelerate github access. | |
# Usage: python3 mini_web_proxy.py | |
# Note: You need to set up a http/https proxy first | |
import http.client | |
import http.server | |
import logging | |
import socketserver | |
import urllib, urllib.request | |
PORT = 2023 | |
class MiddlemanHandler(http.server.BaseHTTPRequestHandler): | |
def do_GET(self): | |
print("MiddlemanHandler got a GET request for", self.path) | |
real_request_url = self.path[1:] | |
logging.info('Real url %s', real_request_url) | |
response = urllib.request.urlopen(real_request_url) | |
self.send_response(response.status) | |
for h in response.headers.items(): | |
self.send_header(h[0], h[1]) | |
self.end_headers() | |
self.wfile.write(response.read()) | |
def serve(): | |
# Set up the upstream | |
proxy = urllib.request.ProxyHandler({'http': 'http://127.0.0.1:1085', 'https': 'http://127.0.0.1:1085'}) | |
logging.info('Proxy set') | |
opener = urllib.request.build_opener(proxy) | |
logging.info('Opener ready') | |
urllib.request.install_opener(opener) | |
logging.info('Opener installed') | |
Handler = MiddlemanHandler | |
logging.info("serving at port %d", PORT) | |
# Kill when keyboard interrupt | |
httpd = socketserver.TCPServer(("", PORT), Handler) | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
httpd.server_close() | |
finally: | |
httpd.server_close() | |
# Main entry | |
if __name__ == '__main__': | |
serve() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment