Added inhibitor support

This commit is contained in:
Ezri Brimhall 2025-09-11 16:48:00 -06:00
parent 425d101f07
commit 3dfe5f0152
Signed by: ezri
GPG Key ID: 058A78E5680C6F24

View File

@ -25,6 +25,7 @@ import re
import psutil
import signal
from datetime import timedelta
from vpn_manager.service.inhibitor import Inhibitor
class LoginTarget(StrEnum):
@ -86,6 +87,20 @@ class GlobalProtectConnection(
logger = logging.getLogger(f"{__name__}.GlobalProtectConnection")
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self._inhibitor = Inhibitor(
Inhibitor.What.sleep,
"vpn-manager",
"Disconnect from VPN before sleep",
"delay",
)
async def inhibitor_disconnect():
await self.disconnect(timeout_sec=2)
self._inhibitor.on_sleep_or_shutdown(inhibitor_disconnect)
def get_auth_cache_timeout(self, options: dict[str, Variant]) -> timedelta | None:
"""Get the timeout for the authdata cache."""
timeout = options.get("auth_cache_timeout")
@ -253,6 +268,15 @@ class GlobalProtectConnection(
"Cowardly refusing to connect to VPN while already connected."
)
# Take logind inhibitor
try:
await self._inhibitor.acquire()
except:
self.logger.warning(
f"Unable to acquire sleep delay inhibitor. Networking will be broken on resume from suspend."
)
# Create subprocess
proc = await create_subprocess_exec(
"/usr/bin/openconnect",
"--protocol=gp",
@ -347,8 +371,10 @@ class GlobalProtectConnection(
self._disconnecting = False
except CancelledError:
self.logger.debug("exit wait canceled.")
finally:
self._inhibitor.release()
async def disconnect(self):
async def disconnect(self, *, timeout_sec: int = 10):
f"""{super().disconnect.__doc__}"""
if self._proc is None:
self.logger.warn(
@ -365,7 +391,7 @@ class GlobalProtectConnection(
return
# If the process takes longer than 10 seconds to exit, kill it sith WIGKILL
try:
async with timeout(10):
async with timeout(timeout_sec):
await self._proc_wait_task
except TimeoutError:
self._proc.kill()