Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🍎 Fix smartdns service restart on MacOS. #114

Merged
merged 2 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ trust-dns-server = { git = "https://github.com/mokeyish/trust-dns.git", rev = "1
# rustls-native-certs = { git = "https://github.com/mokeyish/rustls-native-certs.git" }
surge-ping = { git = "https://github.com/mokeyish/surge-ping.git", rev = "04c51d6" }
hostname = { git = "https://github.com/mokeyish/hostname.git", branch = "dev" }
socket2 = { git = "https://github.com/mokeyish/socket2.git", rev = "80306e7"}

[dependencies]
cfg-if = "1"
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Cli {
};
if let Some(out) = out {
if let Ok(out) = String::from_utf8(out.stdout) {
info!("\n{}", out);
print!("{}", out);
} else {
warn!("get service status failed.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/service/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,11 @@ impl Installer {
match strategy {
InstallStrategy::Backup => {
let mut path = dest_path.to_path_buf();
path.append_extension("old");
path.append_extension("bak");
fs::copy(dest_path.as_path(), path)?;
}
InstallStrategy::Preserve => {
dest_path.append_extension("new");
dest_path.append_extension("default");
}
InstallStrategy::Overide => (),
}
Expand Down
16 changes: 14 additions & 2 deletions src/service/macos/brew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@ pub fn create_service_definition() -> ServiceDefinition {
args: vec!["services".into(), "restart".into(), SERVICE_NAME.into()],
}),
status: Some(ServiceCommand {
program: brew.into(),
args: vec!["services".into(), "info".into(), SERVICE_NAME.into()],
program: "sh".into(),
args: vec![
"-c".into(),
format!(
r#"
O=$(brew services info {}) && echo "$O" | grep -q "Running: true" &&
(echo "$O" && exit 0) || (echo "$O" && exit 1)
"#,
SERVICE_NAME
)
.lines()
.collect::<String>()
.into(),
],
}),
};

Expand Down
26 changes: 18 additions & 8 deletions src/service/service_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ impl From<ServiceDefinition> for ServiceManager {

impl ServiceManager {
pub fn install(&self) -> io::Result<()> {
// try stopping an existing running service.
self.try_stop().unwrap_or_default();

// install files.
self.definition.installer.install()?;

if let Some(install) = self.definition.commands.install.as_ref() {
Expand All @@ -64,7 +68,7 @@ impl ServiceManager {
}

pub fn uninstall(&self, purge: bool) -> io::Result<()> {
self.stop().unwrap_or_default();
self.try_stop().unwrap_or_default();

if let Some(uninstall) = self.definition.commands.uninstall.as_ref() {
uninstall.spawn()?;
Expand All @@ -79,19 +83,23 @@ impl ServiceManager {
pub fn start(&self) -> io::Result<()> {
if !matches!(self.status(), Ok(ServiceStatus::Running(_))) {
self.definition.commands.start.spawn()?;
info!("Successfully started service {}", self.definition.name);
} else {
info!("Service {} already started", self.definition.name);
}

info!("Successfully started service {}", self.definition.name);
Ok(())
}

pub fn stop(&self) -> io::Result<()> {
// Make sure the service is stopped.
self.try_stop()?;
info!("Successfully stopped service {}", self.definition.name);
Ok(())
}

pub fn try_stop(&self) -> io::Result<()> {
if !matches!(self.status(), Ok(ServiceStatus::Dead(_))) {
self.definition.commands.stop.spawn()?;
}

info!("Successfully stopped service {}", self.definition.name);
Ok(())
}

Expand All @@ -102,7 +110,7 @@ impl ServiceManager {
info!("Successfully restarted service {}", self.definition.name);
}
None => {
self.stop().unwrap_or_default();
self.try_stop().unwrap_or_default();
std::thread::sleep(Duration::from_millis(500));
self.start()?;
}
Expand Down Expand Up @@ -250,7 +258,9 @@ mod tests {
assert!(stdout.contains("Linux"));
} else if #[cfg(target_os="macos")] {
assert!(stdout.contains("Darwin"));
} else {
} else if #[cfg(target_os="android")] {
assert!(stdout.contains("Android"));
} else {
unimplemented!()
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/service/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ pub(super) fn create_service_definition() -> ServiceDefinition {
status: Some(ServiceCommand {
program: "cmd.exe".into(),
args: vec![
"/C".into(),
["sc query ", SERVICE_NAME," | findstr STATE.*:.*RUNNING > NUL && (sc query smartdns-rs && exit 0) || (sc query smartdns-rs && exit 1)"].concat().into()
],
"/C".into(),
format!(
r#"
sc query {0} | findstr STATE.*:.*RUNNING > NUL
&& (sc query {0} && exit 0) || (sc query {0} && exit 1)
"#,
SERVICE_NAME
)
.lines()
.collect::<String>()
.into(),
],
}),
};

Expand Down