Skip to content

Commit

Permalink
feat: support of Gateway Subdomain spec (#546)
Browse files Browse the repository at this point in the history
* [feat] Support subdomains

* pr: fixes

* pr: fix test

* fix: remove mut in iroh/src/run.rs
  • Loading branch information
ppodolsky authored Dec 15, 2022
1 parent f457b80 commit dfe3134
Show file tree
Hide file tree
Showing 28 changed files with 883 additions and 324 deletions.
2 changes: 1 addition & 1 deletion iroh-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl Api {
self.client.check().await
}

pub async fn watch(&self) -> LocalBoxStream<'static, iroh_rpc_client::StatusTable> {
pub async fn watch(&self) -> LocalBoxStream<'static, StatusTable> {
self.client.clone().watch().await.boxed_local()
}

Expand Down
11 changes: 4 additions & 7 deletions iroh-bitswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,22 +545,19 @@ impl<S: Store> NetworkBehaviour for Bitswap<S> {
{
// Do not bother trying to dial these for now.
if let Err(err) =
response.send(Err(format!("dial:{}: undialable peer", id)))
response.send(Err(format!("dial:{id}: undialable peer")))
{
debug!("dial:{}: failed to send dial response {:?}", id, err)
debug!("dial:{id}: failed to send dial response {err:?}")
}
continue;
}
_ => {
if self.pause_dialing {
// already connected
if let Err(err) =
response.send(Err(format!("dial:{}: dialing paused", id)))
response.send(Err(format!("dial:{id}: dialing paused")))
{
debug!(
"dial:{}: failed to send dial response {:?}",
id, err
)
debug!("dial:{id}: failed to send dial response {err:?}",)
}
continue;
}
Expand Down
8 changes: 1 addition & 7 deletions iroh-bitswap/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,7 @@ impl Default for MessageSenderConfig {
fn send_timeout(size: usize) -> Duration {
let mut timeout = SEND_LATENCY;
timeout += Duration::from_secs(size as u64 / MIN_SEND_RATE);
if timeout > MAX_SEND_TIMEOUT {
MAX_SEND_TIMEOUT
} else if timeout < MIN_SEND_TIMEOUT {
MIN_SEND_TIMEOUT
} else {
timeout
}
timeout.clamp(MIN_SEND_TIMEOUT, MAX_SEND_TIMEOUT)
}

#[derive(Debug)]
Expand Down
17 changes: 8 additions & 9 deletions iroh-gateway/src/bad_bits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cid::Cid;
use serde::{de, Deserialize, Deserializer, Serialize};
use sha2::{Digest, Sha256};
use std::{collections::HashSet, str::FromStr, sync::Arc, time::Duration};
use std::{collections::HashSet, sync::Arc, time::Duration};
use tokio::{sync::RwLock, task::JoinHandle};
use tracing::{debug, log::error};

Expand Down Expand Up @@ -42,12 +42,8 @@ impl BadBits {
self.denylist = denylist;
}

pub fn is_bad(&self, cid: &str, path: &str) -> bool {
let cid = match Cid::from_str(cid) {
Ok(cid) => cid,
Err(_) => return false,
};
let hash = BadBits::to_anchor(cid, path);
pub fn is_bad(&self, cid: &Cid, path: &str) -> bool {
let hash = BadBits::to_anchor(*cid, path);
self.denylist.contains(&hash)
}

Expand Down Expand Up @@ -106,15 +102,17 @@ pub fn spawn_bad_bits_updater(bad_bits: Arc<Option<RwLock<BadBits>>>) -> Option<

#[cfg(test)]
mod tests {
use crate::config::Config;
use std::str::FromStr;

use super::*;
use hex_literal::hex;
use http::StatusCode;
use iroh_resolver::dns_resolver::Config as DnsResolverConfig;
use iroh_rpc_client::{Client as RpcClient, Config as RpcClientConfig};
use iroh_unixfs::content_loader::{FullLoader, FullLoaderConfig, GatewayUrl};

use super::*;
use crate::config::Config;

#[tokio::test]
async fn bad_bits_anchor() {
let cid =
Expand Down Expand Up @@ -186,6 +184,7 @@ mod tests {
channels: Some(1),
},
);
config.redirect_to_subdomain = false;
config.set_default_headers();

let rpc_addr = "irpc://0.0.0.0:0".parse().unwrap();
Expand Down
15 changes: 13 additions & 2 deletions iroh-gateway/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tokio_util::io::ReaderStream;
use tracing::{info, warn};

use crate::response::ResponseFormat;
use crate::{constants::RECURSION_LIMIT, handlers::GetParams};
use crate::{constants::RECURSION_LIMIT, handler_params::GetParams};

#[derive(Debug, Clone)]
pub struct Client<T: ContentLoader> {
Expand Down Expand Up @@ -231,13 +231,24 @@ impl<T: ContentLoader> Client<T> {
}

#[derive(Debug, Clone)]
pub struct Request {
pub struct IpfsRequest {
pub format: ResponseFormat,
pub cid: CidOrDomain,
pub resolved_path: iroh_resolver::resolver::Path,
pub query_file_name: String,
pub download: bool,
pub query_params: GetParams,
pub subdomain_mode: bool,
}

impl IpfsRequest {
pub fn request_path_for_redirection(&self) -> String {
if self.subdomain_mode {
self.resolved_path.to_relative_string()
} else {
self.resolved_path.to_string()
}
}
}

async fn fetch_car_recursive<T, W>(
Expand Down
9 changes: 9 additions & 0 deletions iroh-gateway/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ pub struct Config {
/// set of user provided headers to attach to all responses
#[serde(with = "http_serde::header_map")]
pub headers: HeaderMap,
/// Redirects to subdomains for path requests
#[serde(default)]
pub redirect_to_subdomain: bool,
}

impl Config {
Expand All @@ -62,6 +65,7 @@ impl Config {
indexer_endpoint: None,
metrics: MetricsConfig::default(),
use_denylist: false,
redirect_to_subdomain: false,
}
}

Expand Down Expand Up @@ -116,6 +120,7 @@ impl Default for Config {
indexer_endpoint: None,
metrics: MetricsConfig::default(),
use_denylist: false,
redirect_to_subdomain: false,
};
t.set_default_headers();
t
Expand Down Expand Up @@ -166,6 +171,10 @@ impl crate::handlers::StateConfig for Config {
fn user_headers(&self) -> &HeaderMap<HeaderValue> {
&self.headers
}

fn redirect_to_subdomain(&self) -> bool {
self.redirect_to_subdomain
}
}

fn collect_headers(headers: &HeaderMap) -> Result<Map<String, Value>, ConfigError> {
Expand Down
2 changes: 2 additions & 0 deletions iroh-gateway/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use axum::http::{header::HeaderName, HeaderValue};

// Headers
pub static HEADER_X_FORWARDED_HOST: HeaderName = HeaderName::from_static("x-forwarded-host");
pub static HEADER_X_FORWARDED_PROTO: HeaderName = HeaderName::from_static("x-forwarded-proto");
pub static HEADER_X_IPFS_PATH: HeaderName = HeaderName::from_static("x-ipfs-path");
pub static HEADER_X_CONTENT_TYPE_OPTIONS: HeaderName =
HeaderName::from_static("x-content-type-options");
Expand Down
Loading

0 comments on commit dfe3134

Please sign in to comment.