tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

stale-echo-client-hints.py (1804B)


      1 import random
      2 import string
      3 
      4 from wptserve.utils import isomorphic_encode
      5 import importlib
      6 client_hints_full_list = importlib.import_module("client-hints.resources.clienthintslist").client_hints_full_list
      7 
      8 def id_token():
      9   letters = string.ascii_lowercase
     10   return u''.join(random.choice(letters) for i in range(20))
     11 
     12 def main(request, response):
     13    client_hint_headers = client_hints_full_list()
     14    client_hints_curr = {i:request.headers.get(i) for i in client_hint_headers}
     15 
     16    token = request.GET.first(b"token", None)
     17    is_query = request.GET.first(b"query", None) is not None
     18    with request.server.stash.lock:
     19      stash = request.server.stash.take(token)
     20      if stash != None:
     21        (value, client_hints_prev) = stash
     22        count = int(value)
     23      else:
     24        count = 0
     25        client_hints_prev = {}
     26 
     27      if is_query:
     28        if count < 2:
     29          request.server.stash.put(token, (count, client_hints_curr))
     30      else:
     31        count = count + 1
     32        request.server.stash.put(token, (count, client_hints_curr))
     33 
     34    for header in client_hint_headers:
     35      if client_hints_curr[header] is not None:
     36        response.headers.set(header+b"-recieved", client_hints_curr[header])
     37      if (header in client_hints_prev) and (client_hints_prev[header] is not None):
     38        response.headers.set(header+b"-previous", client_hints_prev[header])
     39 
     40    if is_query:
     41      headers = [(b"Count", count)]
     42      content = u""
     43      return 200, headers, content
     44    else:
     45      unique_id = id_token()
     46      headers = [(b"Content-Type", b"text/html"),
     47                 (b"Cache-Control", b"private, max-age=0, stale-while-revalidate=60"),
     48                 (b"Unique-Id", isomorphic_encode(unique_id))]
     49      content = u"report('{}')".format(unique_id)
     50      return 200, headers, content