Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added cors header to allow access from anywhere for custom web UIs #61

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion core/dtn7/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
description = "Rust delay-tolerant-networking daemon and CLI tools implementing Bundle Protocol Version 7 (RFC9171)"
edition = "2021"
Expand Down Expand Up @@ -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"
Expand Down
14 changes: 12 additions & 2 deletions core/dtn7/src/dtnd/httpd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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))
Expand All @@ -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::<RequireLocalhost>());
.layer(from_extractor::<RequireLocalhost>())
.layer(cors.clone());

if CONFIG.lock().routing == "external" {
app_local_only = app_local_only.route(
Expand Down Expand Up @@ -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;

Expand Down
Loading