diff --git a/src/config.rs b/src/config.rs index d2febaa..1b84f28 100644 --- a/src/config.rs +++ b/src/config.rs @@ -15,7 +15,6 @@ use crate::utils::create_parent_dir; pub struct Config { pub remote_mihomo_binary_url: String, pub remote_config_url: String, - pub remote_mmdb_url: String, pub mihomo_binary_path: String, pub mihomo_config_root: String, pub user_systemd_root: String, @@ -68,9 +67,6 @@ impl Config { Config { remote_mihomo_binary_url: String::from(""), remote_config_url: String::from(""), - remote_mmdb_url: String::from( - "https://cdn.jsdelivr.net/gh/Dreamacro/maxmind-geoip@release/Country.mmdb", - ), mihomo_binary_path: String::from("~/.local/bin/mihomo"), mihomo_config_root: String::from("~/.config/mihomo"), user_systemd_root: String::from("~/.config/systemd/user"), @@ -81,20 +77,15 @@ impl Config { bind_address: Some(String::from("*")), mode: MihomoMode::Rule, log_level: MihomoLogLevel::Info, - ipv6: Some(false), - external_controller: Some(String::from("127.0.0.1:9090")), - external_ui: None, + ipv6: Some(true), + external_controller: Some(String::from("0.0.0.0:9090")), + external_ui: Some(String::from("ui")), secret: None, }, } } /// Read raw config string from path and parse with crate toml. - /// - /// TODO: Currently this will return error that shows a missing field error when parse fails, - /// however the error message always shows the line and column number as `line 1 column 1`, - /// which is because the function `fs::read_to_string` preserves newline characters as `\n`, - /// resulting in a single-lined string. pub fn setup_from(path: &str) -> Result { let raw_config = fs::read_to_string(path)?; let config: Config = toml::from_str(&raw_config)?; @@ -112,7 +103,7 @@ impl Config { /// /// * If config file does not exist, creates default config file to path and returns error. /// * If found, tries to parse the file and returns error if parse fails or fields found undefined. -pub fn parse_config(path: &str, prefix: &str) -> Result { +pub fn parse_config(path: &str) -> Result { // Create `~/.config` directory if not exists create_parent_dir(path)?; @@ -121,8 +112,7 @@ pub fn parse_config(path: &str, prefix: &str) -> Result { if !config_path.exists() { Config::new().write(config_path)?; bail!( - "{prefix} Created default config at `{path}`, edit as needed\n{prefix} Run again to finish setup", - prefix = prefix.yellow(), + "created default config at `{path}`, run again to finish setup", path = path.underline() ); } @@ -131,7 +121,7 @@ pub fn parse_config(path: &str, prefix: &str) -> Result { let config = Config::setup_from(path)?; let required_urls = [ ("remote_config_url", &config.remote_config_url), - ("remote_mmdb_url", &config.remote_mmdb_url), + // ("remote_mmdb_url", &config.remote_mmdb_url), ("mihomo_binary_path", &config.mihomo_binary_path), ("mihomo_config_root", &config.mihomo_config_root), ("user_systemd_root", &config.user_systemd_root), diff --git a/src/main.rs b/src/main.rs index 962c65a..cda9b54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,7 @@ async fn cli() -> Result<()> { let config_path = tilde(&args.mihoro_config).to_string(); // Initial setup and parse config file - let config: Config = parse_config(&config_path, prefix)?; + let config: Config = parse_config(&config_path)?; // Expand mihomo related paths and target directories let mihomo_gzipped_path = "mihomo.tar.gz"; @@ -59,8 +59,8 @@ async fn cli() -> Result<()> { let mihomo_target_config_root = tilde(&config.mihomo_config_root).to_string(); let mihomo_target_config_path = tilde(&format!("{}/config.yaml", config.mihomo_config_root)).to_string(); - let mihomo_target_mmdb_path = - tilde(&format!("{}/Country.mmdb", config.mihomo_config_root)).to_string(); + // let mihomo_target_mmdb_path = + // tilde(&format!("{}/Country.mmdb", config.mihomo_config_root)).to_string(); let mihomo_target_service_path = tilde(&format!("{}/mihomo.service", config.user_systemd_root)).to_string(); @@ -110,9 +110,6 @@ async fn cli() -> Result<()> { .await?; apply_mihomo_override(&mihomo_target_config_path, &config.mihomo_config)?; - // Download remote Country.mmdb - download_file(&client, &config.remote_mmdb_url, &mihomo_target_mmdb_path).await?; - // Create mihomo.service systemd file create_mihomo_service( &mihomo_target_binary_path, @@ -135,65 +132,52 @@ async fn cli() -> Result<()> { apply_mihomo_override(&mihomo_target_config_path, &config.mihomo_config)?; println!("{} Updated and applied config overrides", prefix.yellow()); - // Download remote Country.mmdb - download_file(&client, &config.remote_mmdb_url, &mihomo_target_mmdb_path).await?; - // Restart mihomo systemd service println!("{} Restart mihomo.service", prefix.green()); Systemctl::new().restart("mihomo.service").execute()?; } Some(Commands::Apply) => { // Apply mihomo config override - apply_mihomo_override(&mihomo_target_config_path, &config.mihomo_config).and_then( - |_| { - println!("{} Applied mihomo config overrides", prefix.green().bold()); - Ok(()) - }, - )?; + apply_mihomo_override(&mihomo_target_config_path, &config.mihomo_config).map(|_| { + println!("{} Applied mihomo config overrides", prefix.green().bold()); + })?; // Restart mihomo systemd service Systemctl::new() .restart("mihomo.service") .execute() - .and_then(|_| { + .map(|_| { println!("{} Restarted mihomo.service", prefix.green().bold()); - Ok(()) })?; } Some(Commands::Start) => { Systemctl::new() .start("mihomo.service") .execute() - .and_then(|_| { + .map(|_| { println!("{} Started mihomo.service", prefix.green()); - Ok(()) })?; } Some(Commands::Status) => { Systemctl::new().status("mihomo.service").execute()?; } Some(Commands::Stop) => { - Systemctl::new() - .stop("mihomo.service") - .execute() - .and_then(|_| { - println!("{} Stopped mihomo.service", prefix.green()); - Ok(()) - })?; + Systemctl::new().stop("mihomo.service").execute().map(|_| { + println!("{} Stopped mihomo.service", prefix.green()); + })?; } Some(Commands::Restart) => { Systemctl::new() .restart("mihomo.service") .execute() - .and_then(|_| { + .map(|_| { println!("{} Restarted mihomo.service", prefix.green()); - Ok(()) })?; } Some(Commands::Log) => { Command::new("journalctl") .arg("--user") - .arg("-u") + .arg("-xeu") .arg("mihomo.service") .arg("-n") .arg("10") diff --git a/src/utils.rs b/src/utils.rs index b044353..f9419c0 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -101,9 +101,8 @@ pub async fn download_file(client: &Client, url: &str, path: &str) -> Result<()> pub fn delete_file(path: &str, prefix: &str) -> Result<()> { // Delete file if exists if Path::new(path).exists() { - fs::remove_file(path).and_then(|_| { + fs::remove_file(path).map(|_| { println!("{} Removed {}", prefix.red(), path.underline().yellow()); - Ok(()) })?; } Ok(()) @@ -147,15 +146,13 @@ After=network.target NetworkManager.service systemd-networkd.service iwd.service Type=simple LimitNPROC=500 LimitNOFILE=1000000 -CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME -AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME Restart=always ExecStartPre=/usr/bin/sleep 1s ExecStart={} -d {} ExecReload=/bin/kill -HUP $MAINPID [Install] -WantedBy=multi-user.target", +WantedBy=default.target", mihomo_binary_path, mihomo_config_root );