Skip to content

Commit

Permalink
fix(deps): update code for new flexible-hyper-server-tls version
Browse files Browse the repository at this point in the history
Signed-off-by: MichaIng <micha@dietpi.com>
  • Loading branch information
MichaIng committed Jun 13, 2024
1 parent 175a4f0 commit 63d74fc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
49 changes: 21 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#![allow(clippy::too_many_lines)]
use crate::shared::CONFIG;
use anyhow::Context;
use hyper::service::{make_service_fn, service_fn};
use hyper::service::service_fn;
use std::{net::IpAddr, str::FromStr};

mod config;
Expand Down Expand Up @@ -36,43 +36,36 @@ async fn main() -> anyhow::Result<()> {
.await
.with_context(|| format!("Couldn't bind to {}", &addr))?;

let make_svc = make_service_fn(|conn: &flexible_hyper_server_tls::HttpOrHttpsConnection| {
let remote_addr = conn.remote_addr();
async move {
Ok::<_, std::convert::Infallible>(service_fn(move |req| async move {
let span = tracing::info_span!("request", %remote_addr);
span.in_scope(|| {
tracing::info!("Request to {}", req.uri().path());
tracing::debug!(
"using {:?}",
req.headers()
.get(hyper::header::USER_AGENT)
.map_or("unknown", |x| x.to_str().unwrap_or("unknown"))
);
});
routes::router(req, span).await
}))
}
});
let builder = flexible_hyper_server_tls::AcceptorBuilder::new(tcp);

let acceptor = if CONFIG.tls {
let tls_acceptor = flexible_hyper_server_tls::tlsconfig::get_tlsacceptor_from_files(
let tls_acceptor = flexible_hyper_server_tls::rustls_helpers::get_tlsacceptor_from_files(
&CONFIG.cert,
&CONFIG.key,
flexible_hyper_server_tls::tlsconfig::HttpProtocol::Http1,
)
.context("Couldn't get TLS config")?;

flexible_hyper_server_tls::HyperHttpOrHttpsAcceptor::new_https(
tcp,
tls_acceptor,
std::time::Duration::from_secs(10),
)
builder.https(tls_acceptor).build()
} else {
flexible_hyper_server_tls::HyperHttpOrHttpsAcceptor::new_http(tcp)
builder.build()
};

let mut server = hyper::server::Server::builder(acceptor).serve(make_svc);
acceptor
.serve(service_fn(move |req| async move {
let remote_addr = conn.remote_addr();
let span = tracing::info_span!("request", %remote_addr);
span.in_scope(|| {
tracing::info!("Request to {}", req.uri().path());
tracing::debug!(
"using {:?}",
req.headers()
.get(hyper::header::USER_AGENT)
.map_or("unknown", |x| x.to_str().unwrap_or("unknown"))
);
});
routes::router(req, span).await
}))
.await;

// Ignore result, because it will never be an error
loop {
Expand Down
17 changes: 9 additions & 8 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use crate::handle_error;
use crate::shared::CONFIG;
use anyhow::Context;
use futures_util::Future;
use hyper::http::{header, HeaderValue};
use hyper::{Body, Method, Request, Response, StatusCode};
use hyper::body::Body;
use hyper::header;
use hyper::{Method, Request, Response, StatusCode};
use ring::digest;
use tracing::Instrument;

Expand Down Expand Up @@ -90,10 +91,10 @@ fn asset_route(path: &str) -> Response<Body> {
}

let mime_type = match path.rsplit_once('.').unwrap_or(("plain", "plain")).1 {
"js" => HeaderValue::from_static("text/javascript"),
"svg" => HeaderValue::from_static("image/svg+xml"),
"png" => HeaderValue::from_static("image/png"),
"css" => HeaderValue::from_static("text/css"),
"js" => header::HeaderValue::from_static("text/javascript"),
"svg" => header::HeaderValue::from_static("image/svg+xml"),
"png" => header::HeaderValue::from_static("image/png"),
"css" => header::HeaderValue::from_static("text/css"),
// There should be no other file types
_ => unreachable!(),
};
Expand All @@ -111,7 +112,7 @@ pub async fn login_route(mut req: Request<Body>) -> anyhow::Result<Response<Body
let mut response = Response::new(Body::empty());
response.headers_mut().insert(
header::ACCESS_CONTROL_ALLOW_ORIGIN,
HeaderValue::from_static("*"),
header::HeaderValue::from_static("*"),
);
if CONFIG.pass {
let shasum = hex::encode(
Expand Down Expand Up @@ -226,7 +227,7 @@ where
.and_then(|x| x.to_str().ok())
.map_or(false, |x| x.contains("websocket"))
&& req.headers().get(header::SEC_WEBSOCKET_VERSION)
== Some(&HeaderValue::from_static("13")))
== Some(&header::HeaderValue::from_static("13")))
{
return Ok(Response::builder()
.status(StatusCode::BAD_REQUEST)
Expand Down
2 changes: 1 addition & 1 deletion src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn get_token_from_list<'a>(
None
}

pub fn get_fingerprint(req: &hyper::Request<hyper::Body>) -> anyhow::Result<Option<String>> {
pub fn get_fingerprint(req: &hyper::Request<hyper::body::Body>) -> anyhow::Result<Option<String>> {
let cookie = if let Some(cookie) = req.headers().get(hyper::header::COOKIE) {
cookie.to_str().context("Invalid cookie list")?
} else {
Expand Down

0 comments on commit 63d74fc

Please # to comment.