rnpath.py
1 #!/usr/bin/env python3 2 3 # Reticulum License 4 # 5 # Copyright (c) 2016-2025 Mark Qvist 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # - The Software shall not be used in any kind of system which includes amongst 15 # its functions the ability to purposefully do harm to human beings. 16 # 17 # - The Software shall not be used, directly or indirectly, in the creation of 18 # an artificial intelligence, machine learning or language model training 19 # dataset, including but not limited to any use that contributes to the 20 # training or development of such a model or algorithm. 21 # 22 # - The above copyright notice and this permission notice shall be included in 23 # all copies or substantial portions of the Software. 24 # 25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 28 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 29 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 30 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 # SOFTWARE. 32 33 import RNS 34 import os 35 import sys 36 import time 37 import argparse 38 39 from RNS._version import __version__ 40 41 remote_link = None 42 output_rst_str = "\r \r" 43 def connect_remote(destination_hash, auth_identity, timeout, no_output = False, purpose="management"): 44 global remote_link, reticulum 45 if not RNS.Transport.has_path(destination_hash): 46 if not no_output: 47 print("Path to "+RNS.prettyhexrep(destination_hash)+" requested", end=" ") 48 sys.stdout.flush() 49 RNS.Transport.request_path(destination_hash) 50 pr_time = time.time() 51 while not RNS.Transport.has_path(destination_hash): 52 time.sleep(0.1) 53 if time.time() - pr_time > timeout: 54 if not no_output: 55 print(output_rst_str, end="") 56 print("Path request timed out") 57 exit(12) 58 59 remote_identity = RNS.Identity.recall(destination_hash) 60 61 def remote_link_closed(link): 62 if link.teardown_reason == RNS.Link.TIMEOUT: 63 if not no_output: 64 print(output_rst_str, end="") 65 print("The link timed out, exiting now") 66 elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED: 67 if not no_output: 68 print(output_rst_str, end="") 69 print("The link was closed by the server, exiting now") 70 else: 71 if not no_output: 72 print(output_rst_str, end="") 73 print("Link closed unexpectedly, exiting now") 74 exit(10) 75 76 def remote_link_established(link): 77 global remote_link 78 if purpose == "management": link.identify(auth_identity) 79 remote_link = link 80 81 if not no_output: 82 print(output_rst_str, end="") 83 print("Establishing link with remote transport instance...", end=" ") 84 sys.stdout.flush() 85 86 if purpose == "management": remote_destination = RNS.Destination(remote_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "rnstransport", "remote", "management") 87 elif purpose == "blackhole": remote_destination = RNS.Destination(remote_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "rnstransport", "info", "blackhole") 88 link = RNS.Link(remote_destination) 89 link.set_link_established_callback(remote_link_established) 90 link.set_link_closed_callback(remote_link_closed) 91 92 def parse_hash(input_str): 93 dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 94 if len(input_str) != dest_len: raise ValueError("Hash length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)) 95 try: 96 hash_bytes = bytes.fromhex(input_str) 97 return hash_bytes 98 except Exception as e: raise ValueError("Invalid hash entered. Check your input.") 99 100 def program_setup(configdir, table, rates, drop, destination_hexhash, verbosity, timeout, drop_queues, 101 drop_via, max_hops, remote=None, management_identity=None, remote_timeout=RNS.Transport.PATH_REQUEST_TIMEOUT, 102 blackholed=False, blackhole=False, unblackhole=False, blackhole_duration=None, blackhole_reason=None, 103 remote_blackhole_list=False, remote_blackhole_list_filter=None, no_output=False, json=False): 104 105 global remote_link, reticulum 106 reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity) 107 if remote: 108 try: 109 dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 110 if len(remote) != dest_len: raise ValueError("Destination length is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2)) 111 try: 112 identity_hash = bytes.fromhex(remote) 113 remote_hash = RNS.Destination.hash_from_name_and_identity("rnstransport.remote.management", identity_hash) 114 except Exception as e: raise ValueError("Invalid destination entered. Check your input.") 115 116 identity = RNS.Identity.from_file(os.path.expanduser(management_identity)) 117 if identity == None: raise ValueError("Could not load management identity from "+str(management_identity)) 118 119 try: connect_remote(remote_hash, identity, remote_timeout, no_output) 120 except Exception as e: raise e 121 122 except Exception as e: 123 print(str(e)) 124 exit(20) 125 126 while remote_link == None: time.sleep(0.1) 127 128 if blackholed or remote_blackhole_list: 129 blackholed_list = None 130 if blackholed: 131 if remote_link: 132 if not no_output: 133 print(output_rst_str, end="") 134 print("Listing blackholed identities on remote instances not yet implemented") 135 exit(255) 136 137 try: blackholed_list = reticulum.get_blackholed_identities() 138 except Exception as e: 139 print(f"Could not get blackholed identities from RNS instance: {e}") 140 exit(20) 141 142 elif remote_blackhole_list: 143 try: identity_hash = parse_hash(destination_hexhash) 144 except Exception as e: 145 print(f"{e}") 146 exit(20) 147 148 remote_hash = RNS.Destination.hash_from_name_and_identity("rnstransport.info.blackhole", identity_hash) 149 connect_remote(remote_hash, None, remote_timeout, no_output, purpose="blackhole") 150 while remote_link == None: time.sleep(0.1) 151 152 if not no_output: 153 print(output_rst_str, end="") 154 print("Sending request...", end=" ") 155 sys.stdout.flush() 156 receipt = remote_link.request("/list") 157 while not receipt.concluded(): time.sleep(0.1) 158 response = receipt.get_response() 159 if type(response) == dict: 160 blackholed_list = response 161 print(output_rst_str, end="") 162 else: 163 if not no_output: 164 print(output_rst_str, end="") 165 print("The remote request failed.") 166 exit(10) 167 168 else: 169 print(f"Nowhere to fetch blackhole list from") 170 exit(255) 171 172 if not blackholed_list: 173 print("No blackholed identity data available") 174 exit(20) 175 176 else: 177 rmlen = 64 178 def trunc(input_str): 179 if len(input_str) <= rmlen: return input_str 180 else: return f"{input_str[:rmlen-1]}…" 181 182 try: 183 now = time.time() 184 for identity_hash in blackholed_list: 185 until = blackholed_list[identity_hash]["until"] 186 reason = blackholed_list[identity_hash]["reason"] 187 source = blackholed_list[identity_hash]["source"] 188 until_str = f"for {RNS.prettytime(max(0, until-now))}" if until else "indefinitely" 189 reason_str = f" ({trunc(reason)})" if reason else "" 190 by_str = f" by {RNS.prettyhexrep(source)}" if source != RNS.Transport.identity.hash else "" 191 filter_str = f"{RNS.prettyhexrep(identity_hash)} {until_str} {reason_str} {by_str}" 192 193 if not remote_blackhole_list: 194 if destination_hexhash and not destination_hexhash in filter_str: continue 195 else: 196 if remote_blackhole_list_filter and not remote_blackhole_list_filter in filter_str: continue 197 198 print(f"{RNS.prettyhexrep(identity_hash)} blackholed {until_str}{reason_str}{by_str}") 199 200 except Exception as e: 201 print(f"Error while displaying collected blackhole data: {e}") 202 exit(20) 203 204 elif blackhole: 205 if remote_link: 206 if not no_output: 207 print(output_rst_str, end="") 208 print("Blackholing identity on remote instances not yet implemented") 209 exit(255) 210 211 try: 212 identity_hash = parse_hash(destination_hexhash) 213 until = time.time()+blackhole_duration*60*60 if blackhole_duration else None 214 result = reticulum.blackhole_identity(identity_hash, until=until, reason=blackhole_reason) 215 if result == True: print(f"Blackholed identity {destination_hexhash}") 216 elif result == None: print(f"Identity {destination_hexhash} already blackholed") 217 else: print(f"Could not blackhole identity {destination_hexhash}") 218 219 except Exception as e: 220 print(f"Could not blackhole identity: {e}") 221 exit(20) 222 223 elif unblackhole: 224 if remote_link: 225 if not no_output: 226 print(output_rst_str, end="") 227 print("Blackholing identity on remote instances not yet implemented") 228 exit(255) 229 230 try: 231 identity_hash = parse_hash(destination_hexhash) 232 result = reticulum.unblackhole_identity(identity_hash) 233 if result == True: print(f"Lifted blackhole for identity {destination_hexhash}") 234 elif result == None: print(f"Identity {destination_hexhash} not blackholed") 235 else: print(f"Could not unblackhole identity {destination_hexhash}") 236 237 except Exception as e: 238 print(f"Could not unblackhole identity: {e}") 239 exit(20) 240 241 elif table: 242 destination_hash = None 243 if destination_hexhash != None: 244 try: 245 dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 246 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)) 247 try: destination_hash = bytes.fromhex(destination_hexhash) 248 except Exception as e: raise ValueError("Invalid destination entered. Check your input.") 249 except Exception as e: 250 print(str(e)) 251 sys.exit(1) 252 253 if not remote_link: table = sorted(reticulum.get_path_table(max_hops=max_hops), key=lambda e: (e["interface"], e["hops"]) ) 254 else: 255 if not no_output: 256 print(output_rst_str, end="") 257 print("Sending request...", end=" ") 258 sys.stdout.flush() 259 receipt = remote_link.request("/path", data = ["table", destination_hash, max_hops]) 260 while not receipt.concluded(): time.sleep(0.1) 261 response = receipt.get_response() 262 if response: 263 table = response 264 print(output_rst_str, end="") 265 else: 266 if not no_output: 267 print(output_rst_str, end="") 268 print("The remote request failed. Likely authentication failure.") 269 exit(10) 270 271 displayed = 0 272 if json: 273 import json 274 for p in table: 275 for k in p: 276 if isinstance(p[k], bytes): p[k] = RNS.hexrep(p[k], delimit=False) 277 278 print(json.dumps(table)) 279 exit() 280 281 else: 282 for path in table: 283 if destination_hash == None or destination_hash == path["hash"]: 284 displayed += 1 285 exp_str = RNS.timestamp_str(path["expires"]) 286 if path["hops"] == 1: m_str = " " 287 else: m_str = "s" 288 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"])) 289 290 if destination_hash != None and displayed == 0: 291 print("No path known") 292 sys.exit(1) 293 294 elif rates: 295 destination_hash = None 296 if destination_hexhash != None: 297 try: 298 dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 299 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)) 300 try: destination_hash = bytes.fromhex(destination_hexhash) 301 except Exception as e: raise ValueError("Invalid destination entered. Check your input.") 302 except Exception as e: 303 print(str(e)) 304 sys.exit(1) 305 306 if not remote_link: table = reticulum.get_rate_table() 307 else: 308 if not no_output: 309 print(output_rst_str, end="") 310 print("Sending request...", end=" ") 311 sys.stdout.flush() 312 receipt = remote_link.request("/path", data = ["rates", destination_hash]) 313 while not receipt.concluded(): 314 time.sleep(0.1) 315 response = receipt.get_response() 316 if response: 317 table = response 318 print(output_rst_str, end="") 319 else: 320 if not no_output: 321 print(output_rst_str, end="") 322 print("The remote request failed. Likely authentication failure.") 323 exit(10) 324 325 table = sorted(table, key=lambda e: e["last"]) 326 if json: 327 import json 328 for p in table: 329 for k in p: 330 if isinstance(p[k], bytes): p[k] = RNS.hexrep(p[k], delimit=False) 331 332 print(json.dumps(table)) 333 exit() 334 else: 335 if len(table) == 0: print("No information available") 336 else: 337 displayed = 0 338 for entry in table: 339 if destination_hash == None or destination_hash == entry["hash"]: 340 displayed += 1 341 try: 342 last_str = pretty_date(int(entry["last"])) 343 start_ts = entry["timestamps"][0] 344 span = max(time.time() - start_ts, 3600.0) 345 span_hours = span/3600.0 346 span_str = pretty_date(int(entry["timestamps"][0])) 347 hour_rate = round(len(entry["timestamps"])/span_hours, 3) 348 if hour_rate-int(hour_rate) == 0: 349 hour_rate = int(hour_rate) 350 351 if entry["rate_violations"] > 0: 352 if entry["rate_violations"] == 1: 353 s_str = "" 354 else: 355 s_str = "s" 356 357 rv_str = ", "+str(entry["rate_violations"])+" active rate violation"+s_str 358 else: 359 rv_str = "" 360 361 if entry["blocked_until"] > time.time(): 362 bli = time.time()-(int(entry["blocked_until"])-time.time()) 363 bl_str = ", new announces allowed in "+pretty_date(int(bli)) 364 else: 365 bl_str = "" 366 367 368 print(RNS.prettyhexrep(entry["hash"])+" last heard "+last_str+" ago, "+str(hour_rate)+" announces/hour in the last "+span_str+rv_str+bl_str) 369 370 except Exception as e: 371 print("Error while processing entry for "+RNS.prettyhexrep(entry["hash"])) 372 print(str(e)) 373 374 if destination_hash != None and displayed == 0: 375 print("No information available") 376 sys.exit(1) 377 378 elif drop_queues: 379 if remote_link: 380 if not no_output: 381 print(output_rst_str, end="") 382 print("Dropping announce queues on remote instances not yet implemented") 383 exit(255) 384 385 print("Dropping announce queues on all interfaces...") 386 reticulum.drop_announce_queues() 387 388 elif drop: 389 if remote_link: 390 if not no_output: 391 print(output_rst_str, end="") 392 print("Dropping path on remote instances not yet implemented") 393 exit(255) 394 395 try: 396 dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 397 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)) 398 try: destination_hash = bytes.fromhex(destination_hexhash) 399 except Exception as e: raise ValueError("Invalid destination entered. Check your input.") 400 except Exception as e: 401 print(str(e)) 402 sys.exit(1) 403 404 if reticulum.drop_path(destination_hash): print("Dropped path to "+RNS.prettyhexrep(destination_hash)) 405 else: 406 print("Unable to drop path to "+RNS.prettyhexrep(destination_hash)+". Does it exist?") 407 sys.exit(1) 408 409 elif drop_via: 410 if remote_link: 411 if not no_output: 412 print(output_rst_str, end="") 413 print("Dropping all paths via specific transport instance on remote instances yet not implemented") 414 exit(255) 415 416 try: 417 dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 418 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)) 419 try: destination_hash = bytes.fromhex(destination_hexhash) 420 except Exception as e: raise ValueError("Invalid destination entered. Check your input.") 421 except Exception as e: 422 print(str(e)) 423 sys.exit(1) 424 425 if reticulum.drop_all_via(destination_hash): print("Dropped all paths via "+RNS.prettyhexrep(destination_hash)) 426 else: 427 print("Unable to drop paths via "+RNS.prettyhexrep(destination_hash)+". Does the transport instance exist?") 428 sys.exit(1) 429 430 else: 431 if remote_link: 432 if not no_output: 433 print(output_rst_str, end="") 434 print("Requesting paths on remote instances not implemented") 435 exit(255) 436 437 try: 438 dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2 439 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)) 440 try: destination_hash = bytes.fromhex(destination_hexhash) 441 except Exception as e: raise ValueError("Invalid destination entered. Check your input.") 442 except Exception as e: 443 print(str(e)) 444 sys.exit(1) 445 446 if not RNS.Transport.has_path(destination_hash): 447 RNS.Transport.request_path(destination_hash) 448 print("Path to "+RNS.prettyhexrep(destination_hash)+" requested ", end=" ") 449 sys.stdout.flush() 450 451 i = 0 452 syms = "⢄⢂⢁⡁⡈⡐⡠" 453 limit = time.time()+timeout 454 while not RNS.Transport.has_path(destination_hash) and time.time()<limit: 455 time.sleep(0.1) 456 print(("\b\b"+syms[i]+" "), end="") 457 sys.stdout.flush() 458 i = (i+1)%len(syms) 459 460 if RNS.Transport.has_path(destination_hash): 461 hops = RNS.Transport.hops_to(destination_hash) 462 next_hop_bytes = reticulum.get_next_hop(destination_hash) 463 if next_hop_bytes == None: 464 print("\r \rError: Invalid path data returned") 465 sys.exit(1) 466 else: 467 next_hop = RNS.prettyhexrep(next_hop_bytes) 468 next_hop_interface = reticulum.get_next_hop_if_name(destination_hash) 469 470 if hops != 1: ms = "s" 471 else: ms = "" 472 473 print("\rPath found, destination "+RNS.prettyhexrep(destination_hash)+" is "+str(hops)+" hop"+ms+" away via "+next_hop+" on "+next_hop_interface) 474 else: 475 print("\r \rPath not found") 476 sys.exit(1) 477 478 479 def main(): 480 try: 481 parser = argparse.ArgumentParser(description="Reticulum Path Management Utility") 482 parser.add_argument("--config", action="store", default=None, help="path to alternative Reticulum config directory", type=str) 483 parser.add_argument("--version", action="version", version="rnpath {version}".format(version=__version__)) 484 parser.add_argument("-t", "--table", action="store_true", help="show all known paths", default=False) 485 parser.add_argument("-m", "--max", action="store", metavar="hops", type=int, help="maximum hops to filter path table by", default=None) 486 parser.add_argument("-r", "--rates", action="store_true", help="show announce rate info", default=False) 487 parser.add_argument("-d", "--drop", action="store_true", help="remove the path to a destination", default=False) 488 parser.add_argument("-D", "--drop-announces", action="store_true", help="drop all queued announces", default=False) 489 parser.add_argument("-x", "--drop-via", action="store_true", help="drop all paths via specified transport instance", default=False) 490 parser.add_argument("-w", action="store", metavar="seconds", type=float, help="timeout before giving up", default=RNS.Transport.PATH_REQUEST_TIMEOUT) 491 parser.add_argument("-R", action="store", metavar="hash", help="transport identity hash of remote instance to manage", default=None, type=str) 492 parser.add_argument("-i", action="store", metavar="path", help="path to identity used for remote management", default=None, type=str) 493 parser.add_argument("-W", action="store", metavar="seconds", type=float, help="timeout before giving up on remote queries", default=RNS.Transport.PATH_REQUEST_TIMEOUT) 494 parser.add_argument("-b", "--blackholed", action="store_true", help="list blackholed identities", default=False) 495 parser.add_argument("-B", "--blackhole", action="store_true", help="blackhole identity", default=False) 496 parser.add_argument("-U", "--unblackhole", action="store_true", help="unblackhole identity", default=False) 497 parser.add_argument( "--duration", action="store", type=float, help="duration of blackhole enforcement in hours", default=None) 498 parser.add_argument( "--reason", action="store", type=str, help="reason for blackholing identity", default=None) 499 parser.add_argument("-p", "--blackholed-list", action="store_true", help="view published blackhole list for remote transport instance", default=False) 500 parser.add_argument("-j", "--json", action="store_true", help="output in JSON format", default=False) 501 parser.add_argument("destination", nargs="?", default=None, help="hexadecimal hash of the destination", type=str) 502 parser.add_argument("list_filter", nargs="?", default=None, help="filter for remote blackhole list view", type=str) 503 parser.add_argument('-v', '--verbose', action='count', default=0) 504 505 args = parser.parse_args() 506 507 if args.config: configarg = args.config 508 else: configarg = None 509 510 if not args.drop_announces and not args.table and not args.rates and not args.destination and not args.drop_via and not args.blackholed: 511 print("") 512 parser.print_help() 513 print("") 514 else: 515 program_setup(configdir = configarg, table = args.table, rates = args.rates, drop = args.drop, destination_hexhash = args.destination, 516 verbosity = args.verbose, timeout = args.w, drop_queues = args.drop_announces, drop_via = args.drop_via, max_hops = args.max, 517 remote=args.R, management_identity=args.i, remote_timeout=args.W, blackholed=args.blackholed, blackhole=args.blackhole, 518 unblackhole=args.unblackhole, blackhole_duration=args.duration, blackhole_reason=args.reason, remote_blackhole_list=args.blackholed_list, 519 remote_blackhole_list_filter=args.list_filter, json=args.json) 520 521 sys.exit(0) 522 523 except KeyboardInterrupt: 524 print("") 525 exit() 526 527 def pretty_date(time=False): 528 from datetime import datetime 529 now = datetime.now() 530 if type(time) is int: diff = now - datetime.fromtimestamp(time) 531 elif isinstance(time,datetime): diff = now - time 532 elif not time: diff = now - now 533 second_diff = diff.seconds 534 day_diff = diff.days 535 if day_diff < 0: return '' 536 if day_diff == 0: 537 if second_diff < 10: return str(second_diff) + " seconds" 538 if second_diff < 60: return str(second_diff) + " seconds" 539 if second_diff < 120: return "1 minute" 540 if second_diff < 3600: return str(int(second_diff / 60)) + " minutes" 541 if second_diff < 7200: return "an hour" 542 if second_diff < 86400: return str(int(second_diff / 3600)) + " hours" 543 if day_diff == 1: return "1 day" 544 if day_diff < 7: return str(day_diff) + " days" 545 if day_diff < 31: return str(int(day_diff / 7)) + " weeks" 546 if day_diff < 365: return str(int(day_diff / 30)) + " months" 547 return str(int(day_diff / 365)) + " years" 548 549 if __name__ == "__main__": main()