Beispiel #1
0
def server(configpath):
	# We must first initialise Reticulum
	reticulum = RNS.Reticulum(configpath)
	
	# Randomly create a new identity for our echo server
	server_identity = RNS.Identity()

	# We create a destination that clients can query. We want
	# to be able to verify echo replies to our clients, so we
	# create a "single" destination that can receive encrypted
	# messages. This way the client can send a request and be
	# certain that no-one else than this destination was able
	# to read it. 
	echo_destination = RNS.Destination(server_identity, RNS.Destination.IN, RNS.Destination.SINGLE, APP_NAME, "echo", "request")

	# We configure the destination to automatically prove all
	# packets adressed to it. By doing this, RNS will automatically
	# generate a proof for each incoming packet and transmit it
	# back to the sender of that packet.
	echo_destination.set_proof_strategy(RNS.Destination.PROVE_ALL)
	
	# Tell the destination which function in our program to
	# run when a packet is received. We do this so we can
	# print a log message when the server receives a request
	echo_destination.packet_callback(server_callback)

	# Everything's ready!
	# Let's Wait for client requests or user input
	announceLoop(echo_destination)
Beispiel #2
0
def program_setup(configpath):
    # We must first initialise Reticulum
    reticulum = RNS.Reticulum(configpath)

    # Randomly create a new identity for our example
    identity = RNS.Identity()

    # Using the identity we just created, we create a destination.
    # Destinations are endpoints in Reticulum, that can be addressed
    # and communicated with. Destinations can also announce their
    # existence, which will let the network know they are reachable
    # and autoomatically create paths to them, from anywhere else
    # in the network.
    destination = RNS.Destination(identity, RNS.Destination.IN,
                                  RNS.Destination.SINGLE, APP_NAME,
                                  "minimalsample")

    # We configure the destination to automatically prove all
    # packets adressed to it. By doing this, RNS will automatically
    # generate a proof for each incoming packet and transmit it
    # back to the sender of that packet. This will let anyone that
    # tries to communicate with the destination know whether their
    # communication was received correctly.
    destination.set_proof_strategy(RNS.Destination.PROVE_ALL)

    # Everything's ready!
    # Let's hand over control to the announce loop
    announceLoop(destination)
Beispiel #3
0
	def __init__(self, identity, direction, type, app_name, *aspects):
		# Check input values and build name string
		if "." in app_name: raise ValueError("Dots can't be used in app names") 
		if not type in Destination.types: raise ValueError("Unknown destination type")
		if not direction in Destination.directions: raise ValueError("Unknown destination direction")
		self.callbacks = Callbacks()
		self.type = type
		self.direction = direction
		self.proof_strategy = Destination.PROVE_NONE
		self.mtu = 0

		self.links = []

		if identity != None and type == Destination.SINGLE:
			aspects = aspects+(identity.hexhash,)

		if identity == None and direction == Destination.IN and self.type != Destination.PLAIN:
			identity = RNS.Identity()
			aspects = aspects+(identity.hexhash,)

		self.identity = identity

		self.name = Destination.getDestinationName(app_name, *aspects)		
		self.hash = Destination.getDestinationHash(app_name, *aspects)
		self.hexhash = self.hash.encode("hex_codec")

		self.callback = None
		self.proofcallback = None

		RNS.Transport.registerDestination(self)
Beispiel #4
0
def server(configpath):
    # We must first initialise Reticulum
    reticulum = RNS.Reticulum(configpath)
    
    # Randomly create a new identity for our link example
    server_identity = RNS.Identity()

    # We create a destination that clients can connect to. We
    # want clients to create links to this destination, so we
    # need to create a "single" destination type.
    server_destination = RNS.Destination(
        server_identity,
        RNS.Destination.IN,
        RNS.Destination.SINGLE,
        APP_NAME,
        "linkexample"
    )

    # We configure a function that will get called every time
    # a new client creates a link to this destination.
    server_destination.set_link_established_callback(client_connected)

    # Everything's ready!
    # Let's Wait for client requests or user input
    server_loop(server_destination)
Beispiel #5
0
    def start():
        if Transport.identity == None:
            transport_identity_path = RNS.Reticulum.configdir + "/transportidentity"
            if os.path.isfile(transport_identity_path):
                Transport.identity = RNS.Identity.from_file(
                    transport_identity_path)

            if Transport.identity == None:
                RNS.log("No valid Transport Identity on disk, creating...",
                        RNS.LOG_VERBOSE)
                Transport.identity = RNS.Identity()
                Transport.identity.save(transport_identity_path)
            else:
                RNS.log("Loaded Transport Identity from disk", RNS.LOG_VERBOSE)

        packet_hashlist_path = RNS.Reticulum.configdir + "/packet_hashlist"
        if os.path.isfile(packet_hashlist_path):
            try:
                file = open(packet_hashlist_path, "r")
                Transport.packet_hashlist = umsgpack.unpackb(file.read())
                file.close()
            except Exception as e:
                RNS.log(
                    "Could not load packet hashlist from disk, the contained exception was: "
                    + str(e), RNS.LOG_ERROR)

        thread = threading.Thread(target=Transport.jobloop)
        thread.setDaemon(True)
        thread.start()

        RNS.log("Transport instance " + str(Transport.identity) + " started")
Beispiel #6
0
def program_setup(configpath):
    # We must first initialise Reticulum
    reticulum = RNS.Reticulum(configpath)

    # Randomly create a new identity for our example
    identity = RNS.Identity()

    # Using the identity we just created, we create two destinations
    # in the "example_utilities.announcesample" application space.
    #
    # Destinations are endpoints in Reticulum, that can be addressed
    # and communicated with. Destinations can also announce their
    # existence, which will let the network know they are reachable
    # and autoomatically create paths to them, from anywhere else
    # in the network.
    destination_1 = RNS.Destination(identity, RNS.Destination.IN,
                                    RNS.Destination.SINGLE, APP_NAME,
                                    "announcesample", "fruits")

    destination_2 = RNS.Destination(identity, RNS.Destination.IN,
                                    RNS.Destination.SINGLE, APP_NAME,
                                    "announcesample", "noble_gases")

    # We configure the destinations to automatically prove all
    # packets adressed to it. By doing this, RNS will automatically
    # generate a proof for each incoming packet and transmit it
    # back to the sender of that packet. This will let anyone that
    # tries to communicate with the destination know whether their
    # communication was received correctly.
    destination_1.set_proof_strategy(RNS.Destination.PROVE_ALL)
    destination_2.set_proof_strategy(RNS.Destination.PROVE_ALL)

    # We create an announce handler and configure it to only ask for
    # announces from "example_utilities.announcesample.fruits".
    # Try changing the filter and see what happens.
    announce_handler = ExampleAnnounceHandler(
        aspect_filter="example_utilities.announcesample.fruits")

    # We register the announce handler with Reticulum
    RNS.Transport.register_announce_handler(announce_handler)

    # Everything's ready!
    # Let's hand over control to the announce loop
    announceLoop(destination_1, destination_2)
Beispiel #7
0
    def __init__(self):
        self.pending_inbound = []
        self.pending_outbound = []
        self.failed_outbound = []
        self.direct_links = {}
        self.delivery_destinations = {}

        self.processing_outbound = False
        self.processing_inbound = False

        self.identity = RNS.Identity()
        self.lxmf_query_destination = RNS.Destination(None, RNS.Destination.IN,
                                                      RNS.Destination.PLAIN,
                                                      APP_NAME, "query")
        self.propagation_destination = RNS.Destination(self.identity,
                                                       RNS.Destination.IN,
                                                       RNS.Destination.SINGLE,
                                                       APP_NAME, "propagation")

        self.__delivery_callback = None

        job_thread = threading.Thread(target=self.jobloop)
        job_thread.setDaemon(True)
        job_thread.start()
Beispiel #8
0
    def __init__(self,
                 configdir=None,
                 rnsconfigdir=None,
                 daemon=False,
                 force_console=False):
        self.version = __version__
        self.enable_client = False
        self.enable_node = False
        self.identity = None

        self.uimode = None

        if configdir == None:
            self.configdir = NomadNetworkApp.configdir
        else:
            self.configdir = configdir

        if force_console:
            self.force_console_log = True
        else:
            self.force_console_log = False

        if NomadNetworkApp._shared_instance == None:
            NomadNetworkApp._shared_instance = self

        self.rns = RNS.Reticulum(configdir=rnsconfigdir)

        self.configpath = self.configdir + "/config"
        self.ignoredpath = self.configdir + "/ignored"
        self.logfilepath = self.configdir + "/logfile"
        self.errorfilepath = self.configdir + "/errors"
        self.storagepath = self.configdir + "/storage"
        self.identitypath = self.configdir + "/storage/identity"
        self.cachepath = self.configdir + "/storage/cache"
        self.resourcepath = self.configdir + "/storage/resources"
        self.conversationpath = self.configdir + "/storage/conversations"
        self.directorypath = self.configdir + "/storage/directory"
        self.peersettingspath = self.configdir + "/storage/peersettings"

        self.pagespath = self.configdir + "/storage/pages"
        self.filespath = self.configdir + "/storage/files"
        self.cachepath = self.configdir + "/storage/cache"

        self.downloads_path = os.path.expanduser("~/Downloads")

        self.firstrun = False
        self.should_run_jobs = True
        self.job_interval = 5
        self.defer_jobs = 90

        self.peer_announce_at_start = True
        self.try_propagation_on_fail = True

        self.periodic_lxmf_sync = True
        self.lxmf_sync_interval = 360 * 60
        self.lxmf_sync_limit = 8

        if not os.path.isdir(self.storagepath):
            os.makedirs(self.storagepath)

        if not os.path.isdir(self.cachepath):
            os.makedirs(self.cachepath)

        if not os.path.isdir(self.resourcepath):
            os.makedirs(self.resourcepath)

        if not os.path.isdir(self.conversationpath):
            os.makedirs(self.conversationpath)

        if not os.path.isdir(self.pagespath):
            os.makedirs(self.pagespath)

        if not os.path.isdir(self.filespath):
            os.makedirs(self.filespath)

        if not os.path.isdir(self.cachepath):
            os.makedirs(self.cachepath)

        if os.path.isfile(self.configpath):
            try:
                self.config = ConfigObj(self.configpath)
                try:
                    self.applyConfig()
                except Exception as e:
                    RNS.log(
                        "The configuration file is invalid. The contained exception was: "
                        + str(e), RNS.LOG_ERROR)
                    nomadnet.panic()

                RNS.log("Configuration loaded from " + self.configpath)
            except Exception as e:
                RNS.log(
                    "Could not parse the configuration at " + self.configpath,
                    RNS.LOG_ERROR)
                RNS.log("Check your configuration file for errors!",
                        RNS.LOG_ERROR)
                nomadnet.panic()
        else:
            RNS.log(
                "Could not load config file, creating default configuration file..."
            )
            self.createDefaultConfig()
            self.firstrun = True

        if os.path.isfile(self.identitypath):
            try:
                self.identity = RNS.Identity.from_file(self.identitypath)
                if self.identity != None:
                    RNS.log("Loaded Primary Identity %s from %s" %
                            (str(self.identity), self.identitypath))
                else:
                    RNS.log(
                        "Could not load the Primary Identity from " +
                        self.identitypath, RNS.LOG_ERROR)
                    nomadnet.panic()
            except Exception as e:
                RNS.log(
                    "Could not load the Primary Identity from " +
                    self.identitypath, RNS.LOG_ERROR)
                RNS.log("The contained exception was: %s" % (str(e)),
                        RNS.LOG_ERROR)
                nomadnet.panic()
        else:
            try:
                RNS.log("No Primary Identity file found, creating new...")
                self.identity = RNS.Identity()
                self.identity.to_file(self.identitypath)
                RNS.log("Created new Primary Identity %s" %
                        (str(self.identity)))
            except Exception as e:
                RNS.log("Could not create and save a new Primary Identity",
                        RNS.LOG_ERROR)
                RNS.log("The contained exception was: %s" % (str(e)),
                        RNS.LOG_ERROR)
                nomadnet.panic()

        if os.path.isfile(self.peersettingspath):
            try:
                file = open(self.peersettingspath, "rb")
                self.peer_settings = msgpack.unpackb(file.read())
                file.close()

                if not "node_last_announce" in self.peer_settings:
                    self.peer_settings["node_last_announce"] = None

                if not "propagation_node" in self.peer_settings:
                    self.peer_settings["propagation_node"] = None

                if not "last_lxmf_sync" in self.peer_settings:
                    self.peer_settings["last_lxmf_sync"] = 0

                if not "node_connects" in self.peer_settings:
                    self.peer_settings["node_connects"] = 0

                if not "served_page_requests" in self.peer_settings:
                    self.peer_settings["served_page_requests"] = 0

                if not "served_file_requests" in self.peer_settings:
                    self.peer_settings["served_file_requests"] = 0

            except Exception as e:
                RNS.log(
                    "Could not load local peer settings from " +
                    self.peersettingspath, RNS.LOG_ERROR)
                RNS.log("The contained exception was: %s" % (str(e)),
                        RNS.LOG_ERROR)
                nomadnet.panic()
        else:
            try:
                RNS.log("No peer settings file found, creating new...")
                self.peer_settings = {
                    "display_name": "Anonymous Peer",
                    "announce_interval": None,
                    "last_announce": None,
                    "node_last_announce": None,
                    "propagation_node": None,
                    "last_lxmf_sync": 0,
                }
                self.save_peer_settings()
                RNS.log("Created new peer settings file")
            except Exception as e:
                RNS.log("Could not create and save a new peer settings file",
                        RNS.LOG_ERROR)
                RNS.log("The contained exception was: %s" % (str(e)),
                        RNS.LOG_ERROR)
                nomadnet.panic()

        self.ignored_list = []
        if os.path.isfile(self.ignoredpath):
            try:
                fh = open(self.ignoredpath, "rb")
                ignored_input = fh.read()
                fh.close()

                ignored_hash_strs = ignored_input.splitlines()

                for hash_str in ignored_hash_strs:
                    if len(hash_str
                           ) == RNS.Identity.TRUNCATED_HASHLENGTH // 8 * 2:
                        try:
                            ignored_hash = bytes.fromhex(
                                hash_str.decode("utf-8"))
                            self.ignored_list.append(ignored_hash)

                        except Exception as e:
                            RNS.log(
                                "Could not decode RNS Identity hash from: " +
                                str(hash_str), RNS.LOG_DEBUG)
                            RNS.log("The contained exception was: " + str(e),
                                    RNS.LOG_DEBUG)

            except Exception as e:
                RNS.log(
                    "Error while fetching loading list of ignored destinations: "
                    + str(e), RNS.LOG_ERROR)

        self.directory = nomadnet.Directory(self)

        self.message_router = LXMF.LXMRouter(identity=self.identity,
                                             storagepath=self.storagepath,
                                             autopeer=True)
        self.message_router.register_delivery_callback(self.lxmf_delivery)

        for destination_hash in self.ignored_list:
            self.message_router.ignore_destination(destination_hash)

        self.lxmf_destination = self.message_router.register_delivery_identity(
            self.identity, display_name=self.peer_settings["display_name"])
        self.lxmf_destination.set_default_app_data(self.get_display_name_bytes)

        RNS.Identity.remember(packet_hash=None,
                              destination_hash=self.lxmf_destination.hash,
                              public_key=self.identity.get_public_key(),
                              app_data=None)

        RNS.log("LXMF Router ready to receive on: " +
                RNS.prettyhexrep(self.lxmf_destination.hash))

        if self.enable_node:
            self.message_router.enable_propagation()
            RNS.log("LXMF Propagation Node started on: " + RNS.prettyhexrep(
                self.message_router.propagation_destination.hash))
            self.node = nomadnet.Node(self)
        else:
            self.node = None

        RNS.Transport.register_announce_handler(nomadnet.Conversation)
        RNS.Transport.register_announce_handler(nomadnet.Directory)

        self.autoselect_propagation_node()

        if self.peer_announce_at_start:

            def delayed_announce():
                time.sleep(NomadNetworkApp.START_ANNOUNCE_DELAY)
                self.announce_now()

            da_thread = threading.Thread(target=delayed_announce)
            da_thread.setDaemon(True)
            da_thread.start()

        atexit.register(self.exit_handler)
        sys.excepthook = self.exception_handler

        job_thread = threading.Thread(target=self.__jobs)
        job_thread.setDaemon(True)
        job_thread.start()

        # Override UI choice from config on --daemon switch
        if daemon:
            self.uimode = nomadnet.ui.UI_NONE

        # This stderr redirect is needed to stop urwid
        # from spewing KeyErrors to the console and thus,
        # messing up the UI. A pull request to fix the
        # bug in urwid was submitted, but until it is
        # merged, this hack will mitigate it.
        strio = io.StringIO()
        with contextlib.redirect_stderr(strio):
            nomadnet.ui.spawn(self.uimode)

        if strio.tell() > 0:
            try:
                strio.seek(0)
                err_file = open(self.errorfilepath, "w")
                err_file.write(strio.read())
                err_file.close()

            except Exception as e:
                RNS.log(
                    "Could not write stderr output to error log file at " +
                    str(self.errorfilepath) + ".", RNS.LOG_ERROR)
                RNS.log("The contained exception was: " + str(e),
                        RNS.LOG_ERROR)