示例#1
0
    def send(self, body, *, encrypt=True):
        if encrypt:
            message = Node.construct_message(self.encryptor, self.name, body)
        else:
            message = body

        response = send_data(self.server_address, message)
        message_status, data = Node.deconstruct_message(self.encryptor, response)

        if message_status == MessageState.ok:
            sender, body, signature = data
            return (True, body)
        elif message_status == MessageState.invalid_data:
            return (False, data)
        else:
            message, signature = data
            return (False, message)
示例#2
0
def run_server(args):
    setup_logging(args)
    with open(args.config_file, "r") as rf:
        node = Node.from_config(MessageHandler(), json.load(rf), hooks=Hooks())

    with run_node(node):
        log("Listening on {0}".format(node.address))
        # NOTE: There is no functionality for flipping the SHUTDOWN switch, so at this point you'll
        #       have to terminate the process manually.
        while not SHUTDOWN:
            time.sleep(1)
示例#3
0
 def test_message(self):
   send_message = b"hello there to you my good friends how is it that you are doing today I really would like to know oh yes I would"
   signed_cipher_text = Node.construct_message(master_encryptor, master_def.name, send_message)
   # self.log_bytes_stats(signed_cipher_text)
   response = send_data(self.addr, signed_cipher_text)
   message, signature = master_encryptor.decrypt(response)
   sender, status, body = message.split(b"\n", 2)
   self.assertEqual(sender, self.node.name.encode())
   self.assertEqual(status, b"ok")
   self.assertEqual(body, send_message.upper())
   self.assertTrue(master_encryptor.verify_message(message, signature))
示例#4
0
 def test_from_config(self):
   cfg = self.node.get_config()
   cfg["address"] = ":".join(str(o) for o in addr_2)
   node = Node.from_config(handle_message, cfg)
示例#5
0
def dump_config(args):
    node = Node(None, "/tmp/pynet-kvp.sock")
    node.add_peer(PeerDefinition("first among peers", "127.0.0.1:1337", Encryptor.new_key().publickey()))
    print(json.dumps(node.get_config(), sort_keys=True, indent=2, separators=(", ", ": ")))