Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name = "crow-server"
version = "0.0.0"
description = "CROW-Server"
readme = "README.md"
requires-python = ">=3.10"
requires-python = ">=3.12"
dependencies = [
"django>=5.2.13",
"django>=6.0.4",
"psycopg2>=2.9.12",
"sqlalchemy>=2.0.49",
"websocket>=0.2.1",
Expand All @@ -14,10 +14,10 @@ dependencies = [

[project.optional-dependencies]
dev = [
"mypy>=1.5.0",
"ruff>=0.4.0",
"pre-commit>=3.0.0",
"build>=1.4.0",
"mypy>=1.20.2",
"ruff>=0.15.12",
"pre-commit>=4.6.0",
"build>=1.5.0",
]

[build-system]
Expand Down
Empty file.
34 changes: 34 additions & 0 deletions src/server/websocket_communication/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import cast
from websockets.sync.client import connect
from helper.types import Request, Response


class WebSocketClient:
def __init__(self):
"""
**change uri for final version!**
"""
self.port: str = "5000"

def send_data(self, data: Request, uri: str) -> None:
"""
Sends data on uri ( see init )\n
via websockets
"""
with connect(uri) as weboscket:
weboscket.send(data)

def receive_data(self, uri: str) -> Response:
"""
Receives data from uri ( see init )
via websockets
"""
with connect(uri) as websocket:
raw_data: str | bytes = websocket.recv()
return cast(Response, raw_data)

def sending(self, data: Request, ip: str) -> Response:
uri: str = f"ws://{ip}:{self.port}"

self.send_data(data, uri)
return self.receive_data(uri)
59 changes: 59 additions & 0 deletions src/server/websocket_communication/handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import subprocess
from .client import WebSocketClient
from .helper.types import Request, Response


class WebSocketHandler:
def __init__(self) -> None:
self.client = WebSocketClient()

def run_cmd(self, ip: str, pid: int, cmd: str, args: list[str]) -> Response:
if self._ping(ip):
request: Request = {
"PID": pid,
"ACTION_TYPE": "cmd",
"COMMAND": cmd,
"ARGS": args,
}
return self.client.sending(data=request, ip=ip)
else:
raise ConnectionError(
f"The Client {ip} is currently not availibel please check the internet connection."
)

def run_shell_cmd(self, ip: str, pid: int, cmd: str) -> Response:
if self._ping(ip):
request: Request = {"PID": pid, "ACTION_TYPE": "shell_cmd", "COMMAND": cmd}
return self.client.sending(data=request, ip=ip)
else:
raise ConnectionError(
f"The Client {ip} is currently not availibel please check the internet connection."
)

def run_python_file(
self, ip: str, pid: int, path: str, python_exe: str = "python"
) -> Response:
if self._ping(ip):
request: Request = {
"PID": pid,
"ACTION_TYPE": "python_file",
"ABSOLUT_PATH": path,
"PYTHON_EXE": python_exe,
}
return self.client.sending(data=request, ip=ip)
else:
raise ConnectionError(
f"The Client {ip} is currently not availibel please check the internet connection."
)

def _ping(self, host: str) -> bool:
try:
result = subprocess.run(
["ping", "-c", "1", host],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
return result.returncode == 0
except Exception:
return False
Empty file.
18 changes: 18 additions & 0 deletions src/server/websocket_communication/helper/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import TypedDict, NotRequired


class Request(TypedDict):
PID: int
ACTION_TYPE: str
COMMAND: NotRequired[str]
ARGS: NotRequired[list[str]]
ABSOLUT_PATH: NotRequired[str]
PYTHON_EXE: NotRequired[str]


class Response(TypedDict):
STATUS: str
CODE: int
PID: int
ACTION_TYPE: str
OUTPUT: str
29 changes: 0 additions & 29 deletions src/server/websocket_communication/websocket_util.py

This file was deleted.

Loading