diff --git a/notebooks/Experimental/Madhava/veilid_1.ipynb b/notebooks/Experimental/Madhava/veilid_1.ipynb new file mode 100644 index 00000000000..c7497907c73 --- /dev/null +++ b/notebooks/Experimental/Madhava/veilid_1.ipynb @@ -0,0 +1,516 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "6920a2bb-3561-4c6a-ab98-93ef9f8e329d", + "metadata": {}, + "outputs": [], + "source": [ + "# stdlib\n", + "\n", + "# third party\n", + "import veilid" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "904cae54", + "metadata": {}, + "outputs": [], + "source": [ + "NONCE_LENGTH = 24\n", + "QUIT = \"QUIT\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1db013a8-680f-4006-a0ce-6fd397839a02", + "metadata": {}, + "outputs": [], + "source": [ + "host = \"localhost\"\n", + "port = 5959" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d98401b9-f3d1-48f8-8a2d-ed8550a56a90", + "metadata": {}, + "outputs": [], + "source": [ + "async def connect(host: str, port: int):\n", + " async def noop_callback(*args, **kwargs):\n", + " # print(\"got callback\", args, kwargs)\n", + " return\n", + "\n", + " conn = await veilid.json_api_connect(host, port, noop_callback)\n", + " return conn" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dadca923-6526-4d08-8952-0cd65edc6a2e", + "metadata": {}, + "outputs": [], + "source": [ + "conn = await connect(host, port)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ba09dc27-6599-4d96-8663-d59da66dce41", + "metadata": {}, + "outputs": [], + "source": [ + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "async with crypto_system:\n", + " print(\"keygen:Generating a keypair\")\n", + " my_keypair = await crypto_system.generate_key_pair()\n", + " print(f\"keygen:Got {my_keypair=}\")\n", + "\n", + "# await config.store_self_key(conn, my_keypair)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96fab6ed-5578-4f5a-852f-b724d932e82a", + "metadata": {}, + "outputs": [], + "source": [ + "# stdlib" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3dd03cf0-5627-48a7-aa51-6f7d455ae940", + "metadata": {}, + "outputs": [], + "source": [ + "def load_keys():\n", + " key_pair = \"eLaCWyV-XoCKbRKx4k_2XxJoiGbDLY9S4y_wfVQ4Bzs:rlWN82ncYWg8uHt_OPfa8n0IXlqd24D8gneVncCyclY\"\n", + " my_keypair = veilid.KeyPair(key_pair)\n", + " print(\"Own keypair:\")\n", + " print(\" Public: \", my_keypair.key())\n", + " print(\" Private: \", my_keypair.secret())\n", + " their_key = veilid.PublicKey(\"7HQ3RmvFLN1Q2Rv_PbVDRvYAOX9Te4VypjglS3g018A\")\n", + " return my_keypair, their_key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a12f9ef0-c473-4005-a757-93ab293d43cd", + "metadata": {}, + "outputs": [], + "source": [ + "my_keypair, their_key = load_keys()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ead0d3c8-2b45-4957-a332-4e30686e2a38", + "metadata": {}, + "outputs": [], + "source": [ + "their_key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f57189e2-aa3f-48eb-ae51-ec72e8661fc4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b7931251-5051-4510-a900-f694a512e6db", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f14a2cbb-0c1e-45ee-add4-3f7ee60c4021", + "metadata": {}, + "outputs": [], + "source": [ + "async def create_sender(\n", + " router: veilid.api.RoutingContext,\n", + " crypto_system: veilid.CryptoSystem,\n", + " key: veilid.TypedKey,\n", + " secret: veilid.SharedSecret,\n", + " send_subkey: veilid.ValueSubkey,\n", + "):\n", + " \"\"\"Read input and write it to the DHT.\"\"\"\n", + "\n", + " async def encrypt(cleartext: str) -> bytes:\n", + " \"\"\"Encrypt the message with the shared secret and a random nonce.\"\"\"\n", + "\n", + " print(\"encrypt:Getting a nonce\")\n", + " nonce = await crypto_system.random_nonce()\n", + " print(f\"encrypt:Received {nonce=}\")\n", + " print(f\"encrypt:Encrypting {cleartext=}, {nonce=}, {secret=}\")\n", + " encrypted = await crypto_system.crypt_no_auth(cleartext.encode(), nonce, secret)\n", + " ciphertext = nonce.to_bytes() + encrypted\n", + " print(f\"encrypt:{ciphertext=}\")\n", + " return ciphertext\n", + "\n", + " async def send(cleartext: str):\n", + " \"\"\"Write the encrypted version of the text to the DHT.\"\"\"\n", + "\n", + " ciphertext = await encrypt(cleartext)\n", + " print(f\"send:Setting DHT {key=}, {send_subkey=}, to {ciphertext=}\")\n", + " await router.set_dht_value(key, send_subkey, ciphertext)\n", + "\n", + " # Prime the pumps. Especially when starting the conversation, this\n", + " # causes the DHT key to propagate to the network.\n", + " return send" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "329f5324-990f-41a3-9e5d-798e311682f4", + "metadata": {}, + "outputs": [], + "source": [ + "async def receiver(\n", + " router: veilid.api.RoutingContext,\n", + " crypto_system: veilid.CryptoSystem,\n", + " key: veilid.TypedKey,\n", + " secret: veilid.SharedSecret,\n", + " recv_subkey: veilid.ValueSubkey,\n", + " name: str,\n", + "):\n", + " \"\"\"Wait for new data from the DHT and write it to the screen.\"\"\"\n", + "\n", + " async def decrypt(payload: bytes) -> str:\n", + " \"\"\"Decrypt the payload with the shared secret and the payload's nonce.\"\"\"\n", + "\n", + " print(f\"decrypt:Decrypting {payload!r}\")\n", + " nonce = veilid.Nonce.from_bytes(payload[:NONCE_LENGTH])\n", + " print(f\"decrypt:Found {nonce=}\")\n", + " ciphertext = payload[NONCE_LENGTH:]\n", + " print(f\"decrypt:Decrypting {ciphertext=}, {nonce=}, {secret=}\")\n", + " cleartext = await crypto_system.crypt_no_auth(ciphertext, nonce, secret)\n", + " print(f\"decrypt:{cleartext=}\")\n", + " return cleartext.decode()\n", + "\n", + " last_seq = -1\n", + " while True:\n", + " # In the real world, don't do this. People may tease you for it.\n", + " # This is meant to be easy to understand for demonstration\n", + " # purposes, not a great pattern. Instead, you'd want to use the\n", + " # callback function to handle events asynchronously.\n", + "\n", + " # Try to get an updated version of the receiving subkey.\n", + " print(f\"receiver:Getting the DHT value of {key=}, {recv_subkey=}\")\n", + " resp = await router.get_dht_value(key, recv_subkey, True)\n", + " if resp is None:\n", + " print(\"receiver:Didn't find a value\")\n", + " continue\n", + "\n", + " # If the other party hasn't sent a newer message, try again.\n", + " if resp.seq == last_seq:\n", + " print(f\"receiver:DHT {key=} is still at {resp.seq=}\")\n", + " continue\n", + "\n", + " msg = await decrypt(resp.data)\n", + " if msg == QUIT:\n", + " print(\"receiver:Received the QUIT signal from the other end.\")\n", + " print(\"Other end closed the chat.\")\n", + " return\n", + "\n", + " print(\"receiver:Got new {msg=} at DHT {key=}, {resp.seq=}\")\n", + " print(f\"\\n{name}> {msg}\")\n", + " last_seq = resp.seq" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f149ea8b-0379-44fb-9f6a-7c49aece27fb", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89740953-4783-42c9-be69-e41da5443bc7", + "metadata": {}, + "outputs": [], + "source": [ + "def create_members(my_keypair, their_key):\n", + " members = [\n", + " veilid.DHTSchemaSMPLMember(my_keypair.key(), 1),\n", + " veilid.DHTSchemaSMPLMember(their_key, 1),\n", + " ]\n", + " return members" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7eb4c147-6063-427b-8c6b-0605c19794bd", + "metadata": {}, + "outputs": [], + "source": [ + "members = create_members(my_keypair, their_key)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9615df55-c28e-4e50-a00b-6798f3f66ec3", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "602aa1f6-2d42-422b-8887-7622706aa89d", + "metadata": {}, + "outputs": [], + "source": [ + "router = await (await conn.new_routing_context()).with_default_safety()\n", + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "\n", + "name = \"friendsname\"\n", + "\n", + "async with crypto_system, router:\n", + " print(\"start:Getting a DH shared secret\")\n", + " secret = await crypto_system.cached_dh(their_key, my_keypair.secret())\n", + " print(f\"start:Got {secret=}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3bd06829-41a4-4e10-95d1-286a18d93b32", + "metadata": {}, + "outputs": [], + "source": [ + "router = await (await conn.new_routing_context()).with_default_safety()\n", + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "\n", + "name = \"friendsname\"\n", + "\n", + "async with crypto_system, router:\n", + " print(\"start:Getting a DH shared secret\")\n", + " secret = await crypto_system.cached_dh(their_key, my_keypair.secret())\n", + " print(f\"start:Got {secret=}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "27179b5f-62d4-43ec-97e8-4bb86fb3c2ae", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4bb2e0dd-f97a-4cab-a0bb-cb22cf6428a8", + "metadata": {}, + "outputs": [], + "source": [ + "router = await (await conn.new_routing_context()).with_default_safety()\n", + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "\n", + "name = \"friendsname\"\n", + "\n", + "async with crypto_system, router:\n", + " print(\"start:Creating a new DHT record\")\n", + " record = await router.create_dht_record(veilid.DHTSchema.smpl(0, members))\n", + " print(f\"New chat key: {record.key}\")\n", + " print(\"Give that to your friend!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1c028545-72dd-48de-8889-98dea2778d37", + "metadata": {}, + "outputs": [], + "source": [ + "record.key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d8ef5b96-79e3-45cf-b545-8d027117017e", + "metadata": {}, + "outputs": [], + "source": [ + "router = await (await conn.new_routing_context()).with_default_safety()\n", + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "\n", + "name = \"friendsname\"\n", + "\n", + "async with crypto_system, router:\n", + " print(f\"start:Reopening the DHT record {record.key=} with {my_keypair=}\")\n", + " await router.close_dht_record(record.key)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "be3945e6-4d34-4fb1-943a-a64ba9feca30", + "metadata": {}, + "outputs": [], + "source": [ + "router = await (await conn.new_routing_context()).with_default_safety()\n", + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "\n", + "name = \"friendsname\"\n", + "\n", + "async with crypto_system, router:\n", + " print(f\"start:Reopening the DHT record {record.key=} with {my_keypair=}\")\n", + " await router.open_dht_record(record.key, my_keypair)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c6210ce0-14a8-4097-ae2d-d96bb0b13471", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cddc089-7711-4fd2-afb4-210f348aa1c3", + "metadata": {}, + "outputs": [], + "source": [ + "router = await (await conn.new_routing_context()).with_default_safety()\n", + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "\n", + "name = \"friendsname\"\n", + "\n", + "async with crypto_system, router:\n", + " # The party initiating the chat writes to subkey 0 and reads from subkey 1.\n", + " # send_task = asyncio.create_task(sender(router, crypto_system, record.key, secret, 0))\n", + " sender = await create_sender(router, crypto_system, record.key, secret, 0)\n", + " await sender(\"testtesttest\")\n", + "\n", + " # send = await send_task\n", + " # await send(\"hello world\")\n", + " # recv_task = asyncio.create_task(receiver(router, crypto_system, record.key, secret, 1, name))\n", + "\n", + " # try:\n", + " # print(\"start:Starting the chat\")\n", + " # await asyncio.wait([send_task, recv_task], return_when=asyncio.FIRST_COMPLETED)\n", + " # finally:\n", + " # print(f\"start:Closing the DHT record {record.key=}\")\n", + " # await router.close_dht_record(record.key)\n", + " # print(f\"start:Deleting the DHT record {record.key=}\")\n", + " # await router.delete_dht_record(record.key)\n", + "\n", + " # print(\"start:Cleaning up\")\n", + " # recv_task.cancel()\n", + " # send_task.cancel()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c9d70f55-d451-49dd-af5e-eb6bbe5ef61e", + "metadata": {}, + "outputs": [], + "source": [ + "record.key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f208817b-dada-4c20-9cdf-48d19ab5a27a", + "metadata": {}, + "outputs": [], + "source": [ + "# await send(\"hello world2\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0bf04175-67ac-4ede-bd42-92f60c188671", + "metadata": {}, + "outputs": [], + "source": [ + "# stdlib\n", + "import time" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a846437e-783c-477e-a693-b7f9f05891ba", + "metadata": {}, + "outputs": [], + "source": [ + "message = f\"Hello from the world! {time.time()}\"\n", + "message" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e4429621-0af4-4aab-b0db-28a148e87773", + "metadata": {}, + "outputs": [], + "source": [ + "# await asyncio.create_task(sender(router, crypto_system, record.key, secret, 0, message))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "24890cde-dbc2-4462-b4c5-ccaff9bcb598", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/Experimental/Madhava/veilid_2.ipynb b/notebooks/Experimental/Madhava/veilid_2.ipynb new file mode 100644 index 00000000000..b7d1a9dca00 --- /dev/null +++ b/notebooks/Experimental/Madhava/veilid_2.ipynb @@ -0,0 +1,541 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "170613d5-d4cc-42dc-ba32-8ac241830b5b", + "metadata": {}, + "outputs": [], + "source": [ + "# import syft as sy\n", + "\n", + "# node = sy.orchestra.launch(name=\"test-domain-1\", port=\"auto\", dev_mode=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c796510-742b-4621-a1b1-24f3c45eb419", + "metadata": {}, + "outputs": [], + "source": [ + "# !pip install veilid==0.2.5" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1a8daffc-338d-4c7d-a0a4-40aa6df03829", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6920a2bb-3561-4c6a-ab98-93ef9f8e329d", + "metadata": {}, + "outputs": [], + "source": [ + "# third party\n", + "import veilid" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d432b10b-9fb2-4a74-b4b9-f93ba1614179", + "metadata": {}, + "outputs": [], + "source": [ + "host = \"localhost\"\n", + "port = 5960" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28956832-eae6-4ba7-a03c-b306a37659ea", + "metadata": {}, + "outputs": [], + "source": [ + "async def noop_callback(*args, **kwargs):\n", + " # print(\"got callback\", args, kwargs)\n", + " return" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5092eaf4-8724-44b3-b023-405d33abd06b", + "metadata": {}, + "outputs": [], + "source": [ + "conn = await veilid.json_api_connect(host, port, noop_callback)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96fab6ed-5578-4f5a-852f-b724d932e82a", + "metadata": {}, + "outputs": [], + "source": [ + "# stdlib" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8933f388-c256-4d6b-8f3d-93f646f91a0b", + "metadata": {}, + "outputs": [], + "source": [ + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "async with crypto_system:\n", + " print(\"keygen:Generating a keypair\")\n", + " my_keypair = await crypto_system.generate_key_pair()\n", + " print(f\"keygen:Got {my_keypair=}\")\n", + "\n", + "# await config.store_self_key(conn, my_keypair)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "50db9b83-bebb-407d-8690-12d9194574a7", + "metadata": {}, + "outputs": [], + "source": [ + "key_pair = \"7HQ3RmvFLN1Q2Rv_PbVDRvYAOX9Te4VypjglS3g018A:Lt5Yf-IRUUrH73Tlvyf1QR2fn9salvnHjEm0zBnQK0Q\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "25464cf0-ac13-4733-9dbb-f74f2a1e79d6", + "metadata": {}, + "outputs": [], + "source": [ + "my_keypair = veilid.KeyPair(key_pair)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "701f4ad1-216f-4b12-ba36-d440db911ca8", + "metadata": {}, + "outputs": [], + "source": [ + "print(\"Own keypair:\")\n", + "print(\" Public: \", my_keypair.key())\n", + "print(\" Private: \", my_keypair.secret())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a12f9ef0-c473-4005-a757-93ab293d43cd", + "metadata": {}, + "outputs": [], + "source": [ + "their_key = veilid.PublicKey(\"eLaCWyV-XoCKbRKx4k_2XxJoiGbDLY9S4y_wfVQ4Bzs\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ead0d3c8-2b45-4957-a332-4e30686e2a38", + "metadata": {}, + "outputs": [], + "source": [ + "their_key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f57189e2-aa3f-48eb-ae51-ec72e8661fc4", + "metadata": {}, + "outputs": [], + "source": [ + "members = [\n", + " veilid.DHTSchemaSMPLMember(my_keypair.key(), 1),\n", + " veilid.DHTSchemaSMPLMember(their_key, 1),\n", + "]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8806c1ee-1924-4b17-8ab5-10bc0ac4915a", + "metadata": {}, + "outputs": [], + "source": [ + "# stdlib" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cc0e6ab9-f4c2-42f1-9265-116d37fbaa45", + "metadata": {}, + "outputs": [], + "source": [ + "key = \"VLD0:r6NoOmRrZxPHVZkpmbC32ob05jOQNBf445kxZwEAF7o\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1ca6fa71-d94c-422d-a7b2-b80aacf8d2c1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2fb0184d-cda6-4472-8309-71494fea39e1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "061d3bae-449c-44a3-8baf-578560659dcf", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f14a2cbb-0c1e-45ee-add4-3f7ee60c4021", + "metadata": {}, + "outputs": [], + "source": [ + "# async def sender(\n", + "# router: veilid.api.RoutingContext,\n", + "# crypto_system: veilid.CryptoSystem,\n", + "# key: veilid.TypedKey,\n", + "# secret: veilid.SharedSecret,\n", + "# send_subkey: veilid.ValueSubkey,\n", + "# ):\n", + "# \"\"\"Read input and write it to the DHT.\"\"\"\n", + "\n", + "# async def encrypt(cleartext: str) -> bytes:\n", + "# \"\"\"Encrypt the message with the shared secret and a random nonce.\"\"\"\n", + "\n", + "# print(\"encrypt:Getting a nonce\")\n", + "# nonce = await crypto_system.random_nonce()\n", + "# print(f\"encrypt:Received {nonce=}\")\n", + "# print(f\"encrypt:Encrypting {cleartext=}, {nonce=}, {secret=}\")\n", + "# encrypted = await crypto_system.crypt_no_auth(cleartext.encode(), nonce, secret)\n", + "# ciphertext = nonce.to_bytes() + encrypted\n", + "# print(f\"encrypt:{ciphertext=}\")\n", + "# return ciphertext\n", + "\n", + "# async def send(cleartext: str):\n", + "# \"\"\"Write the encrypted version of the text to the DHT.\"\"\"\n", + "\n", + "# ciphertext = await encrypt(cleartext)\n", + "# print(f\"send:Setting DHT {key=}, {send_subkey=}, to {ciphertext=}\")\n", + "# await router.set_dht_value(key, send_subkey, ciphertext)\n", + "\n", + "# # Prime the pumps. Especially when starting the conversation, this\n", + "# # causes the DHT key to propagate to the network.\n", + "# await send(\"Hello from the world!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b9904281-3aea-4f72-8338-21db4ad292ca", + "metadata": {}, + "outputs": [], + "source": [ + "NONCE_LENGTH = 24\n", + "QUIT = \"QUIT\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "329f5324-990f-41a3-9e5d-798e311682f4", + "metadata": {}, + "outputs": [], + "source": [ + "async def receiver(\n", + " router: veilid.api.RoutingContext,\n", + " crypto_system: veilid.CryptoSystem,\n", + " key: veilid.TypedKey,\n", + " secret: veilid.SharedSecret,\n", + " recv_subkey: veilid.ValueSubkey,\n", + " name: str,\n", + "):\n", + " \"\"\"Wait for new data from the DHT and write it to the screen.\"\"\"\n", + "\n", + " async def decrypt(payload: bytes) -> str:\n", + " \"\"\"Decrypt the payload with the shared secret and the payload's nonce.\"\"\"\n", + "\n", + " print(f\"decrypt:Decrypting {payload!r}\")\n", + " nonce = veilid.Nonce.from_bytes(payload[:NONCE_LENGTH])\n", + " print(f\"decrypt:Found {nonce=}\")\n", + " ciphertext = payload[NONCE_LENGTH:]\n", + " print(f\"decrypt:Decrypting {ciphertext=}, {nonce=}, {secret=}\")\n", + " cleartext = await crypto_system.crypt_no_auth(ciphertext, nonce, secret)\n", + " print(f\"decrypt:{cleartext=}\")\n", + " return cleartext.decode()\n", + "\n", + " last_seq = -1\n", + " while True:\n", + " # In the real world, don't do this. People may tease you for it.\n", + " # This is meant to be easy to understand for demonstration\n", + " # purposes, not a great pattern. Instead, you'd want to use the\n", + " # callback function to handle events asynchronously.\n", + "\n", + " # Try to get an updated version of the receiving subkey.\n", + " print(f\"receiver:Getting the DHT value of {key=}, {recv_subkey=}\")\n", + " resp = await router.get_dht_value(key, recv_subkey, True)\n", + " if resp is None:\n", + " print(\"receiver:Didn't find a value\")\n", + " continue\n", + "\n", + " # If the other party hasn't sent a newer message, try again.\n", + " if resp.seq == last_seq:\n", + " print(f\"receiver:DHT {key=} is still at {resp.seq=}\")\n", + " continue\n", + "\n", + " msg = await decrypt(resp.data)\n", + " if msg == QUIT:\n", + " print(\"receiver:Received the QUIT signal from the other end.\")\n", + " print(\"Other end closed the chat.\")\n", + " return\n", + "\n", + " print(\"receiver:Got new {msg=} at DHT {key=}, {resp.seq=}\")\n", + " print(f\"\\n{name}> {msg}\")\n", + " last_seq = resp.seq" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f149ea8b-0379-44fb-9f6a-7c49aece27fb", + "metadata": {}, + "outputs": [], + "source": [ + "name = \"friendsname\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cddc089-7711-4fd2-afb4-210f348aa1c3", + "metadata": {}, + "outputs": [], + "source": [ + "# router = await (await conn.new_routing_context()).with_default_safety()\n", + "# crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "# async with crypto_system, router:\n", + "# print(\"start:Getting a DH shared secret\")\n", + "# secret = await crypto_system.cached_dh(their_key, my_keypair.secret())\n", + "# print(f\"start:Got {secret=}\")\n", + "\n", + "# print(\"start:Creating a new DHT record\")\n", + "# record = await router.create_dht_record(veilid.DHTSchema.smpl(0, members))\n", + "# print(f\"New chat key: {record.key}\")\n", + "# print(\"Give that to your friend!\")\n", + "\n", + "# # Close this key first. We'll reopen it for writing with our saved key.\n", + "# print(f\"start:Closing the DHT record {record.key=}\")\n", + "# await router.close_dht_record(record.key)\n", + "\n", + "# print(f\"start:Reopening the DHT record {record.key=} with {my_keypair=}\")\n", + "# await router.open_dht_record(record.key, my_keypair)\n", + "\n", + "# # The party initiating the chat writes to subkey 0 and reads from subkey 1.\n", + "# send_task = asyncio.create_task(sender(router, crypto_system, record.key, secret, 0))\n", + "# recv_task = asyncio.create_task(receiver(router, crypto_system, record.key, secret, 1, name))\n", + "\n", + "# try:\n", + "# print(\"start:Starting the chat\")\n", + "# await asyncio.wait([send_task, recv_task], return_when=asyncio.FIRST_COMPLETED)\n", + "# finally:\n", + "# print(f\"start:Closing the DHT record {record.key=}\")\n", + "# await router.close_dht_record(record.key)\n", + "# print(f\"start:Deleting the DHT record {record.key=}\")\n", + "# await router.delete_dht_record(record.key)\n", + "\n", + "# print(\"start:Cleaning up\")\n", + "# recv_task.cancel()\n", + "# send_task.cancel()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c911ff07-f18f-44d5-ae07-47012481f017", + "metadata": {}, + "outputs": [], + "source": [ + "router = await (await conn.new_routing_context()).with_default_safety()\n", + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "\n", + "name = \"friendsname\"\n", + "\n", + "async with crypto_system, router:\n", + " print(f\"start:Reopening the DHT record {key=} with {my_keypair=}\")\n", + " await router.close_dht_record(key)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1abe5f0f-e77a-44e7-b991-9b526629ffcf", + "metadata": {}, + "outputs": [], + "source": [ + "print(\"respond:Opening a private routing context\")\n", + "router = await (await conn.new_routing_context()).with_default_safety()\n", + "print(\"respond:Getting a crypto system\")\n", + "crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "async with crypto_system, router:\n", + " print(\"respond:Getting a DH shared secret\")\n", + " secret = await crypto_system.cached_dh(their_key, my_keypair.secret())\n", + " print(f\"respond:Got {secret=}\")\n", + "\n", + " print(f\"respond:Opening the DHT record {key=} with {my_keypair=}\")\n", + " await router.open_dht_record(key, my_keypair)\n", + "\n", + " # The party responding to the chat writes to subkey 1 and reads from subkey 0.\n", + " # send_task = asyncio.create_task(sender(router, crypto_system, key, secret, 1))\n", + " # recv_task = asyncio.create_task(receiver(router, crypto_system, key, secret, 0, name))\n", + " await receiver(router, crypto_system, key, secret, 0, name)\n", + "\n", + " # try:\n", + " # LOG.debug(\"respond:Starting the chat\")\n", + " # await asyncio.wait([send_task, recv_task], return_when=asyncio.FIRST_COMPLETED)\n", + " # finally:\n", + " # LOG.debug(f\"respond:Closing the DHT record {key=}\")\n", + " # await router.close_dht_record(key)\n", + " # LOG.debug(f\"respond:Deleting the DHT record {key=}\")\n", + " # await router.delete_dht_record(key)\n", + "\n", + " # LOG.debug(\"respond:Cleaning up\")\n", + " # recv_task.cancel()\n", + " # send_task.cancel()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95bed690-4a13-400f-a6f9-3fb49696ea03", + "metadata": {}, + "outputs": [], + "source": [ + "await receiver(router, crypto_system, key, secret, 0, name)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4fbf15e3-f2dc-4670-aacf-4fee37672c83", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8e478b86-4ab8-4f20-af50-b8d669de8697", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e3e090c0-7058-4996-adeb-0f4f3962d084", + "metadata": {}, + "outputs": [], + "source": [ + "# if await config.load_self_key(conn):\n", + "# print(\"You already have a keypair.\")\n", + "\n", + "# # print the keypair\n", + "# print(await dump_keystore(host, port))\n", + "\n", + "# sys.exit(1)\n", + "\n", + "# LOG.debug(\"keygen:Getting a crypto system\")\n", + "# crypto_system = await conn.get_crypto_system(veilid.CryptoKind.CRYPTO_KIND_VLD0)\n", + "# async with crypto_system:\n", + "# LOG.debug(\"keygen:Generating a keypair\")\n", + "# my_keypair = await crypto_system.generate_key_pair()\n", + "# LOG.debug(f\"keygen:Got {my_keypair=}\")\n", + "\n", + "# await config.store_self_key(conn, my_keypair)\n", + "\n", + "# print(f\"Your new public key is: {my_keypair.key()}\")\n", + "# print(\"Share it with your friends!\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61bbe160-2328-4ce0-9622-88db1ca594a8", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b9f05403-1b8d-437f-8ca8-8defaf4091c7", + "metadata": {}, + "outputs": [], + "source": [ + "# await config.store_friend_key(conn, name, veilid.PublicKey(pubkey))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "07680800-cb9a-4b65-8335-9f4b2ff3b6f8", + "metadata": {}, + "outputs": [], + "source": [ + "# poetry run chat add-friend MyFriend L0nGkEyStR1ng\n", + "# async def store_friend_key(\n", + "# conn: veilid.json_api._JsonVeilidAPI, name: str, pubkey: veilid.PublicKey\n", + "# ):\n", + "# \"\"\"Write a friend's public key to the keystore.\"\"\"\n", + "\n", + "# await store_key(conn, f\"{FRIEND_PREFIX}{name}\", str(pubkey))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/Testing/Veilid/Alice-Veilid-Peer.ipynb b/notebooks/Testing/Veilid/Alice-Veilid-Peer.ipynb new file mode 100644 index 00000000000..0541d90a921 --- /dev/null +++ b/notebooks/Testing/Veilid/Alice-Veilid-Peer.ipynb @@ -0,0 +1,302 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "3ab7de7e-d23e-4cfc-895d-0bd02d9bc17f", + "metadata": {}, + "outputs": [], + "source": [ + "# stdlib\n", + "\n", + "# stdlib\n", + "import asyncio\n", + "\n", + "# third party\n", + "import veilid\n", + "from veilid import KeyPair" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "461ef888-76c1-4256-ad69-cbf405d830e0", + "metadata": {}, + "outputs": [], + "source": [ + "app_message_queue: asyncio.Queue = asyncio.Queue()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad857491-4cad-4a21-bf10-8035f05ef52a", + "metadata": {}, + "outputs": [], + "source": [ + "host = \"localhost\"\n", + "port = 5959" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d5984fd-30eb-4c9f-8bb7-e0a827b6de2c", + "metadata": {}, + "outputs": [], + "source": [ + "async def noop_callback(update: veilid.VeilidUpdate):\n", + " if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE:\n", + " print(\"Received App Message\")\n", + " await app_message_queue.put(update)\n", + "\n", + "\n", + "async def connect(host: str, port: int):\n", + " conn = await veilid.json_api_connect(host, port, noop_callback)\n", + " return conn" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1296f362-60ff-471a-807b-6d96dbf36403", + "metadata": {}, + "outputs": [], + "source": [ + "conn = await connect(host, port)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "418b05e9-eaac-4bb5-b61a-e300221b10ba", + "metadata": {}, + "outputs": [], + "source": [ + "# route_id, blob = await conn.new_private_route()\n", + "# Stable and reliable route\n", + "# Creating a new one\n", + "route_id, blob = await conn.new_custom_private_route(\n", + " [veilid.CryptoKind.CRYPTO_KIND_VLD0],\n", + " veilid.Stability.RELIABLE,\n", + " veilid.Sequencing.ENSURE_ORDERED,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dade6f0b-27f7-4317-84e0-83f5db2f93ea", + "metadata": {}, + "outputs": [], + "source": [ + "route_id, blob" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "de7eb3e2", + "metadata": {}, + "outputs": [], + "source": [ + "# Creating a new routing context\n", + "# Since it is safe by default , we could remove default safety\n", + "router = await (await conn.new_routing_context()).with_default_safety()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f76a7403", + "metadata": {}, + "outputs": [], + "source": [ + "router" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad3b3dbc", + "metadata": {}, + "outputs": [], + "source": [ + "alice_record = await router.create_dht_record(veilid.DHTSchema.dflt(1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3922a41b", + "metadata": {}, + "outputs": [], + "source": [ + "# Creation of a Record in DHT DFLT schema , creates a new public and private key pair for the owner\n", + "# that is different from the NodeID public key\n", + "alice_private_key = alice_record.owner_secret\n", + "alice_public_key = alice_record.owner" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "77dd5ed2", + "metadata": {}, + "outputs": [], + "source": [ + "alice_private_key, alice_public_key, alice_record.key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb3c60bb", + "metadata": {}, + "outputs": [], + "source": [ + "# Close the record\n", + "await router.close_dht_record(alice_record.key)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73249f4a", + "metadata": {}, + "outputs": [], + "source": [ + "key_pair = KeyPair.from_parts(key=alice_public_key, secret=alice_private_key)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b63cb28c", + "metadata": {}, + "outputs": [], + "source": [ + "key_pair" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3afe0d6a", + "metadata": {}, + "outputs": [], + "source": [ + "record_open = await router.open_dht_record(alice_record.key, key_pair)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b12b2281", + "metadata": {}, + "outputs": [], + "source": [ + "record_open.key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "11d2d05d", + "metadata": {}, + "outputs": [], + "source": [ + "await router.set_dht_value(record_open.key, 0, blob)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "833bf7e0-c3f9-4280-aa8d-5b1302b00f0f", + "metadata": {}, + "outputs": [], + "source": [ + "record_open.key[5::]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "df056893-c98d-45b4-9eff-c3e6301ce7e4", + "metadata": {}, + "outputs": [], + "source": [ + "self_prr = await conn.import_remote_private_route(blob)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "75b7efff-3b20-4446-ad8d-94ea0f5f4f26", + "metadata": {}, + "outputs": [], + "source": [ + "message_send = await router.app_message(self_prr, b\"Hello to me\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b6764bf2-30c7-4baa-81e8-c097be06dbea", + "metadata": {}, + "outputs": [], + "source": [ + "value = await app_message_queue.get()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2d52874c-ad2e-46ef-a9c9-7447661bc7fa", + "metadata": {}, + "outputs": [], + "source": [ + "assert value.kind == veilid.VeilidUpdateKind.APP_MESSAGE" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b690487b-20fb-4b36-9076-33915a184354", + "metadata": {}, + "outputs": [], + "source": [ + "value.detail.message" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9e0453a0-4c6d-4d83-aa01-232cab545653", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/Testing/Veilid/Bob-Veilid-Peer.ipynb b/notebooks/Testing/Veilid/Bob-Veilid-Peer.ipynb new file mode 100644 index 00000000000..4c974d0bea4 --- /dev/null +++ b/notebooks/Testing/Veilid/Bob-Veilid-Peer.ipynb @@ -0,0 +1,211 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "3ab7de7e-d23e-4cfc-895d-0bd02d9bc17f", + "metadata": {}, + "outputs": [], + "source": [ + "# stdlib\n", + "\n", + "# third party\n", + "from utils import get_typed_key\n", + "import veilid" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad857491-4cad-4a21-bf10-8035f05ef52a", + "metadata": {}, + "outputs": [], + "source": [ + "host = \"localhost\"\n", + "port = 5960" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8d5984fd-30eb-4c9f-8bb7-e0a827b6de2c", + "metadata": {}, + "outputs": [], + "source": [ + "async def noop_callback(*args, **kwargs):\n", + " return\n", + "\n", + "\n", + "async def connect(host: str, port: int):\n", + " conn = await veilid.json_api_connect(host, port, noop_callback)\n", + " return conn" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1296f362-60ff-471a-807b-6d96dbf36403", + "metadata": {}, + "outputs": [], + "source": [ + "conn = await connect(host, port)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "de7eb3e2", + "metadata": {}, + "outputs": [], + "source": [ + "# Creating a new routing context\n", + "router = await (await conn.new_routing_context()).with_default_safety()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "cbb26e88-6935-471a-a248-a46fdcba8e18", + "metadata": {}, + "outputs": [], + "source": [ + "conn.new_routing_context?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f76a7403", + "metadata": {}, + "outputs": [], + "source": [ + "router" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "40370b01", + "metadata": {}, + "outputs": [], + "source": [ + "# Get this DHT Key from the Previous Notebook\n", + "# paste only the string party without VLD0: prefix\n", + "alice_dht_key_str = input(\"Enter Alice's DHT Key: \")\n", + "dht_key = get_typed_key(alice_dht_key_str)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ad3b3dbc", + "metadata": {}, + "outputs": [], + "source": [ + "alice_record = await router.open_dht_record(key=dht_key, writer=None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "12fdf804", + "metadata": {}, + "outputs": [], + "source": [ + "alice_record_value = await router.get_dht_value(\n", + " key=dht_key, subkey=0, force_refresh=True\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e4e44fc7", + "metadata": {}, + "outputs": [], + "source": [ + "alice_record_value.data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c4db5bf7", + "metadata": {}, + "outputs": [], + "source": [ + "# Import the private route sent by Alice:\n", + "prr_alice = await conn.import_remote_private_route(alice_record_value.data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e249e970-30f1-4121-98e0-e1df0db37e4b", + "metadata": {}, + "outputs": [], + "source": [ + "prr_alice" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6a268e77-84d0-43ad-a9b9-21582992ea64", + "metadata": {}, + "outputs": [], + "source": [ + "message = b\"Hello Alice\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e76b3680-7e84-4e7f-8c8e-450a6de6786d", + "metadata": {}, + "outputs": [], + "source": [ + "message_send = await router.app_message(prr_alice, message)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62bd12ec-1aef-4459-90a9-a4402a8b3a68", + "metadata": {}, + "outputs": [], + "source": [ + "message_send" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "13b87c97-0c10-4112-82c7-6b283a31cc28", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/Testing/Veilid/utils.py b/notebooks/Testing/Veilid/utils.py new file mode 100644 index 00000000000..814ff62758a --- /dev/null +++ b/notebooks/Testing/Veilid/utils.py @@ -0,0 +1,12 @@ +# third party +import veilid + + +def get_typed_key(key: str) -> veilid.types.TypedKey: + return veilid.types.TypedKey.from_value( + kind=veilid.CryptoKind.CRYPTO_KIND_VLD0, value=key + ) + + +# state = await conn.get_state() +# state.config.config diff --git a/packages/grid/veilid/requirements.txt b/packages/grid/veilid/requirements.txt new file mode 100644 index 00000000000..4540e75958c --- /dev/null +++ b/packages/grid/veilid/requirements.txt @@ -0,0 +1 @@ +veilid==0.2.5 diff --git a/packages/grid/veilid/start.sh b/packages/grid/veilid/start.sh new file mode 100644 index 00000000000..a11d10a131e --- /dev/null +++ b/packages/grid/veilid/start.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +/veilid/veilid-server -c /veilid/veilid-server.conf --debug + diff --git a/packages/grid/veilid/veilid-server.conf b/packages/grid/veilid/veilid-server.conf new file mode 100644 index 00000000000..bae004ab415 --- /dev/null +++ b/packages/grid/veilid/veilid-server.conf @@ -0,0 +1,5 @@ +daemon: + enabled: false +client_api: + enabled: true + listen_address: ':5959' diff --git a/packages/grid/veilid/veilid.dockerfile b/packages/grid/veilid/veilid.dockerfile new file mode 100644 index 00000000000..314f1f7787c --- /dev/null +++ b/packages/grid/veilid/veilid.dockerfile @@ -0,0 +1,34 @@ +# ======== [Stage 1] Build Veilid Server ========== # + +FROM rust as build +RUN apt update && apt install -y git +RUN git clone -b v0.2.5 https://gitlab.com/veilid/veilid +WORKDIR /veilid +RUN bash -c "source scripts/earthly/install_capnproto.sh" +RUN bash -c "source scripts/earthly/install_protoc.sh" +RUN cd veilid-server && cargo build --release -p veilid-server + +# ========== [Stage 2] Dependency Install ========== # + +FROM python:3.11-bookworm +COPY --from=build /veilid/target/release/veilid-server /veilid/veilid-server +WORKDIR /app +COPY ./requirements.txt /app/requirements.txt +RUN --mount=type=cache,target=/root/.cache \ + pip install --user -r requirements.txt +COPY ./start.sh /app/start.sh +RUN chmod +x /app/start.sh +COPY ./veilid.py /app/veilid.py +COPY ./veilid-server.conf /veilid + +# ========== [Final] Start Veilid Server and Python Web Server ========== # + +CMD ["sh", "-c", "/app/start.sh"] +EXPOSE 5959/udp +EXPOSE 5959 +EXPOSE 4000 +RUN apt update && apt install netcat-openbsd + +# docker build -f veilid.dockerfile . -t veilid +# docker run -it -p 4000:4000 -p 5959:5959 -p 5959:5959/udp veilid +# /root/.local/share/veilid \ No newline at end of file diff --git a/packages/grid/veilid/veilid.py b/packages/grid/veilid/veilid.py new file mode 100644 index 00000000000..e69de29bb2d