2023-04-19 11:21:37 +08:00
|
|
|
use super::desc::ConfigItem;
|
2023-04-23 15:40:55 +08:00
|
|
|
use hbb_common::{allow_err, bail, config::Config as HbbConfig, lazy_static, log, ResultType};
|
2023-04-19 11:21:37 +08:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
|
|
|
use std::{
|
|
|
|
ops::{Deref, DerefMut},
|
2023-04-23 15:40:55 +08:00
|
|
|
str::FromStr,
|
2023-04-19 11:21:37 +08:00
|
|
|
sync::{Arc, Mutex},
|
|
|
|
{collections::HashMap, path::PathBuf},
|
|
|
|
};
|
|
|
|
|
|
|
|
lazy_static::lazy_static! {
|
2023-04-23 15:40:55 +08:00
|
|
|
static ref CONFIG_SHARED: Arc<Mutex<HashMap<String, SharedConfig>>> = Default::default();
|
|
|
|
static ref CONFIG_SHARED_ITEMS: Arc<Mutex<HashMap<String, Vec<ConfigItem>>>> = Default::default();
|
2023-04-19 11:21:37 +08:00
|
|
|
static ref CONFIG_PEERS: Arc<Mutex<HashMap<String, PeersConfig>>> = Default::default();
|
|
|
|
static ref CONFIG_PEER_ITEMS: Arc<Mutex<HashMap<String, Vec<ConfigItem>>>> = Default::default();
|
2023-04-22 22:21:02 +08:00
|
|
|
static ref CONFIG_MANAGER: Arc<Mutex<ManagerConfig>> = {
|
|
|
|
let conf = hbb_common::config::load_path::<ManagerConfig>(ManagerConfig::path());
|
|
|
|
Arc::new(Mutex::new(conf))
|
|
|
|
};
|
2023-04-19 11:21:37 +08:00
|
|
|
}
|
|
|
|
|
2023-04-23 15:40:55 +08:00
|
|
|
pub(super) const CONFIG_TYPE_SHARED: &str = "shared";
|
2023-04-21 21:40:34 +08:00
|
|
|
pub(super) const CONFIG_TYPE_PEER: &str = "peer";
|
|
|
|
|
2023-04-19 11:21:37 +08:00
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
2023-04-23 15:40:55 +08:00
|
|
|
pub struct SharedConfig(HashMap<String, String>);
|
2023-04-19 11:21:37 +08:00
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
2023-04-20 20:57:47 +08:00
|
|
|
pub struct PeerConfig(HashMap<String, String>);
|
2023-04-19 11:21:37 +08:00
|
|
|
type PeersConfig = HashMap<String, PeerConfig>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn path_plugins(id: &str) -> PathBuf {
|
|
|
|
HbbConfig::path("plugins").join(id)
|
|
|
|
}
|
|
|
|
|
2023-04-23 15:40:55 +08:00
|
|
|
impl Deref for SharedConfig {
|
2023-04-19 11:21:37 +08:00
|
|
|
type Target = HashMap<String, String>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 15:40:55 +08:00
|
|
|
impl DerefMut for SharedConfig {
|
2023-04-19 11:21:37 +08:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for PeerConfig {
|
|
|
|
type Target = HashMap<String, String>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for PeerConfig {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-23 15:40:55 +08:00
|
|
|
impl SharedConfig {
|
2023-04-19 11:21:37 +08:00
|
|
|
#[inline]
|
|
|
|
fn path(id: &str) -> PathBuf {
|
2023-04-23 15:40:55 +08:00
|
|
|
path_plugins(id).join("shared.toml")
|
2023-04-19 11:21:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn load(id: &str) {
|
2023-04-23 20:53:51 +08:00
|
|
|
let conf = hbb_common::config::load_path::<HashMap<String, String>>(Self::path(id));
|
|
|
|
let mut conf = SharedConfig(conf);
|
2023-04-23 15:40:55 +08:00
|
|
|
if let Some(items) = CONFIG_SHARED_ITEMS.lock().unwrap().get(id) {
|
2023-04-19 11:21:37 +08:00
|
|
|
for item in items {
|
|
|
|
if !conf.contains_key(&item.key) {
|
|
|
|
conf.insert(item.key.to_owned(), item.default.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-23 15:40:55 +08:00
|
|
|
CONFIG_SHARED.lock().unwrap().insert(id.to_owned(), conf);
|
2023-04-19 11:21:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get(id: &str, key: &str) -> Option<String> {
|
2023-04-23 15:40:55 +08:00
|
|
|
if let Some(conf) = CONFIG_SHARED.lock().unwrap().get(id) {
|
2023-04-19 11:21:37 +08:00
|
|
|
return conf.get(key).map(|s| s.to_owned());
|
|
|
|
}
|
|
|
|
Self::load(id);
|
2023-04-23 15:40:55 +08:00
|
|
|
CONFIG_SHARED
|
2023-04-19 11:21:37 +08:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.get(id)?
|
|
|
|
.get(key)
|
|
|
|
.map(|s| s.to_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn set(id: &str, key: &str, value: &str) -> ResultType<()> {
|
2023-04-23 15:40:55 +08:00
|
|
|
match CONFIG_SHARED.lock().unwrap().get_mut(id) {
|
2023-04-19 11:21:37 +08:00
|
|
|
Some(config) => {
|
|
|
|
config.insert(key.to_owned(), value.to_owned());
|
|
|
|
hbb_common::config::store_path(Self::path(id), config)
|
|
|
|
}
|
|
|
|
None => bail!("No such plugin {}", id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PeerConfig {
|
|
|
|
#[inline]
|
|
|
|
fn path(id: &str, peer: &str) -> PathBuf {
|
|
|
|
path_plugins(id)
|
|
|
|
.join("peers")
|
|
|
|
.join(format!("{}.toml", peer))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn load(id: &str, peer: &str) {
|
2023-04-23 20:53:51 +08:00
|
|
|
let conf = hbb_common::config::load_path::<HashMap<String, String>>(Self::path(id, peer));
|
|
|
|
let mut conf = PeerConfig(conf);
|
2023-04-19 11:21:37 +08:00
|
|
|
if let Some(items) = CONFIG_PEER_ITEMS.lock().unwrap().get(id) {
|
|
|
|
for item in items {
|
|
|
|
if !conf.contains_key(&item.key) {
|
|
|
|
conf.insert(item.key.to_owned(), item.default.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-23 20:53:51 +08:00
|
|
|
|
|
|
|
if let Some(peers) = CONFIG_PEERS.lock().unwrap().get_mut(id) {
|
|
|
|
peers.insert(peer.to_owned(), conf);
|
|
|
|
return;
|
2023-04-19 11:21:37 +08:00
|
|
|
}
|
2023-04-23 20:53:51 +08:00
|
|
|
|
|
|
|
let mut peers = HashMap::new();
|
|
|
|
peers.insert(peer.to_owned(), conf);
|
|
|
|
CONFIG_PEERS.lock().unwrap().insert(id.to_owned(), peers);
|
2023-04-19 11:21:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get(id: &str, peer: &str, key: &str) -> Option<String> {
|
|
|
|
if let Some(peers) = CONFIG_PEERS.lock().unwrap().get(id) {
|
|
|
|
if let Some(conf) = peers.get(peer) {
|
|
|
|
return conf.get(key).map(|s| s.to_owned());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::load(id, peer);
|
|
|
|
CONFIG_PEERS
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.get(id)?
|
|
|
|
.get(peer)?
|
|
|
|
.get(key)
|
|
|
|
.map(|s| s.to_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn set(id: &str, peer: &str, key: &str, value: &str) -> ResultType<()> {
|
|
|
|
match CONFIG_PEERS.lock().unwrap().get_mut(id) {
|
|
|
|
Some(peers) => match peers.get_mut(peer) {
|
|
|
|
Some(config) => {
|
|
|
|
config.insert(key.to_owned(), value.to_owned());
|
|
|
|
hbb_common::config::store_path(Self::path(id, peer), config)
|
|
|
|
}
|
|
|
|
None => bail!("No such peer {}", peer),
|
|
|
|
},
|
|
|
|
None => bail!("No such plugin {}", id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-04-23 15:40:55 +08:00
|
|
|
pub(super) fn set_shared_items(id: &str, items: &Vec<ConfigItem>) {
|
|
|
|
CONFIG_SHARED_ITEMS
|
2023-04-19 11:21:37 +08:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.insert(id.to_owned(), items.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(super) fn set_peer_items(id: &str, items: &Vec<ConfigItem>) {
|
|
|
|
CONFIG_PEER_ITEMS
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.insert(id.to_owned(), items.clone());
|
|
|
|
}
|
2023-04-22 22:21:02 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct PluginStatus {
|
|
|
|
pub enabled: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
const MANAGER_VERSION: &str = "0.1.0";
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct ManagerConfig {
|
|
|
|
pub version: String,
|
|
|
|
pub enabled: bool,
|
2023-04-23 15:40:55 +08:00
|
|
|
#[serde(default)]
|
|
|
|
pub options: HashMap<String, String>,
|
|
|
|
#[serde(default)]
|
2023-04-22 22:21:02 +08:00
|
|
|
pub plugins: HashMap<String, PluginStatus>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ManagerConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2023-04-23 15:40:55 +08:00
|
|
|
version: MANAGER_VERSION.to_owned(),
|
2023-04-22 22:21:02 +08:00
|
|
|
enabled: true,
|
2023-04-23 15:40:55 +08:00
|
|
|
options: HashMap::new(),
|
2023-04-22 22:21:02 +08:00
|
|
|
plugins: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not care about the `store_path` error, no need to store the old value and restore if failed.
|
|
|
|
impl ManagerConfig {
|
|
|
|
#[inline]
|
|
|
|
fn path() -> PathBuf {
|
|
|
|
HbbConfig::path("plugins").join("manager.toml")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-04-23 15:40:55 +08:00
|
|
|
pub fn get_option(key: &str) -> Option<String> {
|
|
|
|
if key == "enabled" {
|
|
|
|
Some(CONFIG_MANAGER.lock().unwrap().enabled.to_string())
|
|
|
|
} else {
|
|
|
|
CONFIG_MANAGER
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.options
|
|
|
|
.get(key)
|
|
|
|
.map(|s| s.to_owned())
|
|
|
|
}
|
2023-04-22 22:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-04-23 15:40:55 +08:00
|
|
|
pub fn set_option(key: &str, value: &str) -> ResultType<()> {
|
2023-04-22 22:21:02 +08:00
|
|
|
let mut lock = CONFIG_MANAGER.lock().unwrap();
|
2023-04-23 15:40:55 +08:00
|
|
|
if key == "enabled" {
|
|
|
|
let enabled = bool::from_str(value).unwrap_or(false);
|
|
|
|
lock.enabled = enabled;
|
|
|
|
if enabled {
|
|
|
|
allow_err!(super::load_plugins());
|
|
|
|
} else {
|
|
|
|
super::unload_plugins();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lock.options.insert(key.to_owned(), value.to_owned());
|
|
|
|
}
|
2023-04-22 22:21:02 +08:00
|
|
|
hbb_common::config::store_path(Self::path(), &*lock)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2023-04-23 15:40:55 +08:00
|
|
|
pub fn get_plugin_option(id: &str, key: &str) -> Option<String> {
|
2023-04-22 22:21:02 +08:00
|
|
|
let lock = CONFIG_MANAGER.lock().unwrap();
|
2023-04-23 15:40:55 +08:00
|
|
|
let status = lock.plugins.get(id)?;
|
|
|
|
match key {
|
|
|
|
"enabled" => Some(status.enabled.to_string()),
|
|
|
|
_ => None,
|
|
|
|
}
|
2023-04-22 22:21:02 +08:00
|
|
|
}
|
|
|
|
|
2023-04-23 15:40:55 +08:00
|
|
|
pub fn set_plugin_option(id: &str, key: &str, value: &str) -> ResultType<()> {
|
2023-04-22 22:21:02 +08:00
|
|
|
let mut lock = CONFIG_MANAGER.lock().unwrap();
|
|
|
|
if let Some(status) = lock.plugins.get_mut(id) {
|
2023-04-23 15:40:55 +08:00
|
|
|
match key {
|
|
|
|
"enabled" => {
|
|
|
|
let enabled = bool::from_str(value).unwrap_or(false);
|
|
|
|
status.enabled = enabled;
|
|
|
|
if enabled {
|
|
|
|
allow_err!(super::load_plugin(None, Some(id)));
|
|
|
|
} else {
|
|
|
|
super::unload_plugin(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => bail!("No such option {}", key),
|
|
|
|
}
|
2023-04-22 22:21:02 +08:00
|
|
|
} else {
|
|
|
|
bail!("No such plugin {}", id)
|
|
|
|
}
|
2023-04-23 15:40:55 +08:00
|
|
|
hbb_common::config::store_path(Self::path(), &*lock)
|
2023-04-22 22:21:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn add_plugin(id: &str) -> ResultType<()> {
|
|
|
|
let mut lock = CONFIG_MANAGER.lock().unwrap();
|
|
|
|
lock.plugins
|
|
|
|
.insert(id.to_owned(), PluginStatus { enabled: true });
|
|
|
|
hbb_common::config::store_path(Self::path(), &*lock)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn remove_plugin(id: &str) -> ResultType<()> {
|
|
|
|
let mut lock = CONFIG_MANAGER.lock().unwrap();
|
|
|
|
lock.plugins.remove(id);
|
2023-04-23 15:40:55 +08:00
|
|
|
hbb_common::config::store_path(Self::path(), &*lock)?;
|
|
|
|
// to-do: remove plugin config dir
|
|
|
|
Ok(())
|
2023-04-22 22:21:02 +08:00
|
|
|
}
|
|
|
|
}
|