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
|
mod noop;
mod shadowsocks;
use self::shadowsocks::ShadowsocksProxyMonitor;
use async_trait::async_trait;
use std::{fmt, io, path::PathBuf};
use talpid_types::net::openvpn;
#[derive(err_derive::Error, Debug)]
pub enum Error {
#[error(display = "Monitor exited unexpectedly: {}", _0)]
UnexpectedExit(String),
#[error(display = "I/O error")]
Io(io::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
#[async_trait]
pub trait ProxyMonitor: Send {
/// Create a handle than can be used to ask the proxy service to shut down.
fn close_handle(&mut self) -> Box<dyn ProxyMonitorCloseHandle>;
/// Consume monitor and wait for proxy service to shut down.
async fn wait(self: Box<Self>) -> Result<()>;
/// The port bound to.
fn port(&self) -> u16;
}
impl fmt::Debug for dyn ProxyMonitor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ProxyMonitor {{ port: {} }}", self.port())
}
}
pub trait ProxyMonitorCloseHandle: Send {
fn close(self: Box<Self>) -> Result<()>;
}
/// Variables that define the environment to help
/// proxy implementations find their way around.
/// TODO: Move struct to wider scope and use more generic name.
pub struct ProxyResourceData {
pub resource_dir: PathBuf,
pub log_dir: Option<PathBuf>,
}
pub async fn start_proxy(
settings: &openvpn::ProxySettings,
resource_data: &ProxyResourceData,
) -> Result<Box<dyn ProxyMonitor>> {
match settings {
openvpn::ProxySettings::Local(local_settings) => {
// These are generic proxy settings with the proxy client not managed by us.
Ok(Box::new(noop::NoopProxyMonitor::start(
local_settings.port,
)?))
}
openvpn::ProxySettings::Remote(remote_settings) => {
// These are generic proxy settings with the proxy client not managed by us.
Ok(Box::new(noop::NoopProxyMonitor::start(
remote_settings.address.port(),
)?))
}
openvpn::ProxySettings::Shadowsocks(ss_settings) => Ok(Box::new(
ShadowsocksProxyMonitor::start(ss_settings, resource_data).await?,
)),
}
}
|