fix: disconnect VPN when sleep/shutdown

This commit is contained in:
Kevin Yue
2025-01-12 14:41:03 +08:00
parent ec85e857bc
commit 9740231910
18 changed files with 285 additions and 172 deletions

View File

@@ -18,7 +18,7 @@ roxmltree.workspace = true
serde.workspace = true
specta = { workspace = true, features = ["derive"] }
urlencoding.workspace = true
tokio.workspace = true
tokio = { workspace = true, features = ["process", "signal", "macros"] }
serde_json.workspace = true
whoami.workspace = true
tempfile.workspace = true

View File

@@ -3,8 +3,13 @@ use tokio::fs;
use crate::GP_SERVICE_LOCK_FILE;
async fn read_port() -> anyhow::Result<String> {
let port = fs::read_to_string(GP_SERVICE_LOCK_FILE).await?;
Ok(port.trim().to_string())
// PID:PORT
let lock_content = fs::read_to_string(GP_SERVICE_LOCK_FILE).await?;
let port = lock_content
.split(':')
.last()
.ok_or_else(|| anyhow::anyhow!("Invalid lock file content"))?;
Ok(port.to_string())
}
pub async fn http_endpoint() -> anyhow::Result<String> {

View File

@@ -2,18 +2,20 @@ use std::path::PathBuf;
pub struct LockFile {
path: PathBuf,
pid: u32,
}
impl LockFile {
pub fn new<P: Into<PathBuf>>(path: P) -> Self {
Self { path: path.into() }
pub fn new<P: Into<PathBuf>>(path: P, pid: u32) -> Self {
Self { path: path.into(), pid }
}
pub fn exists(&self) -> bool {
self.path.exists()
}
pub fn lock(&self, content: impl AsRef<[u8]>) -> anyhow::Result<()> {
pub fn lock(&self, content: &str) -> anyhow::Result<()> {
let content = format!("{}:{}", self.pid, content);
std::fs::write(&self.path, content)?;
Ok(())
}