Compare commits

..

1 commit

Author SHA1 Message Date
cf133283e4
feat: include user-agent in request headers 2024-11-19 21:54:50 +05:30
3 changed files with 18 additions and 4 deletions

View file

@ -12,7 +12,7 @@ use crate::{
}; };
use bytes::Bytes; use bytes::Bytes;
use futures::StreamExt; use futures::StreamExt;
use http::StatusCode; use http::{header::USER_AGENT, StatusCode};
use httpdate::fmt_http_date; use httpdate::fmt_http_date;
use itertools::Itertools; use itertools::Itertools;
use reqwest::{ use reqwest::{
@ -39,6 +39,7 @@ pub struct SendActivityTask {
pub(crate) inbox: Url, pub(crate) inbox: Url,
pub(crate) private_key: RsaPrivateKey, pub(crate) private_key: RsaPrivateKey,
pub(crate) http_signature_compat: bool, pub(crate) http_signature_compat: bool,
pub(crate) user_agent: String,
} }
impl Display for SendActivityTask { impl Display for SendActivityTask {
@ -83,7 +84,7 @@ impl SendActivityTask {
let request_builder = client let request_builder = client
.post(self.inbox.to_string()) .post(self.inbox.to_string())
.timeout(timeout) .timeout(timeout)
.headers(generate_request_headers(&self.inbox)); .headers(generate_request_headers(&self.inbox, &self.user_agent));
let request = sign_request( let request = sign_request(
request_builder, request_builder,
&self.actor_id, &self.actor_id,
@ -173,6 +174,7 @@ where
activity: activity_serialized.clone(), activity: activity_serialized.clone(),
private_key: private_key.clone(), private_key: private_key.clone(),
http_signature_compat: config.http_signature_compat, http_signature_compat: config.http_signature_compat,
user_agent: config.user_agent().to_owned(),
}) })
}) })
.collect() .collect()
@ -211,7 +213,7 @@ where
.map_err(|e| Error::Other(format!("cloned error: {e}"))) .map_err(|e| Error::Other(format!("cloned error: {e}")))
} }
pub(crate) fn generate_request_headers(inbox_url: &Url) -> HeaderMap { pub(crate) fn generate_request_headers(inbox_url: &Url, user_agent: &str) -> HeaderMap {
let mut host = inbox_url.domain().expect("read inbox domain").to_string(); let mut host = inbox_url.domain().expect("read inbox domain").to_string();
if let Some(port) = inbox_url.port() { if let Some(port) = inbox_url.port() {
host = format!("{}:{}", host, port); host = format!("{}:{}", host, port);
@ -222,6 +224,11 @@ pub(crate) fn generate_request_headers(inbox_url: &Url) -> HeaderMap {
HeaderName::from_static("content-type"), HeaderName::from_static("content-type"),
HeaderValue::from_static(FEDERATION_CONTENT_TYPE), HeaderValue::from_static(FEDERATION_CONTENT_TYPE),
); );
headers.insert(
USER_AGENT,
HeaderValue::from_str(user_agent).expect("Hostname is valid"),
);
headers.insert( headers.insert(
HeaderName::from_static("host"), HeaderName::from_static("host"),
HeaderValue::from_str(&host).expect("Hostname is valid"), HeaderValue::from_str(&host).expect("Hostname is valid"),
@ -230,6 +237,7 @@ pub(crate) fn generate_request_headers(inbox_url: &Url) -> HeaderMap {
"date", "date",
HeaderValue::from_str(&fmt_http_date(SystemTime::now())).expect("Date is valid"), HeaderValue::from_str(&fmt_http_date(SystemTime::now())).expect("Date is valid"),
); );
println!("Generating request headers: {:?}", headers);
headers headers
} }

View file

@ -191,6 +191,11 @@ impl<T: Clone> FederationConfig<T> {
pub fn domain(&self) -> &str { pub fn domain(&self) -> &str {
&self.domain &self.domain
} }
/// Returns user-agent
pub fn user_agent(&self) -> &str {
&self.domain
}
} }
impl<T: Clone> FederationConfigBuilder<T> { impl<T: Clone> FederationConfigBuilder<T> {

View file

@ -11,7 +11,7 @@ use crate::{
FEDERATION_CONTENT_TYPE, FEDERATION_CONTENT_TYPE,
}; };
use bytes::Bytes; use bytes::Bytes;
use http::{HeaderValue, StatusCode}; use http::{header::USER_AGENT, HeaderValue, StatusCode};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use tracing::info; use tracing::info;
@ -115,6 +115,7 @@ async fn fetch_object_http_with_accept<T: Clone, Kind: DeserializeOwned>(
.client .client
.get(url.as_str()) .get(url.as_str())
.header("Accept", content_type) .header("Accept", content_type)
.header(USER_AGENT, data.config.user_agent())
.timeout(config.request_timeout); .timeout(config.request_timeout);
let res = if let Some((actor_id, private_key_pem)) = config.signed_fetch_actor.as_deref() { let res = if let Some((actor_id, private_key_pem)) = config.signed_fetch_actor.as_deref() {