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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
mod error;
mod handshake;
mod secure_stream;
use crate::error::PlaintextError;
use crate::handshake::handshake_plaintext::Remote;
use crate::secure_stream::{SecureStream, SecureStreamReader, SecureStreamWriter};
use libp2prs_core::identity::Keypair;
use libp2prs_core::secure_io::SecureInfo;
use libp2prs_core::transport::{ConnectionInfo, TransportError};
use libp2prs_core::upgrade::{UpgradeInfo, Upgrader};
use libp2prs_core::{Multiaddr, PeerId, PublicKey};
use libp2prs_traits::{ReadEx, SplitEx, SplittableReadWrite, WriteEx};
use std::io;
use async_trait::async_trait;
pub mod structs_proto {
include!(concat!(env!("OUT_DIR"), "/structs_proto.rs"));
}
const MAX_FRAME_SIZE: usize = 1024 * 1024 * 8;
#[derive(Clone)]
pub struct PlainTextConfig {
pub(crate) key: Keypair,
pub(crate) max_frame_length: usize,
}
impl PlainTextConfig {
pub fn new(key: Keypair) -> Self {
PlainTextConfig {
key,
max_frame_length: MAX_FRAME_SIZE,
}
}
pub async fn handshake<T>(self, socket: T) -> Result<(SecureStream<T::Reader, T::Writer>, Remote), PlaintextError>
where
T: SplittableReadWrite,
{
handshake::handshake_plaintext::handshake(socket, self).await
}
}
impl UpgradeInfo for PlainTextConfig {
type Info = &'static [u8];
fn protocol_info(&self) -> Vec<Self::Info> {
vec![b"/plaintext/1.0.0"]
}
}
#[async_trait]
impl<T> Upgrader<T> for PlainTextConfig
where
T: ConnectionInfo + SplittableReadWrite,
{
type Output = PlainTextOutput<T>;
async fn upgrade_inbound(self, socket: T, _info: <Self as UpgradeInfo>::Info) -> Result<Self::Output, TransportError> {
make_secure_output(self, socket).await
}
async fn upgrade_outbound(self, socket: T, _info: <Self as UpgradeInfo>::Info) -> Result<Self::Output, TransportError> {
make_secure_output(self, socket).await
}
}
async fn make_secure_output<T>(config: PlainTextConfig, socket: T) -> Result<PlainTextOutput<T>, TransportError>
where
T: ConnectionInfo + SplittableReadWrite,
{
let pri_key = config.key.clone();
let la = socket.local_multiaddr();
let ra = socket.remote_multiaddr();
let (secure_stream, remote) = config.handshake(socket).await?;
let output = PlainTextOutput {
stream: secure_stream,
local_priv_key: pri_key.clone(),
local_peer_id: pri_key.public().into_peer_id(),
remote_pub_key: remote.public_key,
remote_peer_id: remote.peer_id,
la,
ra,
};
Ok(output)
}
pub struct PlainTextOutput<T: SplitEx> {
pub stream: SecureStream<T::Reader, T::Writer>,
pub local_priv_key: Keypair,
pub local_peer_id: PeerId,
pub remote_pub_key: PublicKey,
pub remote_peer_id: PeerId,
la: Multiaddr,
ra: Multiaddr,
}
impl<T: SplitEx> SecureInfo for PlainTextOutput<T> {
fn local_peer(&self) -> PeerId {
self.local_peer_id.clone()
}
fn remote_peer(&self) -> PeerId {
self.remote_peer_id.clone()
}
fn local_priv_key(&self) -> Keypair {
self.local_priv_key.clone()
}
fn remote_pub_key(&self) -> PublicKey {
self.remote_pub_key.clone()
}
}
impl<T: SplitEx> ConnectionInfo for PlainTextOutput<T> {
fn local_multiaddr(&self) -> Multiaddr {
self.la.clone()
}
fn remote_multiaddr(&self) -> Multiaddr {
self.ra.clone()
}
}
#[async_trait]
impl<S: SplittableReadWrite> ReadEx for PlainTextOutput<S> {
async fn read2(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
self.stream.read2(buf).await
}
}
#[async_trait]
impl<S: SplittableReadWrite> WriteEx for PlainTextOutput<S> {
async fn write2(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
self.stream.write2(buf).await
}
async fn flush2(&mut self) -> Result<(), io::Error> {
self.stream.flush2().await
}
async fn close2(&mut self) -> Result<(), io::Error> {
self.stream.close2().await
}
}
impl<S: SplittableReadWrite> SplitEx for PlainTextOutput<S> {
type Reader = SecureStreamReader<S::Reader>;
type Writer = SecureStreamWriter<S::Writer>;
fn split(self) -> (Self::Reader, Self::Writer) {
let (r, w) = self.stream.split();
(r, w)
}
}
impl From<PlaintextError> for TransportError {
fn from(e: PlaintextError) -> Self {
TransportError::SecurityError(Box::new(e))
}
}