From 27f6414ad1ff451feb0044af62f37dc2a6255ffa Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 5 Feb 2026 22:35:05 +0800 Subject: Remove examples and legacy code, update .gitignore - Delete examples directory and its example action system - Rename actions/ to legacy_actions/ and data/ to legacy_data/ - Update Cargo.toml license file reference - Move setup scripts to scripts/dev/ directory - Add todo.txt patterns to .gitignore --- legacy_data/src/data/vault/vault_config.rs | 233 +++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 legacy_data/src/data/vault/vault_config.rs (limited to 'legacy_data/src/data/vault/vault_config.rs') diff --git a/legacy_data/src/data/vault/vault_config.rs b/legacy_data/src/data/vault/vault_config.rs new file mode 100644 index 0000000..caa8552 --- /dev/null +++ b/legacy_data/src/data/vault/vault_config.rs @@ -0,0 +1,233 @@ +use std::net::{IpAddr, Ipv4Addr}; + +use cfg_file::ConfigFile; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +use crate::constants::{PORT, SERVER_FILE_VAULT}; +use crate::data::member::{Member, MemberId}; + +pub type VaultName = String; +pub type VaultUuid = Uuid; + +#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum AuthMode { + /// Use asymmetric keys: both client and server need to register keys, after which they can connect + Key, + + /// Use password: the password stays on the server, and the client needs to set the password locally for connection + #[default] + Password, + + /// No authentication: generally used in a strongly secure environment, skipping verification directly + NoAuth, +} + +#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum LoggerLevel { + Debug, + Trace, + + #[default] + Info, +} + +#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum ServiceEnabled { + Enable, + + #[default] + Disable, +} + +#[derive(Serialize, Deserialize, Clone, PartialEq, Default)] +#[serde(rename_all = "lowercase")] +pub enum BehaviourEnabled { + Yes, + + #[default] + No, +} + +impl Into for ServiceEnabled { + fn into(self) -> bool { + match self { + ServiceEnabled::Enable => true, + ServiceEnabled::Disable => false, + } + } +} + +impl Into for BehaviourEnabled { + fn into(self) -> bool { + match self { + BehaviourEnabled::Yes => true, + BehaviourEnabled::No => false, + } + } +} + +#[derive(Serialize, Deserialize, ConfigFile)] +#[cfg_file(path = SERVER_FILE_VAULT)] +pub struct VaultConfig { + /// Vault uuid, unique identifier for the vault + #[serde(rename = "uuid")] + vault_uuid: VaultUuid, + + /// Vault name, which can be used as the project name and generally serves as a hint + #[serde(rename = "name")] + vault_name: VaultName, + + /// Vault host ids, a list of member id representing administrator identities + #[serde(rename = "hosts")] + vault_host_list: Vec, + + /// Vault server configuration, which will be loaded when connecting to the server + #[serde(rename = "profile")] + server_config: VaultServerConfig, +} + +#[derive(Serialize, Deserialize)] +pub struct VaultServerConfig { + /// Local IP address to bind to when the server starts + #[serde(rename = "bind")] + local_bind: IpAddr, + + /// TCP port to bind to when the server starts + #[serde(rename = "port")] + port: u16, + + /// Enable logging + #[serde(rename = "logger")] + logger: Option, + + /// Logger Level + #[serde(rename = "logger_level")] + logger_level: Option, + + /// Whether to enable LAN discovery, allowing members on the same LAN to more easily find the upstream server + #[serde(rename = "lan_discovery")] + lan_discovery: Option, // TODO + + /// Authentication mode for the vault server + /// key: Use asymmetric keys for authentication + /// password: Use a password for authentication + /// noauth: No authentication required, requires a strongly secure environment + #[serde(rename = "auth_mode")] + auth_mode: Option, // TODO +} + +impl Default for VaultConfig { + fn default() -> Self { + Self { + vault_uuid: Uuid::new_v4(), + vault_name: "JustEnoughVault".to_string(), + vault_host_list: Vec::new(), + server_config: VaultServerConfig { + local_bind: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), + port: PORT, + logger: Some(BehaviourEnabled::default()), + logger_level: Some(LoggerLevel::default()), + lan_discovery: Some(ServiceEnabled::default()), + auth_mode: Some(AuthMode::Key), + }, + } + } +} + +/// Vault Management +impl VaultConfig { + /// Change name of the vault. + pub fn change_name(&mut self, name: impl Into) { + self.vault_name = name.into() + } + + /// Add admin + pub fn add_admin(&mut self, member: &Member) { + let uuid = member.id(); + if !self.vault_host_list.contains(&uuid) { + self.vault_host_list.push(uuid); + } + } + + /// Remove admin + pub fn remove_admin(&mut self, member: &Member) { + let id = member.id(); + self.vault_host_list.retain(|x| x != &id); + } + + /// Get vault UUID + pub fn vault_uuid(&self) -> &VaultUuid { + &self.vault_uuid + } + + /// Set vault UUID + pub fn set_vault_uuid(&mut self, vault_uuid: VaultUuid) { + self.vault_uuid = vault_uuid; + } + + /// Get vault name + pub fn vault_name(&self) -> &VaultName { + &self.vault_name + } + + /// Set vault name + pub fn set_vault_name(&mut self, vault_name: VaultName) { + self.vault_name = vault_name; + } + + /// Get vault admin list + pub fn vault_host_list(&self) -> &Vec { + &self.vault_host_list + } + + /// Set vault admin list + pub fn set_vault_host_list(&mut self, vault_host_list: Vec) { + self.vault_host_list = vault_host_list; + } + + /// Get server config + pub fn server_config(&self) -> &VaultServerConfig { + &self.server_config + } + + /// Set server config + pub fn set_server_config(&mut self, server_config: VaultServerConfig) { + self.server_config = server_config; + } +} + +impl VaultServerConfig { + /// Get local bind IP address + pub fn local_bind(&self) -> &IpAddr { + &self.local_bind + } + + /// Get port + pub fn port(&self) -> u16 { + self.port + } + + /// Check if LAN discovery is enabled + pub fn is_lan_discovery_enabled(&self) -> bool { + self.lan_discovery.clone().unwrap_or_default().into() + } + + /// Get logger enabled status + pub fn is_logger_enabled(&self) -> bool { + self.logger.clone().unwrap_or_default().into() + } + + /// Get logger level + pub fn logger_level(&self) -> LoggerLevel { + self.logger_level.clone().unwrap_or_default() + } + + /// Get authentication mode + pub fn auth_mode(&self) -> AuthMode { + self.auth_mode.clone().unwrap_or_default() + } +} -- cgit