Prechádzať zdrojové kódy

Add an internal PAC file to bypass QUICK_IMAGES requests and improve performance on not-so-old browsers.

RichardG867 10 rokov pred
rodič
commit
f8f3797c85
2 zmenil súbory, kde vykonal 25 pridanie a 3 odobranie
  1. 3 1
      README.md
  2. 22 2
      waybackproxy.py

+ 3 - 1
README.md

@@ -8,7 +8,9 @@ WaybackProxy is a HTTP proxy that sends all requests through the [Internet Archi
 
 1. Edit `config.py` to your liking
 2. Start `waybackproxy.py`
-3. Set your antique browser to use a HTTP proxy at the IP and port WaybackProxy is listening on
+3. Set up your antique browser:
+	* If your browser supports proxy auto-configuration, set the auto-configuration URL to `http://ip:port/proxy.pac` where `ip` is the IP of the system running WaybackProxy and `port` is the proxy's port (8888 by default).
+	* If proxy auto-configuration is not supported or fails to work, set the browser to use an HTTP proxy at that IP and port instead.
 4. Try it out! You can edit most settings that are in `config.py` by browsing to http://web.archive.org while on the proxy, although you must edit `config.py` to make them permanent.
 5. Press Ctrl+C to stop
 

+ 22 - 2
waybackproxy.py

@@ -36,12 +36,32 @@ class Handler(SocketServer.BaseRequestHandler):
 		host = parsed.netloc.split(':')
 		hostname = host[0]
 		
-		# read out the headers
+		# read out the headers, saving the PAC file host
+		pac_host = '" + location.host + ":' + str(LISTEN_PORT) # may not actually work
 		while line.rstrip('\r\n') != '':
 			line = f.readline()
+			if line[:6].lower() == 'host: ':
+				pac_host = line[6:].rstrip('\r\n')
+				if ':' not in pac_host: # who would run this on port 80 anyway?
+					pac_host += ':80'
 		
 		try:
-			if hostname == 'web.archive.org':
+			if path == '/proxy.pac':
+				# PAC file to bypass QUICK_IMAGES requests
+				pac  = http_version + ''' 200 OK\r\n'''
+				pac += '''Content-Type: application/x-ns-proxy-autoconfig\r\n'''
+				pac += '''\r\n'''
+				pac += '''function FindProxyForURL(url, host)\r\n'''
+				pac += '''{\r\n'''
+				pac += '''	if (shExpMatch(url, "http://web.archive.org/web/*"))\r\n'''
+				pac += '''	{\r\n'''
+				pac += '''		return "DIRECT";\r\n'''
+				pac += '''	}\r\n'''
+				pac += '''	return "PROXY {0}";\r\n'''.format(pac_host)
+				pac += '''}\r\n'''
+				self.request.sendall(pac)
+				return
+			elif hostname == 'web.archive.org':
 				if path[:5] != '/web/':
 					# launch settings
 					return self.handle_settings(parsed.query)