1use std::{
2 io, iter,
3 pin::Pin,
4 task::{Context, Poll},
5};
6
7use futures::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, future::BoxFuture};
8use volans_core::{
9 PeerId, UpgradeInfo,
10 identity::{PublicKey, SignatureError},
11 upgrade::{InboundConnectionUpgrade, OutboundConnectionUpgrade},
12};
13
14#[derive(Clone)]
15pub struct Config {
16 local_pubkey: PublicKey,
17}
18
19impl Config {
20 pub fn new(local_pubkey: PublicKey) -> Self {
21 Self { local_pubkey }
22 }
23
24 async fn handshake<T>(self, mut socket: T) -> Result<(PeerId, IdentifyConnection<T>), Error>
25 where
26 T: AsyncRead + AsyncWrite + Send + Unpin + 'static,
27 {
28 socket.write_all(self.local_pubkey.as_bytes()).await?;
29 socket.flush().await?;
30 let mut key_buf = [0; 32];
31 socket.read_exact(&mut key_buf).await?;
32 let remote_key = PublicKey::from_bytes(&key_buf)?;
33 let peer_id = PeerId::from_bytes(remote_key.as_bytes().clone());
34 Ok((peer_id, IdentifyConnection { socket, remote_key }))
35 }
36}
37
38impl UpgradeInfo for Config {
39 type Info = &'static str;
40 type InfoIter = iter::Once<Self::Info>;
41
42 fn protocol_info(&self) -> Self::InfoIter {
43 iter::once("/v1/identify")
44 }
45}
46
47impl<C> InboundConnectionUpgrade<C> for Config
48where
49 C: AsyncRead + AsyncWrite + Send + Unpin + 'static,
50{
51 type Output = (PeerId, IdentifyConnection<C>);
52 type Error = Error;
53 type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
54
55 fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future {
56 Box::pin(self.handshake(socket))
57 }
58}
59
60impl<C> OutboundConnectionUpgrade<C> for Config
61where
62 C: AsyncRead + AsyncWrite + Send + Unpin + 'static,
63{
64 type Output = (PeerId, IdentifyConnection<C>);
65 type Error = Error;
66 type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;
67
68 fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
69 Box::pin(self.handshake(socket))
70 }
71}
72
73pub struct IdentifyConnection<S>
74where
75 S: AsyncRead + AsyncWrite + Unpin,
76{
77 pub socket: S,
78 pub remote_key: PublicKey,
79}
80
81#[derive(Debug, thiserror::Error)]
82pub enum Error {
83 #[error("I/O error: {0}")]
84 Io(#[from] io::Error),
85 #[error(transparent)]
86 SignatureError(#[from] SignatureError),
87}
88
89impl<T> AsyncRead for IdentifyConnection<T>
90where
91 T: AsyncRead + AsyncWrite + Unpin,
92{
93 fn poll_read(
94 self: Pin<&mut Self>,
95 cx: &mut Context<'_>,
96 buf: &mut [u8],
97 ) -> Poll<io::Result<usize>> {
98 Pin::new(&mut self.get_mut().socket).poll_read(cx, buf)
99 }
100
101 fn poll_read_vectored(
102 self: Pin<&mut Self>,
103 cx: &mut Context<'_>,
104 bufs: &mut [io::IoSliceMut<'_>],
105 ) -> Poll<io::Result<usize>> {
106 Pin::new(&mut self.get_mut().socket).poll_read_vectored(cx, bufs)
107 }
108}
109
110impl<T> AsyncWrite for IdentifyConnection<T>
111where
112 T: AsyncRead + AsyncWrite + Unpin,
113{
114 fn poll_write(
115 self: Pin<&mut Self>,
116 cx: &mut Context<'_>,
117 buf: &[u8],
118 ) -> Poll<Result<usize, io::Error>> {
119 Pin::new(&mut self.get_mut().socket).poll_write(cx, buf)
120 }
121
122 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
123 Pin::new(&mut self.get_mut().socket).poll_flush(cx)
124 }
125
126 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
127 Pin::new(&mut self.get_mut().socket).poll_close(cx)
128 }
129}