use std::time::Duration;
use anyhow::Context;
use async_net::SocketAddr;
use smol::future::FutureExt;
use sosistab::{Multiplex, Session};
pub struct ProtoSession {
pub inner: Session,
pub remote_addr: SocketAddr,
}
impl ProtoSession {
pub fn remote_addr(&self) -> SocketAddr {
self.remote_addr
}
pub fn multiplex(self) -> Multiplex {
self.inner.multiplex()
}
pub async fn hijack(self, other_mplex: &Multiplex, other_id: [u8; 32]) -> anyhow::Result<()> {
log::debug!(
"starting hijack of other_id = {}...",
hex::encode(&other_id[..5])
);
let spam_loop = async {
loop {
self.inner.send_bytes(other_id.as_ref()).await?;
smol::Timer::after(Duration::from_secs(1)).await;
}
};
spam_loop
.race(async {
let down = self
.inner
.recv_bytes()
.await
.context("inner session failed in hijack")?;
log::debug!(
"finished hijack of other_id = {} with downstream data of {}!",
hex::encode(&other_id[..5]),
down.len()
);
Ok::<_, anyhow::Error>(())
})
.await?;
other_mplex.replace_session(self.inner).await;
Ok(())
}
}