mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
refactor: wip
This commit is contained in:
@@ -4,9 +4,37 @@ use once_cell::sync::OnceCell;
|
||||
use parking_lot::Mutex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml::{Mapping, Value};
|
||||
use std::sync::Arc;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct IClashTemp(pub Mapping);
|
||||
|
||||
impl IClashTemp {
|
||||
pub fn new() -> Self {
|
||||
Self(config::read_merge_mapping(dirs::clash_path()))
|
||||
}
|
||||
|
||||
pub fn patch_config(&mut self, patch: Mapping) {
|
||||
for (key, value) in patch.into_iter() {
|
||||
self.0.insert(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_config(&self) -> Result<()> {
|
||||
config::save_yaml(
|
||||
dirs::clash_path(),
|
||||
&self.0,
|
||||
Some("# Default Config For ClashN Core\n\n"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_info(&self) -> Result<ClashInfoN> {
|
||||
Ok(ClashInfoN::from(&self.0))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[deprecated]
|
||||
pub struct ClashN {
|
||||
/// maintain the clash config
|
||||
pub config: Arc<Mutex<Mapping>>,
|
||||
@@ -167,3 +195,71 @@ impl ClashInfoN {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct IClash {
|
||||
pub mixed_port: Option<u16>,
|
||||
pub allow_lan: Option<bool>,
|
||||
pub log_level: Option<String>,
|
||||
pub ipv6: Option<bool>,
|
||||
pub mode: Option<String>,
|
||||
pub external_controller: Option<String>,
|
||||
pub secret: Option<String>,
|
||||
pub dns: Option<IClashDNS>,
|
||||
pub tun: Option<IClashTUN>,
|
||||
pub interface_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct IClashTUN {
|
||||
pub enable: Option<bool>,
|
||||
pub stack: Option<String>,
|
||||
pub auto_route: Option<bool>,
|
||||
pub auto_detect_interface: Option<bool>,
|
||||
pub dns_hijack: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct IClashDNS {
|
||||
pub enable: Option<bool>,
|
||||
pub listen: Option<String>,
|
||||
pub default_nameserver: Option<Vec<String>>,
|
||||
pub enhanced_mode: Option<String>,
|
||||
pub fake_ip_range: Option<String>,
|
||||
pub use_hosts: Option<bool>,
|
||||
pub fake_ip_filter: Option<Vec<String>>,
|
||||
pub nameserver: Option<Vec<String>>,
|
||||
pub fallback: Option<Vec<String>>,
|
||||
pub fallback_filter: Option<IClashFallbackFilter>,
|
||||
pub nameserver_policy: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct IClashFallbackFilter {
|
||||
pub geoip: Option<bool>,
|
||||
pub geoip_code: Option<String>,
|
||||
pub ipcidr: Option<Vec<String>>,
|
||||
pub domain: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
let socket = SocketAddr::new("127.0.0.1".parse().unwrap(), 9090);
|
||||
|
||||
let s = "[::]:8080".parse::<SocketAddr>();
|
||||
|
||||
dbg!(s);
|
||||
|
||||
// match "::8080".parse::<SocketAddr>() {
|
||||
// Ok(_) => {}
|
||||
// Err(err) => {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// assert_eq!(":8080".parse(), Ok(socket));
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
use super::{Draft, IVerge};
|
||||
use super::{Draft, IClashTemp, IProfiles, IVerge};
|
||||
use crate::config::ClashN;
|
||||
use once_cell::sync::OnceCell;
|
||||
use serde_yaml::Mapping;
|
||||
|
||||
pub struct Config {
|
||||
clash_config: Draft<IClashTemp>,
|
||||
verge_config: Draft<IVerge>,
|
||||
profiles_config: Draft<IProfiles>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -10,11 +14,33 @@ impl Config {
|
||||
static CONFIG: OnceCell<Config> = OnceCell::new();
|
||||
|
||||
CONFIG.get_or_init(|| Config {
|
||||
clash_config: Draft::from(IClashTemp::new()),
|
||||
verge_config: Draft::from(IVerge::new()),
|
||||
profiles_config: Draft::from(IProfiles::new()),
|
||||
})
|
||||
}
|
||||
|
||||
// pub fn clash<'a>() -> MappedMutexGuard<'a, IClash> {
|
||||
// Self::global().clash_config.latest()
|
||||
// }
|
||||
|
||||
// pub fn verge<'a>() -> MappedMutexGuard<'a, IVerge> {
|
||||
// Self::global().verge_config.latest()
|
||||
// }
|
||||
|
||||
// pub fn profiles<'a>() -> MappedMutexGuard<'a, IProfiles> {
|
||||
// Self::global().profiles_config.latest()
|
||||
// }
|
||||
|
||||
pub fn clash() -> Draft<IClashTemp> {
|
||||
Self::global().clash_config.clone()
|
||||
}
|
||||
|
||||
pub fn verge() -> Draft<IVerge> {
|
||||
Self::global().verge_config.clone()
|
||||
}
|
||||
|
||||
pub fn profiles() -> Draft<IProfiles> {
|
||||
Self::global().profiles_config.clone()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{IProfiles, IVerge};
|
||||
use super::{IClash, IClashTemp, IProfiles, IVerge};
|
||||
use parking_lot::{MappedMutexGuard, Mutex, MutexGuard};
|
||||
use serde_yaml::Mapping;
|
||||
use std::sync::Arc;
|
||||
@@ -15,6 +15,16 @@ macro_rules! draft_define {
|
||||
MutexGuard::map(self.inner.lock(), |guard| &mut guard.0)
|
||||
}
|
||||
|
||||
pub fn latest(&self) -> MappedMutexGuard<$id> {
|
||||
MutexGuard::map(self.inner.lock(), |inner| {
|
||||
if inner.1.is_none() {
|
||||
&mut inner.0
|
||||
} else {
|
||||
inner.1.as_mut().unwrap()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn draft(&self) -> MappedMutexGuard<$id> {
|
||||
MutexGuard::map(self.inner.lock(), |mut inner| {
|
||||
if inner.1.is_none() {
|
||||
@@ -54,6 +64,8 @@ macro_rules! draft_define {
|
||||
};
|
||||
}
|
||||
|
||||
draft_define!(IClash);
|
||||
draft_define!(IClashTemp);
|
||||
draft_define!(IVerge);
|
||||
draft_define!(Mapping);
|
||||
draft_define!(IProfiles);
|
||||
@@ -85,6 +97,9 @@ fn test_draft() {
|
||||
assert_eq!(draft.draft().enable_auto_launch, Some(false));
|
||||
assert_eq!(draft.draft().enable_tun_mode, Some(true));
|
||||
|
||||
assert_eq!(draft.latest().enable_auto_launch, Some(false));
|
||||
assert_eq!(draft.latest().enable_tun_mode, Some(true));
|
||||
|
||||
assert!(draft.apply().is_some());
|
||||
assert!(draft.apply().is_none());
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::{fs, io::Write};
|
||||
|
||||
#[deprecated]
|
||||
pub struct ProfilesN {
|
||||
pub config: Arc<Mutex<IProfiles>>,
|
||||
}
|
||||
@@ -87,6 +88,10 @@ macro_rules! patch {
|
||||
}
|
||||
|
||||
impl IProfiles {
|
||||
pub fn new() -> Self {
|
||||
Self::read_file()
|
||||
}
|
||||
|
||||
/// read the config from the file
|
||||
pub fn read_file() -> Self {
|
||||
let mut profiles = config::read_yaml::<Self>(dirs::profiles_path());
|
||||
|
||||
@@ -5,6 +5,7 @@ use parking_lot::Mutex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[deprecated]
|
||||
pub struct VergeN {
|
||||
pub config: Arc<Mutex<IVerge>>,
|
||||
}
|
||||
@@ -51,8 +52,7 @@ impl VergeN {
|
||||
/// ### `verge.yaml` schema
|
||||
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct IVerge {
|
||||
/// app listening port
|
||||
/// for app singleton
|
||||
/// app listening port for app singleton
|
||||
pub app_singleton_port: Option<u16>,
|
||||
|
||||
// i18n
|
||||
|
||||
Reference in New Issue
Block a user