diff --git a/Cargo.lock b/Cargo.lock index 455ddfe4..f6ebe090 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -612,6 +612,7 @@ dependencies = [ "tokio-serde", "tokio-tungstenite", "tokio-util", + "tower-http", "tungstenite", "url", ] diff --git a/core/dtn7/Cargo.toml b/core/dtn7/Cargo.toml index 16604d59..c6f60800 100644 --- a/core/dtn7/Cargo.toml +++ b/core/dtn7/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dtn7" -version = "0.20.1" # managed by release.sh +version = "0.20.1" # managed by release.sh authors = ["Lars Baumgaertner "] description = "Rust delay-tolerant-networking daemon and CLI tools implementing Bundle Protocol Version 7 (RFC9171)" edition = "2021" @@ -77,6 +77,7 @@ thiserror = "1.0.31" dtn7-codegen = { path = "../codegen", version = "0.1.2" } sha1 = "0.10.5" glob-match = "0.2.1" +tower-http = { version = "0.3.4", features = ["cors"] } [lib] name = "dtn7" diff --git a/core/dtn7/src/dtnd/httpd.rs b/core/dtn7/src/dtnd/httpd.rs index b11bb643..9e3f6574 100644 --- a/core/dtn7/src/dtnd/httpd.rs +++ b/core/dtn7/src/dtnd/httpd.rs @@ -48,6 +48,8 @@ use std::fmt::Write; use std::net::SocketAddr; use std::time::Instant; use tinytemplate::TinyTemplate; +use tower_http::cors::Any; +use tower_http::cors::CorsLayer; /* #[get("/ws", guard = "fn_guard_localhost")] @@ -733,6 +735,12 @@ async fn download_hex( } pub async fn spawn_httpd() -> Result<()> { + let cors = CorsLayer::new() + // allow `GET` and `POST` when accessing the resource + .allow_methods([http::Method::GET, http::Method::POST, http::Method::DELETE]) + // allow requests from any origin + .allow_origin(Any); + let mut app_local_only = Router::new() .route("/peers/add", get(http_peers_add)) .route("/peers/del", get(http_peers_delete)) @@ -752,7 +760,8 @@ pub async fn spawn_httpd() -> Result<()> { ) .route("/debug/rnd_bundle", get(debug_rnd_bundle)) .route("/debug/rnd_peer", get(debug_rnd_peer)) - .layer(from_extractor::()); + .layer(from_extractor::()) + .layer(cors.clone()); if CONFIG.lock().routing == "external" { app_local_only = app_local_only.route( @@ -791,7 +800,8 @@ pub async fn spawn_httpd() -> Result<()> { .route("/status/bundles/digest", get(status_bundles_digest)) .route("/status/store", get(status_store)) .route("/status/peers", get(status_peers)) - .route("/status/info", get(status_info)); + .route("/status/info", get(status_info)) + .layer(cors.clone()); let port = CONFIG.lock().webport;