Improved error responses
This commit is contained in:
parent
0378d2d90c
commit
516e618f93
28
src/ssh.rs
28
src/ssh.rs
|
@ -196,18 +196,23 @@ impl russh::server::Server for Server {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, hyper::Error> {
|
|
||||||
Full::new(chunk.into())
|
|
||||||
.map_err(|never| match never {})
|
|
||||||
.boxed()
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Service<Request<Incoming>> for Tunnels {
|
impl Service<Request<Incoming>> for Tunnels {
|
||||||
type Response = Response<BoxBody<Bytes, hyper::Error>>;
|
type Response = Response<BoxBody<Bytes, hyper::Error>>;
|
||||||
type Error = hyper::Error;
|
type Error = hyper::Error;
|
||||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
|
||||||
|
|
||||||
fn call(&self, req: Request<Incoming>) -> Self::Future {
|
fn call(&self, req: Request<Incoming>) -> Self::Future {
|
||||||
|
fn response(
|
||||||
|
status_code: StatusCode,
|
||||||
|
body: impl Into<String>,
|
||||||
|
) -> Response<BoxBody<Bytes, hyper::Error>> {
|
||||||
|
Response::builder()
|
||||||
|
.status(status_code)
|
||||||
|
.body(Full::new(Bytes::from(body.into())))
|
||||||
|
.unwrap()
|
||||||
|
.map(|b| b.map_err(|never| match never {}).boxed())
|
||||||
|
}
|
||||||
|
|
||||||
trace!(?req);
|
trace!(?req);
|
||||||
|
|
||||||
let Some(authority) = req
|
let Some(authority) = req
|
||||||
|
@ -221,8 +226,7 @@ impl Service<Request<Incoming>> for Tunnels {
|
||||||
.map(|h| h.to_str().unwrap().to_owned())
|
.map(|h| h.to_str().unwrap().to_owned())
|
||||||
})
|
})
|
||||||
else {
|
else {
|
||||||
let mut resp = Response::new(full("Missing authority or host header"));
|
let resp = response(StatusCode::BAD_REQUEST, "Missing authority or host header");
|
||||||
*resp.status_mut() = StatusCode::BAD_REQUEST;
|
|
||||||
|
|
||||||
return Box::pin(async { Ok(resp) });
|
return Box::pin(async { Ok(resp) });
|
||||||
};
|
};
|
||||||
|
@ -232,8 +236,7 @@ impl Service<Request<Incoming>> for Tunnels {
|
||||||
let tunnels = self.clone();
|
let tunnels = self.clone();
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let Some(tunnel) = tunnels.get_tunnel(&authority).await else {
|
let Some(tunnel) = tunnels.get_tunnel(&authority).await else {
|
||||||
let mut resp = Response::new(full(format!("Unknown tunnel: {authority}")));
|
let resp = response(StatusCode::NOT_FOUND, "Unknown tunnel");
|
||||||
*resp.status_mut() = StatusCode::NOT_FOUND;
|
|
||||||
|
|
||||||
return Ok::<_, hyper::Error>(resp);
|
return Ok::<_, hyper::Error>(resp);
|
||||||
};
|
};
|
||||||
|
@ -242,9 +245,8 @@ impl Service<Request<Incoming>> for Tunnels {
|
||||||
let channel = match tunnel.open_tunnel().await {
|
let channel = match tunnel.open_tunnel().await {
|
||||||
Ok(channel) => channel,
|
Ok(channel) => channel,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!("Failed to tunnel: {err}");
|
warn!("Failed to open tunnel: {err}");
|
||||||
let mut resp = Response::new(full("Failed to open tunnel"));
|
let resp = response(StatusCode::INTERNAL_SERVER_ERROR, "Failed to open tunnel");
|
||||||
*resp.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
|
||||||
|
|
||||||
return Ok::<_, hyper::Error>(resp);
|
return Ok::<_, hyper::Error>(resp);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user