remove wsproxy panics

This commit is contained in:
Toshit Chawda 2025-04-05 22:02:13 -07:00
parent 97fcb94ba6
commit 4920b81f5e
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D

View file

@ -27,25 +27,35 @@ pub async fn handle_wsproxy(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
if udp && !CONFIG.stream.allow_wsproxy_udp { if udp && !CONFIG.stream.allow_wsproxy_udp {
let _ = ws let _ = ws
.close(CloseCode::POLICY_VIOLATION.into(), "udp is blocked") .close(CloseCode::POLICY_VIOLATION, "udp is blocked")
.await; .await;
return Ok(()); return Ok(());
} }
let vec: Vec<&str> = path.split('/').last().unwrap().split(':').collect(); let Some(vec) = path
let Ok(port) = FromStr::from_str(vec[1]) else { .split('/')
let _ = ws .next_back()
.close(CloseCode::POLICY_VIOLATION.into(), "invalid port") .map(|x| x.split(':').collect::<Vec<_>>())
.await; else {
let _ = ws.close(CloseCode::POLICY_VIOLATION, "invalid path").await;
return Ok(()); return Ok(());
}; };
let Some(host) = vec.first().map(ToString::to_string) else {
let _ = ws.close(CloseCode::POLICY_VIOLATION, "invalid host").await;
return Ok(());
};
let Some(port) = vec.get(1).and_then(|x| FromStr::from_str(x).ok()) else {
let _ = ws.close(CloseCode::POLICY_VIOLATION, "invalid port").await;
return Ok(());
};
let connect = ConnectPacket { let connect = ConnectPacket {
stream_type: if udp { stream_type: if udp {
StreamType::Udp StreamType::Udp
} else { } else {
StreamType::Tcp StreamType::Tcp
}, },
host: vec[0].to_string(), host,
port, port,
}; };
@ -53,10 +63,7 @@ pub async fn handle_wsproxy(
let Ok(resolved) = ClientStream::resolve(connect).await else { let Ok(resolved) = ClientStream::resolve(connect).await else {
let _ = ws let _ = ws
.close( .close(CloseCode::INTERNAL_SERVER_ERROR, "failed to resolve host")
CloseCode::INTERNAL_SERVER_ERROR.into(),
"failed to resolve host",
)
.await; .await;
return Ok(()); return Ok(());
}; };
@ -66,7 +73,7 @@ pub async fn handle_wsproxy(
let Ok(stream) = ClientStream::connect(connect).await else { let Ok(stream) = ClientStream::connect(connect).await else {
let _ = ws let _ = ws
.close( .close(
CloseCode::INTERNAL_SERVER_ERROR.into(), CloseCode::INTERNAL_SERVER_ERROR,
"failed to connect to host", "failed to connect to host",
) )
.await; .await;
@ -79,7 +86,7 @@ pub async fn handle_wsproxy(
let Ok(stream) = route_wispnet(server, connect).await else { let Ok(stream) = route_wispnet(server, connect).await else {
let _ = ws let _ = ws
.close( .close(
CloseCode::INTERNAL_SERVER_ERROR.into(), CloseCode::INTERNAL_SERVER_ERROR,
"failed to connect to host", "failed to connect to host",
) )
.await; .await;
@ -90,7 +97,7 @@ pub async fn handle_wsproxy(
ResolvedPacket::NoResolvedAddrs => { ResolvedPacket::NoResolvedAddrs => {
let _ = ws let _ = ws
.close( .close(
CloseCode::INTERNAL_SERVER_ERROR.into(), CloseCode::INTERNAL_SERVER_ERROR,
"host did not resolve to any addrs", "host did not resolve to any addrs",
) )
.await; .await;
@ -98,14 +105,14 @@ pub async fn handle_wsproxy(
} }
ResolvedPacket::Blocked => { ResolvedPacket::Blocked => {
let _ = ws let _ = ws
.close(CloseCode::POLICY_VIOLATION.into(), "host is blocked") .close(CloseCode::POLICY_VIOLATION, "host is blocked")
.await; .await;
return Ok(()); return Ok(());
} }
ResolvedPacket::Invalid => { ResolvedPacket::Invalid => {
let _ = ws let _ = ws
.close( .close(
CloseCode::POLICY_VIOLATION.into(), CloseCode::POLICY_VIOLATION,
"invalid host/port/type combination", "invalid host/port/type combination",
) )
.await; .await;
@ -158,12 +165,10 @@ pub async fn handle_wsproxy(
.await; .await;
match ret { match ret {
Ok(()) => { Ok(()) => {
let _ = ws.close(CloseCode::NORMAL_CLOSURE.into(), "").await; let _ = ws.close(CloseCode::NORMAL_CLOSURE, "").await;
} }
Err(x) => { Err(x) => {
let _ = ws let _ = ws.close(CloseCode::NORMAL_CLOSURE, &x.to_string()).await;
.close(CloseCode::NORMAL_CLOSURE.into(), &x.to_string())
.await;
} }
} }
} }
@ -190,12 +195,10 @@ pub async fn handle_wsproxy(
.await; .await;
match ret { match ret {
Ok(()) => { Ok(()) => {
let _ = ws.close(CloseCode::NORMAL_CLOSURE.into(), "").await; let _ = ws.close(CloseCode::NORMAL_CLOSURE, "").await;
} }
Err(x) => { Err(x) => {
let _ = ws let _ = ws.close(CloseCode::NORMAL_CLOSURE, &x.to_string()).await;
.close(CloseCode::NORMAL_CLOSURE.into(), &x.to_string())
.await;
} }
} }
} }
@ -247,19 +250,17 @@ pub async fn handle_wsproxy(
match ret { match ret {
Ok(()) => { Ok(()) => {
let _ = ws.close(CloseCode::NORMAL_CLOSURE.into(), "").await; let _ = ws.close(CloseCode::NORMAL_CLOSURE, "").await;
} }
Err(x) => { Err(x) => {
let _ = ws let _ = ws.close(CloseCode::NORMAL_CLOSURE, &x.to_string()).await;
.close(CloseCode::NORMAL_CLOSURE.into(), &x.to_string())
.await;
} }
} }
} }
ClientStream::NoResolvedAddrs => { ClientStream::NoResolvedAddrs => {
let _ = ws let _ = ws
.close( .close(
CloseCode::INTERNAL_SERVER_ERROR.into(), CloseCode::INTERNAL_SERVER_ERROR,
"host did not resolve to any addrs", "host did not resolve to any addrs",
) )
.await; .await;
@ -267,12 +268,12 @@ pub async fn handle_wsproxy(
} }
ClientStream::Blocked => { ClientStream::Blocked => {
let _ = ws let _ = ws
.close(CloseCode::POLICY_VIOLATION.into(), "host is blocked") .close(CloseCode::POLICY_VIOLATION, "host is blocked")
.await; .await;
} }
ClientStream::Invalid => { ClientStream::Invalid => {
let _ = ws let _ = ws
.close(CloseCode::POLICY_VIOLATION.into(), "host is invalid") .close(CloseCode::POLICY_VIOLATION, "host is invalid")
.await; .await;
} }
} }