Skip to content

Commit

Permalink
feat: add X-Dragonfly-Use-P2P header for proxy to use P2P to distribu…
Browse files Browse the repository at this point in the history
…te the content (#733)

Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Sep 10, 2024
1 parent 60d11a0 commit d385ef2
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 18 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

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

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ members = [
]

[workspace.package]
version = "0.1.107"
version = "0.1.108"
authors = ["The Dragonfly Developers"]
homepage = "https://d7y.io/"
repository = "https://github.com/dragonflyoss/client.git"
Expand All @@ -22,13 +22,13 @@ readme = "README.md"
edition = "2021"

[workspace.dependencies]
dragonfly-client = { path = "dragonfly-client", version = "0.1.107" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.107" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.107" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.107" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.107" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.107" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.107" }
dragonfly-client = { path = "dragonfly-client", version = "0.1.108" }
dragonfly-client-core = { path = "dragonfly-client-core", version = "0.1.108" }
dragonfly-client-config = { path = "dragonfly-client-config", version = "0.1.108" }
dragonfly-client-storage = { path = "dragonfly-client-storage", version = "0.1.108" }
dragonfly-client-backend = { path = "dragonfly-client-backend", version = "0.1.108" }
dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.108" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.108" }
thiserror = "1.0"
dragonfly-api = "=2.0.154"
reqwest = { version = "0.12.4", features = ["stream", "native-tls", "default-tls", "rustls-tls"] }
Expand Down
21 changes: 21 additions & 0 deletions dragonfly-client/src/proxy/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ pub const DRAGONFLY_REGISTRY_HEADER: &str = "X-Dragonfly-Registry";
// Default value includes the filtered query params of s3, gcs, oss, obs, cos.
pub const DRAGONFLY_FILTERED_QUERY_PARAMS_HEADER: &str = "X-Dragonfly-Filtered-Query-Params";

// DRAGONFLY_USE_P2P_HEADER is the header key of use p2p in http request.
// If the value is "true", the request will use P2P technology to distribute
// the content. If the value is "false", but url matches the regular expression in proxy config.
// The request will also use P2P technology to distribute the content.
pub const DRAGONFLY_USE_P2P_HEADER: &str = "X-Dragonfly-Use-P2P";

// get_tag gets the tag from http header.
#[instrument(skip_all)]
pub fn get_tag(header: &HeaderMap) -> Option<String> {
Expand Down Expand Up @@ -123,3 +129,18 @@ pub fn get_filtered_query_params(
None => default_filtered_query_params,
}
}

// get_use_p2p gets the use p2p from http header.
#[instrument(skip_all)]
pub fn get_use_p2p(header: &HeaderMap) -> bool {
match header.get(DRAGONFLY_USE_P2P_HEADER) {
Some(value) => match value.to_str() {
Ok(value) => value.eq_ignore_ascii_case("true"),
Err(err) => {
error!("get use p2p from header failed: {}", err);
false
}
},
None => false,
}
}
40 changes: 38 additions & 2 deletions dragonfly-client/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub async fn http_handler(
find_matching_rule(config.proxy.rules.clone(), request_uri.to_string().as_str())
{
info!(
"proxy HTTP request via dfdaemon for method: {}, uri: {}",
"proxy HTTP request via dfdaemon by rule config for method: {}, uri: {}",
request.method(),
request_uri
);
Expand All @@ -362,6 +362,24 @@ pub async fn http_handler(
.await;
}

// If the request header contains the X-Dragonfly-Use-P2P header, proxy the request via the
// dfdaemon.
if header::get_use_p2p(request.headers()) {
info!(
"proxy HTTP request via dfdaemon by X-Dragonfly-Use-P2P header for method: {}, uri: {}",
request.method(),
request_uri
);
return proxy_by_dfdaemon(
config,
task,
Rule::default(),
request,
dfdaemon_download_client,
)
.await;
}

if request.uri().scheme().cloned() == Some(http::uri::Scheme::HTTPS) {
info!(
"proxy HTTPS request directly to remote server for method: {}, uri: {}",
Expand Down Expand Up @@ -512,7 +530,7 @@ pub async fn upgraded_handler(
find_matching_rule(config.proxy.rules.clone(), request_uri.to_string().as_str())
{
info!(
"proxy HTTPS request via dfdaemon for method: {}, uri: {}",
"proxy HTTPS request via dfdaemon by rule config for method: {}, uri: {}",
request.method(),
request_uri
);
Expand All @@ -526,6 +544,24 @@ pub async fn upgraded_handler(
.await;
}

// If the request header contains the X-Dragonfly-Use-P2P header, proxy the request via the
// dfdaemon.
if header::get_use_p2p(request.headers()) {
info!(
"proxy HTTP request via dfdaemon by X-Dragonfly-Use-P2P header for method: {}, uri: {}",
request.method(),
request_uri
);
return proxy_by_dfdaemon(
config,
task,
Rule::default(),
request,
dfdaemon_download_client,
)
.await;
}

if request.uri().scheme().cloned() == Some(http::uri::Scheme::HTTPS) {
info!(
"proxy HTTPS request directly to remote server for method: {}, uri: {}",
Expand Down

0 comments on commit d385ef2

Please sign in to comment.