Added remote transport instance status to rnstatus utility
This commit is contained in:
parent
bce37fe8c0
commit
e8d1897edd
|
@ -216,6 +216,7 @@ class Reticulum:
|
|||
Reticulum.identitypath = Reticulum.configdir+"/storage/identities"
|
||||
|
||||
Reticulum.__transport_enabled = False
|
||||
Reticulum.__remote_management_enabled = False
|
||||
Reticulum.__use_implicit_proof = True
|
||||
Reticulum.__allow_probes = False
|
||||
|
||||
|
@ -346,6 +347,7 @@ class Reticulum:
|
|||
self.is_standalone_instance = False
|
||||
self.is_connected_to_shared_instance = True
|
||||
Reticulum.__transport_enabled = False
|
||||
Reticulum.__remote_management_enabled = False
|
||||
Reticulum.__allow_probes = False
|
||||
RNS.log("Connected to locally available Reticulum instance via: "+str(interface), RNS.LOG_DEBUG)
|
||||
except Exception as e:
|
||||
|
@ -396,6 +398,23 @@ class Reticulum:
|
|||
v = self.config["reticulum"].as_bool(option)
|
||||
if v == True:
|
||||
Reticulum.__transport_enabled = True
|
||||
if option == "enable_remote_management":
|
||||
v = self.config["reticulum"].as_bool(option)
|
||||
if v == True:
|
||||
Reticulum.__remote_management_enabled = True
|
||||
if option == "remote_management_allowed":
|
||||
v = self.config["reticulum"].as_list(option)
|
||||
for hexhash in v:
|
||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||
if len(hexhash) != dest_len:
|
||||
raise ValueError("Identity hash length for remote management ACL "+str(hexhash)+" is invalid, must be {hex} hexadecimal characters ({byte} bytes).".format(hex=dest_len, byte=dest_len//2))
|
||||
try:
|
||||
allowed_hash = bytes.fromhex(hexhash)
|
||||
except Exception as e:
|
||||
raise ValueError("Invalid identity hash for remote management ACL: "+str(hexhash))
|
||||
|
||||
if not allowed_hash in RNS.Transport.remote_management_allowed:
|
||||
RNS.Transport.remote_management_allowed.append(allowed_hash)
|
||||
if option == "respond_to_probes":
|
||||
v = self.config["reticulum"].as_bool(option)
|
||||
if v == True:
|
||||
|
@ -1516,6 +1535,19 @@ class Reticulum:
|
|||
"""
|
||||
return Reticulum.__transport_enabled
|
||||
|
||||
@staticmethod
|
||||
def remote_management_enabled():
|
||||
"""
|
||||
Returns whether remote management is enabled for the
|
||||
running instance.
|
||||
|
||||
When remote management is enabled, authenticated peers
|
||||
can remotely query and manage this instance.
|
||||
|
||||
:returns: True if remote management is enabled, False if not.
|
||||
"""
|
||||
return Reticulum.__remote_management_enabled
|
||||
|
||||
@staticmethod
|
||||
def probe_destination_enabled():
|
||||
return Reticulum.__allow_probes
|
||||
|
|
|
@ -110,6 +110,7 @@ class Transport:
|
|||
# for control purposes like path requests
|
||||
control_destinations = []
|
||||
control_hashes = []
|
||||
remote_management_allowed = []
|
||||
|
||||
# Interfaces for communicating with
|
||||
# local clients connected to a shared
|
||||
|
@ -179,6 +180,13 @@ class Transport:
|
|||
Transport.control_destinations.append(Transport.tunnel_synthesize_handler)
|
||||
Transport.control_hashes.append(Transport.tunnel_synthesize_destination.hash)
|
||||
|
||||
if RNS.Reticulum.remote_management_enabled() and not Transport.owner.is_connected_to_shared_instance:
|
||||
Transport.remote_management_destination = RNS.Destination(Transport.identity, RNS.Destination.IN, RNS.Destination.SINGLE, Transport.APP_NAME, "remote", "management")
|
||||
Transport.remote_management_destination.register_request_handler("/status", response_generator = Transport.remote_status_handler, allow = RNS.Destination.ALLOW_LIST, allowed_list=Transport.remote_management_allowed)
|
||||
Transport.control_destinations.append(Transport.remote_management_destination)
|
||||
Transport.control_hashes.append(Transport.remote_management_destination.hash)
|
||||
RNS.log("Enabled remote management on "+str(Transport.remote_management_destination), RNS.LOG_NOTICE)
|
||||
|
||||
Transport.jobs_running = False
|
||||
thread = threading.Thread(target=Transport.jobloop, daemon=True)
|
||||
thread.start()
|
||||
|
@ -2247,6 +2255,25 @@ class Transport:
|
|||
packet.send()
|
||||
Transport.path_requests[destination_hash] = time.time()
|
||||
|
||||
@staticmethod
|
||||
def remote_status_handler(path, data, request_id, link_id, remote_identity, requested_at):
|
||||
if remote_identity != None:
|
||||
response = None
|
||||
try:
|
||||
if isinstance(data, list) and len(data) > 0:
|
||||
response = []
|
||||
response.append(Transport.owner.get_interface_stats())
|
||||
if data[0] == True:
|
||||
response.append(Transport.owner.get_link_count())
|
||||
|
||||
return response
|
||||
|
||||
except Exception as e:
|
||||
RNS.log("An error occurred while processing remote status request from "+RNS.prettyhexrep(remote_identity), RNS.LOG_ERROR)
|
||||
RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def path_request_handler(data, packet):
|
||||
try:
|
||||
|
|
|
@ -23,6 +23,9 @@
|
|||
# SOFTWARE.
|
||||
|
||||
import RNS
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import argparse
|
||||
|
||||
from RNS._version import __version__
|
||||
|
@ -46,17 +49,130 @@ def size_str(num, suffix='B'):
|
|||
|
||||
return "%.2f%s%s" % (num, last_unit, suffix)
|
||||
|
||||
def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=False, astats=False, lstats=False, sorting=None, sort_reverse=False):
|
||||
request_result = None
|
||||
request_concluded = False
|
||||
def get_remote_status(destination_hash, include_lstats, identity, no_output=False):
|
||||
global request_result, request_concluded
|
||||
link_count = None
|
||||
|
||||
if not RNS.Transport.has_path(destination_hash):
|
||||
if not no_output:
|
||||
print("Path to "+RNS.prettyhexrep(destination_hash)+" requested", end=" ")
|
||||
sys.stdout.flush()
|
||||
RNS.Transport.request_path(destination_hash)
|
||||
pr_time = time.time()
|
||||
pr_timeout = 10
|
||||
while not RNS.Transport.has_path(destination_hash):
|
||||
time.sleep(0.1)
|
||||
if time.time() - pr_time > pr_timeout:
|
||||
if not no_output:
|
||||
print("\r \r", end="")
|
||||
print("Path request timed out")
|
||||
exit(12)
|
||||
|
||||
remote_identity = RNS.Identity.recall(destination_hash)
|
||||
|
||||
def remote_link_closed(link):
|
||||
if link.teardown_reason == RNS.Link.TIMEOUT:
|
||||
if not no_output:
|
||||
print("\r \r", end="")
|
||||
print("The link timed out, exiting now")
|
||||
elif link.teardown_reason == RNS.Link.DESTINATION_CLOSED:
|
||||
if not no_output:
|
||||
print("\r \r", end="")
|
||||
print("The link was closed by the server, exiting now")
|
||||
else:
|
||||
if not no_output:
|
||||
print("\r \r", end="")
|
||||
print("Link closed unexpectedly, exiting now")
|
||||
exit(10)
|
||||
|
||||
def request_failed(request_receipt):
|
||||
global request_result, request_concluded
|
||||
if not no_output:
|
||||
print("\r \r", end="")
|
||||
print("The remote status request failed. Likely authentication failure.")
|
||||
request_concluded = True
|
||||
|
||||
def got_response(request_receipt):
|
||||
global request_result, request_concluded
|
||||
response = request_receipt.response
|
||||
if isinstance(response, list):
|
||||
status = response[0]
|
||||
if len(response) > 1:
|
||||
link_count = response[1]
|
||||
else:
|
||||
link_count = None
|
||||
|
||||
request_result = (status, link_count)
|
||||
|
||||
request_concluded = True
|
||||
|
||||
def remote_link_established(link):
|
||||
if not no_output:
|
||||
print("\r \r", end="")
|
||||
print("Sending request...", end=" ")
|
||||
sys.stdout.flush()
|
||||
link.identify(identity)
|
||||
link.request("/status", data = [include_lstats], response_callback = got_response, failed_callback = request_failed)
|
||||
|
||||
if not no_output:
|
||||
print("\r \r", end="")
|
||||
print("Establishing link with remote transport instance...", end=" ")
|
||||
sys.stdout.flush()
|
||||
|
||||
remote_destination = RNS.Destination(remote_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "rnstransport", "remote", "management")
|
||||
link = RNS.Link(remote_destination)
|
||||
link.set_link_established_callback(remote_link_established)
|
||||
link.set_link_closed_callback(remote_link_closed)
|
||||
|
||||
while not request_concluded:
|
||||
time.sleep(0.1)
|
||||
|
||||
if request_result != None:
|
||||
print("\r \r", end="")
|
||||
|
||||
return request_result
|
||||
|
||||
def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=False, astats=False,
|
||||
lstats=False, sorting=None, sort_reverse=False, remote=None, management_identity=None):
|
||||
reticulum = RNS.Reticulum(configdir = configdir, loglevel = 3+verbosity)
|
||||
|
||||
link_count = None
|
||||
stats = None
|
||||
if remote:
|
||||
try:
|
||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
|
||||
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))
|
||||
try:
|
||||
identity_hash = bytes.fromhex(remote)
|
||||
destination_hash = RNS.Destination.hash_from_name_and_identity("rnstransport.remote.management", identity_hash)
|
||||
except Exception as e:
|
||||
raise ValueError("Invalid destination entered. Check your input.")
|
||||
|
||||
identity = RNS.Identity.from_file(os.path.expanduser(management_identity))
|
||||
if identity == None:
|
||||
raise ValueError("Could not load management identity from "+str(management_identity))
|
||||
|
||||
try:
|
||||
remote_status = get_remote_status(destination_hash, lstats, identity, no_output=json)
|
||||
if remote_status != None:
|
||||
stats, link_count = remote_status
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
exit(20)
|
||||
|
||||
else:
|
||||
if lstats:
|
||||
try:
|
||||
link_count = reticulum.get_link_count()
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
stats = None
|
||||
try:
|
||||
stats = reticulum.get_interface_stats()
|
||||
except Exception as e:
|
||||
|
@ -227,6 +343,7 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=
|
|||
print("\n Transport Instance "+RNS.prettyhexrep(stats["transport_id"])+" running")
|
||||
if "probe_responder" in stats and stats["probe_responder"] != None:
|
||||
print(" Probe responder at "+RNS.prettyhexrep(stats["probe_responder"])+ " active")
|
||||
if "transport_uptime" in stats and stats["transport_uptime"] != None:
|
||||
print(" Uptime is "+RNS.prettytime(stats["transport_uptime"])+lstr)
|
||||
else:
|
||||
if lstr != "":
|
||||
|
@ -235,7 +352,10 @@ def program_setup(configdir, dispall=False, verbosity=0, name_filter=None, json=
|
|||
print("")
|
||||
|
||||
else:
|
||||
if not remote:
|
||||
print("Could not get RNS status")
|
||||
else:
|
||||
print("Could not get RNS status from remote transport instance "+RNS.prettyhexrep(identity_hash))
|
||||
|
||||
def main():
|
||||
try:
|
||||
|
@ -292,6 +412,26 @@ def main():
|
|||
default=False
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-R",
|
||||
"--remote",
|
||||
action="store",
|
||||
metavar="hash",
|
||||
help="transport identity hash of remote instance to get status from",
|
||||
default=None,
|
||||
type=str
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-i",
|
||||
"--identity",
|
||||
action="store",
|
||||
metavar="path",
|
||||
help="path to identity used for remote management",
|
||||
default=None,
|
||||
type=str
|
||||
)
|
||||
|
||||
parser.add_argument('-v', '--verbose', action='count', default=0)
|
||||
|
||||
parser.add_argument("filter", nargs="?", default=None, help="only display interfaces with names including filter", type=str)
|
||||
|
@ -313,6 +453,8 @@ def main():
|
|||
lstats=args.link_stats,
|
||||
sorting=args.sort,
|
||||
sort_reverse=args.reverse,
|
||||
remote=args.remote,
|
||||
management_identity=args.identity,
|
||||
)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
|
|
Loading…
Reference in New Issue