Skip to content

Commit

Permalink
wip: "better" error page
Browse files Browse the repository at this point in the history
  • Loading branch information
filipton committed Aug 29, 2024
1 parent 36af85f commit ec91825
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/server/resources/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>

<body>
<h1>ERROR</h1>
<h2>{MSG}</h2>
</body>

</html>
File renamed without changes.
19 changes: 15 additions & 4 deletions src/server/tunnel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use tokio::{
};
use tokio_rustls::{rustls::pki_types, TlsAcceptor, TlsConnector};

const STATIC_HTML: &str = include_str!("./index.html");
const PANEL_HTML: &str = include_str!("./resources/index.html");
const ERROR_HTML: &str = include_str!("./resources/error.html");

pub async fn spawn_tunnel_connector(
remote_addrs: Vec<(&str, bool)>,
Expand Down Expand Up @@ -197,6 +198,16 @@ where
if let Err(_) = tunnel_res {
_ = state.get_tunnel_oneshot(generated_tunnel_id).await;

_ = ::utils::http::write_raw_http_resp(
&mut stream,
404,
"NOT FOUND",
&ERROR_HTML.replace(
"{MSG}",
&format!("Tunnel timeout! REF ID: {generated_tunnel_id}"),
),
)
.await;
tracing::error!("Tunnel timeout");
return Ok(());
}
Expand Down Expand Up @@ -226,7 +237,7 @@ where
stream,
404,
"NOT FOUND",
"That tunnel does not exists!",
&ERROR_HTML.replace("{MSG}", "That tunnel does not exists!"),
)
.await;
anyhow::bail!("Tunnel does not exist!");
Expand All @@ -236,7 +247,7 @@ where
stream,
404,
"NOT FOUND",
"Connector for this tunnel isn't connected!",
&ERROR_HTML.replace("{MSG}", "Connector for this tunnel isn't connected!"),
)
.await;
anyhow::bail!("No connector for tunnel!");
Expand Down Expand Up @@ -310,7 +321,7 @@ where

stream.write_all(response.as_bytes()).await?;
} else if http_header[1] == "/" && http_header[0] == "GET" {
_ = ::utils::http::write_raw_http_resp(stream, 200, "OK", STATIC_HTML).await;
_ = ::utils::http::write_raw_http_resp(stream, 200, "OK", PANEL_HTML).await;
} else {
_ = ::utils::http::write_raw_http_resp(
stream,
Expand Down
2 changes: 2 additions & 0 deletions utils/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ where
Ok(())
}

/// cotent type: text/html
pub fn construct_http_resp(status: u16, status_str: &str, content: &str) -> String {
format!(
"HTTP/1.1 {status} {status_str}\r\n\
Content-Length: {content_len}\r\n\
Content-Type: text/html\r\n\
Connection: close\r\n\
\r\n\
{content}",
Expand Down

0 comments on commit ec91825

Please sign in to comment.