Reticulum/RNS/Utilities/rnpath.py

97 lines
2.8 KiB
Python
Raw Normal View History

2021-09-24 10:42:24 +00:00
#!/usr/bin/env python3
import RNS
import sys
import time
import argparse
from RNS._version import __version__
2021-09-24 13:34:03 +00:00
def program_setup(configdir, destination_hexhash, verbosity):
2021-09-24 10:42:24 +00:00
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))
exit()
2021-09-24 13:34:03 +00:00
reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity)
2021-09-24 10:42:24 +00:00
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 = "⢄⢂⢁⡁⡈⡐⡠"
while not RNS.Transport.has_path(destination_hash):
time.sleep(0.1)
print(("\b\b"+syms[i]+" "), end="")
sys.stdout.flush()
i = (i+1)%len(syms)
2021-09-24 13:34:03 +00:00
hops = RNS.Transport.hops_to(destination_hash)
2021-09-25 09:27:43 +00:00
next_hop = RNS.prettyhexrep(reticulum.get_next_hop(destination_hash))
next_hop_interface = reticulum.get_next_hop_if_name(destination_hash)
2021-09-24 12:16:25 +00:00
2021-09-25 21:22:33 +00:00
if hops != 1:
2021-09-24 13:34:03 +00:00
ms = "s"
else:
ms = ""
print("\rPath found, destination "+RNS.prettyhexrep(destination_hash)+" is "+str(hops)+" hop"+ms+" away via "+next_hop+" on "+next_hop_interface)
2021-09-24 10:42:24 +00:00
def main():
try:
parser = argparse.ArgumentParser(description="Reticulum Path Discovery Utility")
parser.add_argument("--config",
action="store",
default=None,
help="path to alternative Reticulum config directory",
type=str
)
parser.add_argument(
"--version",
action="version",
version="rnpath {version}".format(version=__version__)
)
parser.add_argument(
"destination",
nargs="?",
default=None,
help="hexadecimal hash of the destination",
type=str
)
2021-09-24 13:34:03 +00:00
parser.add_argument('-v', '--verbose', action='count', default=0)
2021-09-24 10:42:24 +00:00
args = parser.parse_args()
if args.config:
configarg = args.config
else:
configarg = None
if not args.destination:
print("")
parser.print_help()
print("")
else:
2021-09-24 13:34:03 +00:00
program_setup(configdir = configarg, destination_hexhash = args.destination, verbosity = args.verbose)
2021-09-24 10:42:24 +00:00
except KeyboardInterrupt:
print("")
exit()
if __name__ == "__main__":
main()