From f0c466afa5b1cc7a5e42e2f097f266d137cece10 Mon Sep 17 00:00:00 2001 From: Jordan Date: Sun, 25 Oct 2020 20:03:23 -0700 Subject: include contact hash in listings, related to #1 --- allium/relays.py | 30 +++++++++--------------------- allium/templates/contact.html | 3 +-- allium/templates/misc-families.html | 2 +- allium/templates/relay-info.html | 4 ++-- allium/templates/relay-list.html | 12 +++++++----- 5 files changed, 20 insertions(+), 31 deletions(-) diff --git a/allium/relays.py b/allium/relays.py index 97e1514..74c091f 100644 --- a/allium/relays.py +++ b/allium/relays.py @@ -20,27 +20,6 @@ ABS_PATH = os.path.dirname(os.path.abspath(__file__)) ENV = Environment(loader=FileSystemLoader(os.path.join(ABS_PATH, 'templates')), trim_blocks=True, lstrip_blocks=True) -def hash_filter(value, hash_type='md5'): - ''' - Custom hash filter for jinja; defaults to "md5" if no type specified - - :param value: value to be hashed - :param hash_type: valid hash type - :return: computed hash as a hexadecimal string - ''' - hash_func = getattr(hashlib, hash_type, None) - - if hash_func: - computed_hash = hash_func(value.encode('utf-8')).hexdigest() - else: - raise AttributeError( - 'No hashing function named {hname}'.format(hname=hash_type) - ) - - return computed_hash - -ENV.filters['hash'] = hash_filter - class Relays: ''' Relay class consisting of relays (list of dict) and onionoo fetch timestamp @@ -58,6 +37,7 @@ class Relays: self._fix_missing_observed_bandwidth() self._sort_by_bandwidth() self._trim_platform() + self._add_hashed_contact() self._categorize() def _fetch_onionoo_details(self): @@ -104,6 +84,14 @@ class Relays: if not relay.get('observed_bandwidth'): self.json['relays'][idx]['observed_bandwidth'] = 0 + def _add_hashed_contact(self): + ''' + Adds a hashed contact key/value for every relay + ''' + for idx, relay in enumerate(self.json['relays']): + c = relay.get('contact', '').encode('utf-8') + self.json['relays'][idx]['contact_md5'] = hashlib.md5(c).hexdigest() + def _sort_by_bandwidth(self): ''' Sort full JSON list by highest observed_bandwidth, retain this order diff --git a/allium/templates/contact.html b/allium/templates/contact.html index ed10631..d060a2d 100644 --- a/allium/templates/contact.html +++ b/allium/templates/contact.html @@ -1,10 +1,9 @@ {% extends "relay-list.html" %} +{% set contact_hash = relays.json['relay_subset'][0]['contact_md5'] %} {% if relays.json['relay_subset'][0]['contact'] %} {% set contact = relays.json['relay_subset'][0]['contact']|escape %} - {% set contact_hash = relays.json['relay_subset'][0]['contact']|hash %} {% else %} {% set contact = 'none' %} - {% set contact_hash = ''|hash %} {% endif %} {% block title %}Tor Relays :: Contact {{ contact_hash }}{% endblock %} {% block header %}Home :: Contact {{ contact_hash }}{% diff --git a/allium/templates/misc-families.html b/allium/templates/misc-families.html index dea08e3..12773c8 100644 --- a/allium/templates/misc-families.html +++ b/allium/templates/misc-families.html @@ -56,7 +56,7 @@ reverse=True) -%} {% if v['contact'] -%} {{ +v['contact_md5'] }}/" title="{{ v['contact']|escape }}">{{ v['contact']|truncate(50)|escape }} {% else -%} none diff --git a/allium/templates/relay-info.html b/allium/templates/relay-info.html index d26dab3..f2aabe7 100644 --- a/allium/templates/relay-info.html +++ b/allium/templates/relay-info.html @@ -17,10 +17,10 @@
Contact
{% if relay['contact'] -%} -
{{ +
{{ relay['contact']|escape }}
{% else -%} -
none
+
none
{% endif -%}
Dir Address
diff --git a/allium/templates/relay-list.html b/allium/templates/relay-list.html index 8283c04..6698755 100644 --- a/allium/templates/relay-list.html +++ b/allium/templates/relay-list.html @@ -7,7 +7,7 @@ Nickname - +Contact Bandwidth IP Address AS Number @@ -49,13 +49,15 @@ relay['fingerprint']|escape }}.html">{{ relay['nickname']|truncate(15)|escape {% if key != 'contact' -%} {% if relay['contact'] -%} - +{{ relay['contact_md5'][0:8] +}} {% else -%} -✉ +none {% endif -%} {% else -%} -✉ +{{ relay['contact_md5'][0:8] +}} {% endif -%} {{ obs_bandwidth }} -- cgit v1.2.3-54-g00ecf