klippy_connection: get unix socket peer credentials
Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
eb6aeddf78
commit
b21f909705
|
@ -13,6 +13,8 @@ import json
|
||||||
import getpass
|
import getpass
|
||||||
import confighelper
|
import confighelper
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import socket
|
||||||
|
import struct
|
||||||
from utils import ServerError
|
from utils import ServerError
|
||||||
|
|
||||||
# Annotation imports
|
# Annotation imports
|
||||||
|
@ -32,6 +34,7 @@ if TYPE_CHECKING:
|
||||||
from websockets import WebRequest, Subscribable
|
from websockets import WebRequest, Subscribable
|
||||||
from components.klippy_apis import KlippyAPI
|
from components.klippy_apis import KlippyAPI
|
||||||
from components.file_manager.file_manager import FileManager
|
from components.file_manager.file_manager import FileManager
|
||||||
|
from asyncio.trsock import TransportSocket
|
||||||
FlexCallback = Callable[..., Optional[Coroutine]]
|
FlexCallback = Callable[..., Optional[Coroutine]]
|
||||||
|
|
||||||
INIT_TIME = .25
|
INIT_TIME = .25
|
||||||
|
@ -54,6 +57,7 @@ class KlippyConnection:
|
||||||
self._klippy_info: Dict[str, Any] = {}
|
self._klippy_info: Dict[str, Any] = {}
|
||||||
self.init_list: List[str] = []
|
self.init_list: List[str] = []
|
||||||
self._missing_reqs: Set[str] = set()
|
self._missing_reqs: Set[str] = set()
|
||||||
|
self._peer_cred: Dict[str, int] = {}
|
||||||
self.init_attempts: int = 0
|
self.init_attempts: int = 0
|
||||||
self._state: str = "disconnected"
|
self._state: str = "disconnected"
|
||||||
self.subscriptions: Dict[Subscribable, Dict[str, Any]] = {}
|
self.subscriptions: Dict[Subscribable, Dict[str, Any]] = {}
|
||||||
|
@ -87,6 +91,10 @@ class KlippyConnection:
|
||||||
def missing_requirements(self) -> List[str]:
|
def missing_requirements(self) -> List[str]:
|
||||||
return list(self._missing_reqs)
|
return list(self._missing_reqs)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def peer_credentials(self) -> Dict[str, int]:
|
||||||
|
return dict(self._peer_cred)
|
||||||
|
|
||||||
async def wait_connected(self) -> bool:
|
async def wait_connected(self) -> bool:
|
||||||
if (
|
if (
|
||||||
self.connection_task is None or
|
self.connection_task is None or
|
||||||
|
@ -212,9 +220,29 @@ class KlippyConnection:
|
||||||
continue
|
continue
|
||||||
logging.info("Klippy Connection Established")
|
logging.info("Klippy Connection Established")
|
||||||
self.writer = writer
|
self.writer = writer
|
||||||
|
self._get_peer_credentials(writer)
|
||||||
self.event_loop.create_task(self._read_stream(reader))
|
self.event_loop.create_task(self._read_stream(reader))
|
||||||
return await self._init_klippy_connection()
|
return await self._init_klippy_connection()
|
||||||
|
|
||||||
|
def _get_peer_credentials(self, writer: asyncio.StreamWriter) -> None:
|
||||||
|
sock: TransportSocket
|
||||||
|
sock = writer.get_extra_info("socket", None)
|
||||||
|
if sock is None:
|
||||||
|
logging.debug(
|
||||||
|
"Unable to get Unix Socket, cant fetch peer credentials"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
data = sock.getsockopt(socket.SOL_SOCKET, socket.SO_PEERCRED, 12)
|
||||||
|
pid, uid, gid = struct.unpack("@LLL", data)
|
||||||
|
self._peer_cred = {
|
||||||
|
"process_id": pid,
|
||||||
|
"user_id": uid,
|
||||||
|
"group_id": gid
|
||||||
|
}
|
||||||
|
logging.debug(
|
||||||
|
f"Klippy Connection: Received Peer Credentials: {self._peer_cred}"
|
||||||
|
)
|
||||||
|
|
||||||
async def _init_klippy_connection(self) -> bool:
|
async def _init_klippy_connection(self) -> bool:
|
||||||
self.init_list = []
|
self.init_list = []
|
||||||
self._missing_reqs.clear()
|
self._missing_reqs.clear()
|
||||||
|
@ -489,6 +517,7 @@ class KlippyConnection:
|
||||||
request.notify(ServerError("Klippy Disconnected", 503))
|
request.notify(ServerError("Klippy Disconnected", 503))
|
||||||
self.pending_requests = {}
|
self.pending_requests = {}
|
||||||
self.subscriptions = {}
|
self.subscriptions = {}
|
||||||
|
self._peer_cred = {}
|
||||||
self._missing_reqs.clear()
|
self._missing_reqs.clear()
|
||||||
logging.info("Klippy Connection Removed")
|
logging.info("Klippy Connection Removed")
|
||||||
await self.server.send_event("server:klippy_disconnect")
|
await self.server.send_event("server:klippy_disconnect")
|
||||||
|
|
Loading…
Reference in New Issue