mirror of
https://github.com/outbackdingo/firezone.git
synced 2026-03-22 06:41:51 +00:00
At present, the GUI client shares the current log-directives with the IPC service via the file system. Supposedly, this has been done to allow the IPC service to start back up with the same log filter as before. This behaviour appears to be buggy though as we are receiving a fair number of error reports where this file is not writable. Instead of relying on the file system to communicate, we send the current log-directives to the IPC service as soon as we start up. The IPC service then uses the file system as a cache that log string and re-apply it on the next startup. This way, no two programs need to read / write the same file. The IPC service runs with higher privileges, so this should resolve the permission errors we are seeing in Sentry.
104 lines
3.1 KiB
Rust
104 lines
3.1 KiB
Rust
//! Everything related to the Settings window, including
|
|
//! advanced settings and code for manipulating diagnostic logs.
|
|
|
|
use anyhow::{Context as _, Result};
|
|
use connlib_model::ResourceId;
|
|
use firezone_headless_client::known_dirs;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::{collections::HashSet, path::PathBuf};
|
|
use url::Url;
|
|
|
|
#[derive(Clone, Deserialize, Serialize)]
|
|
pub struct AdvancedSettings {
|
|
pub auth_base_url: Url,
|
|
pub api_url: Url,
|
|
#[serde(default)]
|
|
pub favorite_resources: HashSet<ResourceId>,
|
|
#[serde(default)]
|
|
pub internet_resource_enabled: Option<bool>,
|
|
pub log_filter: String,
|
|
}
|
|
|
|
#[cfg(debug_assertions)]
|
|
mod defaults {
|
|
pub(crate) const AUTH_BASE_URL: &str = "https://app.firez.one";
|
|
pub(crate) const API_URL: &str = "wss://api.firez.one/";
|
|
pub(crate) const LOG_FILTER: &str = "firezone_gui_client=debug,info";
|
|
}
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
mod defaults {
|
|
pub(crate) const AUTH_BASE_URL: &str = "https://app.firezone.dev";
|
|
pub(crate) const API_URL: &str = "wss://api.firezone.dev/";
|
|
pub(crate) const LOG_FILTER: &str = "info";
|
|
}
|
|
|
|
impl Default for AdvancedSettings {
|
|
fn default() -> Self {
|
|
Self {
|
|
auth_base_url: Url::parse(defaults::AUTH_BASE_URL).expect("static URL is a valid URL"),
|
|
api_url: Url::parse(defaults::API_URL).expect("static URL is a valid URL"),
|
|
favorite_resources: Default::default(),
|
|
internet_resource_enabled: Default::default(),
|
|
log_filter: defaults::LOG_FILTER.to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl AdvancedSettings {
|
|
pub fn internet_resource_enabled(&self) -> bool {
|
|
self.internet_resource_enabled.is_some_and(|v| v)
|
|
}
|
|
}
|
|
|
|
pub fn advanced_settings_path() -> Result<PathBuf> {
|
|
Ok(known_dirs::settings()
|
|
.context("`known_dirs::settings` failed")?
|
|
.join("advanced_settings.json"))
|
|
}
|
|
|
|
/// Saves the settings to disk
|
|
pub async fn save(settings: &AdvancedSettings) -> Result<()> {
|
|
let path = advanced_settings_path()?;
|
|
let dir = path
|
|
.parent()
|
|
.context("settings path should have a parent")?;
|
|
|
|
tokio::fs::create_dir_all(dir).await?;
|
|
tokio::fs::write(&path, serde_json::to_string(settings)?).await?;
|
|
|
|
tracing::debug!(?path, "Saved settings");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// Return advanced settings if they're stored on disk
|
|
///
|
|
/// Uses std::fs, so stick it in `spawn_blocking` for async contexts
|
|
pub fn load_advanced_settings() -> Result<AdvancedSettings> {
|
|
let path = advanced_settings_path()?;
|
|
let text = std::fs::read_to_string(path)?;
|
|
let settings = serde_json::from_str(&text)?;
|
|
Ok(settings)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn load_old_formats() {
|
|
let s = r#"{
|
|
"auth_base_url": "https://example.com/",
|
|
"api_url": "wss://example.com/",
|
|
"log_filter": "info"
|
|
}"#;
|
|
|
|
let actual = serde_json::from_str::<AdvancedSettings>(s).unwrap();
|
|
// Apparently the trailing slash here matters
|
|
assert_eq!(actual.auth_base_url.to_string(), "https://example.com/");
|
|
assert_eq!(actual.api_url.to_string(), "wss://example.com/");
|
|
assert_eq!(actual.log_filter, "info");
|
|
}
|
|
}
|