Skip to content

Commit

Permalink
refactor: remove remote_mmdb_url and update config defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Dec 29, 2023
1 parent 7685864 commit bf69ff1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 50 deletions.
22 changes: 6 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand All @@ -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<Config> {
let raw_config = fs::read_to_string(path)?;
let config: Config = toml::from_str(&raw_config)?;
Expand All @@ -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<Config> {
pub fn parse_config(path: &str) -> Result<Config> {
// Create `~/.config` directory if not exists
create_parent_dir(path)?;

Expand All @@ -121,8 +112,7 @@ pub fn parse_config(path: &str, prefix: &str) -> Result<Config> {
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()
);
}
Expand All @@ -131,7 +121,7 @@ pub fn parse_config(path: &str, prefix: &str) -> Result<Config> {
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),
Expand Down
42 changes: 13 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();

Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down
7 changes: 2 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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
);

Expand Down

0 comments on commit bf69ff1

Please sign in to comment.