ctoolbox/network.rs
1use crate::utilities::*;
2use anyhow::{Context as _, Result, bail};
3use async_trait::async_trait;
4use futures::FutureExt;
5use futures::select;
6use futures_timer::Delay;
7use matchbox_socket::PeerState;
8use matchbox_socket::WebRtcSocket;
9use std::time::Duration;
10use tracing::info;
11
12pub async fn get_url(key: &str) -> Result<Vec<u8>> {
13 return DefaultNetworkBackend.fetch(key.to_string()).await;
14}
15
16/// Networking backend that the IPC layer depends on.
17#[async_trait]
18pub trait NetworkBackend: Send + Sync {
19 /// Fetch bytes from the given URL.
20 async fn fetch(&self, url: String) -> Result<Vec<u8>>;
21
22 /// Fetch bytes over HTTP.
23 async fn fetch_http(&self, url: &str) -> Result<Vec<u8>>;
24
25 /// Fetch bytes over HTTPS.
26 async fn fetch_https(&self, url: &str) -> Result<Vec<u8>>;
27
28 /// Read a local file into memory.
29 async fn read_file(&self, path: String) -> Result<Vec<u8>>;
30}
31
32/// Default implementation. This will eventually include APIs for a
33/// WebRTC transport, but for now returns clear errors for unimplemented paths.
34#[derive(Debug, Default)]
35pub struct DefaultNetworkBackend;
36
37#[async_trait]
38impl NetworkBackend for DefaultNetworkBackend {
39 async fn fetch(&self, url: String) -> Result<Vec<u8>> {
40 if url.starts_with("https://") {
41 return self.fetch_https(&url).await;
42 }
43
44 if url.starts_with("http://") {
45 return self.fetch_http(&url).await;
46 }
47
48 bail!("unsupported scheme for url {url}");
49 }
50
51 async fn fetch_http(&self, url: &str) -> Result<Vec<u8>> {
52 let resp = reqwest::get(url)
53 .await
54 .with_context(|| format!("http(s) GET {url}"))?;
55 let status = resp.status();
56 if !status.is_success() {
57 bail!("http(s) GET {url} failed: {status}");
58 }
59 let bytes = resp.bytes().await.with_context(|| "read http(s) body")?;
60 Ok(bytes.to_vec())
61 }
62
63 async fn fetch_https(&self, url: &str) -> Result<Vec<u8>> {
64 // HTTPS is handled similarly; reqwest negotiates TLS.
65 return self.fetch_http(url).await;
66 }
67
68 async fn read_file(&self, path: String) -> Result<Vec<u8>> {
69 tokio::fs::read(&path)
70 .await
71 .with_context(|| format!("read file at {path}"))
72 }
73}
74
75#[derive(Debug, Default)]
76pub struct MockNetworkBackend;
77#[async_trait]
78impl NetworkBackend for MockNetworkBackend {
79 async fn fetch(&self, url: String) -> Result<Vec<u8>> {
80 if url.starts_with("https://") {
81 return self.fetch_https(&url).await;
82 }
83
84 if url.starts_with("http://") {
85 return self.fetch_http(&url).await;
86 }
87
88 bail!("unsupported scheme for url {url}");
89 }
90
91 async fn fetch_http(&self, url: &str) -> Result<Vec<u8>> {
92 if url == "http://example.com" {
93 return Ok(b"Example HTTP Fetch".to_vec());
94 }
95 bail!(
96 "MockNetworkingBackend only supports http and https fetches of http://example.com"
97 );
98 }
99
100 async fn fetch_https(&self, url: &str) -> Result<Vec<u8>> {
101 if url == "https://example.com" {
102 return Ok(b"Example HTTPS Fetch".to_vec());
103 }
104 bail!(
105 "MockNetworkingBackend only supports http and https fetches of https://example.com"
106 );
107 }
108
109 async fn read_file(&self, path: String) -> Result<Vec<u8>> {
110 if path == "/tmp/example/file.txt" {
111 return Ok(b"Example File Read".to_vec());
112 }
113 bail!("MockNetworkingBackend only provides /tmp/example/file.txt");
114 }
115}
116
117pub async fn data_channel_test() {
118 data_channel_inner().await;
119}
120
121pub fn start_signaling_server() {
122 // TODO: implement signaling server for WebRTC peers.
123}
124
125async fn data_channel_inner() {
126 // Copied from https://github.com/johanhelsing/matchbox/blob/main/examples/simple/src/main.rs
127 /*
128 MIT License
129
130 Permission is hereby granted, free of charge, to any person obtaining a copy
131 of this software and associated documentation files (the "Software"), to deal
132 in the Software without restriction, including without limitation the rights
133 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
134 copies of the Software, and to permit persons to whom the Software is
135 furnished to do so, subject to the following conditions:
136
137 The above copyright notice and this permission notice shall be included in all
138 copies or substantial portions of the Software.
139
140 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
141 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
142 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
143 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
144 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
145 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
146 SOFTWARE.
147
148 Apache License
149 Version 2.0, January 2004
150 http://www.apache.org/licenses/
151
152 TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
153
154 1. Definitions.
155
156 "License" shall mean the terms and conditions for use, reproduction,
157 and distribution as defined by Sections 1 through 9 of this document.
158
159 "Licensor" shall mean the copyright owner or entity authorized by
160 the copyright owner that is granting the License.
161
162 "Legal Entity" shall mean the union of the acting entity and all
163 other entities that control, are controlled by, or are under common
164 control with that entity. For the purposes of this definition,
165 "control" means (i) the power, direct or indirect, to cause the
166 direction or management of such entity, whether by contract or
167 otherwise, or (ii) ownership of fifty percent (50%) or more of the
168 outstanding shares, or (iii) beneficial ownership of such entity.
169
170 "You" (or "Your") shall mean an individual or Legal Entity
171 exercising permissions granted by this License.
172
173 "Source" form shall mean the preferred form for making modifications,
174 including but not limited to software source code, documentation
175 source, and configuration files.
176
177 "Object" form shall mean any form resulting from mechanical
178 transformation or translation of a Source form, including but
179 not limited to compiled object code, generated documentation,
180 and conversions to other media types.
181
182 "Work" shall mean the work of authorship, whether in Source or
183 Object form, made available under the License, as indicated by a
184 copyright notice that is included in or attached to the work
185 (an example is provided in the Appendix below).
186
187 "Derivative Works" shall mean any work, whether in Source or Object
188 form, that is based on (or derived from) the Work and for which the
189 editorial revisions, annotations, elaborations, or other modifications
190 represent, as a whole, an original work of authorship. For the purposes
191 of this License, Derivative Works shall not include works that remain
192 separable from, or merely link (or bind by name) to the interfaces of,
193 the Work and Derivative Works thereof.
194
195 "Contribution" shall mean any work of authorship, including
196 the original version of the Work and any modifications or additions
197 to that Work or Derivative Works thereof, that is intentionally
198 submitted to Licensor for inclusion in the Work by the copyright owner
199 or by an individual or Legal Entity authorized to submit on behalf of
200 the copyright owner. For the purposes of this definition, "submitted"
201 means any form of electronic, verbal, or written communication sent
202 to the Licensor or its representatives, including but not limited to
203 communication on electronic mailing lists, source code control systems,
204 and issue tracking systems that are managed by, or on behalf of, the
205 Licensor for the purpose of discussing and improving the Work, but
206 excluding communication that is conspicuously marked or otherwise
207 designated in writing by the copyright owner as "Not a Contribution."
208
209 "Contributor" shall mean Licensor and any individual or Legal Entity
210 on behalf of whom a Contribution has been received by Licensor and
211 subsequently incorporated within the Work.
212
213 2. Grant of Copyright License. Subject to the terms and conditions of
214 this License, each Contributor hereby grants to You a perpetual,
215 worldwide, non-exclusive, no-charge, royalty-free, irrevocable
216 copyright license to reproduce, prepare Derivative Works of,
217 publicly display, publicly perform, sublicense, and distribute the
218 Work and such Derivative Works in Source or Object form.
219
220 3. Grant of Patent License. Subject to the terms and conditions of
221 this License, each Contributor hereby grants to You a perpetual,
222 worldwide, non-exclusive, no-charge, royalty-free, irrevocable
223 (except as stated in this section) patent license to make, have made,
224 use, offer to sell, sell, import, and otherwise transfer the Work,
225 where such license applies only to those patent claims licensable
226 by such Contributor that are necessarily infringed by their
227 Contribution(s) alone or by combination of their Contribution(s)
228 with the Work to which such Contribution(s) was submitted. If You
229 institute patent litigation against any entity (including a
230 cross-claim or counterclaim in a lawsuit) alleging that the Work
231 or a Contribution incorporated within the Work constitutes direct
232 or contributory patent infringement, then any patent licenses
233 granted to You under this License for that Work shall terminate
234 as of the date such litigation is filed.
235
236 4. Redistribution. You may reproduce and distribute copies of the
237 Work or Derivative Works thereof in any medium, with or without
238 modifications, and in Source or Object form, provided that You
239 meet the following conditions:
240
241 (a) You must give any other recipients of the Work or
242 Derivative Works a copy of this License; and
243
244 (b) You must cause any modified files to carry prominent notices
245 stating that You changed the files; and
246
247 (c) You must retain, in the Source form of any Derivative Works
248 that You distribute, all copyright, patent, trademark, and
249 attribution notices from the Source form of the Work,
250 excluding those notices that do not pertain to any part of
251 the Derivative Works; and
252
253 (d) If the Work includes a "NOTICE" text file as part of its
254 distribution, then any Derivative Works that You distribute must
255 include a readable copy of the attribution notices contained
256 within such NOTICE file, excluding those notices that do not
257 pertain to any part of the Derivative Works, in at least one
258 of the following places: within a NOTICE text file distributed
259 as part of the Derivative Works; within the Source form or
260 documentation, if provided along with the Derivative Works; or,
261 within a display generated by the Derivative Works, if and
262 wherever such third-party notices normally appear. The contents
263 of the NOTICE file are for informational purposes only and
264 do not modify the License. You may add Your own attribution
265 notices within Derivative Works that You distribute, alongside
266 or as an addendum to the NOTICE text from the Work, provided
267 that such additional attribution notices cannot be construed
268 as modifying the License.
269
270 You may add Your own copyright statement to Your modifications and
271 may provide additional or different license terms and conditions
272 for use, reproduction, or distribution of Your modifications, or
273 for any such Derivative Works as a whole, provided Your use,
274 reproduction, and distribution of the Work otherwise complies with
275 the conditions stated in this License.
276
277 5. Submission of Contributions. Unless You explicitly state otherwise,
278 any Contribution intentionally submitted for inclusion in the Work
279 by You to the Licensor shall be under the terms and conditions of
280 this License, without any additional terms or conditions.
281 Notwithstanding the above, nothing herein shall supersede or modify
282 the terms of any separate license agreement you may have executed
283 with Licensor regarding such Contributions.
284
285 6. Trademarks. This License does not grant permission to use the trade
286 names, trademarks, service marks, or product names of the Licensor,
287 except as required for reasonable and customary use in describing the
288 origin of the Work and reproducing the content of the NOTICE file.
289
290 7. Disclaimer of Warranty. Unless required by applicable law or
291 agreed to in writing, Licensor provides the Work (and each
292 Contributor provides its Contributions) on an "AS IS" BASIS,
293 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
294 implied, including, without limitation, any warranties or conditions
295 of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
296 PARTICULAR PURPOSE. You are solely responsible for determining the
297 appropriateness of using or redistributing the Work and assume any
298 risks associated with Your exercise of permissions under this License.
299
300 8. Limitation of Liability. In no event and under no legal theory,
301 whether in tort (including negligence), contract, or otherwise,
302 unless required by applicable law (such as deliberate and grossly
303 negligent acts) or agreed to in writing, shall any Contributor be
304 liable to You for damages, including any direct, indirect, special,
305 incidental, or consequential damages of any character arising as a
306 result of this License or out of the use or inability to use the
307 Work (including but not limited to damages for loss of goodwill,
308 work stoppage, computer failure or malfunction, or any and all
309 other commercial damages or losses), even if such Contributor
310 has been advised of the possibility of such damages.
311
312 9. Accepting Warranty or Additional Liability. While redistributing
313 the Work or Derivative Works thereof, You may choose to offer,
314 and charge a fee for, acceptance of support, warranty, indemnity,
315 or other liability obligations and/or rights consistent with this
316 License. However, in accepting such obligations, You may act only
317 on Your own behalf and on Your sole responsibility, not on behalf
318 of any other Contributor, and only if You agree to indemnify,
319 defend, and hold each Contributor harmless for any liability
320 incurred by, or claims asserted against, such Contributor by reason
321 of your accepting any such warranty or additional liability.
322
323 END OF TERMS AND CONDITIONS*/
324
325 let (mut socket, loop_fut) =
326 WebRtcSocket::new_reliable("ws://localhost:3536/");
327
328 let loop_fut = loop_fut.fuse();
329 futures::pin_mut!(loop_fut);
330
331 let timeout = Delay::new(Duration::from_millis(100));
332 futures::pin_mut!(timeout);
333
334 const CHANNEL_ID: usize = 0;
335
336 loop {
337 // log_string("polling".to_string());
338 // Handle any new peers
339 for (peer, state) in socket.update_peers() {
340 match state {
341 PeerState::Connected => {
342 info!("Peer joined: {peer}");
343 let packet =
344 "hello friend!".as_bytes().to_vec().into_boxed_slice();
345 socket.channel_mut(CHANNEL_ID).send(packet, peer);
346 }
347 PeerState::Disconnected => {
348 info!("Peer left: {peer}");
349 }
350 }
351 }
352
353 // Accept any messages incoming
354 for (peer, packet) in socket.channel_mut(CHANNEL_ID).receive() {
355 let message = String::from_utf8_lossy(&packet);
356 info!("Message from {peer}: {message:?}");
357 }
358
359 select! {
360 // Restart this loop every 100ms
361 () = (&mut timeout).fuse() => {
362 timeout.reset(Duration::from_millis(100));
363 }
364
365 // Or break if the message loop ends (disconnected, closed, etc.)
366 _ = &mut loop_fut => {
367 break;
368 }
369 }
370 }
371}
372
373#[cfg(test)]
374mod tests {}