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
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(())
}
}