diff --git a/.gitignore b/.gitignore index dba52881..04b5e93e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,13 @@ identifier.sqlite obfuscated.sb3 .pytest_cache .ruff_cache + +.coverage +certfile.pem +coverage.xml +keyfile.pem +.test/test.py +.test/teststststs.py +.test/ws_client_test.py +tests/test_tw_cloud_debug_compliance.py .vscode diff --git a/scratchattach/__init__.py b/scratchattach/__init__.py index a2329989..acdcc8ec 100644 --- a/scratchattach/__init__.py +++ b/scratchattach/__init__.py @@ -1,15 +1,33 @@ -from .cloud.cloud import CustomCloud, ScratchCloud, TwCloud, get_cloud, get_scratch_cloud, get_tw_cloud +from .cloud.cloud import ( + CustomCloud, + ScratchCloud, + TwCloud, + get_cloud, + get_scratch_cloud, + get_tw_cloud, +) from .cloud._base import BaseCloud, AnyCloud -from .eventhandlers.cloud_server import init_cloud_server -from .eventhandlers._base import BaseEventHandler +from .eventhandlers.cloud_server import ( + init_cloud_server, + init_ssl_cloud_server, + TwCloudSocket, + TwCloudServer, + TwSSLCloudServer, +) +from .eventhandlers._base import BaseEventHandler, BaseCloudServer from .eventhandlers.filterbot import Filterbot, HardFilter, SoftFilter, SpamFilter from .eventhandlers.cloud_storage import Database from .eventhandlers.combine import MultiEventHandler from .other.other_apis import * - -# from .other.project_json_capabilities import ProjectBody, get_empty_project_pb, get_pb_from_dict, read_sb3_file, download_asset +# from .other.project_json_capabilities import ( +# ProjectBody, +# get_empty_project_pb, +# get_pb_from_dict, +# read_sb3_file, +# download_asset, +# ) from .utils.encoder import Encoding from .utils.enums import Languages, TTSVoices from .utils.exceptions import ( @@ -27,11 +45,29 @@ from .site.cloud_activity import CloudActivity from .site.forum import ForumPost, ForumTopic, get_topic, get_topic_list, youtube_link_to_scratch from .site.project import Project, get_project, search_projects, explore_projects -from .site.session import Session, login, login_by_id, login_by_session_string, login_by_io, login_by_file, login_from_browser +from .site.session import ( + Session, + login, + login_by_id, + login_by_session_string, + login_by_io, + login_by_file, + login_from_browser, +) from .site.studio import Studio, get_studio, search_studios, explore_studios from .site.classroom import Classroom, get_classroom from .site.user import User, get_user, Rank from .site._base import BaseSiteComponent -from .site.browser_cookies import Browser, ANY, FIREFOX, CHROME, CHROMIUM, VIVALDI, EDGE, EDGE_DEV, SAFARI +from .site.browser_cookies import ( + Browser, + ANY, + FIREFOX, + CHROME, + CHROMIUM, + VIVALDI, + EDGE, + EDGE_DEV, + SAFARI, +) from . import editor diff --git a/scratchattach/cloud/_base.py b/scratchattach/cloud/_base.py index ed8aa85b..77c1c5bf 100644 --- a/scratchattach/cloud/_base.py +++ b/scratchattach/cloud/_base.py @@ -1,4 +1,6 @@ from __future__ import annotations +import queue +import threading import traceback import json @@ -13,6 +15,7 @@ from scratchattach.cloud import cloud as cloud_module if TYPE_CHECKING: + from scratchattach.eventhandlers import cloud_server from _typeshed import SupportsRead else: T = TypeVar("T") @@ -91,7 +94,9 @@ def set_var(self, variable: str, value: T, *, max_retries: int = 2) -> None: """ @abstractmethod - def set_vars(self, var_value_dict: dict[str, T], *, intelligent_waits: bool = True, max_retries: int = 2): + def set_vars( + self, var_value_dict: dict[str, T], *, intelligent_waits: bool = True, max_retries: int = 2 + ): """ Sets multiple cloud variables at once (works for an unlimited amount of variables). @@ -104,11 +109,13 @@ def set_vars(self, var_value_dict: dict[str, T], *, intelligent_waits: bool = Tr """ @abstractmethod - def get_var(self, var, *, recorder_initial_values: Optional[dict[str, Any]] = None) -> T: + def get_var(self, var, *, recorder_initial_values: Optional[dict[str, Any]] = None) -> T | None: pass @abstractmethod - def get_all_vars(self, *, recorder_initial_values: Optional[dict[str, Any]] = None) -> dict[str, T]: + def get_all_vars( + self, *, recorder_initial_values: Optional[dict[str, Any]] = None + ) -> dict[str, T]: pass def events(self) -> CloudEvents: @@ -124,10 +131,16 @@ def requests( ) -> CloudRequests: used_cloud_vars = used_cloud_vars or ["1", "2", "3", "4", "5", "6", "7", "8", "9"] return CloudRequests( - self, used_cloud_vars=used_cloud_vars, no_packet_loss=no_packet_loss, respond_order=respond_order, debug=debug + self, + used_cloud_vars=used_cloud_vars, + no_packet_loss=no_packet_loss, + respond_order=respond_order, + debug=debug, ) - def storage(self, *, no_packet_loss: bool = False, used_cloud_vars: Optional[list[str]] = None) -> CloudStorage: + def storage( + self, *, no_packet_loss: bool = False, used_cloud_vars: Optional[list[str]] = None + ) -> CloudStorage: used_cloud_vars = used_cloud_vars or ["1", "2", "3", "4", "5", "6", "7", "8", "9"] return CloudStorage(self, used_cloud_vars=used_cloud_vars, no_packet_loss=no_packet_loss) @@ -136,7 +149,7 @@ def create_event_stream(self) -> EventStream: pass -class DummyCloud(AnyCloud[Any]): +class DummyCloud(AnyCloud[T]): class DummyEventStream(EventStream): def read(self, length=...): return iter(()) @@ -159,13 +172,17 @@ def _enforce_ratelimit(self, *, n: int) -> None: def set_var(self, variable: str, value: T, *, max_retries: int = 2) -> None: pass - def set_vars(self, var_value_dict: dict[str, T], *, intelligent_waits: bool = True, max_retries: int = 2): + def set_vars( + self, var_value_dict: dict[str, T], *, intelligent_waits: bool = True, max_retries: int = 2 + ): pass - def get_var(self, var, *, recorder_initial_values: Optional[dict[str, Any]] = None) -> Any: - pass + def get_var(self, var, *, recorder_initial_values: Optional[dict[str, T]] = None) -> T | None: + return None - def get_all_vars(self, *, recorder_initial_values: Optional[dict[str, Any]] = None) -> dict[str, Any]: + def get_all_vars( + self, *, recorder_initial_values: Optional[dict[str, T]] = None + ) -> dict[str, T]: return {} @@ -197,9 +214,12 @@ def __init__(self, cloud: BaseCloud): try: self.source_cloud.connect() except exceptions.CloudConnectionError: - warnings.warn("Initial cloud connection attempt failed, retrying...", exceptions.UnexpectedWebsocketEventWarning) + warnings.warn( + "Initial cloud connection attempt failed, retrying...", + exceptions.UnexpectedWebsocketEventWarning, + ) self.packets_left = [] - + def wait_before_reconnect(self): if time.time() - self.most_recent_reconnection_time > self.RECENT_RECONNECT_TIME_DELTA: self.recent_reconnect_count = 0 @@ -243,12 +263,16 @@ def read(self, amount: int = -1) -> Iterator[dict[str, Any]]: while not done: # print("Getting data...") try: - self.receive_new(not recv_once, timeout=timeout_end - time.time() if has_timeout else None) + self.receive_new( + not recv_once, timeout=timeout_end - time.time() if has_timeout else None + ) while (not has_timeout or time.time() < timeout_end) and ( (recv_once and self.packets_left) or (not recv_once and i < recv_at_least) ): if not self.packets_left and not recv_once: - self.receive_new(timeout=timeout_end - time.time() if has_timeout else None) + self.receive_new( + timeout=timeout_end - time.time() if has_timeout else None + ) if not self.packets_left: continue i += 1 @@ -276,7 +300,7 @@ def close(self) -> None: self.source_cloud.disconnect() -class BaseCloud(AnyCloud[Union[str, int]]): +class BaseCloud(AnyCloud[Union[str, int, float]]): """ Base class for a project's cloud variables. Represents a cloud. @@ -323,7 +347,9 @@ def __init__(self, *, project_id: Optional[Union[int, str]] = None, _session=Non # Required internal attributes that every object representing a cloud needs to have (no matter what cloud is represented): self._session = _session - self.active_connection = False # whether a connection to a cloud variable server is currently established + self.active_connection = ( + False # whether a connection to a cloud variable server is currently established + ) self.websocket = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE}) self.recorder = None # A CloudRecorder object that records cloud activity for the values to be retrieved later, @@ -368,7 +394,9 @@ def _send_recursive(self, data: str, *, current_depth: int = 0, max_depth=0): self._send_recursive(data, current_depth=current_depth + 1, max_depth=max_depth) else: self.active_connection = False - raise exceptions.CloudConnectionError(f"Sending packet failed {max_depth + 1} tries: {data}") + raise exceptions.CloudConnectionError( + f"Sending packet failed {max_depth + 1} tries: {data}" + ) def _send_packet(self, packet, *, max_retries=2): self._send_recursive(json.dumps(packet) + "\n", max_depth=max_retries) @@ -428,7 +456,8 @@ def _assert_valid_value(self, value): def _enforce_ratelimit(self, *, n): # n is the amount of variables being set if ( - (time.time() - self.first_var_set) / (self.var_sets_since_first + 1) > self.ws_longterm_ratelimit + (time.time() - self.first_var_set) / (self.var_sets_since_first + 1) + > self.ws_longterm_ratelimit ): # if the average delay between cloud variable sets has been bigger than the long-term rate-limit, cloud variables can be set fast (wait time smaller than long-term rate limit) again self.var_sets_since_first = 0 self.first_var_set = time.time() @@ -518,19 +547,25 @@ def _ensure_recorder_running( if recorder_initial_values is None and project_id is not None: recorder_initial_values = _get_cloud_var_initial_data_or_none(project_id) recorder_initial_values = recorder_initial_values or {} - self.recorder = recorder = cloud_recorder.CloudRecorder(self, initial_values=recorder_initial_values) + self.recorder = recorder = cloud_recorder.CloudRecorder( + self, initial_values=recorder_initial_values + ) recorder.start() # print("Started recorder.") recorder.received_data.wait(timeout=1) time.sleep(0.01) return recorder - def get_var(self, var, *, recorder_initial_values: Optional[dict[str, Any]] = None): + def get_var( + self, var, *, recorder_initial_values: Optional[dict[str, Any]] = None + ) -> str | int | float | None: var = "☁ " + var.removeprefix("☁ ") recorder = self._ensure_recorder_running(recorder_initial_values=recorder_initial_values) return recorder.get_var(var) - def get_all_vars(self, *, recorder_initial_values: Optional[dict[str, Any]] = None): + def get_all_vars( + self, *, recorder_initial_values: Optional[dict[str, Any]] = None + ) -> dict[str, str | int | float]: recorder = self._ensure_recorder_running(recorder_initial_values=recorder_initial_values) return recorder.get_all_vars() @@ -563,7 +598,9 @@ def _get_cloud_var_initial_data(project_id: Union[str, int]) -> dict[str, Any]: from scratchattach.site import project data: dict[str, Any] = {} - if isinstance((j := project.get_project(project_id).raw_json()), dict) and isinstance(targets := j.get("targets"), list): + if isinstance((j := project.get_project(project_id).raw_json()), dict) and isinstance( + targets := j.get("targets"), list + ): for target in targets: if not isinstance(target, dict): continue @@ -589,3 +626,126 @@ def _get_cloud_var_initial_data_or_none(project_id: Union[str, int]) -> Optional return _get_cloud_var_initial_data(project_id) except Exception: return None + + +class CloudServerAdapter(AnyCloud[str | int | float]): + server: "cloud_server.BaseCloudServer" + disconnected: threading.Event + project_id: str | int + connected_event_stream_queues: dict[int, queue.Queue[dict[str, Any]]] + + def __init__(self, server: "cloud_server.BaseCloudServer", project_id: str | int): + self.server = server + self.disconnected = threading.Event() + self.project_id = project_id + self.connected_event_stream_queues = {} + + @self.server.event + def on_outgoing_packet(event): + for listener in self.connected_event_stream_queues.values(): + listener.put(event) + + def connect(self): + self.active_connection = True + self.disconnected.clear() + + def disconnect(self): + self.active_connection = False + self.disconnected.set() + + def reconnect(self): + self.disconnect() + time.sleep(0.1) + self.connect() + + def _enforce_ratelimit(self, *, n: int) -> None: + pass + + def set_var(self, variable: str, value: str | int | float, *, max_retries: int = 2) -> None: + """ + Sets a cloud variable. + + Args: + variable (str): The name of the cloud variable that should be set (provided without the cloud emoji) + value (Any): The value the cloud variable should be set to + + Kwargs: + max_retries (int) : Maximum number of times to retry setting the var if setting fails before raising an exception + """ + if self.disconnected.is_set(): + return + self.server.set_var( + self.project_id, + variable, + value, + ) + + def set_vars( + self, + var_value_dict: dict[str, str | int | float], + *, + intelligent_waits: bool = True, + max_retries: int = 2, + ): + """ + Sets multiple cloud variables at once (works for an unlimited amount of variables). + + Args: + var_value_dict (dict): variable:value dictionary with the variables / values to set. The dict should like this: {"var1":"value1", "var2":"value2", ...} + + Kwargs: + intelligent_waits (boolean): When enabled, the method will automatically decide how long to wait before performing this cloud variable set, to make sure no rate limits are triggered + max_retries (int) : Maximum number of times to retry setting the var if setting fails before raising an exception + """ + if self.disconnected.is_set(): + return + self.server.set_project_vars(self.project_id, var_value_dict) + + def get_var( + self, var, *, recorder_initial_values: Optional[dict[str, Any]] = None + ) -> str | int | float | None: + return self.server.get_var(self.project_id, var) + + def get_all_vars( + self, *, recorder_initial_values: Optional[dict[str, Any]] = None + ) -> dict[str, str | int | float]: + return self.server.get_project_vars(self.project_id).copy() + + def create_event_stream(self) -> CloudServerAdapterEventStream: + return CloudServerAdapterEventStream(self) + + +class CloudServerAdapterEventStream(EventStream): + adapter: CloudServerAdapter + disconnected: threading.Event + _queue: queue.Queue[dict[str, Any]] + + def __init__(self, adapter: CloudServerAdapter): + self.adapter = adapter + self.disconnected = threading.Event() + self._queue = queue.Queue() + self.adapter.connected_event_stream_queues[id(self)] = self._queue + + def read(self, amount: int = -1) -> Iterator[dict[str, Any]]: + if self.disconnected.is_set() or self.adapter.disconnected.is_set(): + return + end_time = time.time() + self.timeout if self.timeout is not None else None + progress = 0 + while (progress < amount if amount >= 0 else progress == 0) and ( + time.time() < end_time if end_time is not None else True + ): + try: + yield self._queue.get( + self.timeout is None or self.timeout > 0.0, + max(end_time - time.time(), 0) if end_time is not None else None, + ) + except queue.Empty: + pass + + def __del__(self): + self.close() + + def close(self) -> None: + if not self.disconnected.is_set(): + del self.adapter.connected_event_stream_queues[id(self)] + self.disconnected.set() diff --git a/scratchattach/eventhandlers/_base.py b/scratchattach/eventhandlers/_base.py index 4e4236ac..b2296d1a 100644 --- a/scratchattach/eventhandlers/_base.py +++ b/scratchattach/eventhandlers/_base.py @@ -1,14 +1,23 @@ from __future__ import annotations +import json +import time +import ssl from abc import ABC, abstractmethod -from typing import Optional +from typing import Optional, Any, TYPE_CHECKING from collections import defaultdict from threading import Thread, Event from collections.abc import Callable import traceback + +from SimpleWebSocketServer import WebSocket + +if TYPE_CHECKING: + import scratchattach.cloud._base as cloud_base from scratchattach.utils.requests import requests from scratchattach.utils import exceptions + class BaseEventHandler(ABC): _events: defaultdict[str, list[Callable]] _threaded_events: defaultdict[str, list[Callable]] @@ -41,8 +50,8 @@ def start(self, *, thread=True, ignore_exceptions=True): else: self._thread = None self._updater() - - def call_event(self, event_name, args : list = []): + + def call_event(self, event_name, args: list = []): try: # print(f"Calling for {event_name}...") if event_name in self._threaded_events: @@ -56,20 +65,18 @@ def call_event(self, event_name, args : list = []): func(*args) except Exception as e: if self.ignore_exceptions: - print( - f"Warning: Caught error in event '{event_name}' - Full error below" - ) + print(f"Warning: Caught error in event '{event_name}' - Full error below") try: traceback.print_exc() except Exception: print(e) else: - raise(e) + raise (e) @abstractmethod def _updater(self): pass - + def __del__(self): self.stop() @@ -108,6 +115,7 @@ def event(self, function=None, *, thread=False): """ Decorator function. Adds an event. """ + def inner(function): # called directly if the decorator provides arguments if thread is True: @@ -120,4 +128,233 @@ def inner(function): return inner else: # => the decorator doesn't provide arguments - inner(function) \ No newline at end of file + inner(function) + + +class BaseCloudServer(BaseEventHandler): + """ + Base class for all sa cloud servers. + + If you are developing a custom cloud server with sa, please inherit from this class + and change up the methods as needed. + """ + + hostname: str + "IP address or domain name of the host to bind the server to." + port: int + "Port to bind the server to." + tw_clients: dict[tuple[str, int], dict[str, Any]] + "Dictionary containing information on connected clients." + tw_variables: dict[str, dict[str, Any]] + "Dictionary containing states and data for existing cloud variables." + allow_non_numeric: bool + "Whether or not non-numeric characters are allowed in cloud variable values." + whitelisted_projects: set[str] | None + "Optional list of whitelisted projects." + length_limit: int | None + "Optional limit on the length of cloud variable values." + allow_nonscratch_names: bool + "Whether or not usernames that do not exist on scratch are allowed." + blocked_ips: list[str] + "List of blocked IP addresses." + sync_players: bool + log_var_sets: bool + linked_clouds: dict[str, "cloud_base.CloudServerAdapter"] + + def __init__( + self, + hostname: str, + *, + port: int, + length_limit: int | None = None, + allow_non_numeric: bool = True, + whitelisted_projects: list[Any] | None = None, + allow_nonscratch_names: bool = True, + blocked_ips: list[str] | None = None, + sync_players: bool = True, + log_var_sets: bool = True, + ): + + if blocked_ips is None: + blocked_ips = [] + + BaseEventHandler.__init__(self) + + self.tw_clients = {} # saves connected clients + self.tw_variables = {} # holds cloud variable states + + self.hostname = hostname + self.port = port + + # server config + self.allow_non_numeric = allow_non_numeric + self.whitelisted_projects = ( + {str(i) for i in whitelisted_projects} if whitelisted_projects else None + ) + self.length_limit = length_limit + self.allow_nonscratch_names = allow_nonscratch_names + self.blocked_ips = blocked_ips + self.sync_players = sync_players + self.log_var_sets = log_var_sets + + self.linked_clouds = {} + + def check_for_ip_ban(self, client): + if ( + client.address[0] in self.blocked_ips + or client.address[0] + ":" + str(client.address[1]) in self.blocked_ips + or client.address in self.blocked_ips + ): + client.sendMessage("You have been banned from this server") + client.close(4002) + print(client.address[0] + ":" + str(client.address[1]), "(IP-banned) was disconnected") + return True + return False + + def active_projects(self): + only_active = {} + for project_id in self.tw_variables: + if self.active_user_ips(project_id) != []: + only_active[project_id] = self.tw_variables[project_id] + return only_active + + def active_user_names(self, project_id): + return [self.tw_clients[user]["username"] for user in self.active_user_ips(project_id)] + + def active_user_ips(self, project_id: Any): + project_id = str(project_id) + return [ + user + for user in self.tw_clients + if str(self.tw_clients[user]["project_id"]) == project_id + ] + + def get_global_vars(self): + return self.tw_variables + + def get_project_vars(self, project_id: Any): + project_id = str(project_id) + return self.tw_variables.get(project_id, {}) + + def get_var(self, project_id: Any, var_name: str, *, no_prefix: bool = False): + project_id = str(project_id) + if not no_prefix: + var_name = "☁ " + var_name.removeprefix("☁ ") + if project_id in self.tw_variables: + if var_name in self.tw_variables[project_id]: + return self.tw_variables[project_id][var_name] + else: + return None + else: + return None + + def set_global_vars( + self, + data: dict[str, dict[str, Any]], + no_prefix: bool = False, + ): + for project_id, project_data in data.items(): + self.set_project_vars(project_id, project_data, no_prefix=no_prefix) + + def set_project_vars( + self, + project_id: Any, + data: dict[str, Any], + *, + user: str = "@server", + no_prefix: bool = False, + ): + project_id = str(project_id) + if not no_prefix: + data = {"☁ " + key.removeprefix("☁ "): value for key, value in data.items()} + self.tw_variables[project_id].update(data) + packets = [ + { + "method": "set", + "project_id": project_id, + "name": varname, + "value": data[varname], + "server": "scratchattach/3", + "timestamp": time.time() * 1000, + "user": user, + } + for varname in data + ] + packets_string = "\n".join(json.dumps(packet) for packet in packets) + for client in (self.tw_clients[ip]["client"] for ip in self.active_user_ips(project_id)): + client.sendMessage(packets_string) + for packet in packets: + self.call_event("outgoing_packet", [packet]) + + def set_var( + self, + project_id: Any, + var_name: str, + value: Any, + *, + user: str = "@server", + skip_broadcast_for: WebSocket | None = None, + no_prefix: bool = False, + ): + if not no_prefix: + var_name = "☁ " + var_name.removeprefix("☁ ") + project_id = str(project_id) + if project_id not in self.tw_variables: + self.tw_variables[project_id] = {} + self.tw_variables[project_id][var_name] = value + + packet = { + "method": "set", + "project_id": project_id, + "name": var_name, + "value": value, + "server": "scratchattach/3", + "timestamp": time.time() * 1000, + "user": user, + } + if self.sync_players is True: + for client in ( + self.tw_clients[ip]["client"] for ip in self.active_user_ips(project_id) + ): + if client == skip_broadcast_for: + continue + client.sendMessage(json.dumps(packet)) + self.call_event("outgoing_packet", [packet]) + + def _check_value(self, value): + # Checks if a received cloud value satisfies the server's constraints + if self.length_limit is not None: + if len(str(value)) > self.length_limit: + return False + if self.allow_non_numeric is False: + x = value.replace(".", "") + x = x.replace("-", "") + if not (x.isnumeric() or x == ""): + return False + return True + + def _updater(self): + try: + # Function called when .start() is executed (.start is inherited from BaseEventHandler) + print(f"Serving websocket server: ws://{self.hostname}:{self.port}") + while self.running: + self.serveonce() + except Exception as e: + raise exceptions.WebsocketServerError(str(e)) + + def get_project_cloud(self, project_id: Any) -> "cloud_base.CloudServerAdapter": + project_id = str(project_id) + if project_id not in self.linked_clouds: + from scratchattach.cloud import _base as cloud_base + self.linked_clouds[project_id] = cloud_base.CloudServerAdapter(self, project_id) + return self.linked_clouds[project_id] + + def pause(self): + self.running = False + + def resume(self): + self.running = True + + def stop(self, wait_call_threads: bool = True): + BaseEventHandler.stop(self, wait_call_threads) + self.close() diff --git a/scratchattach/eventhandlers/cloud_events.py b/scratchattach/eventhandlers/cloud_events.py index 64b3281a..3d89e64c 100644 --- a/scratchattach/eventhandlers/cloud_events.py +++ b/scratchattach/eventhandlers/cloud_events.py @@ -63,7 +63,7 @@ def _updater(self): # continue cloud_activity_dict = cast(CloudActivityDict, data) cloud_activity_dict["variable_name"] = cloud_activity_dict["name"] - cloud_activity_dict["name"] = cloud_activity_dict["variable_name"].replace("☁ ", "") + cloud_activity_dict["name"] = cloud_activity_dict["variable_name"].removeprefix("☁ ") _a._update_from_dict(cloud_activity_dict) # print(f"sending event {_a}") self.call_event(f"on_{_a.type}", [_a]) diff --git a/scratchattach/eventhandlers/cloud_recorder.py b/scratchattach/eventhandlers/cloud_recorder.py index 1dcaf36e..e556af26 100644 --- a/scratchattach/eventhandlers/cloud_recorder.py +++ b/scratchattach/eventhandlers/cloud_recorder.py @@ -24,12 +24,12 @@ def __init__(self, cloud, *, initial_values: Optional[dict[str, Any]] = None): self.has_data.set() self.event(self.on_set) - def get_var(self, var): + def get_var(self, var) -> Any | None: if var not in self.cloud_values: return None return self.cloud_values[var] - def get_all_vars(self): + def get_all_vars(self) -> dict[str, Any]: return self.cloud_values.copy() def on_set(self, activity: cloud_activity.CloudActivity): diff --git a/scratchattach/eventhandlers/cloud_server.py b/scratchattach/eventhandlers/cloud_server.py index 0bda1f84..e3b4bea9 100644 --- a/scratchattach/eventhandlers/cloud_server.py +++ b/scratchattach/eventhandlers/cloud_server.py @@ -1,34 +1,42 @@ from __future__ import annotations +from scratchattach.site.typed_dicts import CloudActivityDict -from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket -from threading import Thread -from scratchattach.utils import exceptions import json import time +import ssl +import traceback +from typing import Any +import warnings + +from SimpleWebSocketServer import SimpleSSLWebSocketServer, SimpleWebSocketServer, WebSocket + +from scratchattach.utils import exceptions from scratchattach.site import cloud_activity from scratchattach.site.user import User -from ._base import BaseEventHandler -import traceback +from scratchattach.cloud import BaseCloud, DummyCloud, AnyCloud +from ._base import BaseCloudServer class TwCloudSocket(WebSocket): - server: TwCloudServer + server: TwCloudServer | TwSSLCloudServer - def handle_set(self, data: dict): + def handle_set(self, data: dict[Any, Any]): # cloud variable set received # check if project_id is in whitelisted projects (if there's a list of whitelisted projects) - if self.server.whitelisted_projects is not None: - if data["project_id"] not in self.server.whitelisted_projects: - self.close(4002) - if self.server.log_var_sets: - print( - self.address[0] + ":" + str(self.address[1]), - "tried to set a var on non-whitelisted project and was disconnected, project:", - data["project_id"], - "user:", - data["user"], - ) - return + if ( + self.server.whitelisted_projects is not None + and str(data["project_id"]) not in self.server.whitelisted_projects + ): + self.close(4002) + if self.server.log_var_sets: + print( + self.address[0] + ":" + str(self.address[1]), + "tried to set a var on non-whitelisted project and was disconnected, project:", + data["project_id"], + "user:", + data["user"], + ) + return # check if value is valid if not self.server._check_value(data["value"]): if self.server.log_var_sets: @@ -43,56 +51,71 @@ def handle_set(self, data: dict): "user:", data["user"], ) - self.server.set_var(data["project_id"], data["name"], data["value"], user=data["user"], skip_forward=self) - send_to_clients = { + self.server.set_var( + data["project_id"], + data["name"], + data["value"], + user=data["user"], + skip_broadcast_for=self, + no_prefix=True, + ) + send_to_clients: CloudActivityDict = { "method": "set", - "user": data["user"], "project_id": data["project_id"], "name": data["name"], "value": data["value"], - "timestamp": round(time.time() * 1000), - "server": "scratchattach/2.0.0", + # TODO: Add a cloud to the activity dict (possibly some kind of adapter) + "cloud": self.server.get_project_cloud(data["project_id"]), } # raise event - _a = cloud_activity.CloudActivity(timestamp=time.time() * 1000) - data["name"] = data["name"].replace("☁ ", "") + _a = cloud_activity.CloudActivity( + username=data["user"], + timestamp=time.time() * 1000 + ) _a._update_from_dict(send_to_clients) self.server.call_event("on_set", [_a, self]) - def handle_handshake(self, data: dict): + def handle_handshake(self, data: dict[Any, Any]): # check if handshake is valid if not "user" in data: - print(self.address[0] + ":" + str(self.address[1]), "tried to handshake without providing a username") + print( + self.address[0] + ":" + str(self.address[1]), + "tried to handshake without providing a username", + ) self.close(4002) return if not "project_id" in data: - print(self.address[0] + ":" + str(self.address[1]), "tried to handshake without providing a project_id") + print( + self.address[0] + ":" + str(self.address[1]), + "tried to handshake without providing a project_id", + ) self.close(4002) return # check if project_id is in username is allowed - if self.server.allow_nonscratch_names is False: - if not User(username=data["user"]).does_exist(): - print( - self.address[0] + ":" + str(self.address[1]), - "tried to handshake using a username not existing on Scratch, project:", - data["project_id"], - "user:", - data["user"], - ) - self.close(4002) - return + if not self.server.allow_nonscratch_names and not User(username=data["user"]).does_exist(): + print( + self.address[0] + ":" + str(self.address[1]), + "tried to handshake using a username not existing on Scratch, project:", + data["project_id"], + "user:", + data["user"], + ) + self.close(4002) + return # check if project_id is in whitelisted projects (if there's a list of whitelisted projects) - if self.server.whitelisted_projects is not None: - if str(data["project_id"]) not in self.server.whitelisted_projects: - self.close(4002) - print( - self.address[0] + ":" + str(self.address[1]), - "tried to handshake on a non-whitelisted project:", - data["project_id"], - "user:", - data["user"], - ) - return + if ( + self.server.whitelisted_projects is not None + and str(data["project_id"]) not in self.server.whitelisted_projects + ): + self.close(4002) + print( + self.address[0] + ":" + str(self.address[1]), + "tried to handshake on a non-whitelisted project:", + data["project_id"], + "user:", + data["user"], + ) + return # register handshake in users list (save username and project_id) print( self.address[0] + ":" + str(self.address[1]), @@ -111,16 +134,16 @@ def handle_handshake(self, data: dict): { "method": "set", "project_id": data["project_id"], - "name": "☁ " + varname, + "name": varname, "value": self.server.tw_variables[str(data["project_id"])][varname], - "server": "scratchattach/2.0.0", + "server": "scratchattach/3", } ) for varname in self.server.get_project_vars(str(data["project_id"])) ] ) ) - self.sendMessage("This server uses @TimMcCool's scratchattach 2.0.0") + self.sendMessage("This server uses @TimMcCool's scratchattach v3 library.") # raise event self.server.call_event("on_handshake", [data["user"], data["project_id"], self]) @@ -144,7 +167,6 @@ def handleMessage(self): self.address[0] + ":" + str(self.address[1]), "sent a message without providing a valid method (set, handshake)", ) - except Exception as e: print("Internal error in handleMessage:", e, traceback.format_exc()) @@ -156,7 +178,11 @@ def handleConnected(self): return print(self.address[0] + ":" + str(self.address[1]), "connected") - self.server.tw_clients[self.address] = {"client": self, "username": None, "project_id": None} + self.server.tw_clients[self.address] = { + "client": self, + "username": None, + "project_id": None, + } # raise event self.server.call_event("on_connect", [self]) except Exception as e: @@ -181,184 +207,103 @@ def handleClose(self): print("Internal error in handleClose:", e) -class TwCloudServer(SimpleWebSocketServer, BaseEventHandler): +class TwCloudServer(BaseCloudServer, SimpleWebSocketServer): def __init__( self, - hostname, + hostname: str, *, - port, - websocketclass, - length_limit=None, - allow_non_numeric=True, - whitelisted_projects=None, - allow_nonscratch_names=True, - blocked_ips=None, - sync_players=True, - log_var_sets=True, + port: int, + websocketclass: type[WebSocket], + length_limit: int | None = None, + allow_non_numeric: bool = True, + whitelisted_projects: list[Any] | None = None, + allow_nonscratch_names: bool = True, + blocked_ips: list[str] | None = None, + sync_players: bool = True, + log_var_sets: bool = True, ): if blocked_ips is None: blocked_ips = [] SimpleWebSocketServer.__init__(self, hostname, port=port, websocketclass=websocketclass) - BaseEventHandler.__init__(self) - - self.running = False - self._events = {} # saves event functions called on cloud updates - - self.tw_clients = {} # saves connected clients - self.tw_variables = {} # holds cloud variable states - self.hostname = hostname - self.port = port - - # server config - self.allow_non_numeric = allow_non_numeric - self.whitelisted_projects = whitelisted_projects - self.length_limit = length_limit - self.allow_nonscratch_names = allow_nonscratch_names - self.blocked_ips = blocked_ips - self.sync_players = sync_players - self.log_var_sets = log_var_sets - - def check_for_ip_ban(self, client): - if ( - client.address[0] in self.blocked_ips - or client.address[0] + ":" + str(client.address[1]) in self.blocked_ips - or client.address in self.blocked_ips - ): - client.sendMessage("You have been banned from this server") - client.close(4002) - print(client.address[0] + ":" + str(client.address[1]), "(IP-banned) was disconnected") - return True - return False - - def active_projects(self): - only_active = {} - for project_id in self.tw_variables: - if self.active_user_ips(project_id) != []: - only_active[project_id] = self.tw_variables[project_id] - return only_active - - def active_user_names(self, project_id): - return [self.tw_clients[user]["username"] for user in self.active_user_ips(project_id)] - - def active_user_ips(self, project_id): - return list(filter(lambda user: str(self.tw_clients[user]["project_id"]) == str(project_id), self.tw_clients)) - - def get_global_vars(self): - return self.tw_variables - - def get_project_vars(self, project_id): - project_id = str(project_id) - if project_id in self.tw_variables: - return self.tw_variables[project_id] - else: - return {} - - def get_var(self, project_id, var_name): - project_id = str(project_id) - var_name = var_name.replace("☁ ", "") - if project_id in self.tw_variables: - if var_name in self.tw_variables[project_id]: - return self.tw_variables[project_id][var_name] - else: - return None - else: - return None - - def set_global_vars(self, data): - for project_id in data: - self.set_project_vars(project_id, data[project_id]) - - def set_project_vars(self, project_id, data, *, user="@server"): - project_id = str(project_id) - self.tw_variables[project_id] = data - for client in [self.tw_clients[ip]["client"] for ip in self.active_user_ips(project_id)]: - client.sendMessage( - "\n".join( - [ - json.dumps( - { - "method": "set", - "project_id": project_id, - "name": "☁ " + varname, - "value": data[varname], - "server": "scratchattach/2.0.0", - "timestamp": time.time() * 1000, - "user": user, - } - ) - for varname in data - ] - ) - ) + BaseCloudServer.__init__( + self, + hostname=hostname, + port=port, + length_limit=length_limit, + allow_non_numeric=allow_non_numeric, + whitelisted_projects=whitelisted_projects, + allow_nonscratch_names=allow_nonscratch_names, + blocked_ips=blocked_ips, + sync_players=sync_players, + log_var_sets=log_var_sets, + ) - def set_var(self, project_id, var_name, value, *, user="@server", skip_forward=None): - var_name = var_name.replace("☁ ", "") - project_id = str(project_id) - if project_id not in self.tw_variables: - self.tw_variables[project_id] = {} - self.tw_variables[project_id][var_name] = value - if self.sync_players is True: - for client in [self.tw_clients[ip]["client"] for ip in self.active_user_ips(project_id)]: - if client == skip_forward: - continue - client.sendMessage( - json.dumps( - { - "method": "set", - "project_id": project_id, - "name": "☁ " + var_name, - "value": value, - "timestamp": time.time() * 1000, - "user": user, - } - ) - ) +class TwSSLCloudServer(BaseCloudServer, SimpleSSLWebSocketServer): + def __init__( + self, + hostname: str, + *, + certfile: str | None = None, + keyfile: str | None = None, + ssl_version: int = ssl.PROTOCOL_TLSv1_2, + ssl_context: ssl.SSLContext | None = None, + port: int, + websocketclass: type[WebSocket], + length_limit: int | None = None, + allow_non_numeric: bool = True, + whitelisted_projects: list[Any] | None = None, + allow_nonscratch_names: bool = True, + blocked_ips: list[str] | None = None, + sync_players: bool = True, + log_var_sets: bool = True, + ): + SimpleSSLWebSocketServer.__init__( + self, + hostname, + port=port, + websocketclass=websocketclass, + certfile=certfile, + keyfile=keyfile, + version=ssl_version, + ssl_context=ssl_context, + ) - def _check_value(self, value): - # Checks if a received cloud value satisfies the server's constraints - if self.length_limit is not None: - if len(str(value)) > self.length_limit: - return False - if self.allow_non_numeric is False: - x = value.replace(".", "") - x = x.replace("-", "") - if not (x.isnumeric() or x == ""): - return False - return True + BaseCloudServer.__init__( + self, + hostname=hostname, + port=port, + length_limit=length_limit, + allow_non_numeric=allow_non_numeric, + whitelisted_projects=whitelisted_projects, + allow_nonscratch_names=allow_nonscratch_names, + blocked_ips=blocked_ips, + sync_players=sync_players, + log_var_sets=log_var_sets, + ) def _updater(self): try: # Function called when .start() is executed (.start is inherited from BaseEventHandler) - print(f"Serving websocket server: ws://{self.hostname}:{self.port}") + print(f"Serving websocket server: wss://{self.hostname}:{self.port}") self.serveforever() except Exception as e: raise exceptions.WebsocketServerError(str(e)) - def pause(self): - self.running = False - - def resume(self): - self.running = True - - def stop(self, wait_call_threads: bool = True): - BaseEventHandler.stop(self, wait_call_threads) - self.close() - def init_cloud_server( - hostname="127.0.0.1", - port=8080, + hostname: str = "127.0.0.1", + port: int = 8080, *, - length_limit=None, - allow_non_numeric=True, - whitelisted_projects=None, - allow_nonscratch_names=True, - blocked_ips=None, - sync_players=True, - log_var_sets=True, + length_limit: int | None = None, + allow_non_numeric: bool = True, + whitelisted_projects: list[Any] | None = None, + allow_nonscratch_names: bool = True, + blocked_ips: list[str] | None = None, + sync_players: bool = True, + log_var_sets: bool = True, ): """ Inits a websocket server which can be used with TurboWarp's ?cloud_host URL parameter. @@ -380,3 +325,48 @@ def init_cloud_server( sync_players=sync_players, log_var_sets=log_var_sets, ) + + +def init_ssl_cloud_server( + hostname: str = "127.0.0.1", + port: int = 8080, + *, + certfile: str | None = None, + keyfile: str | None = None, + ssl_version: int = ssl.PROTOCOL_TLSv1_2, + ssl_context: ssl.SSLContext | None = None, + length_limit: int | None = None, + allow_non_numeric: bool = True, + whitelisted_projects: list[Any] | None = None, + allow_nonscratch_names: bool = True, + blocked_ips: list[str] | None = None, + sync_players: bool = True, + log_var_sets: bool = True, +) -> TwSSLCloudServer: + """ + Inits a websocket server which can be used with TurboWarp's ?cloud_host URL parameter. + + Prints out the websocket address in the console. + """ + if (certfile is None or keyfile is None) and ssl_context is None: + warnings.warn( + "To init a ssl cloud server, you need provide `certfile` and " + + "`keyfile` or `ssl_context`." + ) + + return TwSSLCloudServer( + hostname, + port=port, + websocketclass=TwCloudSocket, + certfile=certfile, + keyfile=keyfile, + ssl_version=ssl_version, + ssl_context=ssl_context, + length_limit=length_limit, + allow_non_numeric=allow_non_numeric, + whitelisted_projects=whitelisted_projects, + allow_nonscratch_names=allow_nonscratch_names, + blocked_ips=blocked_ips, + sync_players=sync_players, + log_var_sets=log_var_sets, + )