This commit is contained in:
Aiden McClelland
2023-06-15 11:47:02 -06:00
committed by Aiden McClelland
parent c1fe8e583f
commit ab3f5956d4
5 changed files with 97 additions and 30 deletions

View File

@@ -1,3 +1,4 @@
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@@ -59,7 +60,14 @@ async fn setup_or_init(cfg_path: Option<PathBuf>) -> Result<(), Error> {
let ctx = InstallContext::init(cfg_path).await?; let ctx = InstallContext::init(cfg_path).await?;
let server = WebServer::install(([0, 0, 0, 0], 80).into(), ctx.clone()).await?; let server = WebServer::install(
[
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 80),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 80),
],
ctx.clone(),
)
.await?;
tokio::time::sleep(Duration::from_secs(1)).await; // let the record state that I hate this tokio::time::sleep(Duration::from_secs(1)).await; // let the record state that I hate this
CHIME.play().await?; CHIME.play().await?;
@@ -81,7 +89,14 @@ async fn setup_or_init(cfg_path: Option<PathBuf>) -> Result<(), Error> {
{ {
let ctx = SetupContext::init(cfg_path).await?; let ctx = SetupContext::init(cfg_path).await?;
let server = WebServer::setup(([0, 0, 0, 0], 80).into(), ctx.clone()).await?; let server = WebServer::setup(
[
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 80),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 80),
],
ctx.clone(),
)
.await?;
tokio::time::sleep(Duration::from_secs(1)).await; // let the record state that I hate this tokio::time::sleep(Duration::from_secs(1)).await; // let the record state that I hate this
CHIME.play().await?; CHIME.play().await?;
@@ -192,7 +207,14 @@ async fn inner_main(cfg_path: Option<PathBuf>) -> Result<Option<Shutdown>, Error
) )
.await?; .await?;
let server = WebServer::diagnostic(([0, 0, 0, 0], 80).into(), ctx.clone()).await?; let server = WebServer::diagnostic(
[
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 80),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 80),
],
ctx.clone(),
)
.await?;
let shutdown = ctx.shutdown.subscribe().recv().await.unwrap(); let shutdown = ctx.shutdown.subscribe().recv().await.unwrap();

View File

@@ -1,3 +1,4 @@
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
@@ -26,7 +27,14 @@ async fn inner_main(cfg_path: Option<PathBuf>) -> Result<Option<Shutdown>, Error
) )
.await?; .await?;
embassy::hostname::sync_hostname(&rpc_ctx.account.read().await.hostname).await?; embassy::hostname::sync_hostname(&rpc_ctx.account.read().await.hostname).await?;
let server = WebServer::main(([0, 0, 0, 0], 80).into(), rpc_ctx.clone()).await?; let server = WebServer::main(
[
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 80),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 80),
],
rpc_ctx.clone(),
)
.await?;
let mut shutdown_recv = rpc_ctx.shutdown.subscribe(); let mut shutdown_recv = rpc_ctx.shutdown.subscribe();
@@ -141,8 +149,14 @@ fn main() {
) )
.await?; .await?;
let server = let server = WebServer::diagnostic(
WebServer::diagnostic(([0, 0, 0, 0], 80).into(), ctx.clone()).await?; [
SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 80),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 80),
],
ctx.clone(),
)
.await?;
let mut shutdown = ctx.shutdown.subscribe(); let mut shutdown = ctx.shutdown.subscribe();

View File

@@ -46,7 +46,7 @@ use crate::s9pk::reader::S9pkReader;
use crate::status::{MainStatus, Status}; use crate::status::{MainStatus, Status};
use crate::util::io::{copy_and_shutdown, response_to_reader}; use crate::util::io::{copy_and_shutdown, response_to_reader};
use crate::util::serde::{display_serializable, Port}; use crate::util::serde::{display_serializable, Port};
use crate::util::{assure_send, display_none, AsyncFileExt, Version}; use crate::util::{display_none, AsyncFileExt, Version};
use crate::version::{Current, VersionT}; use crate::version::{Current, VersionT};
use crate::volume::{asset_dir, script_dir}; use crate::volume::{asset_dir, script_dir};
use crate::{Error, ErrorKind, ResultExt}; use crate::{Error, ErrorKind, ResultExt};
@@ -474,12 +474,17 @@ pub async fn sideload(
} }
}); });
recv.await; if let Ok(_) = recv.await {
Response::builder()
Response::builder() .status(StatusCode::OK)
.status(StatusCode::OK) .body(Body::empty())
.body(Body::empty()) .with_kind(ErrorKind::Network)
.with_kind(ErrorKind::Network) } else {
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from("installation aborted before upload completed"))
.with_kind(ErrorKind::Network)
}
} }
.boxed() .boxed()
}); });

View File

@@ -1,6 +1,6 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::convert::Infallible; use std::convert::Infallible;
use std::net::{IpAddr, SocketAddr}; use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::str::FromStr; use std::str::FromStr;
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
@@ -88,9 +88,15 @@ struct VHostServer {
impl VHostServer { impl VHostServer {
async fn new(port: u16, ssl: Arc<SslManager>) -> Result<Self, Error> { async fn new(port: u16, ssl: Arc<SslManager>) -> Result<Self, Error> {
// check if port allowed // check if port allowed
let listener = TcpListener::bind(SocketAddr::new([0, 0, 0, 0].into(), port)) let listener = TcpListener::bind(
.await [
.with_kind(crate::ErrorKind::Network)?; SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 80),
SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 80),
]
.as_ref(),
)
.await
.with_kind(crate::ErrorKind::Network)?;
let mapping = Arc::new(RwLock::new(BTreeMap::new())); let mapping = Arc::new(RwLock::new(BTreeMap::new()));
Ok(Self { Ok(Self {
mapping: Arc::downgrade(&mapping), mapping: Arc::downgrade(&mapping),

View File

@@ -4,8 +4,10 @@ use std::net::SocketAddr;
use futures::future::ready; use futures::future::ready;
use futures::FutureExt; use futures::FutureExt;
use helpers::NonDetachingJoinHandle; use helpers::NonDetachingJoinHandle;
use hyper::server::conn::AddrIncoming;
use hyper::service::{make_service_fn, service_fn}; use hyper::service::{make_service_fn, service_fn};
use hyper::Server; use hyper::Server;
use tokio::net::TcpListener;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use crate::context::{DiagnosticContext, InstallContext, RpcContext, SetupContext}; use crate::context::{DiagnosticContext, InstallContext, RpcContext, SetupContext};
@@ -20,17 +22,23 @@ pub struct WebServer {
thread: NonDetachingJoinHandle<()>, thread: NonDetachingJoinHandle<()>,
} }
impl WebServer { impl WebServer {
pub fn new(bind: SocketAddr, router: HttpHandler) -> Self { pub fn new(
bind: impl AsRef<[SocketAddr]> + Send + Sync + 'static,
router: HttpHandler,
) -> Self {
let (shutdown, shutdown_recv) = oneshot::channel(); let (shutdown, shutdown_recv) = oneshot::channel();
let thread = NonDetachingJoinHandle::from(tokio::spawn(async move { let thread = NonDetachingJoinHandle::from(tokio::spawn(async move {
let server = Server::bind(&bind) let server = Server::builder(
.http1_preserve_header_case(true) AddrIncoming::from_listener(TcpListener::bind(bind.as_ref()).await.unwrap())
.http1_title_case_headers(true) .unwrap(),
.serve(make_service_fn(move |_| { )
let router = router.clone(); .http1_preserve_header_case(true)
ready(Ok::<_, Infallible>(service_fn(move |req| router(req)))) .http1_title_case_headers(true)
})) .serve(make_service_fn(move |_| {
.with_graceful_shutdown(shutdown_recv.map(|_| ())); let router = router.clone();
ready(Ok::<_, Infallible>(service_fn(move |req| router(req))))
}))
.with_graceful_shutdown(shutdown_recv.map(|_| ()));
if let Err(e) = server.await { if let Err(e) = server.await {
tracing::error!("Spawning hyper server error: {}", e); tracing::error!("Spawning hyper server error: {}", e);
} }
@@ -43,19 +51,31 @@ impl WebServer {
self.thread.await.unwrap() self.thread.await.unwrap()
} }
pub async fn main(bind: SocketAddr, ctx: RpcContext) -> Result<Self, Error> { pub async fn main(
bind: impl AsRef<[SocketAddr]> + Send + Sync + 'static,
ctx: RpcContext,
) -> Result<Self, Error> {
Ok(Self::new(bind, main_ui_server_router(ctx).await?)) Ok(Self::new(bind, main_ui_server_router(ctx).await?))
} }
pub async fn setup(bind: SocketAddr, ctx: SetupContext) -> Result<Self, Error> { pub async fn setup(
bind: impl AsRef<[SocketAddr]> + Send + Sync + 'static,
ctx: SetupContext,
) -> Result<Self, Error> {
Ok(Self::new(bind, setup_ui_file_router(ctx).await?)) Ok(Self::new(bind, setup_ui_file_router(ctx).await?))
} }
pub async fn diagnostic(bind: SocketAddr, ctx: DiagnosticContext) -> Result<Self, Error> { pub async fn diagnostic(
bind: impl AsRef<[SocketAddr]> + Send + Sync + 'static,
ctx: DiagnosticContext,
) -> Result<Self, Error> {
Ok(Self::new(bind, diag_ui_file_router(ctx).await?)) Ok(Self::new(bind, diag_ui_file_router(ctx).await?))
} }
pub async fn install(bind: SocketAddr, ctx: InstallContext) -> Result<Self, Error> { pub async fn install(
bind: impl AsRef<[SocketAddr]> + Send + Sync + 'static,
ctx: InstallContext,
) -> Result<Self, Error> {
Ok(Self::new(bind, install_ui_file_router(ctx).await?)) Ok(Self::new(bind, install_ui_file_router(ctx).await?))
} }
} }