Added inhibitor support
This commit is contained in:
parent
425d101f07
commit
3dfe5f0152
@ -25,6 +25,7 @@ import re
|
|||||||
import psutil
|
import psutil
|
||||||
import signal
|
import signal
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from vpn_manager.service.inhibitor import Inhibitor
|
||||||
|
|
||||||
|
|
||||||
class LoginTarget(StrEnum):
|
class LoginTarget(StrEnum):
|
||||||
@ -86,6 +87,20 @@ class GlobalProtectConnection(
|
|||||||
|
|
||||||
logger = logging.getLogger(f"{__name__}.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:
|
def get_auth_cache_timeout(self, options: dict[str, Variant]) -> timedelta | None:
|
||||||
"""Get the timeout for the authdata cache."""
|
"""Get the timeout for the authdata cache."""
|
||||||
timeout = options.get("auth_cache_timeout")
|
timeout = options.get("auth_cache_timeout")
|
||||||
@ -253,6 +268,15 @@ class GlobalProtectConnection(
|
|||||||
"Cowardly refusing to connect to VPN while already connected."
|
"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(
|
proc = await create_subprocess_exec(
|
||||||
"/usr/bin/openconnect",
|
"/usr/bin/openconnect",
|
||||||
"--protocol=gp",
|
"--protocol=gp",
|
||||||
@ -347,8 +371,10 @@ class GlobalProtectConnection(
|
|||||||
self._disconnecting = False
|
self._disconnecting = False
|
||||||
except CancelledError:
|
except CancelledError:
|
||||||
self.logger.debug("exit wait canceled.")
|
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__}"""
|
f"""{super().disconnect.__doc__}"""
|
||||||
if self._proc is None:
|
if self._proc is None:
|
||||||
self.logger.warn(
|
self.logger.warn(
|
||||||
@ -365,7 +391,7 @@ class GlobalProtectConnection(
|
|||||||
return
|
return
|
||||||
# If the process takes longer than 10 seconds to exit, kill it sith WIGKILL
|
# If the process takes longer than 10 seconds to exit, kill it sith WIGKILL
|
||||||
try:
|
try:
|
||||||
async with timeout(10):
|
async with timeout(timeout_sec):
|
||||||
await self._proc_wait_task
|
await self._proc_wait_task
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
self._proc.kill()
|
self._proc.kill()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user