mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 01:00:29 +08:00
improved some logging
This commit is contained in:
parent
eeece34312
commit
499cbb0cc5
@ -28,8 +28,8 @@ from typing import Dict
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
class IpmiPasswdError(Exception):
|
class IpmiPasswdError(Exception):
|
||||||
def __init__(self, msg: str) -> None:
|
def __init__(self, path: str, number: int, msg: str) -> None:
|
||||||
super().__init__("Incorrect IPMI passwd file: " + msg)
|
super().__init__(f"Syntax error at {path}:{number}: {msg}")
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
@ -42,6 +42,7 @@ class IpmiUserCredentials:
|
|||||||
|
|
||||||
class IpmiAuthManager:
|
class IpmiAuthManager:
|
||||||
def __init__(self, path: str) -> None:
|
def __init__(self, path: str) -> None:
|
||||||
|
self.__path = path
|
||||||
with open(path) as passwd_file:
|
with open(path) as passwd_file:
|
||||||
self.__credentials = self.__parse_passwd_file(passwd_file.read().split("\n"))
|
self.__credentials = self.__parse_passwd_file(passwd_file.read().split("\n"))
|
||||||
|
|
||||||
@ -61,25 +62,25 @@ class IpmiAuthManager:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if " -> " not in line:
|
if " -> " not in line:
|
||||||
raise IpmiPasswdError(f"Missing ' -> ' operator at line #{number}")
|
raise IpmiPasswdError(self.__path, number, "Missing ' -> ' operator")
|
||||||
|
|
||||||
(left, right) = map(str.lstrip, line.split(" -> ", 1))
|
(left, right) = map(str.lstrip, line.split(" -> ", 1))
|
||||||
for (name, pair) in [("left", left), ("right", right)]:
|
for (name, pair) in [("left", left), ("right", right)]:
|
||||||
if ":" not in pair:
|
if ":" not in pair:
|
||||||
raise IpmiPasswdError(f"Missing ':' operator in {name} credentials at line #{number}")
|
raise IpmiPasswdError(self.__path, number, f"Missing ':' operator in {name} credentials")
|
||||||
|
|
||||||
(ipmi_user, ipmi_passwd) = left.split(":")
|
(ipmi_user, ipmi_passwd) = left.split(":")
|
||||||
ipmi_user = ipmi_user.strip()
|
ipmi_user = ipmi_user.strip()
|
||||||
if len(ipmi_user) == 0:
|
if len(ipmi_user) == 0:
|
||||||
raise IpmiPasswdError(f"Empty IPMI user (left) at line #{number}")
|
raise IpmiPasswdError(self.__path, number, "Empty IPMI user (left)")
|
||||||
|
|
||||||
(kvmd_user, kvmd_passwd) = right.split(":")
|
(kvmd_user, kvmd_passwd) = right.split(":")
|
||||||
kvmd_user = kvmd_user.strip()
|
kvmd_user = kvmd_user.strip()
|
||||||
if len(kvmd_user) == 0:
|
if len(kvmd_user) == 0:
|
||||||
raise IpmiPasswdError(f"Empty KVMD user (left) at line #{number}")
|
raise IpmiPasswdError(self.__path, number, "Empty KVMD user (left)")
|
||||||
|
|
||||||
if ipmi_user in credentials:
|
if ipmi_user in credentials:
|
||||||
raise IpmiPasswdError(f"Found duplicating user {ipmi_user!r} (left) at line #{number}")
|
raise IpmiPasswdError(self.__path, number, f"Found duplicating user {ipmi_user!r} (left)")
|
||||||
|
|
||||||
credentials[ipmi_user] = IpmiUserCredentials(
|
credentials[ipmi_user] = IpmiUserCredentials(
|
||||||
ipmi_user=ipmi_user,
|
ipmi_user=ipmi_user,
|
||||||
|
|||||||
@ -32,8 +32,8 @@ from ...logging import get_logger
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
class VncAuthError(Exception):
|
class VncAuthError(Exception):
|
||||||
def __init__(self, msg: str) -> None:
|
def __init__(self, path: str, number: int, msg: str) -> None:
|
||||||
super().__init__(f"Incorrect VNCAuth passwd file: {msg}")
|
super().__init__(f"Syntax error at {path}:{number}: {msg}")
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
@ -73,19 +73,19 @@ class VncAuthManager:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if " -> " not in line:
|
if " -> " not in line:
|
||||||
raise VncAuthError(f"Missing ' -> ' operator at line #{number}")
|
raise VncAuthError(self.__path, number, "Missing ' -> ' operator")
|
||||||
|
|
||||||
(vnc_passwd, kvmd_userpass) = map(str.lstrip, line.split(" -> ", 1))
|
(vnc_passwd, kvmd_userpass) = map(str.lstrip, line.split(" -> ", 1))
|
||||||
if ":" not in kvmd_userpass:
|
if ":" not in kvmd_userpass:
|
||||||
raise VncAuthError(f"Missing ':' operator in KVMD credentials (right part) at line #{number}")
|
raise VncAuthError(self.__path, number, "Missing ':' operator in KVMD credentials (right part)")
|
||||||
|
|
||||||
(kvmd_user, kvmd_passwd) = kvmd_userpass.split(":")
|
(kvmd_user, kvmd_passwd) = kvmd_userpass.split(":")
|
||||||
kvmd_user = kvmd_user.strip()
|
kvmd_user = kvmd_user.strip()
|
||||||
if len(kvmd_user) == 0:
|
if len(kvmd_user) == 0:
|
||||||
raise VncAuthError(f"Empty KVMD user (right part) at line #{number}")
|
raise VncAuthError(self.__path, number, "Empty KVMD user (right part)")
|
||||||
|
|
||||||
if vnc_passwd in credentials:
|
if vnc_passwd in credentials:
|
||||||
raise VncAuthError(f"Found duplicating VNC password (left part) at line #{number}")
|
raise VncAuthError(self.__path, number, "Duplicating VNC password (left part)")
|
||||||
|
|
||||||
credentials[vnc_passwd] = VncAuthKvmdCredentials(kvmd_user, kvmd_passwd)
|
credentials[vnc_passwd] = VncAuthKvmdCredentials(kvmd_user, kvmd_passwd)
|
||||||
return credentials
|
return credentials
|
||||||
|
|||||||
@ -25,7 +25,6 @@ import pkgutil
|
|||||||
import functools
|
import functools
|
||||||
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
import Xlib.keysymdef
|
import Xlib.keysymdef
|
||||||
|
|
||||||
@ -47,35 +46,32 @@ class SymmapWebKey:
|
|||||||
|
|
||||||
def build_symmap(path: str) -> Dict[int, SymmapWebKey]:
|
def build_symmap(path: str) -> Dict[int, SymmapWebKey]:
|
||||||
# https://github.com/qemu/qemu/blob/95a9457fd44ad97c518858a4e1586a5498f9773c/ui/keymaps.c
|
# https://github.com/qemu/qemu/blob/95a9457fd44ad97c518858a4e1586a5498f9773c/ui/keymaps.c
|
||||||
return {
|
logger = get_logger()
|
||||||
x11_code: web_key
|
|
||||||
for (x11_code, at1_key) in [
|
symmap: Dict[int, SymmapWebKey] = {}
|
||||||
*list(X11_TO_AT1.items()),
|
for (src, items) in [
|
||||||
*list(_read_keyboard_layout(path).items()),
|
("<builtin>", list(X11_TO_AT1.items())),
|
||||||
]
|
(path, list(_read_keyboard_layout(path).items())),
|
||||||
if (web_key := _make_safe_web_key(at1_key)) is not None
|
]:
|
||||||
}
|
for (code, key) in items:
|
||||||
|
if (web_name := AT1_TO_WEB.get(key.code)) is not None:
|
||||||
|
if (
|
||||||
|
(web_name in ["ShiftLeft", "ShiftRight"] and key.shift) # pylint: disable=too-many-boolean-expressions
|
||||||
|
or (web_name in ["AltLeft", "AltRight"] and key.altgr)
|
||||||
|
or (web_name in ["ControlLeft", "ControlRight"] and key.ctrl)
|
||||||
|
):
|
||||||
|
logger.error("Invalid modifier key at mapping %s: %s / %s", src, web_name, key)
|
||||||
|
continue
|
||||||
|
symmap[code] = SymmapWebKey(
|
||||||
|
name=web_name,
|
||||||
|
shift=key.shift,
|
||||||
|
altgr=key.altgr,
|
||||||
|
ctrl=key.ctrl,
|
||||||
|
)
|
||||||
|
return symmap
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
def _make_safe_web_key(key: At1Key) -> Optional[SymmapWebKey]:
|
|
||||||
if (web_name := AT1_TO_WEB.get(key.code)) is not None:
|
|
||||||
if (
|
|
||||||
(web_name in ["ShiftLeft", "ShiftRight"] and key.shift) # pylint: disable=too-many-boolean-expressions
|
|
||||||
or (web_name in ["AltLeft", "AltRight"] and key.altgr)
|
|
||||||
or (web_name in ["ControlLeft", "ControlRight"] and key.ctrl)
|
|
||||||
):
|
|
||||||
get_logger().error("Invalid modifier key: %s / %s", web_name, key)
|
|
||||||
return None # Ехал модификатор через модификатор
|
|
||||||
return SymmapWebKey(
|
|
||||||
name=web_name,
|
|
||||||
shift=key.shift,
|
|
||||||
altgr=key.altgr,
|
|
||||||
ctrl=key.ctrl,
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
@functools.lru_cache()
|
@functools.lru_cache()
|
||||||
def _get_keysyms() -> Dict[str, int]:
|
def _get_keysyms() -> Dict[str, int]:
|
||||||
keysyms: Dict[str, int] = {}
|
keysyms: Dict[str, int] = {}
|
||||||
@ -117,14 +113,14 @@ def _read_keyboard_layout(path: str) -> Dict[int, At1Key]: # Keysym to evdev (a
|
|||||||
|
|
||||||
parts = line.split()
|
parts = line.split()
|
||||||
if len(parts) >= 2:
|
if len(parts) >= 2:
|
||||||
if (x11_code := _resolve_keysym(parts[0])) != 0:
|
if (code := _resolve_keysym(parts[0])) != 0:
|
||||||
try:
|
try:
|
||||||
layout[x11_code] = At1Key(
|
layout[code] = At1Key(
|
||||||
code=int(parts[1], 16),
|
code=int(parts[1], 16),
|
||||||
shift=("shift" in parts[2:]),
|
shift=("shift" in parts[2:]),
|
||||||
altgr=("altgr" in parts[2:]),
|
altgr=("altgr" in parts[2:]),
|
||||||
ctrl=("ctrl" in parts[2:]),
|
ctrl=("ctrl" in parts[2:]),
|
||||||
)
|
)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
logger.error("Can't parse layout line #%d: %s", number, str(err))
|
logger.error("Syntax error at %s:%d: %s", path, number, err)
|
||||||
return layout
|
return layout
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user