Skip to content

Commit a108d10

Browse files
committed
Fix cert not regenerated when daemon already running
ensure_bundle was skipped when reconcile_running_state found an existing daemon, so server certs cleared by migration were never regenerated. Move ensure_bundle before the running-state check, and restart the daemon when it is already running so new certs take effect.
1 parent 299cfd3 commit a108d10

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

src/service.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio::sync::watch;
1313
use crate::certs::{ensure_bundle, load_or_create_bundle};
1414
use crate::config::AppConfig;
1515
use crate::hosts::{
16-
apply_hosts, backup_hosts_file, hosts_are_applied, remove_hosts, restore_hosts_file,
16+
apply_hosts, backup_hosts_file, remove_hosts, restore_hosts_file,
1717
validate_hosts_backup_file,
1818
};
1919
use crate::hosts_store::{BackupState, backup_state, clear_hosts_backup};
@@ -145,18 +145,18 @@ pub fn helper_start(config_path: Option<PathBuf>) -> Result<()> {
145145
ensure_elevated(&config, true)?;
146146
ensure_loopback_alias(&config)?;
147147

148-
let current = reconcile_running_state(&paths, &config)?;
149-
if current.running {
150-
ensure_runtime_environment(&paths, &config)?;
151-
log_info(&paths, "helper-start", "检测到服务已在运行,跳过重复启动");
152-
return Ok(());
153-
}
154-
155148
let bundle = ensure_bundle(&config, &paths.cert_dir)?;
156149
#[cfg(not(target_os = "macos"))]
157150
install_ca(&bundle.ca_cert_path, &config.ca_common_name)?;
158151
#[cfg(target_os = "macos")]
159152
let _ = bundle;
153+
154+
let current = reconcile_running_state(&paths, &config)?;
155+
if current.running {
156+
log_info(&paths, "helper-start", "检测到旧服务仍在运行,正在重启以加载新证书");
157+
stop_running_daemon(&paths, &config)?;
158+
}
159+
160160
apply_hosts(&config, &paths)?;
161161
let _ = flush_dns_cache();
162162
state::mark_starting(&paths)?;
@@ -438,18 +438,6 @@ fn reconcile_running_state(paths: &AppPaths, config: &AppConfig) -> Result<state
438438
Ok(current)
439439
}
440440

441-
fn ensure_runtime_environment(paths: &AppPaths, config: &AppConfig) -> Result<()> {
442-
ensure_loopback_alias(config)?;
443-
444-
if !hosts_are_applied(config)? {
445-
apply_hosts(config, paths)?;
446-
let _ = flush_dns_cache();
447-
log_info(&paths, "helper-start", "已修复缺失的 hosts 接管规则");
448-
}
449-
450-
Ok(())
451-
}
452-
453441
fn wait_until_running(paths: &AppPaths, config: &AppConfig, timeout: Duration) -> Result<()> {
454442
let deadline = Instant::now() + timeout;
455443
while Instant::now() < deadline {
@@ -557,6 +545,20 @@ fn terminate_running_service(paths: &AppPaths) -> Result<Option<String>> {
557545
Ok(None)
558546
}
559547

548+
fn stop_running_daemon(paths: &AppPaths, config: &AppConfig) -> Result<()> {
549+
if let Some(issue) = terminate_running_service(paths)? {
550+
log_warn(
551+
paths,
552+
"helper-start",
553+
&format!("停止旧服务时出现问题:{issue}"),
554+
);
555+
}
556+
let _ = reconcile_running_state(paths, config);
557+
let _ = state::clear_pid(paths);
558+
thread::sleep(Duration::from_millis(300));
559+
Ok(())
560+
}
561+
560562
fn cleanup_hosts_state(paths: &AppPaths) -> (String, Option<String>) {
561563
match backup_state(paths) {
562564
BackupState::Ready => match validate_hosts_backup_file(paths) {

0 commit comments

Comments
 (0)