dementor 1.0.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dementor/loader.py ADDED
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2025-Present MatrixEditor
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ import argparse
21
+ import types
22
+ import os
23
+ import dementor
24
+
25
+ from importlib.machinery import SourceFileLoader
26
+ from dementor.config import SessionConfig
27
+ from dementor.paths import DEMENTOR_PATH
28
+
29
+
30
+ class ProtocolLoader:
31
+ def __init__(self) -> None:
32
+ self.rs_path = os.path.join(DEMENTOR_PATH, "protocols")
33
+
34
+ def load_protocol(self, protocol_path: str) -> types.ModuleType:
35
+ loader = SourceFileLoader("protocol", protocol_path)
36
+ protocol = types.ModuleType(loader.name)
37
+ loader.exec_module(protocol)
38
+ return protocol
39
+
40
+ def get_protocols(self, session=None):
41
+ protocols = {}
42
+ protocol_paths = [
43
+ os.path.join(os.path.dirname(dementor.__file__), "protocols"),
44
+ self.rs_path,
45
+ ]
46
+
47
+ if session is not None:
48
+ protocol_paths.extend(session.extra_modules)
49
+
50
+ for path in protocol_paths:
51
+ if not os.path.exists(path):
52
+ continue
53
+
54
+ # TODO: check for directory
55
+
56
+ for filename in os.listdir(path):
57
+ if not filename.endswith(".py") or filename == "__init__.py":
58
+ continue
59
+
60
+ protocol_path = os.path.join(path, filename)
61
+ name = filename[:-3]
62
+ protocols[name] = protocol_path
63
+
64
+ return protocols
65
+
66
+ def apply_config(self, protocol: types.ModuleType, session: SessionConfig):
67
+ # if config is defined, apply it
68
+ if hasattr(protocol, "apply_config"):
69
+ # sgnature is: apply_config(session: SessionConfig)
70
+ protocol.apply_config(session)
71
+
72
+ else:
73
+ # maybe another submodule?
74
+ if hasattr(protocol, "config"):
75
+ config_mod = protocol.config
76
+ # try again
77
+ self.apply_config(config_mod, session)
78
+
79
+ def create_servers(
80
+ self,
81
+ protocol: types.ModuleType,
82
+ session: SessionConfig,
83
+ ) -> list:
84
+ # creates servers for the given protocol (if defined)
85
+ if hasattr(protocol, "create_server_threads"):
86
+ # TODO: must be a thread instance
87
+ return protocol.create_server_threads(session)
88
+
89
+ return []
90
+
dementor/logger.py ADDED
@@ -0,0 +1,224 @@
1
+ # Copyright (c) 2025-Present MatrixEditor
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ import argparse
21
+ import inspect
22
+ import logging
23
+ import threading
24
+ import pathlib
25
+ import datetime
26
+ import sys
27
+
28
+ from abc import abstractmethod
29
+ from logging.handlers import RotatingFileHandler
30
+
31
+ from rich.console import Console
32
+ from rich.logging import RichHandler
33
+ from rich.markup import render
34
+
35
+ from dementor.config import TomlConfig, Attribute as A
36
+
37
+ dm_console = Console(
38
+ soft_wrap=True,
39
+ tab_size=4,
40
+ highlight=False,
41
+ highlighter=None,
42
+ )
43
+
44
+ dm_console_lock = threading.Lock()
45
+
46
+
47
+ class LoggingConfig(TomlConfig):
48
+ _section_ = "Log"
49
+ _fields_ = [
50
+ A("log_debug_loggers", "DebugLoggers", list),
51
+ A("log_dir", "LogDir", "logs"),
52
+ A("log_enable", "Enabled", True),
53
+ ]
54
+
55
+
56
+ def init():
57
+ debug_parser = argparse.ArgumentParser(add_help=False)
58
+ debug_parser.add_argument("--debug", action="store_true")
59
+ debug_parser.add_argument("--verbose", action="store_true")
60
+ argv, _ = debug_parser.parse_known_args()
61
+
62
+ config = TomlConfig.build_config(LoggingConfig)
63
+ loggers = {name: logging.getLogger(name) for name in config.log_debug_loggers}
64
+
65
+ for debug_logger in loggers.values():
66
+ debug_logger.disabled = True
67
+
68
+ handler = RichHandler(
69
+ console=dm_console,
70
+ rich_tracebacks=False,
71
+ tracebacks_show_locals=False,
72
+ highlighter=None,
73
+ markup=False,
74
+ keywords=[],
75
+ omit_repeated_times=False,
76
+ )
77
+ # should be disabled
78
+ handler.highlighter = None
79
+ logging.basicConfig(
80
+ format="(%(name)s) %(message)s",
81
+ datefmt="[%X]",
82
+ handlers=[handler],
83
+ encoding="utf-8",
84
+ )
85
+
86
+ root_logger = logging.getLogger("root")
87
+ if argv.verbose:
88
+ dm_logger.logger.setLevel(logging.DEBUG)
89
+ root_logger.setLevel(logging.DEBUG)
90
+ elif argv.debug:
91
+ dm_logger.logger.setLevel(logging.DEBUG)
92
+ root_logger.setLevel(logging.DEBUG)
93
+
94
+ for debug_logger in loggers.values():
95
+ debug_logger.disabled = False
96
+ else:
97
+ dm_logger.logger.setLevel(logging.INFO)
98
+ root_logger.setLevel(logging.INFO)
99
+
100
+
101
+ def dm_print(msg, *args, **kwargs) -> None:
102
+ # If someone has a better idea I'll be open for it. This is just
103
+ # a here to synchronize the logging output
104
+ if kwargs.pop("locked", False):
105
+ dm_console.print(msg, *args, **kwargs)
106
+ return
107
+
108
+ with dm_console_lock:
109
+ dm_console.print(msg, *args, **kwargs)
110
+
111
+
112
+ class ProtocolLogger(logging.LoggerAdapter):
113
+ def __init__(self, extra=None) -> None:
114
+ super().__init__(logging.getLogger("dementor"), extra or {})
115
+
116
+ def format(self, msg, *args, **kwargs):
117
+ if self.extra is None:
118
+ return f"{msg}", kwargs
119
+
120
+ module_name = self.extra.get("protocol", self.extra.get("module_name", None))
121
+ host = self.extra.get("host", kwargs.pop("host", "<no-host>"))
122
+ port = self.extra.get("port", kwargs.pop("port", "<no-port>"))
123
+ module_color = self.extra.get(
124
+ "protocol_color", self.extra.get("module_color", "cyan")
125
+ )
126
+ return (
127
+ f"[bold {module_color}]{module_name:<10}[/] {host:<25} {port:<6} {msg}",
128
+ kwargs,
129
+ )
130
+
131
+ def success(self, msg, color=None, *args, **kwargs):
132
+ color = color or "green"
133
+ prefix = r"[bold %s]\[+][/bold %s]" % (color, color)
134
+ msg, kwargs = self.format(f"{prefix} [white]{msg}[/white]", **kwargs)
135
+ dm_print(msg, *args, **kwargs)
136
+ self._emit_log_entry(msg, *args, **kwargs)
137
+
138
+ def display(self, msg, *args, **kwargs):
139
+ prefix = r"[bold %s]\[*][/bold %s]" % ("blue", "blue")
140
+ msg, kwargs = self.format(f"{prefix} [white]{msg}[/white]", **kwargs)
141
+ dm_print(msg, *args, **kwargs)
142
+ self._emit_log_entry(msg, *args, **kwargs)
143
+
144
+ def highlight(self, msg, *args, **kwargs):
145
+ msg, kwargs = self.format(f"[bold yellow]{msg}[/yellow bold]", **kwargs)
146
+ dm_print(msg, *args, **kwargs)
147
+ self._emit_log_entry(msg, *args, **kwargs)
148
+
149
+ def fail(self, msg, color=None, *args, **kwargs):
150
+ color = color or "red"
151
+ prefix = r"[bold %s]\[-][/bold %s]" % (color, color)
152
+ msg, kwargs = self.format(f"{prefix} {msg}", **kwargs)
153
+ dm_print(msg, *args, **kwargs)
154
+ self._emit_log_entry(msg, *args, **kwargs)
155
+
156
+ def _emit_log_entry(self, text, *args, **kwargs) -> None:
157
+ caller = inspect.currentframe().f_back.f_back.f_back
158
+ text = render(text).plain
159
+ if len(self.logger.handlers) > 0: # file handler
160
+ for handler in self.logger.handlers:
161
+ handler.handle(
162
+ logging.LogRecord(
163
+ "dementor",
164
+ logging.INFO,
165
+ pathname=caller.f_code.co_filename,
166
+ lineno=caller.f_lineno,
167
+ msg=text,
168
+ args=args,
169
+ kwargs=kwargs,
170
+ exc_info=False,
171
+ )
172
+ )
173
+
174
+ def add_logfile(self, log_file_path: str) -> None:
175
+ formatter = logging.Formatter(
176
+ "%(asctime)s | %(filename)s:%(lineno)s - %(levelname)s (%(name)s): %(message)s",
177
+ datefmt="[%X]",
178
+ )
179
+ outfile = pathlib.Path(log_file_path)
180
+ file_exists = outfile.exists()
181
+ if not file_exists:
182
+ open(str(outfile), "x")
183
+
184
+ handler = RotatingFileHandler(
185
+ outfile,
186
+ maxBytes=100000,
187
+ encoding="utf-8",
188
+ )
189
+ with handler._open() as fp:
190
+ time = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")
191
+ args = " ".join(sys.argv[1:])
192
+ line = f"[{time}]> LOG_START\n[{time}]> ARGS: {args}\n"
193
+ if not file_exists:
194
+ fp.write(line)
195
+ else:
196
+ fp.write(f"\n{line}")
197
+
198
+ handler.setFormatter(formatter)
199
+ self.logger.addHandler(handler)
200
+ self.logger.debug(f"Created log file handler for {log_file_path}")
201
+
202
+ @staticmethod
203
+ def init_logfile(session) -> None:
204
+ config = TomlConfig.build_config(LoggingConfig)
205
+ if not config.log_enable:
206
+ return
207
+
208
+ workspace = pathlib.Path(session.workspace_path)
209
+ workspace /= config.log_dir or "logs"
210
+ workspace.mkdir(parents=True, exist_ok=True)
211
+ log_name = f"log_{datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.log"
212
+ dm_logger.add_logfile(str(workspace / log_name))
213
+
214
+
215
+ class ProtocolLoggerMixin:
216
+ def __init__(self) -> None:
217
+ self.logger = self.proto_logger()
218
+
219
+ @abstractmethod
220
+ def proto_logger(self) -> ProtocolLogger:
221
+ pass
222
+
223
+
224
+ dm_logger = ProtocolLogger()
dementor/paths.py ADDED
@@ -0,0 +1,26 @@
1
+ # Copyright (c) 2025-Present MatrixEditor
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ import os
21
+ import dementor
22
+
23
+ DEMENTOR_PATH = os.path.expanduser("~/.dementor")
24
+ CONFIG_PATH = os.path.join(DEMENTOR_PATH, "Dementor.conf")
25
+ ASSETS_PATH = os.path.join(os.path.dirname(dementor.__file__), "assets")
26
+ BANNER_PATH = os.path.join(ASSETS_PATH, "banner.txt")
@@ -0,0 +1,19 @@
1
+ # Copyright (c) 2025-Present MatrixEditor
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
@@ -0,0 +1,104 @@
1
+ # Copyright (c) 2025-Present MatrixEditor
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
20
+ from dementor.config import Attribute as A, TomlConfig, get_value
21
+ from dementor.logger import ProtocolLogger
22
+ from dementor.servers import BaseProtoHandler, ThreadingTCPServer, ServerThread
23
+ from dementor.database import _CLEARTEXT
24
+
25
+ ReplyCodes = {
26
+ 220: b"220 Service ready for new user.",
27
+ 331: b"331 User name okay, need password.",
28
+ 501: b"501 Syntax error in parameters or arguments.",
29
+ 502: b"502 Command not implemented.",
30
+ 530: b"530 Not logged in.",
31
+ }
32
+
33
+
34
+ class FTPServerConfig(TomlConfig):
35
+ _section_ = "FTP"
36
+ _fields_ = [A("ftp_port", "Port")]
37
+
38
+
39
+ def apply_config(session) -> None:
40
+ session.ftp_config = []
41
+ if session.ftp_enabled:
42
+ for server_config in get_value("FTP", "Server", default=[]):
43
+ session.ftp_config.append(FTPServerConfig(server_config))
44
+
45
+
46
+ def create_server_threads(session) -> list:
47
+ return [
48
+ ServerThread(session, FTPServer, server_address=("", server_config.ftp_port))
49
+ for server_config in session.ftp_config
50
+ ]
51
+
52
+
53
+ class FTPHandler(BaseProtoHandler):
54
+ def proto_logger(self) -> ProtocolLogger:
55
+ return ProtocolLogger(
56
+ extra={
57
+ "protocol": "FTP",
58
+ "protocol_color": "medium_purple2",
59
+ "host": self.client_host,
60
+ "port": self.server.server_address[1],
61
+ }
62
+ )
63
+
64
+ def handle_data(self, data, transport) -> None:
65
+ # Server ready for new user
66
+ self.reply(220)
67
+
68
+ data = transport.recv(1024)
69
+ if len(data) < 4:
70
+ # ignore short packets and return error
71
+ self.reply(502)
72
+ return
73
+
74
+ if data[:4] == b"USER":
75
+ user_name = data[4:].decode(errors="replace").strip()
76
+ if not user_name:
77
+ self.reply(501)
78
+ return
79
+
80
+ self.reply(331)
81
+ data = transport.recv(1024)
82
+ if len(data) >= 4 and data[:4] == b"PASS":
83
+ password = data[4:].decode(errors="replace").strip()
84
+
85
+ self.config.db.add_auth(
86
+ client=self.client_address,
87
+ credtype=_CLEARTEXT,
88
+ username=user_name,
89
+ password=password,
90
+ logger=self.logger,
91
+ )
92
+ self.reply(502) # Command not implemented rather than error
93
+ return
94
+
95
+ self.reply(501)
96
+
97
+ def reply(self, code: int) -> None:
98
+ self.request.send(ReplyCodes[code] + b"\r\n")
99
+
100
+
101
+ class FTPServer(ThreadingTCPServer):
102
+ default_port = 21
103
+ default_handler_class = FTPHandler
104
+ service_name = "FTP"