Skip to content

Commit

Permalink
feat: add tls socks5 proxy with defalte client example (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
pymongo authored Nov 21, 2023
1 parent 89c0bec commit fcf2258
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ serde = { version = "1", features = ["derive"] }
hproxy = { version = "0.1", features = ["sync", "async"] }
sproxy = { version = "0.1", features = ["sync", "async"] }
tabled = "0.13.0"
socks = "0.3.4"
# benchmark deps
tungstenite = "0.20.0"
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
59 changes: 59 additions & 0 deletions examples/tls_proxy_deflate_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use socks::Socks5Stream;
use tracing_subscriber::util::SubscriberInitExt;
use ws_tool::{
codec::{DeflateCodec, PMDConfig, WindowBit},
frame::OpCode,
ClientBuilder, ClientConfig,
};

fn main() {
tracing_subscriber::fmt::fmt()
.with_max_level(tracing::Level::DEBUG)
.finish()
.try_init()
.expect("failed to init log");

let url = std::env::var("URL").expect("env URL not set");
let uri = url.parse::<http::Uri>().unwrap();
let host = uri.host().unwrap();
let proxy_addr = std::env::var("SOCKS5_PROXY").expect("env SOCKS5_PROXY not set");
let mut stream =
Socks5Stream::connect(proxy_addr.trim_start_matches("socks5://"), (host, 443)).unwrap();
stream
.get_mut()
.set_read_timeout(Some(std::time::Duration::from_secs(5)))
.unwrap();
let stream = ws_tool::connector::wrap_rustls(stream, host, Vec::new()).unwrap();

let pmd_config = PMDConfig {
server_no_context_takeover: ClientConfig::default().context_take_over,
client_no_context_takeover: ClientConfig::default().context_take_over,
server_max_window_bits: WindowBit::Fifteen,
client_max_window_bits: WindowBit::Fifteen,
};
let mut stream = ClientBuilder::new()
.extension(pmd_config.ext_string())
.with_stream(uri, stream, DeflateCodec::check_fn)
.unwrap();
loop {
let (header, data) = stream.receive().unwrap();
match &header.code {
OpCode::Text => {
let data = String::from_utf8(data.to_vec()).unwrap();
tracing::info!("receive {data}");
}
OpCode::Close => {
stream.send(OpCode::Close, &[]).unwrap();
tracing::info!("receive Close");
break;
}
OpCode::Ping => {
stream.send(OpCode::Pong, &[]).unwrap();
}
OpCode::Pong => {}
_ => {
unreachable!()
}
}
}
}

0 comments on commit fcf2258

Please sign in to comment.