Compare commits
2 Commits
3df7c56b1b
...
f95ee5f411
| Author | SHA1 | Date | |
|---|---|---|---|
| f95ee5f411 | |||
| b21ac4258b |
@@ -1,4 +1,19 @@
|
|||||||
|
# FUNCTIONAL IMPORTS
|
||||||
|
import websockets, asyncio, logging, sys
|
||||||
|
from typing import List, TypedDict
|
||||||
|
|
||||||
|
log_format = logging.Formatter(
|
||||||
|
"[%(asctime)s:%(name)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
||||||
|
)
|
||||||
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
||||||
|
stdout_handler.setFormatter(log_format)
|
||||||
|
log = logging.getLogger("CollabVMLib")
|
||||||
|
log.setLevel("INFO")
|
||||||
|
log.addHandler(stdout_handler)
|
||||||
|
|
||||||
|
# TYPE IMPORTS
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
|
from websockets import Subprotocol, Origin
|
||||||
|
|
||||||
# ENUMS
|
# ENUMS
|
||||||
class CollabVMState(IntEnum):
|
class CollabVMState(IntEnum):
|
||||||
@@ -37,6 +52,11 @@ class CollabVMClientRenameStatus(IntEnum):
|
|||||||
FAILED_REJECTED = 3
|
FAILED_REJECTED = 3
|
||||||
"""The rename attempt was authoritatively rejected."""
|
"""The rename attempt was authoritatively rejected."""
|
||||||
|
|
||||||
|
class CollabVMLibConnectionOptions(TypedDict):
|
||||||
|
WS_URL: str
|
||||||
|
NODE_ID: str
|
||||||
|
CREDENTIALS: str | None
|
||||||
|
|
||||||
# GUACAMOLE
|
# GUACAMOLE
|
||||||
def guac_encode(elements: list) -> str:
|
def guac_encode(elements: list) -> str:
|
||||||
return ','.join([f'{len(element)}.{element}' for element in elements]) + ';'
|
return ','.join([f'{len(element)}.{element}' for element in elements]) + ';'
|
||||||
@@ -82,4 +102,27 @@ def guac_decode(instruction: str) -> list:
|
|||||||
case _:
|
case _:
|
||||||
raise ValueError(f"Unexpected '{instruction[position]}' in guacamole instruction at character {position}")
|
raise ValueError(f"Unexpected '{instruction[position]}' in guacamole instruction at character {position}")
|
||||||
|
|
||||||
return elements
|
return elements
|
||||||
|
|
||||||
|
# HELPER FUNCS
|
||||||
|
|
||||||
|
def get_origin_from_ws_url(ws_url: str) -> str:
|
||||||
|
domain = (
|
||||||
|
ws_url.removeprefix("ws:")
|
||||||
|
.removeprefix("wss:")
|
||||||
|
.removeprefix("/")
|
||||||
|
.removeprefix("/")
|
||||||
|
.split("/", 1)[0]
|
||||||
|
)
|
||||||
|
is_wss = ws_url.startswith("wss:")
|
||||||
|
return f"http{'s' if is_wss else ''}://{domain}/"
|
||||||
|
|
||||||
|
# HELPER FUNCS ASYNC
|
||||||
|
async def send_chat_message(websocket, message: str):
|
||||||
|
log.debug(f"Sending chat message: {message}")
|
||||||
|
await websocket.send(guac_encode(["chat", message]))
|
||||||
|
|
||||||
|
async def send_guac(websocket, *args: str):
|
||||||
|
await websocket.send(guac_encode(list(args)))
|
||||||
|
|
||||||
|
log.info("CollabVMLib imported ...")
|
||||||
|
|||||||
25
cvmsentry.py
25
cvmsentry.py
@@ -14,11 +14,11 @@ LOG_LEVEL = getattr(config, "log_level", "INFO")
|
|||||||
if not os.path.exists("logs"):
|
if not os.path.exists("logs"):
|
||||||
os.makedirs("logs")
|
os.makedirs("logs")
|
||||||
log_format = logging.Formatter(
|
log_format = logging.Formatter(
|
||||||
"[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
"[%(asctime)s:%(name)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s"
|
||||||
)
|
)
|
||||||
stdout_handler = logging.StreamHandler(sys.stdout)
|
stdout_handler = logging.StreamHandler(sys.stdout)
|
||||||
stdout_handler.setFormatter(log_format)
|
stdout_handler.setFormatter(log_format)
|
||||||
log = logging.getLogger("cvmsentry")
|
log = logging.getLogger("CVMSentry")
|
||||||
log.setLevel(LOG_LEVEL)
|
log.setLevel(LOG_LEVEL)
|
||||||
log.addHandler(stdout_handler)
|
log.addHandler(stdout_handler)
|
||||||
|
|
||||||
@@ -123,16 +123,17 @@ async def connect(vm_name: str):
|
|||||||
log_data[utc_day] = []
|
log_data[utc_day] = []
|
||||||
|
|
||||||
if backlog:
|
if backlog:
|
||||||
for i in range(0, len(backlog), 2):
|
pass
|
||||||
backlog_user = backlog[i]
|
# for i in range(0, len(backlog), 2):
|
||||||
backlog_message = backlog[i + 1]
|
# backlog_user = backlog[i]
|
||||||
if not any(entry["message"] == backlog_message and entry["username"] == backlog_user for entry in log_data[utc_day]):
|
# backlog_message = backlog[i + 1]
|
||||||
log.info(f"[{vm_name} - {backlog_user} (backlog)]: {backlog_message}")
|
# if not any(entry["message"] == backlog_message and entry["username"] == backlog_user for entry in log_data[utc_day]):
|
||||||
log_data[utc_day].append({
|
# log.info(f"[{vm_name} - {backlog_user} (backlog)]: {backlog_message}")
|
||||||
"timestamp": timestamp,
|
# log_data[utc_day].append({
|
||||||
"username": backlog_user,
|
# "timestamp": timestamp,
|
||||||
"message": backlog_message
|
# "username": backlog_user,
|
||||||
})
|
# "message": backlog_message
|
||||||
|
# })
|
||||||
|
|
||||||
log_data[utc_day].append({
|
log_data[utc_day].append({
|
||||||
"timestamp": timestamp,
|
"timestamp": timestamp,
|
||||||
|
|||||||
Reference in New Issue
Block a user