1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::duplex::Duplex;
use crate::Protocol;
use futures_lite::io::{AsyncRead, AsyncWrite};
/// Options for a Protocol instance.
#[derive(Debug)]
pub struct Options {
/// Whether this peer initiated the IO connection for this protoccol
pub is_initiator: bool,
/// Enable or disable the handshake.
/// Disabling the handshake will also disable capabilitity verification.
/// Don't disable this if you're not 100% sure you want this.
pub noise: bool,
/// Enable or disable transport encryption.
pub encrypted: bool,
}
impl Options {
/// Create with default options.
pub fn new(is_initiator: bool) -> Self {
Self {
is_initiator,
noise: true,
encrypted: true,
}
}
}
/// Build a Protocol instance with options.
#[derive(Debug)]
pub struct Builder(Options);
impl Builder {
/// Create a protocol builder.
pub fn new(is_initiator: bool) -> Self {
Self(Options {
is_initiator,
noise: true,
encrypted: true,
})
}
/// Default options for an initiating endpoint.
pub fn initiator() -> Self {
Self::new(true)
}
/// Default options for a responding endpoint.
pub fn responder() -> Self {
Self::new(false)
}
/// Set encrypted option.
pub fn set_encrypted(mut self, encrypted: bool) -> Self {
self.0.encrypted = encrypted;
self
}
/// Set handshake option.
pub fn set_noise(mut self, noise: bool) -> Self {
self.0.noise = noise;
self
}
/// Create the protocol from a stream that implements AsyncRead + AsyncWrite + Clone.
pub fn connect<IO>(self, io: IO) -> Protocol<IO>
where
IO: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
Protocol::new(io, self.0)
}
/// Create the protocol from an AsyncRead reader and AsyncWrite writer.
pub fn connect_rw<R, W>(self, reader: R, writer: W) -> Protocol<Duplex<R, W>>
where
R: AsyncRead + Send + Unpin + 'static,
W: AsyncWrite + Send + Unpin + 'static,
{
let io = Duplex::new(reader, writer);
Protocol::new(io, self.0)
}
}