2024-04-25 15:15:00 +03:00
|
|
|
// Copyright 2024 The Grim Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
use std::path::PathBuf;
|
2024-04-25 15:15:00 +03:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use crate::Settings;
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Tor configuration.
|
2024-04-25 15:15:00 +03:00
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
2024-04-30 18:15:03 +03:00
|
|
|
pub struct TorConfig {
|
|
|
|
// Flag to check if Tor bridges usage is needed.
|
|
|
|
pub(crate) use_bridges: Option<bool>
|
2024-04-25 15:15:00 +03:00
|
|
|
}
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
impl Default for TorConfig {
|
2024-04-25 15:15:00 +03:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2024-04-30 18:15:03 +03:00
|
|
|
use_bridges: Some(false)
|
2024-04-25 15:15:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
impl TorConfig {
|
2024-04-27 02:19:40 +03:00
|
|
|
/// Tor configuration file name.
|
|
|
|
pub const FILE_NAME: &'static str = "tor.toml";
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Directory for Tor data files.
|
2024-04-27 02:19:40 +03:00
|
|
|
const DIR_NAME: &'static str = "tor";
|
|
|
|
|
|
|
|
/// Subdirectory name for Tor state.
|
|
|
|
const STATE_SUB_DIR: &'static str = "state";
|
|
|
|
/// Subdirectory name for Tor cache.
|
|
|
|
const CACHE_SUB_DIR: &'static str = "cache";
|
|
|
|
/// Subdirectory name for Tor keystore.
|
|
|
|
const KEYSTORE_DIR: &'static str = "keystore";
|
2024-04-25 15:15:00 +03:00
|
|
|
|
|
|
|
/// Save application configuration to the file.
|
|
|
|
pub fn save(&self) {
|
2024-04-30 18:15:03 +03:00
|
|
|
Settings::write_to_file(self, Settings::get_config_path(Self::FILE_NAME, None));
|
2024-04-27 02:19:40 +03:00
|
|
|
}
|
|
|
|
|
2024-04-30 18:15:03 +03:00
|
|
|
/// Get path from subdirectory name.
|
2024-04-27 02:19:40 +03:00
|
|
|
fn sub_dir_path(name: &str) -> String {
|
|
|
|
let mut base = Settings::get_base_path(Some(Self::DIR_NAME.to_string()));
|
|
|
|
base.push(name);
|
|
|
|
base.to_str().unwrap().to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get Tor state directory path.
|
|
|
|
pub fn state_path() -> String {
|
|
|
|
Self::sub_dir_path(Self::STATE_SUB_DIR)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get Tor cache directory path.
|
|
|
|
pub fn cache_path() -> String {
|
|
|
|
Self::sub_dir_path(Self::CACHE_SUB_DIR)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get Tor keystore directory path.
|
|
|
|
pub fn keystore_path() -> String {
|
2024-04-30 18:15:03 +03:00
|
|
|
let mut base = PathBuf::from(Self::state_path());
|
|
|
|
base.push(Self::KEYSTORE_DIR);
|
|
|
|
base.to_str().unwrap().to_string()
|
2024-04-25 15:15:00 +03:00
|
|
|
}
|
|
|
|
}
|