Skip to content

Commit 34aac57

Browse files
committed
chore: fmt code
Signed-off-by: Xin Liu <[email protected]>
1 parent 5d4ceed commit 34aac57

File tree

7 files changed

+58
-39
lines changed

7 files changed

+58
-39
lines changed

Cargo.toml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
8+
# Error handling
9+
anyhow = "1.0"
710
# Web framework
811
axum = { version = "0.8", features = ["ws"] }
9-
tokio = { version = "1", features = ["full"] }
1012

1113
# CLI argument parsing
1214
clap = { version = "4.5", features = ["derive", "env"] }
15+
futures-util = "0.3"
1316

1417
# HTTP client - Using rustls instead of native-tls/openssl
1518
reqwest = { version = "0.12", features = ["json", "rustls-tls"], default-features = false }
1619

17-
# Database
18-
sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite"] }
19-
2020
# Serialization
2121
serde = { version = "1.0", features = ["derive"] }
2222
serde_json = "1.0"
2323

24-
# Logging
25-
tracing = "0.1"
26-
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
27-
28-
# Error handling
29-
anyhow = "1.0"
24+
# Database
25+
sqlx = { version = "0.8", features = ["runtime-tokio", "sqlite"] }
3026
thiserror = "1.0"
27+
tokio = { version = "1", features = ["full"] }
3128

3229
# WebSocket
3330
tokio-tungstenite = "0.24"
34-
futures-util = "0.3"
3531

3632
# Utilities
3733
tower = "0.5"
3834
tower-http = { version = "0.6", features = ["trace", "cors"] }
35+
36+
# Logging
37+
tracing = "0.1"
38+
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

src/db/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use sqlx::{sqlite::SqlitePool, Error as SqlxError};
1+
use sqlx::{Error as SqlxError, sqlite::SqlitePool};
22
use tracing::info;
33

44
use crate::models::Session;

src/handlers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub mod http;
22
pub mod websocket;
33

4-
pub use http::{health_check, http_proxy_handler, AppState};
4+
pub use http::{AppState, health_check, http_proxy_handler};
55
pub use websocket::websocket_handler;

src/handlers/websocket.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use axum::{
22
extract::{
3-
ws::{WebSocket, WebSocketUpgrade},
43
Path, State,
4+
ws::{WebSocket, WebSocketUpgrade},
55
},
66
http::StatusCode,
77
response::Response,
@@ -18,7 +18,10 @@ pub async fn websocket_handler(
1818
Path(session_id): Path<String>,
1919
ws: WebSocketUpgrade,
2020
) -> Result<Response, StatusCode> {
21-
info!("Received WebSocket connection request: session_id={}", session_id);
21+
info!(
22+
"Received WebSocket connection request: session_id={}",
23+
session_id
24+
);
2225

2326
// 1. Query database to get session information
2427
let session = match db::get_session(&pool, &session_id).await {

src/main.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::{
2-
routing::{any, get},
32
Router,
3+
routing::{any, get},
44
};
55
use clap::Parser;
66
use std::{sync::Arc, time::Duration};
@@ -15,7 +15,7 @@ mod models;
1515
mod proxy;
1616

1717
use config::Config;
18-
use handlers::{health_check, http_proxy_handler, websocket_handler, AppState};
18+
use handlers::{AppState, health_check, http_proxy_handler, websocket_handler};
1919
use proxy::HttpProxy;
2020

2121
/// SS Proxy - HTTP/HTTPS/WebSocket Proxy Server
@@ -53,8 +53,9 @@ async fn main() -> anyhow::Result<()> {
5353
// Initialize logging with CLI-specified log level
5454
tracing_subscriber::registry()
5555
.with(
56-
tracing_subscriber::EnvFilter::try_from_default_env()
57-
.unwrap_or_else(|_| format!("ss_proxy={},tower_http={}", log_level, log_level).into()),
56+
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
57+
format!("ss_proxy={},tower_http={}", log_level, log_level).into()
58+
}),
5859
)
5960
.with(tracing_subscriber::fmt::layer())
6061
.init();
@@ -73,7 +74,10 @@ async fn main() -> anyhow::Result<()> {
7374
let http_proxy = HttpProxy::new(Duration::from_secs(config.request_timeout));
7475

7576
// Create shared state
76-
let http_state = Arc::new(AppState { pool: pool.clone(), http_proxy });
77+
let http_state = Arc::new(AppState {
78+
pool: pool.clone(),
79+
http_proxy,
80+
});
7781
let ws_state = Arc::new(pool);
7882

7983
// Build router
@@ -100,4 +104,3 @@ async fn main() -> anyhow::Result<()> {
100104

101105
Ok(())
102106
}
103-

src/proxy/http_proxy.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ impl HttpProxy {
4848
key_str,
4949
"host" | "connection" | "transfer-encoding" | "content-length"
5050
) {
51-
if let Ok(name) = reqwest::header::HeaderName::from_bytes(key.as_str().as_bytes())
52-
{
51+
if let Ok(name) = reqwest::header::HeaderName::from_bytes(key.as_str().as_bytes()) {
5352
if let Ok(val) = reqwest::header::HeaderValue::from_bytes(value.as_bytes()) {
5453
request_headers.insert(name, val);
5554
}
@@ -68,7 +67,10 @@ impl HttpProxy {
6867
ProxyError::RequestFailed(e.to_string())
6968
})?;
7069

71-
info!("Received response from downstream server: {}", response.status());
70+
info!(
71+
"Received response from downstream server: {}",
72+
response.status()
73+
);
7274

7375
// Build response
7476
let mut builder = axum::http::Response::builder().status(response.status());
@@ -85,10 +87,12 @@ impl HttpProxy {
8587
})?;
8688

8789
// Build final response
88-
let final_response = builder.body(axum::body::Body::from(body_bytes)).map_err(|e| {
89-
error!("Failed to build response: {}", e);
90-
ProxyError::ResponseBuildFailed(e.to_string())
91-
})?;
90+
let final_response = builder
91+
.body(axum::body::Body::from(body_bytes))
92+
.map_err(|e| {
93+
error!("Failed to build response: {}", e);
94+
ProxyError::ResponseBuildFailed(e.to_string())
95+
})?;
9296

9397
Ok(final_response)
9498
}

src/proxy/ws_proxy.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ impl WsProxy {
1212
client_ws: WebSocket,
1313
downstream_url: &str,
1414
) -> Result<(), WsProxyError> {
15-
info!("Establishing connection to downstream WebSocket: {}", downstream_url);
15+
info!(
16+
"Establishing connection to downstream WebSocket: {}",
17+
downstream_url
18+
);
1619

1720
// Connect to downstream WebSocket server
18-
let (downstream_ws, _) = connect_async(downstream_url)
19-
.await
20-
.map_err(|e| {
21-
error!("Failed to connect to downstream WebSocket: {}", e);
22-
WsProxyError::ConnectionFailed(e.to_string())
23-
})?;
21+
let (downstream_ws, _) = connect_async(downstream_url).await.map_err(|e| {
22+
error!("Failed to connect to downstream WebSocket: {}", e);
23+
WsProxyError::ConnectionFailed(e.to_string())
24+
})?;
2425

2526
info!("Successfully connected to downstream WebSocket");
2627

@@ -32,7 +33,10 @@ impl WsProxy {
3233
while let Some(msg) = client_read.next().await {
3334
match msg {
3435
Ok(Message::Text(buffer)) => {
35-
info!("Client -> Downstream: Text message ({} bytes)", buffer.len());
36+
info!(
37+
"Client -> Downstream: Text message ({} bytes)",
38+
buffer.len()
39+
);
3640
if let Err(e) = downstream_write
3741
.send(TungsteniteMessage::Text(buffer.to_string()))
3842
.await
@@ -42,7 +46,10 @@ impl WsProxy {
4246
}
4347
}
4448
Ok(Message::Binary(data)) => {
45-
info!("Client -> Downstream: Binary message ({} bytes)", data.len());
49+
info!(
50+
"Client -> Downstream: Binary message ({} bytes)",
51+
data.len()
52+
);
4653
if let Err(e) = downstream_write
4754
.send(TungsteniteMessage::Binary(data.into()))
4855
.await
@@ -96,7 +103,10 @@ impl WsProxy {
96103
}
97104
}
98105
Ok(TungsteniteMessage::Binary(data)) => {
99-
info!("Downstream -> Client: Binary message ({} bytes)", data.len());
106+
info!(
107+
"Downstream -> Client: Binary message ({} bytes)",
108+
data.len()
109+
);
100110
if let Err(e) = client_write.send(Message::Binary(data.into())).await {
101111
error!("Failed to forward binary message to client: {}", e);
102112
break;
@@ -153,4 +163,3 @@ pub enum WsProxyError {
153163
#[error("Failed to connect to downstream WebSocket: {0}")]
154164
ConnectionFailed(String),
155165
}
156-

0 commit comments

Comments
 (0)