1use std::{
2 io, iter,
3 pin::Pin,
4 task::{Context, Poll},
5};
6
7use airio_core::{PeerId, Upgrade, UpgradeInfo};
8use ed25519_dalek::PUBLIC_KEY_LENGTH;
9use ed25519_dalek::{SignatureError, VerifyingKey};
10use futures::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, future::BoxFuture};
11
12pub use ed25519_dalek as ed25519;
13
14#[derive(Clone)]
15pub struct Config {
16 local_pubkey: VerifyingKey,
17}
18
19impl Config {
20 pub fn new(local_pubkey: VerifyingKey) -> 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; PUBLIC_KEY_LENGTH];
31 socket.read_exact(&mut key_buf).await?;
32 let remote_key = VerifyingKey::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> Upgrade<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 fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
60 Box::pin(self.handshake(socket))
61 }
62}
63
64pub struct IdentifyConnection<S>
65where
66 S: AsyncRead + AsyncWrite + Unpin,
67{
68 pub socket: S,
69 pub remote_key: VerifyingKey,
70}
71
72#[derive(Debug, thiserror::Error)]
73pub enum Error {
74 #[error("I/O error: {0}")]
75 Io(#[from] io::Error),
76 #[error(transparent)]
77 SignatureError(#[from] SignatureError),
78}
79
80impl<T> AsyncRead for IdentifyConnection<T>
81where
82 T: AsyncRead + AsyncWrite + Unpin,
83{
84 fn poll_read(
85 self: Pin<&mut Self>,
86 cx: &mut Context<'_>,
87 buf: &mut [u8],
88 ) -> Poll<io::Result<usize>> {
89 Pin::new(&mut self.get_mut().socket).poll_read(cx, buf)
90 }
91
92 fn poll_read_vectored(
93 self: Pin<&mut Self>,
94 cx: &mut Context<'_>,
95 bufs: &mut [io::IoSliceMut<'_>],
96 ) -> Poll<io::Result<usize>> {
97 Pin::new(&mut self.get_mut().socket).poll_read_vectored(cx, bufs)
98 }
99}
100
101impl<T> AsyncWrite for IdentifyConnection<T>
102where
103 T: AsyncRead + AsyncWrite + Unpin,
104{
105 fn poll_write(
106 self: Pin<&mut Self>,
107 cx: &mut Context<'_>,
108 buf: &[u8],
109 ) -> Poll<Result<usize, io::Error>> {
110 Pin::new(&mut self.get_mut().socket).poll_write(cx, buf)
111 }
112
113 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
114 Pin::new(&mut self.get_mut().socket).poll_flush(cx)
115 }
116
117 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
118 Pin::new(&mut self.get_mut().socket).poll_close(cx)
119 }
120}