#!/usr/bin/env python3 # MIT License # # Copyright (c) 2016-2022 Mark Qvist / unsigned.io # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import RNS import sys import time import argparse from RNS._version import __version__ def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity, timeout, drop_queues, drop_via): if table: destination_hash = None if destination_hexhash != None: try: dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)) try: destination_hash = bytes.fromhex(destination_hexhash) except Exception as e: raise ValueError("Invalid destination entered. Check your input.") except Exception as e: print(str(e)) sys.exit(1) reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) table = sorted(reticulum.get_path_table(), key=lambda e: (e["interface"], e["hops"]) ) displayed = 0 for path in table: if destination_hash == None or destination_hash == path["hash"]: displayed += 1 exp_str = RNS.timestamp_str(path["expires"]) if path["hops"] == 1: m_str = " " else: m_str = "s" print(RNS.prettyhexrep(path["hash"])+" is "+str(path["hops"])+" hop"+m_str+" away via "+RNS.prettyhexrep(path["via"])+" on "+path["interface"]+" expires "+RNS.timestamp_str(path["expires"])) if destination_hash != None and displayed == 0: print("No path known") sys.exit(1) elif rates: destination_hash = None if destination_hexhash != None: try: dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)) try: destination_hash = bytes.fromhex(destination_hexhash) except Exception as e: raise ValueError("Invalid destination entered. Check your input.") except Exception as e: print(str(e)) sys.exit(1) reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) table = sorted(reticulum.get_rate_table(), key=lambda e: e["last"] ) if len(table) == 0: print("No information available") else: displayed = 0 for entry in table: if destination_hash == None or destination_hash == entry["hash"]: displayed += 1 try: last_str = pretty_date(int(entry["last"])) start_ts = entry["timestamps"][0] span = max(time.time() - start_ts, 3600.0) span_hours = span/3600.0 span_str = pretty_date(int(entry["timestamps"][0])) hour_rate = round(len(entry["timestamps"])/span_hours, 3) if hour_rate-int(hour_rate) == 0: hour_rate = int(hour_rate) if entry["rate_violations"] > 0: if entry["rate_violations"] == 1: s_str = "" else: s_str = "s" rv_str = ", "+str(entry["rate_violations"])+" active rate violation"+s_str else: rv_str = "" if entry["blocked_until"] > time.time(): bli = time.time()-(int(entry["blocked_until"])-time.time()) bl_str = ", new announces allowed in "+pretty_date(int(bli)) else: bl_str = "" print(RNS.prettyhexrep(entry["hash"])+" last heard "+last_str+" ago, "+str(hour_rate)+" announces/hour in the last "+span_str+rv_str+bl_str) except Exception as e: print("Error while processing entry for "+RNS.prettyhexrep(entry["hash"])) print(str(e)) if destination_hash != None and displayed == 0: print("No information available") sys.exit(1) elif drop_queues: reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) RNS.log("Dropping announce queues on all interfaces...") reticulum.drop_announce_queues() elif drop: try: dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)) try: destination_hash = bytes.fromhex(destination_hexhash) except Exception as e: raise ValueError("Invalid destination entered. Check your input.") except Exception as e: print(str(e)) sys.exit(1) reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) if reticulum.drop_path(destination_hash): print("Dropped path to "+RNS.prettyhexrep(destination_hash)) else: print("Unable to drop path to "+RNS.prettyhexrep(destination_hash)+". Does it exist?") sys.exit(1) elif drop_via: try: dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)) try: destination_hash = bytes.fromhex(destination_hexhash) except Exception as e: raise ValueError("Invalid destination entered. Check your input.") except Exception as e: print(str(e)) sys.exit(1) reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) if reticulum.drop_all_via(destination_hash): print("Dropped all paths via "+RNS.prettyhexrep(destination_hash)) else: print("Unable to drop paths via "+RNS.prettyhexrep(destination_hash)+". Does the transport instance exist?") sys.exit(1) else: try: dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 if len(destination_hexhash) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)) try: destination_hash = bytes.fromhex(destination_hexhash) except Exception as e: raise ValueError("Invalid destination entered. Check your input.") except Exception as e: print(str(e)) sys.exit(1) reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) if not RNS.Transport.has_path(destination_hash): RNS.Transport.request_path(destination_hash) print("Path to "+RNS.prettyhexrep(destination_hash)+" requested ", end=" ") sys.stdout.flush() i = 0 syms = "⢄⢂⢁⡁⡈⡐⡠" limit = time.time()+timeout while not RNS.Transport.has_path(destination_hash) and time.time()