refactor: wip

This commit is contained in:
GyDi
2022-11-16 01:26:41 +08:00
parent 902aed671a
commit f5f865a139
11 changed files with 247 additions and 90 deletions

View File

@@ -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));
}