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
mod obfs_tls;
mod obfs_udp;
use std::{ops::Deref, sync::Arc};
use async_trait::async_trait;
use bytes::Bytes;
pub use obfs_tls::*;
pub use obfs_udp::*;
use smol::future::FutureExt;
#[async_trait]
pub trait Pipe: Send + Sync + 'static {
async fn send(&self, to_send: Bytes);
async fn recv(&self) -> std::io::Result<Bytes>;
fn protocol(&self) -> &str;
fn peer_metadata(&self) -> &str;
fn peer_addr(&self) -> String;
}
#[async_trait]
impl<P: Pipe + ?Sized, T: Deref<Target = P> + Send + Sync + 'static> Pipe for T {
async fn send(&self, to_send: Bytes) {
self.deref().send(to_send).await
}
async fn recv(&self) -> std::io::Result<Bytes> {
self.deref().recv().await
}
fn protocol(&self) -> &str {
self.deref().protocol()
}
fn peer_metadata(&self) -> &str {
self.deref().peer_metadata()
}
fn peer_addr(&self) -> String {
self.deref().peer_addr()
}
}
#[async_trait]
pub trait PipeListener: Sized + Send + Sync {
async fn accept_pipe(&self) -> std::io::Result<Arc<dyn Pipe>>;
fn or<T: PipeListener>(self, other: T) -> OrPipeListener<Self, T> {
OrPipeListener {
left: self,
right: other,
}
}
}
pub struct OrPipeListener<T: PipeListener + Sized, U: PipeListener + Sized> {
left: T,
right: U,
}
#[async_trait]
impl<T: PipeListener, U: PipeListener> PipeListener for OrPipeListener<T, U> {
async fn accept_pipe(&self) -> std::io::Result<Arc<dyn Pipe>> {
self.left.accept_pipe().or(self.right.accept_pipe()).await
}
}