syncweb-core 0.1.1-alpha.4

delay-tolerant web surfing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
    time::Duration,
};

use serde::{Deserialize, Serialize};

use crate::error::{Result, SyncwebError};
use crate::net::RelayConfig;
use crate::schedule::{ScheduleConfig, TimeWindow, parse_rate};

#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct Config {
    #[serde(default)]
    pub bep: BepConfig,
    #[serde(default)]
    pub schedule: ScheduleConfig,
    #[serde(default)]
    pub bandwidth: BandwidthConfig,
    #[serde(default)]
    pub parallel: ParallelConfig,
    #[serde(default)]
    pub cache: CacheConfig,
    #[serde(default)]
    pub advanced: AdvancedConfig,
    #[serde(default)]
    pub discovery: DiscoveryConfig,
    #[serde(default)]
    pub default_path: Option<PathBuf>,
    #[serde(default = "default_sync_mode")]
    pub default_sync_mode: String,
    #[serde(default)]
    pub subscribe: SubscribeConfig,
}

impl Config {
    /// # Errors
    ///
    /// Returns an error if the config file cannot be read or parsed.
    pub fn load(path: impl AsRef<Path>) -> Result<Self> {
        let path_ref = path.as_ref();
        if !path_ref.exists() {
            return Ok(Self::default());
        }

        let contents = std::fs::read_to_string(path_ref)
            .map_err(|error| SyncwebError::operation("failed to read config", error))?;
        toml::from_str(&contents).map_err(|error| SyncwebError::operation("failed to parse config", error))
    }

    /// # Errors
    ///
    /// Returns an error if the config cannot be serialized or written to disk.
    pub fn save(&self, path: impl AsRef<Path>) -> Result<()> {
        let path_ref = path.as_ref();
        let contents = toml::to_string_pretty(self)
            .map_err(|error| SyncwebError::operation("failed to serialize config", error))?;
        crate::fs::atomic::atomic_write(path_ref, contents.as_bytes())
    }

    #[must_use]
    pub fn relay_config(&self) -> RelayConfig {
        self.bep.relay_config()
    }

    /// # Errors
    ///
    /// Returns an error if the key is unknown or the value cannot be parsed.
    pub fn set(&mut self, key: &str, value: &str) -> Result<()> {
        match key {
            "bep.enabled" => self.bep.enabled = parse_bool(key, value)?,
            "bep.relay_urls" => self.bep.relay_urls = parse_string_list(key, value)?,
            "bep.relay_timeout" => {
                self.bep.relay_timeout = value.parse().map_err(|error| {
                    SyncwebError::InvalidConfig(format!("{key} must be a non-negative integer: {error}"))
                })?;
            }
            "bep.auto_fallback" => self.bep.auto_fallback = parse_bool(key, value)?,
            "schedule.active_hours" => {
                TimeWindow::parse(value)?;
                value.clone_into(&mut self.schedule.active_hours);
            }
            "bandwidth.max_upload" => {
                parse_rate(value)?;
                value.clone_into(&mut self.bandwidth.max_upload);
            }
            "bandwidth.max_download" => {
                parse_rate(value)?;
                value.clone_into(&mut self.bandwidth.max_download);
            }
            "parallel.threads" => {
                self.parallel.threads = value.parse().map_err(|error| {
                    SyncwebError::InvalidConfig(format!("{key} must be a non-negative integer: {error}"))
                })?;
            }
            "cache.max_cache_size" => {
                self.cache.max_cache_size = value.parse().map_err(|error| {
                    SyncwebError::InvalidConfig(format!("{key} must be a non-negative integer: {error}"))
                })?;
            }
            "advanced.blob_cache_size_gb" => {
                self.advanced.blob_cache_size_gb = value.parse().map_err(|error| {
                    SyncwebError::InvalidConfig(format!("{key} must be a non-negative integer: {error}"))
                })?;
            }
            "discovery.mdns" => self.discovery.mdns = parse_bool(key, value)?,
            "discovery.beacon" => self.discovery.beacon = parse_bool(key, value)?,
            "discovery.beacon_base_port" => {
                self.discovery.beacon_base_port = value.parse().map_err(|error| {
                    SyncwebError::InvalidConfig(format!("{key} must be a valid port number: {error}"))
                })?;
            }
            "discovery.beacon_interval_ms" => {
                self.discovery.beacon_interval_ms = value.parse().map_err(|error| {
                    SyncwebError::InvalidConfig(format!("{key} must be a non-negative integer: {error}"))
                })?;
            }
            "discovery.interface" => self.discovery.interface = Some(value.to_owned()),
            "default_path" => self.default_path = Some(PathBuf::from(value)),
            "default_sync_mode" => {
                value.parse::<crate::folder::SyncMode>()?;
                value.clone_into(&mut self.default_sync_mode);
            }
            _ if key.ends_with(".subscribe") => {
                if let Some(namespace) = key.strip_suffix(".subscribe") {
                    let namespace_id = namespace.parse::<iroh_docs::NamespaceId>().map_err(|error| {
                        SyncwebError::InvalidConfig(format!("invalid namespace {namespace:?} in {key}: {error}"))
                    })?;
                    let enabled = parse_bool(key, value)?;
                    if enabled {
                        let entry = self.subscribe.folders.entry(namespace_id.to_string()).or_default();
                        entry.enabled = true;
                    } else {
                        self.subscribe.folders.remove(namespace_id.to_string().as_str());
                    }
                }
            }
            _ => {
                return Err(SyncwebError::InvalidConfig(format!(
                    "unsupported config key {key:?}; supported keys: \
                     bep.enabled, bep.relay_urls, bep.relay_timeout, bep.auto_fallback, schedule.active_hours, \
                     bandwidth.max_upload, bandwidth.max_download, parallel.threads, cache.max_cache_size, \
                     advanced.blob_cache_size_gb, discovery.mdns, discovery.beacon, discovery.beacon_base_port, \
                     discovery.beacon_interval_ms, discovery.interface, default_path, default_sync_mode, \
                     <namespace>.subscribe"
                )));
            }
        }
        Ok(())
    }

    /// Set the live-syncing (`subscribe-changes`) state and filters for a folder.
    ///
    /// The folder's entry is created on first use, so a plain `join` writes
    /// `enabled: false` and a later `join --subscribe` flips it to `true`
    /// without dropping the persisted filters.
    pub fn set_subscribe(&mut self, namespace: &str, enabled: bool, filters: &SubscribeFilters) {
        let entry = self.subscribe.folders.entry(namespace.to_owned()).or_default();
        entry.enabled = enabled;
        if !filters.is_default() {
            entry.filters = filters.clone();
        }
    }

    /// Remove the per-folder `subscribe-changes` entry, e.g. when leaving.
    pub fn remove_subscribe(&mut self, namespace: &str) -> bool {
        self.subscribe.folders.remove(namespace).is_some()
    }
}

/// Per-folder live-sync (`subscribe-changes`) settings.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct SubscribeConfig {
    /// Map of folder namespace to its live-sync setting.
    #[serde(default)]
    pub folders: BTreeMap<String, SubscribeFolderConfig>,
}

/// The live-sync state and filters for a single folder.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct SubscribeFolderConfig {
    /// Whether the daemon auto-supervises this folder (default: false, D7).
    #[serde(default)]
    pub enabled: bool,
    /// Subscription filters applied when the daemon supervises the folder.
    #[serde(default)]
    pub filters: SubscribeFilters,
}

/// Persisted subscription filters, mirroring the `join --subscribe` flags.
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct SubscribeFilters {
    /// Only deliver entries ingested after live syncing is enabled.
    #[serde(default)]
    pub ingest_only: bool,
    /// Ignore this device's own writes.
    #[serde(default)]
    pub ignore_self: bool,
    /// Area prefix filter for subscription entries.
    #[serde(default)]
    pub sync_prefix: Option<PathBuf>,
    /// Glob filter for subscription entries.
    #[serde(default)]
    pub glob: Option<String>,
    /// Maximum number of entries to deliver.
    #[serde(default)]
    pub max_count: Option<u64>,
    /// Maximum total bytes to deliver.
    #[serde(default)]
    pub max_size: Option<u64>,
}

impl SubscribeFilters {
    /// Construct filters from the `join --subscribe` filter flags.
    #[must_use]
    pub const fn new(
        ingest_only: bool,
        ignore_self: bool,
        sync_prefix: Option<PathBuf>,
        glob: Option<String>,
        max_count: Option<u64>,
        max_size: Option<u64>,
    ) -> Self {
        Self {
            ingest_only,
            ignore_self,
            sync_prefix,
            glob,
            max_count,
            max_size,
        }
    }

    #[must_use]
    pub fn is_default(&self) -> bool {
        *self == Self::default()
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct BandwidthConfig {
    #[serde(default)]
    pub max_upload: String,
    #[serde(default)]
    pub max_download: String,
}
impl Default for BandwidthConfig {
    fn default() -> Self {
        Self {
            max_upload: "0".to_owned(),
            max_download: "0".to_owned(),
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
#[derive(Default)]
pub struct ParallelConfig {
    /// Thread count for parallel operations. `0` (the default) means
    /// auto-detect the number of available CPU cores.
    #[serde(default)]
    pub threads: usize,
}

/// Controls the in-memory peer availability cache used during blob resolution.
/// This is **not** a blob data cache — it limits how many peer entries are tracked
/// for which peers serve which blobs.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct CacheConfig {
    /// Maximum number of entries in the peer availability tracker.
    #[serde(default = "default_cache_size")]
    pub max_cache_size: usize,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            max_cache_size: default_cache_size(),
        }
    }
}

/// Advanced / experimental configuration options.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct AdvancedConfig {
    /// Maximum Iroh blob cache size in GiB (default: 2). Controls how much
    /// local disk space is used for caching synced blob data.
    #[serde(default = "default_blob_cache_size")]
    pub blob_cache_size_gb: u64,
}

impl Default for AdvancedConfig {
    fn default() -> Self {
        Self {
            blob_cache_size_gb: default_blob_cache_size(),
        }
    }
}

/// Local discovery settings. `interface` only applies to the UDP beacon; the
/// mDNS lookup always uses the system's default multicast interface.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct DiscoveryConfig {
    /// Whether the mDNS address lookup is registered (default: true).
    #[serde(default = "default_discovery_enabled")]
    pub mdns: bool,
    /// Whether the UDP beacon address lookup is registered (default: true).
    #[serde(default = "default_discovery_enabled")]
    pub beacon: bool,
    /// Base UDP port the beacon spreads network scopes over (default: 15200).
    #[serde(default = "default_beacon_base_port")]
    pub beacon_base_port: u16,
    /// How often the beacon re-broadcasts, in milliseconds (default: 1000).
    #[serde(default = "default_beacon_interval_ms")]
    pub beacon_interval_ms: u64,
    /// Restrict the beacon to a single network interface by name, if any.
    #[serde(default)]
    pub interface: Option<String>,
}

impl Default for DiscoveryConfig {
    fn default() -> Self {
        Self {
            mdns: default_discovery_enabled(),
            beacon: default_discovery_enabled(),
            beacon_base_port: default_beacon_base_port(),
            beacon_interval_ms: default_beacon_interval_ms(),
            interface: None,
        }
    }
}

const fn default_discovery_enabled() -> bool {
    true
}

const fn default_beacon_base_port() -> u16 {
    crate::node::beacon_lookup::DEFAULT_BEACON_PORT
}

const fn default_beacon_interval_ms() -> u64 {
    1_000
}

fn default_sync_mode() -> String {
    "sendreceive".to_owned()
}

const fn default_cache_size() -> usize {
    5_000
}

const fn default_blob_cache_size() -> u64 {
    2
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct BepConfig {
    #[serde(default)]
    pub enabled: bool,
    #[serde(default)]
    pub relay_urls: Vec<String>,
    #[serde(default = "default_relay_timeout")]
    pub relay_timeout: u64,
    #[serde(default = "default_auto_fallback")]
    pub auto_fallback: bool,
}

impl Default for BepConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            relay_urls: Vec::new(),
            relay_timeout: default_relay_timeout(),
            auto_fallback: default_auto_fallback(),
        }
    }
}

impl BepConfig {
    #[must_use]
    pub fn relay_config(&self) -> RelayConfig {
        RelayConfig {
            relay_urls: self.relay_urls.clone(),
            timeout: Duration::from_secs(self.relay_timeout),
            auto_fallback: self.enabled && self.auto_fallback,
        }
    }
}

const fn default_relay_timeout() -> u64 {
    10
}

const fn default_auto_fallback() -> bool {
    true
}

fn parse_bool(key: &str, value: &str) -> Result<bool> {
    value
        .parse()
        .map_err(|error| SyncwebError::InvalidConfig(format!("{key} must be true or false: {error}")))
}

fn parse_string_list(key: &str, value: &str) -> Result<Vec<String>> {
    let parsed: toml::Value = toml::from_str(&format!("value = {value}")).map_err(|error| {
        SyncwebError::InvalidConfig(format!(
            "{key} must be a TOML string array, for example [\"tcp://relay:22270\"]: {error}"
        ))
    })?;
    parsed
        .get("value")
        .and_then(toml::Value::as_array)
        .filter(|values| values.iter().all(toml::Value::is_str))
        .map(|values| {
            values
                .iter()
                .map(|v| v.as_str().unwrap_or_default().to_owned())
                .collect()
        })
        .ok_or_else(|| {
            SyncwebError::InvalidConfig(format!(
                "{key} must be a TOML string array, for example [\"tcp://relay:22270\"]"
            ))
        })
}