mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-05-20 07:26:58 -04:00
fix: disconnect VPN when sleep/shutdown
This commit is contained in:
@@ -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
|
||||
|
@@ -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> {
|
||||
|
@@ -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(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user