Skip to content

Commit 63717bb

Browse files
committed
feat: preserve full request path in WebSocket proxy and fix Docker configurations
Signed-off-by: Xin Liu <[email protected]>
1 parent f2058e7 commit 63717bb

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

docker-compose.test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ services:
1919
# JSON API 测试服务(替代 jsonplaceholder)
2020
# 使用 json-server 提供类似的 REST API
2121
json-api:
22-
image: clue/json-server:latest
22+
image: williamyeh/json-server:latest # clue/json-server:latest
2323
container_name: ss-proxy-test-json
2424
ports:
2525
- "8889:80"
@@ -41,13 +41,13 @@ services:
4141
image: python:3.11-alpine
4242
container_name: ss-proxy-test-ws
4343
ports:
44-
- "8890:8890"
44+
- "8890:8080"
4545
volumes:
4646
- ./tests/mock-data/ws-echo.py:/app/ws-echo.py:ro
4747
working_dir: /app
4848
command: sh -c "pip install --no-cache-dir websockets && python ws-echo.py"
4949
healthcheck:
50-
test: ["CMD", "sh", "-c", "timeout 5 sh -c 'cat < /dev/null > /dev/tcp/localhost/8890' || exit 1"]
50+
test: ["CMD", "sh", "-c", "timeout 5 sh -c 'cat < /dev/null > /dev/tcp/localhost/8080' || exit 1"]
5151
interval: 10s
5252
timeout: 5s
5353
retries: 3

src/handlers/websocket.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use axum::{
22
extract::{
3-
Path, State,
3+
Request, State,
44
ws::{WebSocket, WebSocketUpgrade},
55
},
66
http::StatusCode,
@@ -15,13 +15,28 @@ use crate::{db, proxy::WsProxy};
1515
/// WebSocket proxy handler
1616
pub async fn websocket_handler(
1717
State(pool): State<Arc<SqlitePool>>,
18-
Path(session_id): Path<String>,
1918
ws: WebSocketUpgrade,
19+
req: Request,
2020
) -> Result<Response, StatusCode> {
21-
info!(
22-
"Received WebSocket connection request: session_id={}",
23-
session_id
24-
);
21+
// Extract full path from request
22+
let full_path = req.uri().path().to_string();
23+
24+
info!("Received WebSocket connection request: path={}", full_path);
25+
26+
// Extract session_id from path (format: /ws/{session_id} or /{session_id})
27+
let session_id = full_path
28+
.trim_start_matches('/')
29+
.split('/')
30+
.nth(1) // Get the second segment (after 'ws')
31+
.unwrap_or("")
32+
.to_string();
33+
34+
if session_id.is_empty() {
35+
warn!("Invalid WebSocket path: {}", full_path);
36+
return Err(StatusCode::BAD_REQUEST);
37+
}
38+
39+
info!("Extracted session_id: {}", session_id);
2540

2641
// 1. Query database to get session information
2742
let session = match db::get_session(&pool, &session_id).await {
@@ -41,11 +56,11 @@ pub async fn websocket_handler(
4156
return Err(StatusCode::SERVICE_UNAVAILABLE);
4257
}
4358

44-
// 3. Convert downstream URL to WebSocket format and append session_id to path
59+
// 3. Convert downstream URL to WebSocket format and append full path
4560
let downstream_ws_url = format!(
46-
"{}/{}",
61+
"{}{}",
4762
convert_to_ws_url(&session.downstream_server_url).trim_end_matches('/'),
48-
session_id
63+
full_path
4964
);
5065
info!("Downstream WebSocket URL: {}", downstream_ws_url);
5166

0 commit comments

Comments
 (0)