Ticket #22755: create_all_descriptors.patch

File create_all_descriptors.patch, 3.0 KB (added by atagar, 8 months ago)

Creates extrainfo descriptors and consensus too.

  • scripts/create-descriptors

    From a6b0dc93dec0c92eb5fb7b4617484a25d650bdd3 Mon Sep 17 00:00:00 2001
    From: Damian Johnson <atagar@torproject.org>
    Date: Fri, 30 Jun 2017 11:09:50 -0700
    Subject: [PATCH] Create extrainfo descriptors and consensus too
    
    Turns out BridgeDB needs all descriptor types, not just server descriptors.
    Creating the extrainfo descriptors and consensus too.
    ---
     scripts/create-descriptors |   49 +++++++++++++++++++++++++++++++++++---------
     1 file changed, 39 insertions(+), 10 deletions(-)
    
    diff --git a/scripts/create-descriptors b/scripts/create-descriptors
    index e6711c1..636d6a8 100755
    a b  
    11#!/usr/bin/env python
    22
     3import base64
     4import binascii
    35import os
    46import sys
    57
    68try:
    79  import stem
     10  import stem.descriptor
    811  from stem.descriptor.server_descriptor import RelayDescriptor
     12  from stem.descriptor.extrainfo_descriptor import RelayExtraInfoDescriptor
     13  from stem.descriptor.networkstatus import NetworkStatusDocumentV3
     14  from stem.descriptor.router_status_entry import RouterStatusEntryV3
    915except ImportError:
    1016  print('Creating descriptors requires stem (https://stem.torproject.org/)')
    1117  sys.exit(1)
    1218
    13 if not hasattr(RelayDescriptor, 'content'):
     19if not hasattr(stem.descriptor, 'create_signing_key'):
    1420  print('This requires stem version 1.6 or later, you are running version %s' % stem.__version__)
    1521  sys.exit(1)
    1622
    17 if len(sys.argv) < 2 or not sys.argv[1].isdigit():
    18   print('We need a numeric argument indicating how many descriptors to make.')
    19   sys.exit(1)
     23OUTPUT_DIR = os.path.join(os.getcwd(), 'test_descriptors')
     24
     25
     26def write_descriptor(desc, filename):
     27  if not os.path.exists(OUTPUT_DIR):
     28    os.mkdir(OUTPUT_DIR)
     29
     30  with open(os.path.join(OUTPUT_DIR, filename), 'w') as descriptor_file:
     31    descriptor_file.write(str(desc))
     32
     33
     34def make_descriptors(count):
     35  consensus_entries = []
     36
     37  for i in range(count):
     38    signing_key = stem.descriptor.create_signing_key()
     39    server_desc = RelayDescriptor.create(signing_key = signing_key)
     40    consensus_entries.append(server_desc.make_router_status_entry())
     41
     42    extrainfo_desc = RelayExtraInfoDescriptor.create({
     43      'extra-info': '%s %s' % (server_desc.nickname, server_desc.fingerprint),
     44    }, signing_key = signing_key)
     45
     46    write_descriptor(server_desc, 'server_descriptor_%i' % i)
     47    write_descriptor(extrainfo_desc, 'extrainfo_descriptor_%i' % i)
    2048
    21 output_dir = os.path.join(os.getcwd(), 'test_descriptors')
     49  consensus = NetworkStatusDocumentV3.create(routers = consensus_entries)
     50  write_descriptor(consensus, 'consensus')
    2251
    23 if not os.path.exists(output_dir):
    24   os.mkdir(output_dir)
    2552
    26 for i in range(int(sys.argv[1])):
    27   with open(os.path.join(output_dir, 'descriptor_%i' % i), 'w') as descriptor_file:
    28     descriptor_file.write(RelayDescriptor.content(sign = True))
     53if __name__ == '__main__':
     54  if len(sys.argv) < 2 or not sys.argv[1].isdigit():
     55    print('We need a numeric argument indicating how many descriptors to make.')
     56    sys.exit(1)
    2957
     58  make_descriptors(int(sys.argv[1]))