Skip to content

Commit 299cfd3

Browse files
committed
Auto-delete server certs on config upgrade to force re-signing
Add migrate_config_if_needed() that checks the config version marker, backs up the old config, writes the new default, and deletes the server cert + key so the next setup/start re-signs with the updated certificate_domains (now including idcflare.com). Called from init_config, setup, prepare_certificate, run_foreground, and helper_start — every entry point that touches ensure_bundle.
1 parent 1192f45 commit 299cfd3

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/certs.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ pub fn ensure_bundle(config: &AppConfig, root: &Path) -> Result<CertificateBundl
2626
generate_bundle(config, root, false)
2727
}
2828

29+
pub fn clear_server_certs(root: &Path) {
30+
for name in [
31+
"linuxdo-accelerator-server.crt",
32+
"linuxdo-accelerator-server.key",
33+
] {
34+
let path = root.join(name);
35+
if path.exists() {
36+
let _ = fs::remove_file(&path);
37+
}
38+
}
39+
}
40+
2941
pub fn load_or_create_bundle(config: &AppConfig, root: &Path) -> Result<CertificateBundle> {
3042
generate_bundle(config, root, false)
3143
}

src/config.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,33 @@ fn marker_matches_current_version(marker_path: &Path) -> bool {
167167
}
168168

169169
impl AppConfig {
170+
pub fn migrate_config_if_needed(path: &Path, cert_dir: &Path) -> Result<bool> {
171+
if let Some(parent) = path.parent() {
172+
fs::create_dir_all(parent)
173+
.with_context(|| format!("failed to create {}", parent.display()))?;
174+
}
175+
176+
let marker_path = version_marker_path(path);
177+
if path.exists() && marker_matches_current_version(&marker_path) {
178+
return Ok(false);
179+
}
180+
181+
let backup_path = backup_config_path(path);
182+
fs::copy(path, &backup_path).with_context(|| {
183+
format!(
184+
"failed to back up {} to {}",
185+
path.display(),
186+
backup_path.display()
187+
)
188+
})?;
189+
fs::write(path, DEFAULT_APP_CONFIG)
190+
.with_context(|| format!("failed to write config {}", path.display()))?;
191+
write_version_marker(&marker_path)?;
192+
crate::certs::clear_server_certs(cert_dir);
193+
cleanup_legacy_network_profile(path)?;
194+
Ok(true)
195+
}
196+
170197
pub fn load_or_create(path: &Path) -> Result<Self> {
171198
if let Some(parent) = path.parent() {
172199
fs::create_dir_all(parent)

src/service.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub fn resolve_paths(config_override: Option<PathBuf>) -> Result<AppPaths> {
3535

3636
pub fn init_config(config_path: Option<PathBuf>) -> Result<PathBuf> {
3737
let paths = resolve_paths(config_path)?;
38+
let _ = AppConfig::migrate_config_if_needed(&paths.config_path, &paths.cert_dir)?;
3839
let _ = AppConfig::load_or_create(&paths.config_path)?;
3940
Ok(paths.config_path)
4041
}
@@ -43,6 +44,7 @@ pub fn setup(config_path: Option<PathBuf>) -> Result<()> {
4344
let paths = resolve_paths(config_path)?;
4445
log_info(&paths, "setup", "开始准备系统加速环境");
4546
let result = (|| -> Result<()> {
47+
let _ = AppConfig::migrate_config_if_needed(&paths.config_path, &paths.cert_dir)?;
4648
let config = AppConfig::load_or_create(&paths.config_path)?;
4749
ensure_elevated(&config, true)?;
4850
ensure_loopback_alias(&config)?;
@@ -64,6 +66,7 @@ pub fn prepare_certificate(config_path: Option<PathBuf>) -> Result<()> {
6466
let paths = resolve_paths(config_path)?;
6567
log_info(&paths, "prepare-cert", "开始准备根证书");
6668
let result = (|| -> Result<()> {
69+
let _ = AppConfig::migrate_config_if_needed(&paths.config_path, &paths.cert_dir)?;
6770
let config = AppConfig::load_or_create(&paths.config_path)?;
6871
let bundle = ensure_bundle(&config, &paths.cert_dir)?;
6972
install_ca(&bundle.ca_cert_path, &config.ca_common_name)?;
@@ -87,6 +90,7 @@ pub async fn run_foreground(config_path: Option<PathBuf>, with_setup: bool) -> R
8790
"守护进程启动:直接进入前台代理"
8891
},
8992
);
93+
let _ = AppConfig::migrate_config_if_needed(&paths.config_path, &paths.cert_dir)?;
9094
let config = AppConfig::load_or_create(&paths.config_path)?;
9195
ensure_elevated(&config, with_setup)?;
9296
ensure_loopback_alias(&config)?;
@@ -135,6 +139,7 @@ pub async fn run_foreground(config_path: Option<PathBuf>, with_setup: bool) -> R
135139
pub fn helper_start(config_path: Option<PathBuf>) -> Result<()> {
136140
let paths = resolve_paths(config_path)?;
137141
log_info(&paths, "helper-start", "收到启动请求,开始准备加速环境");
142+
let _ = AppConfig::migrate_config_if_needed(&paths.config_path, &paths.cert_dir)?;
138143
let config = AppConfig::load_or_create(&paths.config_path)?;
139144
let start_result = (|| -> Result<()> {
140145
ensure_elevated(&config, true)?;

0 commit comments

Comments
 (0)