waybackproxy.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #!/usr/bin/env python3
  2. import base64, datetime, json, lrudict, re, socket, socketserver, string, sys, threading, time, traceback, urllib.parse
  3. try:
  4. import urllib3
  5. except ImportError:
  6. print('WaybackProxy now requires urllib3 to be installed. Follow setup step 3 on the readme to fix this.')
  7. sys.exit(1)
  8. from config_handler import *
  9. class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
  10. """TCPServer with ThreadingMixIn added."""
  11. pass
  12. class SharedState:
  13. """Class for storing shared state across instances of Handler."""
  14. def __init__(self):
  15. # Create urllib3 connection pool.
  16. self.http = urllib3.PoolManager(maxsize=4, block=True)
  17. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  18. # Create internal LRU dictionary for preserving URLs on redirect.
  19. self.date_cache = lrudict.LRUDict(maxduration=86400, maxsize=1024)
  20. # Create internal LRU dictionary for date availability.
  21. self.availability_cache = lrudict.LRUDict(maxduration=86400, maxsize=1024)
  22. # Read domain whitelist file.
  23. try:
  24. with open('whitelist.txt', 'r') as f:
  25. self.whitelist = f.read().splitlines()
  26. except:
  27. self.whitelist = []
  28. shared_state = SharedState()
  29. class Handler(socketserver.BaseRequestHandler):
  30. """Main request handler."""
  31. def setup(self, *args, **kwargs):
  32. """Set up this instance of Handler."""
  33. super().setup(*args, **kwargs)
  34. # Store a local pointer to SharedState.
  35. self.shared_state = shared_state
  36. def handle(self):
  37. """Handle a request."""
  38. # readline is pretty convenient
  39. f = self.request.makefile()
  40. # read request line
  41. reqline = line = f.readline()
  42. split = line.rstrip().split(' ')
  43. http_version = len(split) > 2 and split[2] or 'HTTP/0.9'
  44. if len(split) < 2 or split[0] != 'GET':
  45. # only GET is implemented
  46. return self.send_error_page(http_version, 501, 'Not Implemented')
  47. # read out the headers
  48. request_host = None
  49. pac_host = '" + location.host + ":' + str(LISTEN_PORT) # may not actually work
  50. effective_date = DATE
  51. auth = None
  52. while line.strip() != '':
  53. line = f.readline()
  54. ll = line.lower()
  55. if ll[:6] == 'host: ':
  56. pac_host = request_host = line[6:].rstrip()
  57. if ':' not in pac_host: # explicitly specify port if running on port 80
  58. pac_host += ':80'
  59. elif ll[:21] == 'x-waybackproxy-date: ':
  60. # API for a personal project of mine
  61. effective_date = line[21:].rstrip()
  62. elif ll[:21] == 'authorization: basic ':
  63. # asset date code passed as username:password
  64. auth = base64.b64decode(ll[21:])
  65. # parse the URL
  66. pac_file_paths = ('/proxy.pac', '/wpad.dat', '/wpad.da')
  67. if split[1][0] == '/' and split[1] not in pac_file_paths:
  68. # just a path (not corresponding to a PAC file) => transparent proxy
  69. # Host header and therefore HTTP/1.1 are required
  70. if not request_host:
  71. return self.send_error_page(http_version, 400, 'Host header missing')
  72. archived_url = 'http://' + request_host + split[1]
  73. else:
  74. # full URL => explicit proxy
  75. archived_url = split[1]
  76. request_url = archived_url
  77. parsed = urllib.parse.urlparse(request_url)
  78. # make a path
  79. path = parsed.path
  80. if parsed.query:
  81. path += '?' + parsed.query
  82. elif path == '':
  83. path == '/'
  84. # get the hostname for later
  85. host = parsed.netloc.split(':')
  86. hostname = host[0]
  87. # get cached date for redirects, if available
  88. original_date = effective_date
  89. effective_date = self.shared_state.date_cache.get(str(effective_date) + '\x00' + str(archived_url), effective_date)
  90. # get date from username:password, if available
  91. if auth:
  92. effective_date = auth.replace(':', '')
  93. # Effectively handle the request.
  94. try:
  95. if path in pac_file_paths:
  96. # PAC file to bypass QUICK_IMAGES requests if WAYBACK_API is not enabled.
  97. pac = http_version + ''' 200 OK\r\n'''
  98. pac += '''Content-Type: application/x-ns-proxy-autoconfig\r\n'''
  99. pac += '''\r\n'''
  100. pac += '''function FindProxyForURL(url, host)\r\n'''
  101. pac += '''{\r\n'''
  102. if not WAYBACK_API:
  103. pac += ''' if (shExpMatch(url, "http://web.archive.org/web/*") && !shExpMatch(url, "http://web.archive.org/web/??????????????if_/*"))\r\n'''
  104. pac += ''' {\r\n'''
  105. pac += ''' return "DIRECT";\r\n'''
  106. pac += ''' }\r\n'''
  107. pac += ''' return "PROXY ''' + pac_host + '''";\r\n'''
  108. pac += '''}\r\n'''
  109. self.request.sendall(pac.encode('ascii', 'ignore'))
  110. return
  111. elif hostname in self.shared_state.whitelist:
  112. _print('[>] [byp]', archived_url)
  113. elif hostname == 'web.archive.org':
  114. if path[:5] != '/web/':
  115. # Launch settings if enabled.
  116. if SETTINGS_PAGE:
  117. return self.handle_settings(parsed.query)
  118. else:
  119. return self.send_error_page(http_version, 404, 'Not Found')
  120. else:
  121. # Pass requests through to web.archive.org. Required for QUICK_IMAGES.
  122. split = request_url.split('/')
  123. effective_date = split[4]
  124. archived_url = '/'.join(split[5:])
  125. _print('[>] [QI]', archived_url)
  126. elif GEOCITIES_FIX and hostname == 'www.geocities.com':
  127. # Apply GEOCITIES_FIX and pass it through.
  128. _print('[>]', archived_url)
  129. split = archived_url.split('/')
  130. hostname = split[2] = 'www.oocities.org'
  131. request_url = '/'.join(split)
  132. else:
  133. # Get from the Wayback Machine.
  134. _print('[>]', archived_url)
  135. request_url = 'https://web.archive.org/web/{0}if_/{1}'.format(effective_date, archived_url)
  136. # Check Wayback Machine Availability API where applicable, to avoid archived 404 pages and other site errors.
  137. split = request_url.split('/')
  138. if split[2] == 'web.archive.org':
  139. # Remove extraneous :80 from URL.
  140. if ':' in split[5]:
  141. if split[7][-3:] == ':80':
  142. split[7] = split[7][:-3]
  143. elif split[5][-3:] == ':80':
  144. split[5] = split[5][:-3]
  145. # Check availability LRU cache.
  146. availability_url = '/'.join(split[5:])
  147. new_url = self.shared_state.availability_cache.get(availability_url, None)
  148. if new_url:
  149. # In cache => replace URL immediately.
  150. request_url = new_url
  151. elif WAYBACK_API:
  152. # Not in cache => contact API.
  153. try:
  154. availability_endpoint = 'https://archive.org/wayback/available?url=' + urllib.parse.quote_plus(availability_url) + '&timestamp=' + effective_date[:14]
  155. availability = json.loads(self.shared_state.http.request('GET', availability_endpoint, timeout=10, retries=1).data)
  156. closest = availability.get('archived_snapshots', {}).get('closest', {})
  157. new_date = closest.get('timestamp', None)
  158. except:
  159. _print('[!] Failed to fetch Wayback availability data')
  160. new_date = None
  161. if new_date and new_date != effective_date[:14]:
  162. # Returned date is different.
  163. new_url = closest['url']
  164. # Add asset tag to the date.
  165. split = new_url.split('/')
  166. if len(effective_date) > 14:
  167. split[4] += effective_date[14:]
  168. else:
  169. split[4] += 'if_'
  170. new_url = '/'.join(split)
  171. # Replace URL and add it to the availability cache.
  172. request_url = self.shared_state.availability_cache[availability_url] = new_url
  173. # Start fetching the URL.
  174. retry = urllib3.util.retry.Retry(total=10, connect=10, read=5, redirect=0, backoff_factor=1)
  175. while True:
  176. conn = self.shared_state.http.urlopen('GET', request_url, redirect=False, retries=retry, preload_content=False)
  177. # Check for redirects.
  178. destination = conn.get_redirect_location()
  179. if destination:
  180. self.drain_conn(conn)
  181. conn.release_conn()
  182. # Check if the redirect goes to a different Wayback URL.
  183. match = re.search('''(?:(?:https?:)?//web.archive.org)?/web/([^/]+/)(.+)''', destination)
  184. if match:
  185. archived_dest = match.group(2)
  186. # Add missing protocol, just in case.
  187. split = archived_dest.split('/')
  188. if split[0][-1:] != ':':
  189. split = ['http:', ''] + split
  190. # Remove extraneous :80 from URL.
  191. if split[2][-3:] == ':80':
  192. split[2] = split[2][:-3]
  193. # Check if the archived URL is different.
  194. if archived_dest != archived_url:
  195. # Add destination to availability cache and redirect the client.
  196. _print('[r]', archived_dest)
  197. new_url = '/'.join(split)
  198. self.shared_state.availability_cache[archived_dest] = 'http://web.archive.org/web/' + match.group(1) + archived_dest
  199. return self.send_redirect_page(http_version, archived_dest, conn.status)
  200. # Not an archived URL or same URL, redirect ourselves.
  201. request_url = destination
  202. continue
  203. # Wayback will add its JavaScript to anything it thinks is JavaScript.
  204. # If this is detected, redirect ourselves through the raw asset interface.
  205. content_type = conn.headers.get('Content-Type')
  206. guessed_content_type = conn.headers.get('X-Archive-Guessed-Content-Type')
  207. if not guessed_content_type:
  208. guessed_content_type = content_type
  209. if 'javascript' in guessed_content_type:
  210. match = re.match('''(https?://web\\.archive\\.org/web/[0-9]+)([^/]*)(.+)''', request_url)
  211. if match and match.group(2) != 'im_':
  212. self.drain_conn(conn)
  213. conn.release_conn()
  214. request_url = match.group(1) + 'im_' + match.group(3)
  215. continue
  216. # This request can proceed.
  217. break
  218. except urllib3.exceptions.MaxRetryError as e:
  219. _print('[!] Fetch retries exceeded:', e.reason)
  220. return self.send_error_page(http_version, 504, 'Gateway Timeout')
  221. except:
  222. # Some other fetch exception has occurred.
  223. _print('[!] Fetch exception:')
  224. traceback.print_exc()
  225. return self.send_error_page(http_version, 502, 'Bad Gateway')
  226. # Check for HTTP errors.
  227. if conn.status != 200:
  228. if conn.status in (403, 404): # not found
  229. if self.guess_and_send_redirect(http_version, archived_url):
  230. self.drain_conn(conn)
  231. conn.release_conn()
  232. return
  233. #elif conn.status in (301, 302): # redirect loop detection currently unused
  234. # self.drain_conn(conn)
  235. # conn.release_conn()
  236. # return self.send_error_page(http_version, 508, 'Infinite Redirect Loop')
  237. if conn.status != 412: # tolerance exceeded has its own error message above
  238. _print('[!]', conn.status, conn.reason)
  239. # If the memento Link header is present, this is a website error
  240. # instead of a Wayback error. Pass it along if that's the case.
  241. if 'Link' not in conn.headers:
  242. self.drain_conn(conn)
  243. conn.release_conn()
  244. return self.send_error_page(http_version, conn.status, conn.reason)
  245. # Adjust content type.
  246. if content_type == None:
  247. content_type = 'text/html'
  248. elif not CONTENT_TYPE_ENCODING:
  249. idx = content_type.find(';')
  250. if idx > -1:
  251. content_type = content_type[:idx]
  252. # Set the archive mode.
  253. if GEOCITIES_FIX and hostname in ('www.oocities.org', 'www.oocities.com'):
  254. mode = 1 # oocities
  255. else:
  256. mode = 0 # Wayback Machine
  257. # Check content type to determine if this is HTML we need to patch.
  258. # Wayback will add its HTML to anything it thinks is HTML.
  259. if 'text/html' in guessed_content_type:
  260. # Some dynamically-generated links may end up pointing to
  261. # web.archive.org. Correct that by redirecting the Wayback
  262. # portion of the URL away if it ends up being HTML consumed
  263. # through the QUICK_IMAGES interface.
  264. if hostname == 'web.archive.org':
  265. self.drain_conn(conn)
  266. conn.release_conn()
  267. archived_url = '/'.join(request_url.split('/')[5:])
  268. _print('[r] [QI]', archived_url)
  269. return self.send_redirect_page(http_version, archived_url, 301)
  270. # Check if the date is within tolerance.
  271. if DATE_TOLERANCE != None:
  272. match = re.search('''(?://web\\.archive\\.org|^)/web/([0-9]+)''', conn.geturl() or '')
  273. if match:
  274. requested_date = match.group(1)
  275. if self.wayback_to_datetime(requested_date) > self.wayback_to_datetime(original_date) + datetime.timedelta(int(DATE_TOLERANCE)):
  276. self.drain_conn(conn)
  277. conn.release_conn()
  278. _print('[!]', requested_date, 'is outside the configured tolerance of', DATE_TOLERANCE, 'days')
  279. if not self.guess_and_send_redirect(http_version, archived_url):
  280. self.send_error_page(http_version, 412, 'Snapshot ' + requested_date + ' not available')
  281. return
  282. # Consume all data.
  283. data = conn.read()
  284. conn.release_conn()
  285. # Patch the page.
  286. if mode == 0: # Wayback Machine
  287. # Check if this is a Wayback Machine page.
  288. if b'<title>Wayback Machine</title>' in data:
  289. # Check if this is an exclusion (robots.txt?) error page.
  290. if b'<p>This URL has been excluded from the Wayback Machine.</p>' in data:
  291. return self.send_error_page(http_version, 403, 'URL excluded')
  292. # Check if this is a media playback iframe page.
  293. # Some websites (especially ones that use frames)
  294. # inexplicably render inside a media playback iframe.
  295. # In that case, a simple redirect would result in a
  296. # redirect loop, so fetch and render the URL instead.
  297. match = re.search(b'''<iframe id="playback" src="((?:(?:https?:)?//web.archive.org)?/web/[^"]+)"''', data)
  298. if match:
  299. # Extract the content URL.
  300. request_url = match.group(1).decode('ascii', 'ignore')
  301. archived_url = '/'.join(request_url.split('/')[5:])
  302. # Start fetching the URL.
  303. _print('[f]', archived_url)
  304. conn = self.shared_state.http.urlopen('GET', request_url, retries=retry, preload_content=False)
  305. if conn.status != 200:
  306. _print('[!]', conn.status, conn.reason)
  307. # If the memento Link header is present, this is a website error
  308. # instead of a Wayback error. Pass it along if that's the case.
  309. if 'Link' not in conn.headers:
  310. self.drain_conn(conn)
  311. conn.release_conn()
  312. return self.send_error_page(http_version, conn.status, conn.reason)
  313. # Identify content type so we don't modify non-HTML content.
  314. content_type = conn.headers.get('Content-Type')
  315. if not CONTENT_TYPE_ENCODING:
  316. idx = content_type.find(';')
  317. if idx > -1:
  318. content_type = content_type[:idx]
  319. if 'text/html' in content_type:
  320. # Consume all data and proceed with patching the page.
  321. data = conn.read()
  322. conn.release_conn()
  323. else:
  324. # Pass non-HTML data through.
  325. return self.send_passthrough(conn, http_version, content_type, request_url)
  326. # Check if this is a Wayback Machine redirect page.
  327. if b'<title></title>' in data and b'<span class="label style-scope media-button"><!---->Wayback Machine<!----></span>' in data:
  328. match = re.search(b'''<p class="impatient"><a href="(?:(?:https?:)?//web\\.archive\\.org)?/web/([^/]+)/([^"]+)">Impatient\\?</a></p>''', data)
  329. if match:
  330. # Sanitize the URL.
  331. archived_url = self.sanitize_redirect(match.group(2).decode('ascii', 'ignore'))
  332. # Add URL to the date LRU cache.
  333. self.shared_state.date_cache[str(effective_date) + '\x00' + archived_url] = match.group(1).decode('ascii', 'ignore')
  334. # Get the original HTTP redirect code.
  335. match = re.search(b'''<p class="code shift red">Got an HTTP ([0-9]+)''', data)
  336. try:
  337. redirect_code = int(match.group(1))
  338. except:
  339. redirect_code = 302
  340. # Redirect client to the URL.
  341. _print('[r]', archived_url)
  342. return self.send_redirect_page(http_version, archived_url, redirect_code)
  343. # Remove pre-toolbar scripts and CSS.
  344. data = re.sub(b'''(?:<!-- is_embed=True -->\\r?\\n?)?<script (?:type="text/javascript" )?src="[^"]*/_static/js/.*<!-- End Wayback Rewrite JS Include -->\\r?\\n''', b'', data, count=1, flags=re.S)
  345. # Remove toolbar. The if_ asset tag serves no toolbar, but we remove it just in case.
  346. data = re.sub(b'''<!-- BEGIN WAYBACK TOOLBAR INSERT -->.*<!-- END WAYBACK TOOLBAR INSERT -->''', b'', data, count=1, flags=re.S)
  347. # Remove comments on footer.
  348. data = re.sub(b'''<!--\\r?\\n FILE ARCHIVED .*$''', b'', data, flags=re.S)
  349. # Fix base tag.
  350. data = re.sub(b'''(<base\\s+[^>]*href=["']?)(?:(?:https?:)?//web.archive.org)?/web/[^/]+/(?:[^:/]+://)?''', b'\\1http://', data, flags=re.I + re.S)
  351. # Remove extraneous :80 from links.
  352. data = re.sub(b'((?:(?:https?:)?//web.archive.org)?/web/)([^/]+)/([^/:]+)://([^/:]+):80/', b'\\1\\2/\\3://\\4/', data)
  353. # Fix links.
  354. if QUICK_IMAGES:
  355. # QUICK_IMAGES works by intercepting asset URLs (those
  356. # with a date code ending in im_, js_...) and letting the
  357. # proxy pass them through. This may reduce load time
  358. # because Wayback doesn't have to hunt down the closest
  359. # copy of that asset to DATE, as those URLs have specific
  360. # date codes. This taints the HTML with web.archive.org
  361. # URLs. QUICK_IMAGES=2 uses the original URLs with an added
  362. # username:password, which taints less but is not supported
  363. # by all browsers - IE notably kills the whole page if it
  364. # sees an iframe pointing to an invalid URL.
  365. def filter_asset(match):
  366. if match.group(2) in (None, b'if_', b'fw_'): # non-asset URL
  367. return match.group(3) == b'https://' and b'http://' or match.group(3) # convert secure non-asset URLs to regular HTTP
  368. asset_type = match.group(2)
  369. if asset_type == b'js_': # cut down on the JavaScript detector's second request
  370. asset_type = b'im_'
  371. if QUICK_IMAGES == 2:
  372. return b'http://' + match.group(1) + b':' + asset_type + b'@'
  373. else:
  374. return b'http://web.archive.org/web/' + match.group(1) + asset_type + b'/' + match.group(3)
  375. data = re.sub(b'(?:(?:https?:)?//web.archive.org)?/web/([0-9]+)([a-z]+_)?/([^:/]+:(?://)?)', filter_asset, data)
  376. else:
  377. # Remove asset URLs while simultaneously adding them to the date LRU cache
  378. # with their respective date and converting secure URLs to regular HTTP.
  379. def add_to_date_cache(match):
  380. orig_url = match.group(2)
  381. if orig_url[:8] == b'https://':
  382. orig_url = b'http://' + orig_url[8:]
  383. self.shared_state.date_cache[str(effective_date) + '\x00' + orig_url.decode('ascii', 'ignore')] = match.group(1).decode('ascii', 'ignore').replace('js_', 'im_')
  384. return orig_url
  385. data = re.sub(b'''(?:(?:https?:)?//web.archive.org)?/web/([^/]+)/([^"\\'#<>]+)''', add_to_date_cache, data)
  386. elif mode == 1: # oocities
  387. # Remove viewport/cache-control/max-width code from the header.
  388. data = re.sub(b'''^.*?\n\n''', b'', data, flags=re.S)
  389. # Remove archive notice and tracking code from the footer.
  390. data = re.sub(b'''<style> \n.zoomout { -webkit-transition: .*$''', b'', data, flags=re.S)
  391. # Remove clearly labeled snippets from Geocities.
  392. data = re.sub(b'''^.*<\\!-- text above generated by server\\. PLEASE REMOVE -->''', b'', data, flags=re.S)
  393. data = re.sub(b'''<\\!-- following code added by server\\. PLEASE REMOVE -->.*<\!-- preceding code added by server\. PLEASE REMOVE -->''', b'', data, flags=re.S)
  394. data = re.sub(b'''<\\!-- text below generated by server\\. PLEASE REMOVE -->.*$''', b'', data, flags=re.S)
  395. # Fix links.
  396. data = re.sub(b'''//([^\\.]*\\.)?oocities\\.com/''', b'//\\1geocities.com/', data, flags=re.S)
  397. # Send patched page.
  398. self.send_response_headers(conn, http_version, content_type, request_url, content_length=len(data))
  399. self.request.sendall(data)
  400. self.request.close()
  401. else:
  402. # Pass non-HTML data through.
  403. self.send_passthrough(conn, http_version, content_type, request_url)
  404. def send_passthrough(self, conn, http_version, content_type, request_url):
  405. """Pass data through to the client unmodified (save for our headers)."""
  406. self.send_response_headers(conn, http_version, content_type, request_url, content_length=True)
  407. for data in conn.stream(1024):
  408. self.request.sendall(data)
  409. conn.release_conn()
  410. self.request.close()
  411. def send_response_headers(self, conn, http_version, content_type, request_url, content_length=False):
  412. """Generate and send the response headers."""
  413. # Pass the HTTP version, and error code if there is one.
  414. response = '{0} {1} {2}'.format(http_version, conn.status, conn.reason.replace('\n', ' '))
  415. # Add Content-Type, Content-Length and the caching ETag.
  416. response += '\r\nContent-Type: ' + content_type
  417. if type(content_length) == int:
  418. response += '\r\nContent-Length: ' + str(content_length)
  419. content_length = False # don't pass the original length through
  420. response += '\r\nETag: "' + request_url.replace('"', '') + '"'
  421. response += '\r\nConnection: close' # helps with IE6 trying to use proxy keep alive and holding half-open connections
  422. # Pass X-Archive-Orig-* (and Content-Length if requested) headers through.
  423. for header in conn.headers:
  424. if header.find('X-Archive-Orig-') == 0:
  425. orig_header = header[15:]
  426. # Skip headers which may affect client behavior.
  427. if orig_header.lower() not in ('connection', 'location', 'content-type', 'content-length', 'etag', 'authorization', 'set-cookie'):
  428. response += '\r\n' + orig_header + ': ' + conn.headers[header]
  429. elif content_length and header.lower() == 'content-length':
  430. response += '\r\n' + header + ': ' + conn.headers[header]
  431. # Finish and send the request.
  432. response += '\r\n\r\n'
  433. self.request.sendall(response.encode('utf8', 'ignore'))
  434. def send_error_page(self, http_version, code, reason):
  435. """Generate an error page."""
  436. # Get a description for this error code.
  437. if code in (404, 508): # page not archived or redirect loop
  438. description = 'This page may not be archived by the Wayback Machine.'
  439. elif code == 403: # not crawled due to exclusion
  440. description = 'This page was not archived due to a Wayback Machine exclusion.'
  441. elif code == 501: # method not implemented
  442. description = 'WaybackProxy only implements the GET method.'
  443. elif code == 502: # exception
  444. description = 'This page could not be fetched due to an unknown error.'
  445. elif code == 504: # timeout
  446. description = 'This page could not be fetched due to a Wayback Machine server error.'
  447. elif code == 412: # outside of tolerance
  448. description = 'The earliest snapshot for this page is outside of the configured tolerance interval.'
  449. elif code == 400 and reason == 'Host header missing': # no host header in transparent mode
  450. description = 'WaybackProxy\'s transparent mode requires an HTTP/1.1 compliant client.'
  451. else: # another error
  452. description = 'Unknown error. The Wayback Machine may be experiencing technical difficulties.'
  453. # Read error page file.
  454. try:
  455. with open('error.html', 'r', encoding='utf8', errors='ignore') as f:
  456. error_page = f.read()
  457. except:
  458. # Just send the code and reason as a backup.
  459. error_page = '${code} ${reason}'
  460. # Format error page template.
  461. signature = self.signature()
  462. error_page = string.Template(error_page).substitute(**locals())
  463. error_page_len = len(error_page)
  464. # Send formatted error page and stop.
  465. self.request.sendall(
  466. '{http_version} {code} {reason}\r\n'
  467. 'Content-Type: text/html\r\n'
  468. 'Content-Length: {error_page_len}\r\n'
  469. '\r\n'
  470. '{error_page}'
  471. .format(**locals()).encode('utf8', 'ignore')
  472. )
  473. self.request.close()
  474. def send_redirect_page(self, http_version, target, code=302):
  475. """Generate a redirect page."""
  476. # make redirect page
  477. redirectpage = '<html><head><title>Redirect</title><meta http-equiv="refresh" content="0;url='
  478. redirectpage += target
  479. redirectpage += '"></head><body><p>If you are not redirected, <a href="'
  480. redirectpage += target
  481. redirectpage += '">click here</a>.</p></body></html>'
  482. # send redirect page and stop
  483. self.request.sendall('{0} {1} Found\r\nLocation: {2}\r\nContent-Type: text/html\r\nContent-Length: {3}\r\n\r\n{4}'.format(http_version, code, target, len(redirectpage), redirectpage).encode('utf8', 'ignore'))
  484. self.request.close()
  485. def guess_and_send_redirect(self, http_version, guess_url):
  486. # Heuristically determine the static URL for some redirect scripts.
  487. parsed = urllib.parse.urlparse(guess_url)
  488. match = re.search('''(?:^|&)[^=]+=((?:https?(?:%3A|:)(?:%2F|/)|www[0-9]*\\.[^/%]+)?(?:%2F|/)[^&]+)''', parsed.query, re.I) # URL in query parameters
  489. if not match:
  490. full_path = parsed.path
  491. if parsed.query:
  492. full_path += '?' + parsed.query
  493. match = re.search('''((?:https?(?:%3A|:)(?:%2F|/)|www[0-9]*\\.[^/%]+)(?:(?:%2F|/).+|$))''', full_path, re.I) # URL in path or full query
  494. if match: # found URL
  495. # Decode and sanitize the URL.
  496. new_url = self.sanitize_redirect(urllib.parse.unquote_plus(match.group(1)))
  497. # Redirect client to the URL.
  498. _print('[r] [g]', new_url)
  499. self.send_redirect_page(http_version, new_url)
  500. return True
  501. return False
  502. def handle_settings(self, query):
  503. """Generate the settings page."""
  504. global DATE, DATE_TOLERANCE, GEOCITIES_FIX, QUICK_IMAGES, WAYBACK_API, CONTENT_TYPE_ENCODING, SILENT, SETTINGS_PAGE
  505. if query != '': # handle any parameters that may have been sent
  506. parsed = urllib.parse.parse_qs(query)
  507. if 'date' in parsed and 'dateTolerance' in parsed:
  508. if DATE != parsed['date'][0]:
  509. DATE = parsed['date'][0]
  510. self.shared_state.date_cache.clear()
  511. self.shared_state.availability_cache.clear()
  512. if DATE_TOLERANCE != parsed['dateTolerance'][0]:
  513. DATE_TOLERANCE = parsed['dateTolerance'][0]
  514. GEOCITIES_FIX = 'gcFix' in parsed
  515. QUICK_IMAGES = 'quickImages' in parsed
  516. CONTENT_TYPE_ENCODING = 'ctEncoding' in parsed
  517. # send the page and stop
  518. settingspage = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n'
  519. settingspage += '<html><head><title>WaybackProxy Settings</title></head><body><p><b>'
  520. settingspage += self.signature()
  521. settingspage += '</b></p><form method="get" action="/">'
  522. settingspage += '<p>Date to get pages from: <input type="text" name="date" size="8" value="'
  523. settingspage += str(DATE)
  524. settingspage += '"><p>Date tolerance: <input type="text" name="dateTolerance" size="8" value="'
  525. settingspage += str(DATE_TOLERANCE)
  526. settingspage += '"> days<br><input type="checkbox" name="gcFix"'
  527. if GEOCITIES_FIX:
  528. settingspage += ' checked'
  529. settingspage += '> Geocities Fix<br><input type="checkbox" name="quickImages"'
  530. if QUICK_IMAGES:
  531. settingspage += ' checked'
  532. settingspage += '> Quick images<br><input type="checkbox" name="ctEncoding"'
  533. if CONTENT_TYPE_ENCODING:
  534. settingspage += ' checked'
  535. settingspage += '> Encoding in Content-Type</p><p><input type="submit" value="Save"></p></form></body></html>'
  536. self.request.send(settingspage.encode('utf8', 'ignore'))
  537. self.request.close()
  538. def sanitize_redirect(self, url):
  539. """Sanitize an URL for client-side redirection."""
  540. if url[0] != '/' and '://' not in url:
  541. # Add protocol if the URL is absolute but missing a protocol.
  542. return 'http://' + url
  543. elif url[:8].lower() == 'https://':
  544. # Convert secure URLs to regular HTTP.
  545. return 'http://' + url[8:]
  546. else:
  547. # No changes required.
  548. return url
  549. def signature(self):
  550. """Return the server signature."""
  551. return 'WaybackProxy on {0}'.format(socket.gethostname())
  552. def wayback_to_datetime(self, date):
  553. """Convert a Wayback format date string to a datetime.datetime object."""
  554. date = str(date)
  555. fmt = '%Y%m%d%H%M%S'
  556. fmt_len = 14
  557. while fmt:
  558. try:
  559. return datetime.datetime.strptime(date[:fmt_len], fmt)
  560. except:
  561. fmt = fmt[:-2]
  562. fmt_len -= 2
  563. def drain_conn(self, conn):
  564. getattr(conn, 'drain_conn', conn.read)()
  565. print_lock = threading.Lock()
  566. def _print(*args, **kwargs):
  567. """Logging function."""
  568. if SILENT:
  569. return
  570. with print_lock:
  571. print(*args, **kwargs, flush=True)
  572. def main():
  573. """Starts the server."""
  574. server = ThreadingTCPServer(('', LISTEN_PORT), Handler)
  575. _print('[-] Now listening on port', LISTEN_PORT)
  576. _print('[-] Date set to', DATE)
  577. try:
  578. server.serve_forever()
  579. except KeyboardInterrupt: # Ctrl+C to stop
  580. pass
  581. if __name__ == '__main__':
  582. main()