def create_multiple_endpoints(negotiation): data_list = apply_multipart_consensus(negotiation) for data in data_list: endpoint_id = data["endpoint_id"] prepare_output_file() ui.inform("Created endpoint %s." % endpoint_id) return data_list
def get_own_endpoint_input(endpoint_id): try: r = client.get_input_from_link(endpoint_id, INBOX) ui.inform("Collected input for inbox of %s." % endpoint_id) except InputNotReady: raise Block("Waiting to collect inbox.") return endpoint_id
def _check_ep_negotiation(negotiation_id, initial_contrib): contributions = filter_data_only(client.contribution_list(negotiation_id)) contributions = [c for c in contributions if c["latest"]] combined_peer_id = cfg.get("CREATE_COMBINED_PEER_ID") combined_peer = client.peer_info(combined_peer_id) owners = set(unpack_owners(combined_peer["owners"])) orig_body = None for contribution in contributions: signer = contribution["signer_key_id"] text = get_contribution_text(contribution) body = text["body"] if orig_body is None: orig_body = body if orig_body != body: raise ValueError("contribution texts differ") check_signer(owners, signer) if owners: raise Block("Contribution pending from: %s" % owners) ui.inform("All peer owners have agreed. Sending accept contribution.") text = get_contribution_text(initial_contrib) body = text["body"] meta = text["meta"] meta = hash_meta_next_negotiation(meta) r = client.run_contribution(negotiation_id, body, accept=True, extra_meta=meta) d = r.json() contribution = d["data"] ui.inform("Sent contribution %s" % contribution["id"]) return contribution
def join_combined_peer(negotiation): text = get_negotiation_text(negotiation) peer_id = text["body"]["data"]["peer_id"] peer = client.peer_info(peer_id) if peer is not None: ui.inform("Combined peer %s is created." % peer_id) return peer_id raise Block("Waiting for the combined peer to be created.")
def print_information(negotiation_id, contribution_id, invitations): combined_invitations = [ INV_SEP.join([negotiation_id, inv]) for inv in invitations ] ui.inform("Send invitations to peers:\n %s" % "\n ".join(combined_invitations)) ui.inform("Your initial proposal is contribution: %s" % contribution_id) return True
def join_ep_contribution(negotiation_id, initial_contrib): response = get_join_response() text = get_contribution_text(initial_contrib) body = text["body"] r = client.run_contribution(negotiation_id, body, accept=False) d = r.json() contribution = d["data"] ui.inform("Sent contribution %s" % contribution["id"]) return contribution
def close_own_endpoint(endpoint_id): params = client.close_on_minimum_prepare(endpoint_id) if params == "wrongstatus": abort("Wrong status") if params == "nomin": raise Block("Waiting until minimum inbox size is reached.") endpoint = client.with_self_consensus(client.endpoint_action, params) endpoint_id = endpoint["endpoint_id"] ui.inform("Closed endpoint %s" % endpoint_id) return endpoint_id
def process_own_endpoint(endpoint_id): peer_id = cfg.get("PEER_ID") messages, log = client.inbox_process(endpoint_id, peer_id, upload=True) params = client.record_process_prepare(endpoint_id, log) if params == "wrongstatus": abort("Wrong status") endpoint = client.with_self_consensus(client.endpoint_action, params) endpoint_id = endpoint["endpoint_id"] ui.inform("Processed endpoint %s" % endpoint_id) return endpoint_id
def send_message(text=None, recipient=None): if recipient is None: recipient = ui.ask_value("recipient", "Message recipient") if text is None: text = ui.ask_value("text", "Message text") payload = {"recipient": recipient, "message": text} r = requests.post(SEND_ADDRESS, data=payload) if r.status_code != requests.codes.ok: ui.inform("Failed with code: %s" % r.status_code) exit() ui.inform("Sent message with id %s" % r.content)
def join_combined_endpoint(negotiation): text = get_negotiation_text(negotiation) endpoints = filter_data_only(text["body"]) for endpoint_descr in endpoints: endpoint_id = endpoint_descr["endpoint_id"] endpoint = client.endpoint_info(endpoint_id) if endpoint is not None: ui.inform("Endpoint '%s' is created." % endpoint_id) else: raise Block("Waiting for endpoint '%s' to be created." % endpoint_id) return endpoints
def get_endpoint_input(endpoint_id, box, dry_run=False): try: responses, msg_hashes = client.get_input_from_link(endpoint_id, box, serialized=True, dry_run=dry_run) if not dry_run: ui.inform("Collected input for %s of '%s'." % (box, endpoint_id)) return msg_hashes except InputNotReady: m = "Waiting to collect %s for endpoint '%s'" % (box, endpoint_id) raise Block(m) except NoLinks: return None
def join_contribution(negotiation_id, initial_contrib, invitation_id): response = get_join_response() text = get_contribution_text(initial_contrib) body = text["body"] extra_meta = text["meta"].copy() extra_meta["invitation_id"] = invitation_id r = client.run_contribution(negotiation_id, body, accept=False, extra_meta=extra_meta) d = r.json() contribution_id = d["data"]["id"] ui.inform("Sent contribution %s" % contribution_id) return contribution_id
def _join_second_contribution(negotiation_id, contrib): text = get_contribution_text(contrib) body = text["body"] meta = text["meta"] check_hashed_next_negotiation_id(meta) ui.inform("Sending second join contribution.") r = client.run_contribution(negotiation_id, body, accept=True, extra_meta=meta) d = r.json() contribution_id = d["data"]["id"] ui.inform("Sent contribution %s" % contribution_id) return contribution_id
def check_negotiation_wizard(negotiation_id, invitations): invitations = list(invitations) me = cfg.get("PEER_ID") contributions = filter_data_only(client.contribution_list(negotiation_id)) contributions = [c for c in contributions if c["latest"]] signers = [me] orig_body = None orig_next_negotiation_id = read_next_negotiation_id() for contribution in contributions: signer = contribution["signer_key_id"] text = get_contribution_text(contribution) body = text["body"] if orig_body is None: orig_body = body if orig_body != body: raise ValueError("contribution texts differ") meta = text["meta"] invitation_id = meta.get("invitation_id") if invitation_id is not None: check_invitation(invitations, invitation_id) client.peer_import(signer) signers.append(signer) # print "Imported peer %s" % signer elif signer != me: raise ValueError("uninvited contribution!") next_negotiation_id = meta.get("next_negotiation_id") if next_negotiation_id != orig_next_negotiation_id: raise ValueError("wrong next_negotiation_id") if invitations: raise Block("Invitations pending: %s" % invitations) ui.inform("All invited peers have joined. Sending accept contribution.") name = orig_body["data"]["name"] next_negotiation_id = orig_body["info"].get("next_negotiation_id") hashed_next_negotiation_id = utils.hash_string(orig_next_negotiation_id) is_contrib, d = client.peer_create( name, set_key=True, owners=signers, negotiation_id=negotiation_id, accept=True, next_negotiation_id=hashed_next_negotiation_id) assert is_contrib contrib_id = d["data"]["id"] ui.inform("Your new contribution id is: %s" % contrib_id) return contrib_id
def handle_endpoints_wizard(role): while True: cycle = on("CYCLE", lambda: 1) peer_id = cfg.get("PEER_ID") combined_peer_id = cfg.get("COMBINED_PEER_ID") if role == "create": endpoints = create_endpoint_wizard(cycle, combined_peer_id) elif role == "join": endpoints = join_endpoint_wizard(cycle) endpoint_ids = [e["endpoint_id"] for e in endpoints] remaining = endpoint_ids while remaining: ui.inform("Still need to handle endpoints %s." % remaining) remaining = operate(remaining, peer_id, combined_peer_id, role) time.sleep(3) get_restart_response(role) ui.inform("Starting a new cycle...") cfg.set_value("CYCLE", cycle + 1)
def join_ep_process_contribution(endpoint_id, negotiation_id, initial_contrib): text = get_contribution_text(initial_contrib) body = text["body"] suggested_hashes = body["data"]["message_hashes"] r = client.get_input_from_link(endpoint_id, PROCESSBOX, serialized=True, dry_run=True) if r is None: raise ValueError("input is missing") responses, computed_hashes = r if suggested_hashes != computed_hashes: abort("Couldn't agree on message hashes when processing.") meta = hash_meta_next_negotiation(text["meta"]) r = client.run_contribution(negotiation_id, body, accept=True, extra_meta=meta) d = r.json() contribution = d["data"] ui.inform("Sent contribution %s" % contribution["id"]) return contribution
def join_ep_close_contribution(endpoint_id, negotiation_id, initial_contrib): text = get_contribution_text(initial_contrib) body = text["body"] suggested_hashes = body["data"]["message_hashes"] endpoint = client.endpoint_info(endpoint_id) try: hashes = get_endpoint_input(endpoint_id, INBOX, dry_run=True) if hashes is not None and suggested_hashes != hashes: abort("Hash mismatch for linked inbox") except (InputNotReady, NoLinks) as e: pass computed_hashes = client.check_endpoint_on_minimum(endpoint) if suggested_hashes != computed_hashes: abort("Couldn't agree on message hashes when closing.") meta = hash_meta_next_negotiation(text["meta"]) r = client.run_contribution(negotiation_id, body, accept=True, extra_meta=meta) d = r.json() contribution = d["data"] ui.inform("Sent contribution %s" % contribution["id"]) return contribution
def main(): ui.inform("Welcome to Panoramix server wizard!") ui.inform("Configuration file is: %s" % config_file) ui.inform("Set PANORAMIX_CONFIG environment variable to override") on("CATALOG_URL", set_catalog_url) backend = on("CRYPTO_BACKEND", common.select_backend_wizard) server.register_backend(backend) on("CRYPTO_PARAMS", lambda: common.crypto_params_wizard_on(server)) on("INFORM_MIGRATE", inform_migrate) inform_launch()
def main(text=None, recipient=None): import os ui.inform("Welcome to Panoramix sphinxmix agent!") ui.inform("Configuration file is: %s" % common.config_file) ui.inform("Set PANORAMIX_CONFIG environment variable to override") url = on("MIXNET_URL", lambda: ui.ask_value("MIXNET_URL", "Give sphinxmix mixnet URL")) mixnet_url_process(url) on("KEY", common.set_key_wizard) agent.main()
def main(): args = parser.parse_args() global autodefault autodefault = args.yes ui.inform("Welcome to Panoramix wizard!") ui.inform("Configuration file is: %s" % common.config_file) ui.inform("Set PANORAMIX_CONFIG environment variable to override") catalog_url = on("CATALOG_URL", set_catalog_url_wizard) client.register_catalog_url(catalog_url) role = on("SETUP_ROLE", lambda: ui.ask_value("role", "Choose 'create' or 'join' mixnet")) mixnet_creation_wizard(role) handle_endpoints_wizard(role)
def initial_contribution_info(contrib): register_next_negotiation_id(contrib) contrib_id = contrib["id"] signer = contrib["signer_key_id"] ui.inform("Negotiation initialized by peer %s with contribution %s." % (signer, contrib_id))
def create_combined_peer(negotiation): data = apply_consensus(negotiation) peer_id = data["peer_id"] ui.inform("Created combined peer %s." % peer_id) return peer_id
def check_consensus(negotiation): consensus = negotiation["consensus"] if consensus is None: raise Block("No consensus yet.") ui.inform("Consensus reached: %s" % consensus) return negotiation
def apply_multipart_consensus(negotiation): consensus = negotiation["consensus"] text = get_negotiation_text(negotiation) ui.inform("Negotiation finished successfully. Applying consensus.") rs = client.apply_multipart_consensus(text["body"], consensus) return filter_data_only(rs)
def apply_consensus(negotiation): consensus = negotiation["consensus"] text = get_negotiation_text(negotiation) ui.inform("Negotiation finished successfully. Applying consensus.") return client.apply_consensus(text["body"], consensus)["data"]
def inform_migrate(): ui.inform("You need to setup your database once with\n" " panoramix-manage migrate") return True
def inform_launch(): catalog_url = cfg.get("CATALOG_URL") ui.inform("Start server with\n" " %s=%s panoramix-manage runserver %s" % (common.ENV_CONFIG, config_file, clean_url(catalog_url)))
def do_register_wizard(peer_name): peer = client.with_self_consensus(client.peer_create, {"name": peer_name}) peer_id = peer["peer_id"] ui.inform("Registered peer with PEER_ID: %s" % peer_id) return peer_id
def inform_send_message(endpoints): public_endpoint = [e for e in endpoints if e["public"]][0] public_endpoint_href = client.mk_endpoint_hyperlink( public_endpoint["endpoint_id"]) ui.inform("Ready to accept messages to mixnet %s\n." % public_endpoint_href)
def prepare_output_file(): cycle = cfg.get("CYCLE") - 1 with open(output_cycle, "w") as f: f.write("%s" % cycle) ui.inform("Wrote cycle id: %s" % cycle)