From 3dfe5f0152b63d60af1baa5715d27b455fd9c127 Mon Sep 17 00:00:00 2001 From: Ezri Brimhall Date: Thu, 11 Sep 2025 16:48:00 -0600 Subject: [PATCH] Added inhibitor support --- src/vpn_manager_globalprotect/backend.py | 30 ++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/vpn_manager_globalprotect/backend.py b/src/vpn_manager_globalprotect/backend.py index 17cb137..853e24f 100644 --- a/src/vpn_manager_globalprotect/backend.py +++ b/src/vpn_manager_globalprotect/backend.py @@ -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()