Skip to content

Commit 86e8655

Browse files
committed
Oops! Add multitasking
1 parent 4b234c5 commit 86e8655

4 files changed

Lines changed: 21 additions & 12 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "triangle-proxy"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
edition = "2024"
55
license-file = "LICENSE"
66
description = "Simple TLS-with-SNI to SOCKS5 proxy"

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{net::SocketAddr, time::Duration};
1+
use std::net::SocketAddr;
22

33
use serde::Deserialize;
44

src/main.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod proxy;
44
mod tls;
55

66
use std::net::SocketAddr;
7+
use std::sync::Arc;
78
use std::time::Duration;
89

910
use color_eyre::eyre::Result;
@@ -27,8 +28,11 @@ async fn handle_connection(
2728
mut stream: TcpStream,
2829
addr: SocketAddr,
2930
) -> Result<(), ProxyError> {
30-
let (hostname, initial_buf) =
31-
tokio::time::timeout(Duration::from_millis(config.timeout_ms), parse_sni(&mut stream)).await??;
31+
let (hostname, initial_buf) = tokio::time::timeout(
32+
Duration::from_millis(config.timeout_ms),
33+
parse_sni(&mut stream),
34+
)
35+
.await??;
3236

3337
let forward = config
3438
.forwards
@@ -56,20 +60,25 @@ async fn main() -> Result<()> {
5660
tracing_subscriber::fmt::init();
5761
color_eyre::install()?;
5862

59-
let config: Config = Figment::new()
60-
.merge(Json::file("sniproxy.json"))
61-
.merge(Env::prefixed("TRIANGLE_"))
62-
.extract()?;
63+
let config: Arc<Config> = Arc::new(
64+
Figment::new()
65+
.merge(Json::file("sniproxy.json"))
66+
.merge(Env::prefixed("TRIANGLE_"))
67+
.extract()?,
68+
);
6369

6470
tracing::info!("{config:?}");
6571

6672
let lc = TcpListener::bind(config.listen_addr).await.unwrap();
6773
loop {
6874
match lc.accept().await {
6975
Ok((stream, addr)) => {
70-
if let Err(err) = handle_connection(&config, stream, addr).await {
71-
tracing::error!("error proxying connection: {err}");
72-
}
76+
let config = config.clone();
77+
tokio::spawn(async move {
78+
if let Err(err) = handle_connection(&config.clone(), stream, addr).await {
79+
tracing::error!("error proxying connection: {err}");
80+
}
81+
});
7382
}
7483
Err(err) => tracing::error!("error accepting connection: {err}"),
7584
}

0 commit comments

Comments
 (0)