Skip to content

Commit 3b9ab37

Browse files
authored
Merge pull request #723 from IONOS-Productivity/ms/user_agent_support
Allow configuring the User-Agent sent to Nextcloud
2 parents 0512b57 + 7a12c6f commit 3b9ab37

5 files changed

Lines changed: 33 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ variables:
163163
- `DATABASE_PREFIX` database prefix configured in Nextcloud, e.g. `oc_`
164164
- `REDIS_URL` connection url for redis, e.g. `redis://redis_host`
165165
- `NEXTCLOUD_URL` url for the nextcloud instance, e.g. `https://cloud.example.com`
166+
- `USER_AGENT` user agent to send when connecting to the nextcloud instance, no `User-Agent` header is sent if unset
166167

167168
Or you can specify the options as command line arguments, see `notify_push --help` for information about the command
168169
line arguments.

src/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub struct Opt {
8383
/// Disable validating of certificates when connecting to the nextcloud instance
8484
#[clap(long)]
8585
pub allow_self_signed: bool,
86+
/// The user agent to use when connecting to the nextcloud instance
87+
#[clap(long)]
88+
pub user_agent: Option<String>,
8689
/// The path to the nextcloud config file
8790
#[clap(name = "CONFIG_FILE")]
8891
pub config_file: Option<PathBuf>,
@@ -125,6 +128,7 @@ pub struct Config {
125128
pub log_level: String,
126129
pub bind: Bind,
127130
pub allow_self_signed: bool,
131+
pub user_agent: Option<String>,
128132
pub no_ansi: bool,
129133
pub tls: Option<TlsConfig>,
130134
pub max_debounce_time: usize,
@@ -225,6 +229,7 @@ impl TryFrom<PartialConfig> for Config {
225229
log_level: config.log_level.unwrap_or_else(|| String::from("warn")),
226230
bind,
227231
allow_self_signed: config.allow_self_signed.unwrap_or(false),
232+
user_agent: config.user_agent,
228233
no_ansi: config.no_ansi.unwrap_or(false),
229234
tls: config.tls,
230235
max_debounce_time: config.max_debounce_time.unwrap_or(15),
@@ -262,6 +267,7 @@ struct PartialConfig {
262267
pub socket: Option<PathBuf>,
263268
pub socket_permissions: Option<String>,
264269
pub allow_self_signed: Option<bool>,
270+
pub user_agent: Option<String>,
265271
pub no_ansi: Option<bool>,
266272
pub tls: Option<TlsConfig>,
267273
pub max_debounce_time: Option<usize>,
@@ -288,6 +294,7 @@ impl PartialConfig {
288294
let socket = var("SOCKET_PATH").map(PathBuf::from).ok();
289295
let socket_permissions = var("SOCKET_PERMISSIONS").ok();
290296
let allow_self_signed = var("ALLOW_SELF_SIGNED").map(|val| val == "true").ok();
297+
let user_agent = var("USER_AGENT").ok();
291298
let no_ansi = var("NO_ANSI").map(|val| val == "true").ok();
292299

293300
let tls_cert = parse_var("TLS_CERT")?;
@@ -340,6 +347,7 @@ impl PartialConfig {
340347
socket,
341348
socket_permissions,
342349
allow_self_signed,
350+
user_agent,
343351
no_ansi,
344352
tls,
345353
max_debounce_time,
@@ -431,6 +439,7 @@ impl PartialConfig {
431439
} else {
432440
None
433441
},
442+
user_agent: opt.user_agent,
434443
no_ansi: if opt.no_ansi { Some(true) } else { None },
435444
tls,
436445
max_debounce_time: opt.max_debounce_time,
@@ -456,6 +465,7 @@ impl PartialConfig {
456465
socket: self.socket.or(fallback.socket),
457466
socket_permissions: self.socket_permissions.or(fallback.socket_permissions),
458467
allow_self_signed: self.allow_self_signed.or(fallback.allow_self_signed),
468+
user_agent: self.user_agent.or(fallback.user_agent),
459469
no_ansi: self.no_ansi.or(fallback.no_ansi),
460470
tls: self.tls.or(fallback.tls),
461471
max_debounce_time: self.max_debounce_time.or(fallback.max_debounce_time),

src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ pub struct App {
6868
impl App {
6969
pub async fn new(config: Config, log_handle: LoggerHandle) -> Result<Self> {
7070
let connections = ActiveConnections::default();
71-
let nc_client = nc::Client::new(&config.nextcloud_url, config.allow_self_signed)?;
71+
let nc_client = nc::Client::new(
72+
&config.nextcloud_url,
73+
config.allow_self_signed,
74+
config.user_agent.as_deref(),
75+
)?;
7276
let test_cookie = AtomicU32::new(0);
7377

7478
let storage_mapping = StorageMapping::new(config.database, config.database_prefix).await?;
@@ -98,7 +102,11 @@ impl App {
98102
allow_self_signed: bool,
99103
) -> Result<Self> {
100104
let connections = ActiveConnections::default();
101-
let nc_client = nc::Client::new(&config.nextcloud_url, allow_self_signed)?;
105+
let nc_client = nc::Client::new(
106+
&config.nextcloud_url,
107+
allow_self_signed,
108+
config.user_agent.as_deref(),
109+
)?;
102110
let test_cookie = AtomicU32::new(0);
103111

104112
let storage_mapping = StorageMapping::from_connection(connection, config.database_prefix);

src/nc.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,23 @@ pub struct Client {
1818
}
1919

2020
impl Client {
21-
pub fn new(base_url: &str, allow_self_signed: bool) -> Result<Self, NextCloudError> {
21+
pub fn new(
22+
base_url: &str,
23+
allow_self_signed: bool,
24+
user_agent: Option<&str>,
25+
) -> Result<Self, NextCloudError> {
2226
let base_url = Url::parse(base_url)?;
23-
let http = reqwest::Client::builder()
27+
let mut builder = reqwest::Client::builder()
2428
.tls_certs_merge(
2529
webpki_root_certs::TLS_SERVER_ROOT_CERTS
2630
.iter()
2731
.map(|root| Certificate::from_der(root).unwrap()),
2832
)
29-
.tls_danger_accept_invalid_certs(allow_self_signed)
30-
.build()?;
33+
.tls_danger_accept_invalid_certs(allow_self_signed);
34+
if let Some(user_agent) = user_agent {
35+
builder = builder.user_agent(user_agent);
36+
}
37+
let http = builder.build()?;
3138
Ok(Client { http, base_url })
3239
}
3340

tests/integration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ impl Services {
171171
log_level: "".to_string(),
172172
bind: Bind::Tcp(self.nextcloud),
173173
allow_self_signed: false,
174+
user_agent: None,
174175
no_ansi: false,
175176
tls: None,
176177
max_debounce_time: 15,

0 commit comments

Comments
 (0)