diff --git a/Cargo.lock b/Cargo.lock index c704b21..b0e47f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "backtrace" version = "0.3.69" @@ -68,6 +74,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cl-aux" +version = "4.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f157ead6471a99b758bcf322b38e78a81c9b65434b9ba9afe79e60979584af3" +dependencies = [ + "tinyvec", +] + [[package]] name = "cpufeatures" version = "0.2.10" @@ -150,13 +165,17 @@ name = "irec" version = "0.1.0" dependencies = [ "home", - "rustls-pemfile", "tokio", "tokio-rustls", - "webpki-roots", "wtx", ] +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + [[package]] name = "libc" version = "0.2.149" @@ -169,6 +188,28 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" +[[package]] +name = "mini-internal" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51c55587ac25c2d63a75e4171221b2803a9b9e83aeb7bdfde5833c4cd578b50d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "miniserde" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621df4a46e5de4c1541242407da38281ea0e320b94e238477688a36a1a059f7" +dependencies = [ + "itoa", + "mini-internal", + "ryu", +] + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -216,18 +257,18 @@ checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -254,42 +295,79 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustls" -version = "0.21.8" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "446e14c5cda4f3f30fe71863c34ec70f5ac79d6087097ad0bb433e1be5edf04c" +checksum = "5bc238b76c51bbc449c55ffbc39d03772a057cc8cf783c49d4af4c2537b74a8b" dependencies = [ "ring", + "rustls-pki-types", "rustls-webpki", - "sct", + "subtle", + "zeroize", ] [[package]] name = "rustls-pemfile" -version = "1.0.3" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d3987094b1d07b653b7dfdc3f70ce9a1da9c51ac18c1b06b662e4f9a0e9f4b2" +checksum = "35e4980fa29e4c4b212ffb3db068a564cbf560e51d3944b7c88bd8bf5bec64f4" dependencies = [ "base64", + "rustls-pki-types", ] +[[package]] +name = "rustls-pki-types" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7673e0aa20ee4937c6aacfc12bb8341cfbf054cdd21df6bec5fd0629fe9339b" + [[package]] name = "rustls-webpki" -version = "0.101.7" +version = "0.102.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" +checksum = "de2635c8bc2b88d367767c5de8ea1d8db9af3f6219eba28442242d9ab81d1b89" dependencies = [ "ring", + "rustls-pki-types", "untrusted", ] [[package]] -name = "sct" -version = "0.7.1" +name = "ryu" +version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" +checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" + +[[package]] +name = "serde" +version = "1.0.195" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ - "ring", - "untrusted", + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.195" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" +dependencies = [ + "proc-macro2", + "quote", + "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]] @@ -319,17 +397,38 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "syn" -version = "2.0.38" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.35.1" @@ -360,11 +459,12 @@ dependencies = [ [[package]] name = "tokio-rustls" -version = "0.24.1" +version = "0.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" +checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" dependencies = [ "rustls", + "rustls-pki-types", "tokio", ] @@ -400,9 +500,12 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "webpki-roots" -version = "0.25.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc" +checksum = "0de2cfda980f21be5a7ed2eadb3e6fe074d56022bea2cdeb1a62eb220fc04188" +dependencies = [ + "rustls-pki-types", +] [[package]] name = "windows-sys" @@ -538,13 +641,28 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "wtx" -version = "0.11.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e403128f78ae86c4481beb712e27bfefd9fbdb73d458e4ce91392b36111577f9" +checksum = "31b783d3f32be64579ec69e09d6429001200cb5fbd0156544c04df482c48b5d3" dependencies = [ + "arrayvec", "base64", + "cl-aux", "httparse", + "miniserde", + "ring", + "rustls-pemfile", + "rustls-pki-types", + "serde", + "serde_json", "sha1", "tokio", "tokio-rustls", + "webpki-roots", ] + +[[package]] +name = "zeroize" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" diff --git a/Cargo.toml b/Cargo.toml index 3a8b87c..07ab72a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,19 +1,16 @@ [[bin]] name = "server" -required-features = ["rustls-pemfile", "tokio/macros", "tokio/rt-multi-thread"] +required-features = ["tokio/macros", "tokio/rt-multi-thread"] [dependencies] home = { default-features = false, version = "0.5" } -rustls-pemfile = { default-features = false, optional = true, version = "1.0" } -tokio = { default-features = false, features = ["fs", "net", "time", "macros", "rt-multi-thread"], version = "1.35" } -tokio-rustls = { default-features = false, features = ["tls12"], version = "0.24" } -wtx = { default-features = false, features = ["tokio-rustls", "web-socket-handshake"], version = "0.11" } - -[dev-dependencies] -webpki-roots = { default-features = false, version = "0.25" } +tokio = { default-features = false, features = ["fs", "net", "time", "macros", "rt-multi-thread"], version = "1.0" } +tokio-rustls = { default-features = false, features = ["tls12"], version = "0.25" } +wtx = { default-features = false, features = ["rustls-pemfile", "tokio-rustls", "web-socket-handshake", "webpki-roots"], version = "0.13" } [package] edition = "2021" +license = "Apache-2.0" name = "irec" publish = false version = "0.1.0" \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ae9ecd4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2018 Caio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/src/api/v1.rs b/src/api/v1.rs index 9fb1b22..128d8c6 100644 --- a/src/api/v1.rs +++ b/src/api/v1.rs @@ -13,8 +13,7 @@ use tokio::{ use tokio_rustls::server::TlsStream; use wtx::{ rng::StdRng, - web_socket::{FrameBufferVec, OpCode, WebSocketServer}, - PartitionedBuffer, + web_socket::{FrameBufferVec, OpCode, WebSocketBuffer, WebSocketServer}, }; /// Stores an arbitrary sequence of bytes into a ".webm" file located at the home directory. @@ -22,7 +21,7 @@ use wtx::{ pub async fn store( fb: &mut FrameBufferVec, ft: FileTy, - ws: &mut WebSocketServer<(), PartitionedBuffer, StdRng, &mut TlsStream>, + ws: &mut WebSocketServer<(), StdRng, &mut TlsStream, WebSocketBuffer>, ) -> crate::Result<()> { let mut irec_dir = irec_dir(ft)?; if !irec_dir.exists() { diff --git a/src/bin/server/main.rs b/src/bin/server/main.rs index e7dc2c1..6e65965 100644 --- a/src/bin/server/main.rs +++ b/src/bin/server/main.rs @@ -1,19 +1,14 @@ use irec::{api::v1::store, FileTy}; -use rustls_pemfile::{certs, pkcs8_private_keys}; -use std::{env, fs, io, str, sync::Arc}; +use std::{env, fs, str}; use tokio::{io::AsyncWriteExt, net::TcpListener}; -use tokio_rustls::{ - rustls::{Certificate, PrivateKey, ServerConfig}, - TlsAcceptor, -}; use wtx::{ http::Request, + misc::{TokioRustlsAcceptor, Uri}, rng::StdRng, web_socket::{ handshake::{WebSocketAccept, WebSocketAcceptRaw}, - FrameBufferVec, + FrameBufferVec, WebSocketBuffer, }, - PartitionedBuffer, UriParts, }; #[tokio::main] @@ -39,9 +34,9 @@ fn manage_accept( req: &dyn Request, key_cb: &mut impl FnMut(&str) -> bool, ) -> bool { - let uri_parts_from_req = UriParts::from(str::from_utf8(req.path()).unwrap_or_default()); + let uri_parts_from_req = Uri::new(str::from_utf8(req.path()).unwrap_or_default()); let question_fn = || { - let mut iter = uri_parts_from_req.href.split('?'); + let mut iter = uri_parts_from_req.href().split('?'); let before_question = iter.next()?; let after_question = iter.next().unwrap_or(""); Some((before_question, after_question)) @@ -78,7 +73,7 @@ async fn serve( mut key_cb: impl Copy + FnMut(&str) -> bool + Send + 'static, ) -> irec::Result<()> { let listener = TcpListener::bind(addr).await?; - let tls_acceptor = tls_acceptor(cert, key)?; + let tls_acceptor = TokioRustlsAcceptor::default().with_cert_chain_and_priv_key(cert, key)?; loop { let (stream, _) = listener.accept().await?; let local_tls_acceptor = tls_acceptor.clone(); @@ -89,10 +84,9 @@ async fn serve( let mut tls_stream = local_tls_acceptor.accept(stream).await?; let ws_rslt = WebSocketAcceptRaw { compression: (), - key_buffer: &mut <_>::default(), - pb: PartitionedBuffer::default(), rng: StdRng::default(), stream: &mut tls_stream, + wsb: WebSocketBuffer::default(), } .accept(|req| manage_accept(&mut endpoint, &mut ft, req, &mut key_cb)) .await; @@ -117,20 +111,6 @@ async fn serve( } } -fn tls_acceptor(mut cert: &[u8], mut key: &[u8]) -> wtx::Result { - let key = pkcs8_private_keys(&mut key)? - .into_iter() - .map(PrivateKey) - .next() - .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "No private key"))?; - let certs: Vec<_> = certs(&mut cert)?.into_iter().map(Certificate).collect(); - let config = ServerConfig::builder() - .with_safe_defaults() - .with_no_client_auth() - .with_single_cert(certs, key) - .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?; - Ok(TlsAcceptor::from(Arc::new(config))) -} enum Endpoint { Store, } @@ -139,24 +119,18 @@ enum Endpoint { mod tests { use crate::serve; use irec::{irec_dir, FileTy}; - use std::{io::Cursor, sync::Arc, time::Duration}; + use std::time::Duration; use tokio::{ fs::{read_dir, read_to_string, remove_dir_all}, - net::TcpStream, time::sleep, }; - use tokio_rustls::{ - rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore, ServerName}, - TlsConnector, - }; - use webpki_roots::TLS_SERVER_ROOTS; use wtx::{ + misc::{TokioRustlsConnector, Uri}, rng::StdRng, web_socket::{ handshake::{WebSocketConnect, WebSocketConnectRaw}, - FrameBufferVec, FrameMutVec, OpCode, + FrameBufferVec, FrameMutVec, OpCode, WebSocketBuffer, }, - PartitionedBuffer, }; #[tokio::test] @@ -181,16 +155,13 @@ mod tests { compression: (), fb: &mut fb, headers_buffer: &mut <_>::default(), - pb: PartitionedBuffer::default(), rng: StdRng::default(), - stream: tls_connector() - .connect( - ServerName::try_from("localhost").map_err(|_err| wtx::Error::MissingHost).unwrap(), - TcpStream::connect("localhost:3000").await.unwrap(), - ) + stream: TokioRustlsConnector::from_webpki_roots() + .with_tcp_stream("localhost:3000".parse().unwrap(), "localhost") .await .unwrap(), - uri: &format!("ws://localhost:3000/store?ft=audio,key={key}"), + uri: &Uri::new(&format!("ws://localhost:3000/store?ft=audio,key={key}")), + wsb: WebSocketBuffer::default(), } .connect() .await @@ -206,20 +177,4 @@ mod tests { assert!(read_dir.next_entry().await.unwrap().is_none()); assert_eq!(read_to_string(dir_entry.path()).await.unwrap(), content) } - - fn tls_connector() -> TlsConnector { - let mut root_store = RootCertStore::empty(); - root_store.add_trust_anchors(TLS_SERVER_ROOTS.iter().map(|ta| { - OwnedTrustAnchor::from_subject_spki_name_constraints(ta.subject, ta.spki, ta.name_constraints) - })); - let _ = root_store.add_parsable_certificates( - &rustls_pemfile::certs(&mut Cursor::new(include_bytes!("../../../.certs/root-ca.crt"))) - .unwrap(), - ); - let config = ClientConfig::builder() - .with_safe_defaults() - .with_root_certificates(root_store) - .with_no_client_auth(); - TlsConnector::from(Arc::new(config)) - } }