From 7ee40b7dbe89ad736ef59737c8bb6f9a2a7a3614 Mon Sep 17 00:00:00 2001 From: "Federico Pasqua (eisterman)" Date: Tue, 30 Jan 2024 16:07:09 +0100 Subject: [PATCH] Various experiments --- .cargo/config.toml | 2 + Cargo.lock | 898 +++++++++++++++++++++++++++ bonknet_client/Cargo.toml | 5 + bonknet_client/src/bin/client.rs | 73 +++ bonknet_client/src/bin/requester.rs | 2 + bonknet_client/src/main.rs | 67 -- bonknet_server/Cargo.toml | 11 +- bonknet_server/src/bin/init_certs.rs | 39 ++ bonknet_server/src/bin/server.rs | 134 ++-- libbonknet/Cargo.toml | 2 + libbonknet/src/lib.rs | 33 +- 11 files changed, 1150 insertions(+), 116 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 bonknet_client/src/bin/client.rs create mode 100644 bonknet_client/src/bin/requester.rs delete mode 100644 bonknet_client/src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..bff29e6 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +rustflags = ["--cfg", "tokio_unstable"] diff --git a/Cargo.lock b/Cargo.lock index b6cd942..2d372f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,12 +17,105 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" + +[[package]] +name = "async-stream" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51" +dependencies = [ + "async-stream-impl", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "async-stream-impl" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "async-trait" +version = "0.1.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "axum" +version = "0.6.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf" +dependencies = [ + "async-trait", + "axum-core", + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "hyper", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "rustversion", + "serde", + "sync_wrapper", + "tower", + "tower-layer", + "tower-service", +] + +[[package]] +name = "axum-core" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c" +dependencies = [ + "async-trait", + "bytes", + "futures-util", + "http", + "http-body", + "mime", + "rustversion", + "tower-layer", + "tower-service", +] + [[package]] name = "backtrace" version = "0.3.69" @@ -54,21 +147,39 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" name = "bonknet_client" version = "0.1.0" dependencies = [ + "futures", + "libbonknet", + "rmp-serde", "rustls-pemfile", + "serde", "tokio", "tokio-rustls", + "tokio-util", ] [[package]] name = "bonknet_server" version = "0.1.0" dependencies = [ + "console-subscriber", + "futures", + "libbonknet", "rcgen", + "rmp-serde", "rustls-pemfile", + "serde", "tokio", "tokio-rustls", + "tokio-util", + "tracing", ] +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.5.0" @@ -90,6 +201,67 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "console-api" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd326812b3fd01da5bb1af7d340d0d555fd3d4b641e7f1dfcf5962a902952787" +dependencies = [ + "futures-core", + "prost", + "prost-types", + "tonic", + "tracing-core", +] + +[[package]] +name = "console-subscriber" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7481d4c57092cd1c19dd541b92bdce883de840df30aa5d03fd48a3935c01842e" +dependencies = [ + "console-api", + "crossbeam-channel", + "crossbeam-utils", + "futures-task", + "hdrhistogram", + "humantime", + "prost-types", + "serde", + "serde_json", + "thread_local", + "tokio", + "tokio-stream", + "tonic", + "tracing", + "tracing-core", + "tracing-subscriber", +] + +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "176dc175b78f56c0f321911d9c8eb2b77a78a4860b9c19db83835fea1a46649b" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + [[package]] name = "deranged" version = "0.3.11" @@ -99,6 +271,123 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "flate2" +version = "1.0.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46303f565772937ffe1d394a4fac6f411c6013172fadde9dcdb1e147a086940e" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "getrandom" version = "0.2.12" @@ -116,15 +405,180 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" +[[package]] +name = "h2" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb2c4422095b67ee78da96fbb51a4cc413b3b25883c7717ff7ca1ab31022c9c9" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap 2.1.0", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hashbrown" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" + +[[package]] +name = "hdrhistogram" +version = "7.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "765c9198f173dd59ce26ff9f95ef0aafd0a0fe01fb9d72841bc5066a4c06511d" +dependencies = [ + "base64", + "byteorder", + "flate2", + "nom", + "num-traits", +] + [[package]] name = "hermit-abi" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" +[[package]] +name = "http" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "hyper" +version = "0.14.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-timeout" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1" +dependencies = [ + "hyper", + "pin-project-lite", + "tokio", + "tokio-io-timeout", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + +[[package]] +name = "indexmap" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +dependencies = [ + "equivalent", + "hashbrown 0.14.3", +] + +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libbonknet" version = "0.1.0" +dependencies = [ + "rustls-pemfile", + "tokio-rustls", +] [[package]] name = "libc" @@ -148,12 +602,39 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + +[[package]] +name = "matchit" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94" + [[package]] name = "memchr" version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -174,6 +655,25 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-traits" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -193,6 +693,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + [[package]] name = "parking_lot" version = "0.12.1" @@ -216,6 +722,12 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" + [[package]] name = "pem" version = "3.0.3" @@ -226,18 +738,56 @@ dependencies = [ "serde", ] +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "pin-project-lite" version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "powerfmt" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro2" version = "1.0.76" @@ -247,6 +797,38 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prost" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-derive" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" +dependencies = [ + "anyhow", + "itertools", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "prost-types" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "193898f59edcf43c26227dcd4c8427f00d99d61e95dcde58dabd49fa291d470e" +dependencies = [ + "prost", +] + [[package]] name = "quote" version = "1.0.35" @@ -256,6 +838,36 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "rcgen" version = "0.12.0" @@ -277,6 +889,50 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.4", + "regex-syntax 0.8.2", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b7fa1134405e2ec9353fd416b17f8dacd46c473d7d3fd1cf202706a14eb792a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.2", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + [[package]] name = "ring" version = "0.17.7" @@ -291,6 +947,28 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "rmp" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f9860a6cc38ed1da53456442089b4dfa35e7cedaa326df63017af88385e6b20" +dependencies = [ + "byteorder", + "num-traits", + "paste", +] + +[[package]] +name = "rmp-serde" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bffea85eea980d8a74453e5d02a8d93028f3c34725de143085a844ebe953258a" +dependencies = [ + "byteorder", + "rmp", + "serde", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -338,6 +1016,18 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustversion" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" + +[[package]] +name = "ryu" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" + [[package]] name = "scopeguard" version = "1.2.0" @@ -364,6 +1054,26 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -373,6 +1083,15 @@ dependencies = [ "libc", ] +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + [[package]] name = "smallvec" version = "1.11.2" @@ -412,6 +1131,22 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "sync_wrapper" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" + +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.31" @@ -446,9 +1181,20 @@ dependencies = [ "signal-hook-registry", "socket2", "tokio-macros", + "tracing", "windows-sys", ] +[[package]] +name = "tokio-io-timeout" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30b74022ada614a1b4834de765f9bb43877f910cc8ce4be40e89042c9223a8bf" +dependencies = [ + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-macros" version = "2.2.0" @@ -471,6 +1217,143 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419f34732d9eb6ee4c3578b7989078579b7f039cbbb9ca2c4da015749371e15" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", +] + +[[package]] +name = "tonic" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d560933a0de61cf715926b9cac824d4c883c2c43142f787595e48280c40a1d0e" +dependencies = [ + "async-stream", + "async-trait", + "axum", + "base64", + "bytes", + "h2", + "http", + "http-body", + "hyper", + "hyper-timeout", + "percent-encoding", + "pin-project", + "prost", + "tokio", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" +dependencies = [ + "futures-core", + "futures-util", + "indexmap 1.9.3", + "pin-project", + "pin-project-lite", + "rand", + "slab", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0" + +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "once_cell", + "regex", + "sharded-slab", + "thread_local", + "tracing", + "tracing-core", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -483,6 +1366,21 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "want" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa7760aed19e106de2c7c0b581b509f2f25d3dacaf737cb82ac61bc6d760b0e" +dependencies = [ + "try-lock", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/bonknet_client/Cargo.toml b/bonknet_client/Cargo.toml index 46e35a4..ed04f1f 100644 --- a/bonknet_client/Cargo.toml +++ b/bonknet_client/Cargo.toml @@ -6,6 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +libbonknet = { path = "../libbonknet" } tokio = { version = "1", features = ["full"] } tokio-rustls = "0.25.0" +tokio-util = { version = "0.7.10", features = ["codec"] } +futures = "0.3" rustls-pemfile = "2.0.0" +serde = { version = "1.0", features = ["derive"] } +rmp-serde = "1.1.2" diff --git a/bonknet_client/src/bin/client.rs b/bonknet_client/src/bin/client.rs new file mode 100644 index 0000000..5e2a76d --- /dev/null +++ b/bonknet_client/src/bin/client.rs @@ -0,0 +1,73 @@ +use std::sync::Arc; +use futures::SinkExt; +use tokio::net::TcpStream; +use tokio_rustls::rustls::{ClientConfig, RootCertStore}; +use tokio_rustls::rustls::pki_types::{ServerName}; +use tokio_rustls::TlsConnector; +use tokio_util::codec::{Framed, LengthDelimitedCodec}; +use serde::{Serialize, Deserialize}; +use libbonknet::{load_cert, load_prkey}; + +#[derive(Debug, Serialize, Deserialize)] +enum ClientMessage { + Response { status_code: u32, msg: Option }, + Announce { name: String }, + Required { id: String }, + NotRequired { id: String }, +} + +#[tokio::main] +async fn main() -> std::io::Result<()> { + let client_name = "Polnareffland1"; + // Root certs to verify the server is the right one + let mut server_root_cert_store = RootCertStore::empty(); + let server_root_cert_der = load_cert("server_root_cert.pem").unwrap(); + server_root_cert_store.add(server_root_cert_der).unwrap(); + // Auth Cert to send the server who am I + let root_client_cert = load_cert("client_root_cert.pem").unwrap(); + let client_cert = load_cert("client_cert.pem").unwrap(); + let client_prkey = load_prkey("client_key.pem").unwrap(); + // Load TLS Config + let tlsconfig = ClientConfig::builder() + .with_root_certificates(server_root_cert_store) + // .with_no_client_auth(); + .with_client_auth_cert(vec![client_cert, root_client_cert], client_prkey.into()) + .unwrap(); + let connector = TlsConnector::from(Arc::new(tlsconfig)); + let dnsname = ServerName::try_from("localhost").unwrap(); + + let stream = TcpStream::connect("localhost:6379").await?; + let stream = connector.connect(dnsname, stream).await?; + + let mut transport = Framed::new(stream, LengthDelimitedCodec::new()); + + let msg1 = ClientMessage::Announce { name: client_name.into() }; + transport.send(rmp_serde::to_vec(&msg1).unwrap().into()).await.unwrap(); + for i in 0..10 { + let msg = ClientMessage::Response { status_code: 100+i, msg: Some(format!("yay {}", i)) }; + transport.send(rmp_serde::to_vec(&msg).unwrap().into()).await.unwrap(); + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + } + + // transport.for_each(|item| async move { + // let a: ClientMessage = rmp_serde::from_slice(&item.unwrap()).unwrap(); + // println!("{:?}", a); + // }).await; + + // let mut buf = vec![0;1024]; + // let (mut rd,mut tx) = split(stream); + // + // + // tokio::spawn(async move { + // let mut stdout = tokio::io::stdout(); + // tokio::io::copy(&mut rd, &mut stdout).await.unwrap(); + // }); + // + // let mut reader = tokio::io::BufReader::new(tokio::io::stdin()).lines(); + // + // while let Some(line) = reader.next_line().await.unwrap() { + // tx.write_all(line.as_bytes()).await.unwrap(); + // } + + Ok(()) +} \ No newline at end of file diff --git a/bonknet_client/src/bin/requester.rs b/bonknet_client/src/bin/requester.rs new file mode 100644 index 0000000..7f755fb --- /dev/null +++ b/bonknet_client/src/bin/requester.rs @@ -0,0 +1,2 @@ +#[tokio::main] +async fn main() {} diff --git a/bonknet_client/src/main.rs b/bonknet_client/src/main.rs deleted file mode 100644 index e194a61..0000000 --- a/bonknet_client/src/main.rs +++ /dev/null @@ -1,67 +0,0 @@ -use std::io::{BufReader, Error, ErrorKind}; -use std::sync::Arc; -use rustls_pemfile::{Item, read_one}; -use tokio::io::{AsyncBufReadExt, AsyncWriteExt, split}; -use tokio::net::TcpStream; -use tokio_rustls::rustls::{ClientConfig, RootCertStore}; -use tokio_rustls::rustls::pki_types::{CertificateDer, PrivatePkcs8KeyDer, ServerName}; -use tokio_rustls::TlsConnector; - -fn load_cert(filename: &str) -> std::io::Result { - let cert_file = std::fs::File::open(filename).unwrap(); - let mut buf = std::io::BufReader::new(cert_file); - if let Item::X509Certificate(cert) = read_one(&mut buf).unwrap().unwrap() { - Ok(cert) - } else { - eprintln!("File {} doesn't contain a X509 Certificate", filename); - Err(Error::new(ErrorKind::InvalidInput, "no x509 cert")) - } -} - -fn load_prkey(filename: &str) -> std::io::Result { - let prkey_file = std::fs::File::open(filename).unwrap(); - let mut buf = BufReader::new(prkey_file); - if let Item::Pkcs8Key(pkey) = read_one(&mut buf).unwrap().unwrap() { - Ok(pkey) - } else { - eprintln!("File {} doesn't contain a Pkcs8 Private Key", filename); - Err(Error::new(ErrorKind::InvalidInput, "no pkcs8key")) - } -} - -#[tokio::main] -async fn main() -> std::io::Result<()> { - // Root certs to verify the server is the right one - let mut server_root_cert_store = RootCertStore::empty(); - let server_root_cert_der = load_cert("server_root_cert.pem").unwrap(); - server_root_cert_store.add(server_root_cert_der).unwrap(); - // Auth Cert to send the server who am I - let root_client_cert = load_cert("client_root_cert.pem").unwrap(); - let client_cert = load_cert("client_cert.pem").unwrap(); - let client_prkey = load_prkey("client_key.pem").unwrap(); - // Load TLS Config - let tlsconfig = ClientConfig::builder() - .with_root_certificates(server_root_cert_store) - // .with_no_client_auth(); - .with_client_auth_cert(vec![client_cert, root_client_cert], client_prkey.into()) - .unwrap(); - let connector = TlsConnector::from(Arc::new(tlsconfig)); - let dnsname = ServerName::try_from("localhost").unwrap(); - - let stream = TcpStream::connect("localhost:6379").await?; - let stream = connector.connect(dnsname, stream).await?; - let (mut rd,mut tx) = split(stream); - - tokio::spawn(async move { - let mut stdout = tokio::io::stdout(); - tokio::io::copy(&mut rd, &mut stdout).await.unwrap(); - }); - - let mut reader = tokio::io::BufReader::new(tokio::io::stdin()).lines(); - - while let Some(line) = reader.next_line().await.unwrap() { - tx.write_all(line.as_bytes()).await.unwrap(); - } - - Ok(()) -} \ No newline at end of file diff --git a/bonknet_server/Cargo.toml b/bonknet_server/Cargo.toml index 3702966..d264c65 100644 --- a/bonknet_server/Cargo.toml +++ b/bonknet_server/Cargo.toml @@ -6,7 +6,14 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -tokio = { version = "1", features = ["full"] } +libbonknet = { path = "../libbonknet" } +tokio = { version = "1", features = ["full", "tracing"] } +tokio-util = { version = "0.7.10", features = ["codec"] } +futures = "0.3" rcgen = "0.12.0" tokio-rustls = "0.25.0" -rustls-pemfile = "2.0.0" \ No newline at end of file +rustls-pemfile = "2.0.0" +serde = { version = "1.0", features = ["derive"] } +rmp-serde = "1.1.2" +tracing = "0.1" +console-subscriber = "0.2.0" \ No newline at end of file diff --git a/bonknet_server/src/bin/init_certs.rs b/bonknet_server/src/bin/init_certs.rs index 7ec6887..0de4c72 100644 --- a/bonknet_server/src/bin/init_certs.rs +++ b/bonknet_server/src/bin/init_certs.rs @@ -46,6 +46,28 @@ fn client_cert() -> Certificate { Certificate::from_params(params).unwrap() } +fn requester_root_cert() -> Certificate { + let subject_alt_names = vec!["hello.world.example".into()]; + let mut certparams = CertificateParams::new(subject_alt_names); + certparams.is_ca = rcgen::IsCa::Ca(BasicConstraints::Unconstrained); + let mut distname = rcgen::DistinguishedName::new(); + distname.push(DnType::OrganizationName, "Eister Corporation"); + distname.push(DnType::CommonName, "Bonknet Requester Root Cert CA"); + certparams.distinguished_name = distname; + Certificate::from_params(certparams).unwrap() +} + +fn requester_cert() -> Certificate { + let mut params = CertificateParams::new(vec!["entity.other.host".into(), "bonk.client.1".into()]); + params.distinguished_name.push(DnType::CommonName, "Requester 1"); + params.use_authority_key_identifier_extension = true; + params.key_usages.push(rcgen::KeyUsagePurpose::DigitalSignature); + params + .extended_key_usages + .push(rcgen::ExtendedKeyUsagePurpose::ClientAuth); + Certificate::from_params(params).unwrap() +} + fn main() -> std::io::Result<()> { // SERVER let server_root_cert = server_root_cert(); @@ -81,6 +103,23 @@ fn main() -> std::io::Result<()> { let mut privkfile = File::create("client_key.pem")?; privkfile.write_all(client_leaf_cert.serialize_private_key_pem().as_bytes())?; } + // CLIENT + let requester_root_cert = requester_root_cert(); + // The certificate is now valid for localhost and the domain "hello.world.example" + { + let mut certfile = File::create("requester_root_cert.pem")?; + certfile.write_all(requester_root_cert.serialize_pem().unwrap().as_bytes())?; + let mut privkfile = File::create("requester_root_key.pem")?; + privkfile.write_all(requester_root_cert.serialize_private_key_pem().as_bytes())?; + } + // Now create the leaf + let requester_leaf_cert = requester_cert(); + { + let mut certfile = File::create("requester_cert.pem")?; + certfile.write_all(requester_leaf_cert.serialize_pem_with_signer(&requester_root_cert).unwrap().as_bytes())?; + let mut privkfile = File::create("requester_key.pem")?; + privkfile.write_all(requester_leaf_cert.serialize_private_key_pem().as_bytes())?; + } println!("Certificates created"); Ok(()) } \ No newline at end of file diff --git a/bonknet_server/src/bin/server.rs b/bonknet_server/src/bin/server.rs index 396ab69..2ce4e2d 100644 --- a/bonknet_server/src/bin/server.rs +++ b/bonknet_server/src/bin/server.rs @@ -1,35 +1,84 @@ -use tokio::net::{TcpListener}; -use std::io::{BufReader, Error, ErrorKind}; +use std::collections::HashMap; +use tokio::net::{TcpListener, TcpStream}; +use std::net::{SocketAddr}; use std::sync::{Arc}; +use std::time::Instant; +use futures::stream::{Stream, StreamExt}; use tokio_rustls::{TlsAcceptor}; use tokio_rustls::rustls::{RootCertStore, ServerConfig}; use tokio_rustls::rustls::server::WebPkiClientVerifier; -use rustls_pemfile::{read_one, Item}; -use tokio::io::{AsyncWriteExt, copy, split}; -use tokio_rustls::rustls::pki_types::{CertificateDer, PrivatePkcs8KeyDer}; +use serde::{Serialize, Deserialize}; +use tokio::task::JoinHandle; +use tokio_rustls::server::TlsStream; +use tokio_util::codec::{Framed, LengthDelimitedCodec}; +use libbonknet::{load_prkey, load_cert}; -fn load_cert(filename: &str) -> std::io::Result { - let cert_file = std::fs::File::open(filename).unwrap(); - let mut buf = std::io::BufReader::new(cert_file); - if let Item::X509Certificate(cert) = read_one(&mut buf).unwrap().unwrap() { - Ok(cert) - } else { - eprintln!("File {} doesn't contain a X509 Certificate", filename); - Err(Error::new(ErrorKind::InvalidInput, "no x509 cert")) - } +type FramedStream = Framed,LengthDelimitedCodec>; + +#[derive(Debug, Serialize, Deserialize)] +enum ClientMessage { + Response { status_code: u32, msg: Option }, + Announce { name: String }, + Required { id: String }, + NotRequired { id: String }, } -fn load_prkey(filename: &str) -> std::io::Result { - let prkey_file = std::fs::File::open(filename).unwrap(); - let mut buf = BufReader::new(prkey_file); - if let Item::Pkcs8Key(pkey) = read_one(&mut buf).unwrap().unwrap() { - Ok(pkey) - } else { - eprintln!("File {} doesn't contain a Pkcs8 Private Key", filename); - Err(Error::new(ErrorKind::InvalidInput, "no pkcs8key")) - } +async fn process_client(stream: TlsStream, peer_addr: SocketAddr) { + let transport = Framed::new(stream, LengthDelimitedCodec::new()); + transport.for_each(|item| async move { + match item { + Ok(frame) => { + let a: ClientMessage = rmp_serde::from_slice(&frame).unwrap(); + println!("{:?}: {:?}", peer_addr, a); + }, + Err(e) => { + println!("{:?}: ERROR: {}", peer_addr, e); + } + } + }).await; } +struct ClientState { + framedstream: FramedStream, + last_life_signal: Instant, +} + +struct ClientConnectionManager { + registered_clients: Arc>, + unreg_clients: Arc>, +} + +impl ClientConnectionManager { + async fn new_and_initialize(port: u16, tlsconfig: ServerConfig) -> ClientConnectionManager { + let acceptor = TlsAcceptor::from(Arc::new(tlsconfig)); + let listener = TcpListener::bind(format!("localhost:{}", port)).await.unwrap(); + let registered_clients = Arc::new(HashMap::new()); + let unreg_clients = Arc::new(Vec::new()); + tokio::spawn(async move { + let task_acceptor = acceptor; + loop { + let (stream, peer_addr) = listener.accept().await.unwrap(); + let acceptor = task_acceptor.clone(); + let stream = acceptor.accept(stream).await.unwrap(); + + tokio::spawn(async move { + //let transport = Framed::new(stream, LengthDelimitedCodec::new()); + process_client(stream, peer_addr).await; + }); + } + }); + let ccm = ClientConnectionManager { registered_clients, unreg_clients}; + ccm + } + + async fn process_new_client(&mut self, transport: FramedStream, _peer_addr: SocketAddr) { + let state = ClientState{ + framedstream: transport, + last_life_signal: Instant::now(), + }; + self.unreg_clients.push(state); + } +} #[tokio::main] async fn main() { @@ -52,24 +101,37 @@ async fn main() { let listener = TcpListener::bind("localhost:6379").await.unwrap(); + // Create Queue Binder + loop { let (stream, peer_addr) = listener.accept().await.unwrap(); let acceptor = acceptor.clone(); - - let fut = async move { - let stream = acceptor.accept(stream).await?; - let (mut reader, mut writer) = split(stream); - let n = copy(&mut reader, &mut writer).await?; - writer.flush().await?; - println!("Echo: {} - {}", peer_addr, n); - - Ok(()) as std::io::Result<()> - }; + let stream = acceptor.accept(stream).await.unwrap(); tokio::spawn(async move { - if let Err(err) = fut.await { - eprintln!("{:?}", err); - } + //let transport = Framed::new(stream, LengthDelimitedCodec::new()); + process_client(stream, peer_addr).await; }); + + // let msg1 = ClientMessage::Required { id: "Testo".into() }; + // let msg2 = ClientMessage::NotRequired { id: "Testo2".into() }; + // transport.send(rmp_serde::to_vec(&msg1).unwrap().into()).await.unwrap(); + // transport.send(rmp_serde::to_vec(&msg2).unwrap().into()).await.unwrap(); + + // let fut = async move { + // let stream = acceptor.accept(stream).await?; + // let (mut reader, mut writer) = split(stream); + // let n = copy(&mut reader, &mut writer).await?; + // writer.flush().await?; + // println!("Echo: {} - {}", peer_addr, n); + // + // Ok(()) as std::io::Result<()> + // }; + // + // tokio::spawn(async move { + // if let Err(err) = fut.await { + // eprintln!("{:?}", err); + // } + // }); } } diff --git a/libbonknet/Cargo.toml b/libbonknet/Cargo.toml index 4f7d560..0fcb0e0 100644 --- a/libbonknet/Cargo.toml +++ b/libbonknet/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +tokio-rustls = "0.25.0" +rustls-pemfile = "2.0.0" diff --git a/libbonknet/src/lib.rs b/libbonknet/src/lib.rs index 7d12d9a..8ed59b3 100644 --- a/libbonknet/src/lib.rs +++ b/libbonknet/src/lib.rs @@ -1,14 +1,25 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right -} +use std::io::{BufReader, Error, ErrorKind}; +use rustls_pemfile::{read_one, Item}; +use tokio_rustls::rustls::pki_types::{CertificateDer, PrivatePkcs8KeyDer}; -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); +pub fn load_cert(filename: &str) -> std::io::Result { + let cert_file = std::fs::File::open(filename).unwrap(); + let mut buf = std::io::BufReader::new(cert_file); + if let Item::X509Certificate(cert) = read_one(&mut buf).unwrap().unwrap() { + Ok(cert) + } else { + eprintln!("File {} doesn't contain a X509 Certificate", filename); + Err(Error::new(ErrorKind::InvalidInput, "no x509 cert")) + } +} + +pub fn load_prkey(filename: &str) -> std::io::Result { + let prkey_file = std::fs::File::open(filename).unwrap(); + let mut buf = BufReader::new(prkey_file); + if let Item::Pkcs8Key(pkey) = read_one(&mut buf).unwrap().unwrap() { + Ok(pkey) + } else { + eprintln!("File {} doesn't contain a Pkcs8 Private Key", filename); + Err(Error::new(ErrorKind::InvalidInput, "no pkcs8key")) } }