Skip to content

Commit

Permalink
fmt code
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Apr 10, 2022
1 parent 11ad16f commit 25ff9f4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 39 deletions.
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 18 additions & 15 deletions cmd/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,42 @@ use kaminari::tls::TlsConnect;

use kaminari_cmd::{Endpoint, parse_cmd, parse_env};


#[tokio::main]
async fn main() -> Result<()> {
let (Endpoint{local, remote}, options) = parse_env().or_else(|_| parse_cmd())?;
let (Endpoint { local, remote }, options) = parse_env().or_else(|_| parse_cmd())?;

let ws = opt::get_ws_conf(&options);
let tls = opt::get_tls_client_conf(&options);

eprintln!("ws: {:?}", &ws);
eprintln!("tls: {:?}", &tls);

let lis = TcpListener::bind(local).await?;

while let Ok((stream, _)) = lis.accept().await {
match (ws.clone(), tls.clone()) {
(None, None) => tokio::spawn(relay(stream, remote, NopConnect{})),
(Some(ws), None) => tokio::spawn(relay(stream, remote, WsConnect::new(
NopConnect{}, ws
))),
(None, Some(tls)) => tokio::spawn(relay(stream, remote, TlsConnect::new(
NopConnect{}, tls
))),
(Some(ws), Some(tls)) => tokio::spawn(relay(stream, remote, WsConnect::new(TlsConnect::new(
NopConnect{}, tls
), ws)))
(None, None) => tokio::spawn(relay(stream, remote, NopConnect {})),
(Some(ws), None) => {
tokio::spawn(relay(stream, remote, WsConnect::new(NopConnect {}, ws)))
}
(None, Some(tls)) => {
tokio::spawn(relay(stream, remote, TlsConnect::new(NopConnect {}, tls)))
}
(Some(ws), Some(tls)) => tokio::spawn(relay(
stream,
remote,
WsConnect::new(TlsConnect::new(NopConnect {}, tls), ws),
)),
};
}

Ok(())
}

async fn relay<T>(mut local: TcpStream, remote: SocketAddr, client: T) -> Result<()>
where T:AsyncConnect<TcpStream>{
async fn relay<T>(mut local: TcpStream, remote: SocketAddr, client: T) -> Result<()>
where
T: AsyncConnect<TcpStream>,
{
let remote = TcpStream::connect(remote).await?;

let mut remote = client.connect(remote).await?;
Expand Down
19 changes: 13 additions & 6 deletions cmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,29 @@ pub fn parse_env() -> Result<(Endpoint, String)> {
let plugin_opts = env::var("SS_PLUGIN_OPTIONS")?;

let local = format!("{}:{}", local_host, local_port)
.to_socket_addrs()?.next().unwrap();
.to_socket_addrs()?
.next()
.unwrap();

let remote = format!("{}:{}", remote_host, remote_port)
.to_socket_addrs()?.next().unwrap();

Ok((Endpoint{local, remote}, plugin_opts))
.to_socket_addrs()?
.next()
.unwrap();

Ok((Endpoint { local, remote }, plugin_opts))
}

pub fn parse_cmd() -> Result<(Endpoint, String)> {
let args: Vec<String> = env::args().collect();

anyhow::ensure!(args.len() == 4, "only allow 3 params: <local> <remote> <options>");
anyhow::ensure!(
args.len() == 4,
"only allow 3 params: <local> <remote> <options>"
);

let local = args[1].to_socket_addrs()?.next().unwrap();
let remote = args[2].to_socket_addrs()?.next().unwrap();
let plugin_opts = args[3].clone();

Ok((Endpoint{local, remote}, plugin_opts))
Ok((Endpoint { local, remote }, plugin_opts))
}
33 changes: 19 additions & 14 deletions cmd/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ use kaminari::tls::TlsAccept;

use kaminari_cmd::{Endpoint, parse_cmd, parse_env};


#[tokio::main]
async fn main() -> Result<()>{
let (Endpoint{local, remote}, options) = parse_env().or_else(|_| parse_cmd())?;
async fn main() -> Result<()> {
let (Endpoint { local, remote }, options) = parse_env().or_else(|_| parse_cmd())?;

let ws = opt::get_ws_conf(&options);
let tls = opt::get_tls_server_conf(&options);
Expand All @@ -25,23 +24,29 @@ async fn main() -> Result<()>{

while let Ok((stream, _)) = lis.accept().await {
match (ws.clone(), tls.clone()) {
(None, None) => tokio::spawn(relay(stream, remote, NopAccept{})),
(Some(ws), None) => tokio::spawn(relay(stream, remote, WsAccept::new(
NopAccept{}, ws
))),
(None, Some(tls)) => tokio::spawn(relay(stream, remote, TlsAccept::new(
NopAccept{}, tls
))),
(Some(ws), Some(tls)) => tokio::spawn(relay(stream, remote, WsAccept::new(TlsAccept::new(
NopAccept{}, tls
), ws)))
(None, None) => tokio::spawn(relay(stream, remote, NopAccept {})),
(Some(ws), None) => {
tokio::spawn(relay(stream, remote, WsAccept::new(NopAccept {}, ws)))
}
(None, Some(tls)) => {
tokio::spawn(relay(stream, remote, TlsAccept::new(NopAccept {}, tls)))
}
(Some(ws), Some(tls)) => tokio::spawn(relay(
stream,
remote,
WsAccept::new(TlsAccept::new(NopAccept {}, tls), ws),
)),
};
}

Ok(())
}

async fn relay<T:AsyncAccept<TcpStream>>(local: TcpStream, remote: SocketAddr, server: T) -> Result<()> {
async fn relay<T: AsyncAccept<TcpStream>>(
local: TcpStream,
remote: SocketAddr,
server: T,
) -> Result<()> {
let mut local = server.accept(local).await?;

let mut remote = TcpStream::connect(remote).await?;
Expand Down

0 comments on commit 25ff9f4

Please sign in to comment.