Reticulum/RNS/Utilities/rnsh/rnsh.py

190 lines
7.7 KiB
Python
Raw Normal View History

2026-04-26 22:24:00 +02:00
#!/usr/bin/env python3
#
# Based on the original rnsh program by Aaron Heise (@acehoss)
# https://github.com/acehoss/rnsh - MIT License - Copyright (c) 2023 Aaron Heise
# This version of rnsh is included in RNS under the Reticulum License
#
# Reticulum License
#
# Copyright (c) 2016-2026 Mark Qvist
2026-04-26 22:24:00 +02:00
#
# 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 Software shall not be used in any kind of system which includes amongst
# its functions the ability to purposefully do harm to human beings.
#
# - The Software shall not be used, directly or indirectly, in the creation of
# an artificial intelligence, machine learning or language model training
# dataset, including but not limited to any use that contributes to the
# training or development of such a model or algorithm.
#
# - The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
2026-04-26 22:24:00 +02:00
#
# 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.
from __future__ import annotations
import asyncio
import re
import os
import sys
import RNS
2026-04-27 00:06:33 +02:00
import RNS.Utilities.rnsh.process as process
import RNS.Utilities.rnsh.session as session
import RNS.Utilities.rnsh.args
import RNS.Utilities.rnsh.loop
import RNS.Utilities.rnsh.listener as listener
import RNS.Utilities.rnsh.initiator as initiator
from RNS.Utilities.rnsh.args import parse_arguments
2026-04-26 22:24:00 +02:00
APP_NAME = "rnsh"
loop: asyncio.AbstractEventLoop | None = None
def _sanitize_service_name(service_name:str) -> str: return re.sub(r'\W+', '', service_name)
def prepare_identity(identity_path=None, service_name=None, configdir=None):
2026-04-26 22:24:00 +02:00
service_name = _sanitize_service_name(service_name or "")
if not identity_path:
identity_path = f"{configdir}/identity" + (f".{service_name}" if service_name and len(service_name) > 0 else "")
2026-04-26 22:24:00 +02:00
identity = None
if os.path.isfile(identity_path):
RNS.log(f"Loading identity from {identity_path}", RNS.LOG_VERBOSE)
2026-04-26 22:24:00 +02:00
identity = RNS.Identity.from_file(identity_path)
if identity is None:
RNS.log(f"Creating identity {identity_path}", RNS.LOG_NOTICE)
2026-04-26 22:24:00 +02:00
identity = RNS.Identity()
identity.to_file(identity_path)
return identity
def print_identity(configdir, rnsconfigdir, identitypath, service_name, include_destination: bool):
reticulum = RNS.Reticulum(configdir=rnsconfigdir, loglevel=RNS.LOG_INFO)
if service_name and len(service_name) > 0: print(f"Using service name \"{service_name}\"")
identity = prepare_identity(identity_path=identitypath, service_name=service_name, configdir=configdir)
2026-04-26 22:24:00 +02:00
destination = RNS.Destination(identity, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME)
print("Identity : " + str(identity))
if include_destination: print("Listening on : " + RNS.prettyhexrep(destination.hash))
2026-04-26 22:24:00 +02:00
exit(0)
verbose_set = False
def ensure_config_directory(configdir=None):
if configdir:
if os.path.isdir(os.path.expanduser(configdir)): return os.path.expanduser(configdir)
else:
try:
RNS.log(f"Creating configuration directory: {os.path.expanduser(configdir)}")
os.makedirs(os.path.expanduser(configdir))
return os.path.expanduser(configdir)
except Exception as e:
RNS.log(f"Could not get or create rnsh configuration directory, aborting", RNS.LOG_CRITICAL)
os._exit(1)
elif os.path.isdir(os.path.expanduser("~/.config/rnsh")): return os.path.expanduser("~/.config/rnsh")
elif os.path.isdir(os.path.expanduser("~/.rnsh")): return os.path.expanduser("~/.rnsh")
2026-04-26 22:24:00 +02:00
else:
try:
RNS.log(f"Creating configuration directory: {os.path.expanduser('~/.rnsh')}")
2026-04-26 22:24:00 +02:00
os.makedirs(os.path.expanduser("~/.rnsh"))
return os.path.expanduser("~/.rnsh")
except Exception as e:
RNS.log(f"Could not get or create rnsh configuration directory, aborting", RNS.LOG_CRITICAL)
os._exit(1)
async def _rnsh_cli_main():
global verbose_set
2026-04-27 00:06:33 +02:00
args, parser = parse_arguments()
2026-07-18 19:23:02 +02:00
verbose_set = args.verbose > 0
configdir = ensure_config_directory(args.config)
if not configdir:
RNS.log(f"Could not resolve rnsh configuration directory", RNS.LOG_CRITICAL)
os._exit(1)
2026-04-26 22:24:00 +02:00
if args.print_identity:
print_identity(configdir, args.rnsconfig, args.identity, args.service, args.listen)
2026-04-26 22:24:00 +02:00
return 0
logfile = f"{configdir}/logfile"
2026-04-26 22:24:00 +02:00
if args.listen:
allowed_file = None
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH//8)*2
if os.path.isfile(os.path.expanduser(f"{configdir}/allowed_identities")):
allowed_file = os.path.expanduser(f"{configdir}/allowed_identities")
2026-04-26 22:24:00 +02:00
await listener.listen(configdir=configdir,
rnsconfigdir=args.rnsconfig,
2026-04-27 00:06:33 +02:00
command=args.command,
2026-04-26 22:24:00 +02:00
identitypath=args.identity,
logfile=logfile,
2026-04-27 00:06:33 +02:00
service_name=args.service,
2026-04-26 22:24:00 +02:00
verbosity=args.verbose,
quietness=args.quiet,
2026-04-27 00:06:33 +02:00
allowed=args.allowed or [],
2026-04-26 22:24:00 +02:00
allowed_file=allowed_file,
disable_auth=args.no_auth,
announce_period=args.announce,
2026-04-27 00:06:33 +02:00
no_remote_command=args.no_remote_command,
remote_cmd_as_args=args.remote_command_as_args)
2026-04-26 22:24:00 +02:00
return 0
if args.destination is not None:
return_code = await initiator.initiate(configdir=configdir,
rnsconfigdir=args.rnsconfig,
2026-04-26 22:24:00 +02:00
identitypath=args.identity,
logfile=f"{logfile}.initiator",
2026-04-26 22:24:00 +02:00
verbosity=args.verbose,
quietness=args.quiet,
noid=args.no_id,
destination=args.destination,
timeout=args.timeout,
2026-04-27 00:06:33 +02:00
command=args.command
2026-04-26 22:24:00 +02:00
)
2026-07-26 14:29:47 +02:00
RNS.log(f"Sesssion with <{args.destination}> ended", RNS.LOG_INFO)
2026-04-26 22:24:00 +02:00
return return_code if args.mirror else 0
else:
print("")
2026-04-27 00:06:33 +02:00
parser.print_help()
2026-04-26 22:24:00 +02:00
print("")
return 1
def main():
global verbose_set
return_code = 1
exc = None
try: return_code = asyncio.run(_rnsh_cli_main())
except SystemExit: pass
except KeyboardInterrupt: pass
2026-04-28 15:46:04 +02:00
except Exception as e:
print(f"{e}")
exc = e
2026-04-26 22:24:00 +02:00
process.tty_unset_reader_callbacks(0)
if verbose_set and exc: raise exc
sys.exit(return_code if return_code is not None else 255)
if __name__ == "__main__": main()