def initialize(args): """Initialize the server's database and logger. """ print(args) # Setup logging global logger logger = logging.getLogger("ha_server", args) # Connect to the database global db db = ServerDatabase(args.database)
def main(): parser = argparse.ArgumentParser( description="Read Cuckoo filter and check against contacts" ) parser.add_argument( "-d", "--debug", help="Run in debug mode logging to stderr", action="store_true" ) parser.add_argument( "-D", "--database", help="Specify the database location", default="/var/lib/epidose/client-database.db", ) parser.add_argument("-o", "--observation", help="Observation hash to check") parser.add_argument( "-v", "--verbose", help="Set verbose logging", action="store_true" ) parser.add_argument("filter", help="Cuckoo filter file") args = parser.parse_args() # Setup logging global logger logger = logging.getLogger("check_infection", args) with open(args.filter, "rb") as f: (capacity,) = struct.unpack(">Q", f.read(8)) cuckoo_filter = TracingDataBatch(fh=f, capacity=capacity) if args.observation: if ( bytes(bytearray.fromhex(args.observation)) in cuckoo_filter.infected_observations ): print("Found") sys.exit(0) else: print("Not found") sys.exit(1) else: ct = ContactTracer(None, args.database, transmitter=False, receiver=False) matches = ct.matches_with_batch(cuckoo_filter) logger.info(f"{'Contact match' if matches else 'No contact match'}") if matches: led_set(True) sys.exit(0) else: led_set(False) sys.exit(1)
def main(): parser = argparse.ArgumentParser( description="Create Cuckoo filter with reported infections") parser.add_argument("-d", "--debug", help="Run in debug mode logging to stderr", action="store_true") parser.add_argument( "-D", "--database", help="Specify the database location", default="/var/lib/epidose/server-database.db", ) parser.add_argument("-s", "--seeds-file", help="File containing epochs and seeds") parser.add_argument("-v", "--verbose", help="Set verbose logging", action="store_true") parser.add_argument("filter", help="File where filter will be stored") args = parser.parse_args() # Setup logging global logger logger = logging.getLogger("create_filter", args) # Obtain seeds if args.seeds_file: tracing_seeds = read_seeds(args.seeds_file) else: db = ServerDatabase(args.database) tracing_seeds = db.get_epochs_seeds_tuple() # Create and save filter cuckoo_filter = TracingDataBatch(tracing_seeds) handle, name = mkstemp(dir=os.path.dirname(args.filter)) # Write filter to a temparary file with os.fdopen(handle, "wb") as f: f.write( cuckoo_filter.capacity.to_bytes(8, byteorder="big", signed=False)) cuckoo_filter.tofile(f) # Atomically replace any existing filter file with the new one logger.debug(f"Rename {name} to {args.filter}") os.rename(name, args.filter)
def main(): parser = argparse.ArgumentParser( description="Contact tracing beacon receiver") parser.add_argument("-d", "--debug", help="Run in debug mode logging to stderr", action="store_true") parser.add_argument( "-D", "--database", help="Specify the database location", default="/var/lib/epidose/client-database.db", ) parser.add_argument( "-i", "--iface", help="Listen on the specified interface number, not [hci]0", type=int, default=0, ) parser.add_argument("-t", "--test", help="Test script", action="store_true") parser.add_argument("-v", "--verbose", help="Set verbose logging", action="store_true") args = parser.parse_args() if args.test: args.debug = True args.database = ":memory:" # Setup logging global logger logger = logging.getLogger("beacon_rx", args) # Receive and process beacon packets global receiver receiver = ContactTracer(None, args.database, transmitter=False) if args.test: sys.exit(0) socket = bluez.hci_open_dev(args.iface) set_receive(socket) while True: process_packet(socket)
def main(): parser = argparse.ArgumentParser(description="Contact tracing device I/O") parser.add_argument("-1", "--on", help="Turn LED on", action="store_true") parser.add_argument("-0", "--off", help="Turn LED off", action="store_true") parser.add_argument( "-d", "--debug", help="Run in debug mode logging to stderr", action="store_true" ) parser.add_argument("-l", "--led", help="Turn LED on or off", type=int) parser.add_argument( "-t", "--test", help="Toggle LED with button", action="store_true" ) parser.add_argument( "-v", "--verbose", help="Set verbose logging", action="store_true" ) parser.add_argument("-w", "--wait", help="Wait for key press", action="store_true") args = parser.parse_args() # Setup logging global logger logger = logging.getLogger("device_io", args) setup() if args.test: toggle() elif args.off: logger.debug("Turn LED off") led_set(False) elif args.on: logger.debug("Turn LED on") led_set(True) elif args.wait: logger.debug("Waiting for button press; press ^C and button to abort") wait_for_button_press() logger.debug("Button pressed") # Debounce time.sleep(0.2)
def main(): parser = argparse.ArgumentParser( description="Contact tracing beacon trasmitter") parser.add_argument("-a", "--authorization", help="Upload authorization code", default=":NONE:") parser.add_argument("-d", "--debug", help="Run in debug mode logging to stderr", action="store_true") parser.add_argument( "-D", "--database", help="Specify the database location", default="/var/lib/epidose/client-database.db", ) parser.add_argument("-t", "--test", help="Test script", action="store_true") parser.add_argument("-s", "--server", help="Server URL", default=SERVER_URL) parser.add_argument("-v", "--verbose", help="Set verbose logging", action="store_true") parser.add_argument( "start_time", help="The (ISO) time from which contact tracing should start") parser.add_argument( "end_time", help="The (ISO) time at which contact tracing should end") args = parser.parse_args() if args.test: args.debug = True args.database = ":memory:" # Setup logging global logger logger = logging.getLogger("upload_contacts", args) # Obtain the specified seeds ct = ContactTracer(None, args.database, transmitter=False, receiver=False) (epochs, seeds) = ct.get_tracing_information( datetime.fromisoformat(args.start_time), datetime.fromisoformat(args.end_time)) # Create dictionary for JSON logger.debug("Creating request") post = {"authorization": args.authorization, "data": []} i = 0 for e in epochs: post["data"].append({"epoch": e, "seed": seeds[i].hex()}) i += 1 # Send request and check response logger.debug("Sending request") res = requests.post(f"{args.server}/add_contagious", json=post) logger.debug("Request sent") exit(0 if res.ok else 1)
def main(): parser = argparse.ArgumentParser(description="Contact tracing beacon trasmitter") parser.add_argument( "-d", "--debug", help="Run in debug mode logging to stderr", action="store_true" ) parser.add_argument( "-D", "--database", help="Specify the database location", default="/var/lib/epidose/client-database.db", ) parser.add_argument( "-i", "--iface", help="Transmit on the specified interface, not [hci]0", type=int, default=0, ) parser.add_argument( "-n", "--dry-run", help="Do not execute the required command(s)", action="store_true", ) parser.add_argument( "-r", "--rssi", help="Specify the received signal strength indication", type=int, default=0xC0, ) parser.add_argument("-t", "--test", help="Test script", action="store_true") parser.add_argument( "-v", "--verbose", help="Set verbose logging", action="store_true" ) args = parser.parse_args() if args.test: args.debug = True args.dry_run = True args.database = ":memory:" # Setup logging global logger logger = logging.getLogger("beacon_tx", args) global dry_run dry_run = args.dry_run interface = f"hci{args.iface}" # Enable the Bluetooth interface run_command(["hciconfig", interface, "up"]) # Enable non connectable undirected LE advertising run_command(["hciconfig", interface, "leadv", "3"]) # Transmit and store beacon packets current_ephid = None transmitter = ContactTracer(None, args.database, receiver=False) while True: now = datetime.now() transmitter.check_advance_day(now) ephid = transmitter.get_ephid_for_time(now) if ephid != current_ephid: set_transmit(interface, ephid, args.rssi) current_ephid = ephid logger.debug(f"Change ephid to {ephid.hex()}") # Wait for the current epoch (e.g. 15 minutes) to pass # Sample every minute time.sleep(EPOCH_LENGTH / 60) if args.test: break