Skip to content

Commit

Permalink
feat: added cors header to allow access from anywhere (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
gh0st42 authored Mar 7, 2024
1 parent f6b921a commit 02cb223
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
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 <baumgaertner@cs.tu-darmstadt.de>"]
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

0 comments on commit 02cb223

Please sign in to comment.