dementor 1.0.0.dev12__tar.gz → 1.0.0.dev14__tar.gz
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-1.0.0.dev12 → dementor-1.0.0.dev14}/PKG-INFO +11 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/README.md +11 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/__init__.py +1 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/assets/Dementor.toml +6 -12
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/config/session.py +88 -13
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/config/toml.py +9 -4
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/config/util.py +4 -4
- dementor-1.0.0.dev14/dementor/db/__init__.py +27 -0
- dementor-1.0.0.dev14/dementor/db/connector.py +87 -0
- dementor-1.0.0.dev12/dementor/database.py → dementor-1.0.0.dev14/dementor/db/model.py +142 -127
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/filters.py +11 -10
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/loader.py +36 -17
- dementor-1.0.0.dev14/dementor/log/hexdump.py +24 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/log/logger.py +46 -16
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/log/stream.py +21 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/ftp.py +10 -3
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/http.py +57 -24
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/imap.py +22 -6
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/ipp.py +34 -10
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/kerberos.py +11 -5
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/ldap.py +34 -16
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/llmnr.py +18 -8
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/mdns.py +27 -17
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/msrpc/rpc.py +34 -6
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/msrpc.py +3 -2
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/mssql.py +36 -13
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/mysql.py +46 -14
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/netbios.py +31 -19
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/pop3.py +19 -5
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/quic.py +16 -9
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/smb.py +20 -5
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/smtp.py +25 -12
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/spnego.py +1 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/ssdp.py +13 -5
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/upnp.py +35 -14
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/x11.py +18 -11
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/servers.py +37 -10
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/standalone.py +83 -30
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor.egg-info/PKG-INFO +11 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor.egg-info/SOURCES.txt +4 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/docs/README.pypi.md +11 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/pyproject.toml +1 -1
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/LICENSE +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/__main__.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/assets/banner.txt +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/assets/www/error_page.html +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/assets/www/upnp-default/dd.xml +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/assets/www/upnp-default/present.html +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/assets/www/upnp-default/scpd.xml +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/assets/www/wpad.dat +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/config/__init__.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/config/attr.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/log/__init__.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/paths.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/__init__.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/msrpc/__init__.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/msrpc/dcom.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/msrpc/epm.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor/protocols/ntlm.py +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor.egg-info/dependency_links.txt +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor.egg-info/entry_points.txt +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor.egg-info/requires.txt +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/dementor.egg-info/top_level.txt +0 -0
- {dementor-1.0.0.dev12 → dementor-1.0.0.dev14}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dementor
|
|
3
|
-
Version: 1.0.0.
|
|
3
|
+
Version: 1.0.0.dev14
|
|
4
4
|
Summary: LLMNR/NBT-NS/mDNS Poisoner and rogue service provider
|
|
5
5
|
Author: MatrixEditor
|
|
6
6
|
Maintainer: MatrixEditor
|
|
@@ -114,3 +114,13 @@ Take a look at the [Documentation on GitHub-Pages](https://matrixeditor.github.i
|
|
|
114
114
|
## License
|
|
115
115
|
|
|
116
116
|
Distributed under the MIT License. See LICENSE for more information.
|
|
117
|
+
|
|
118
|
+
## Disclaimer
|
|
119
|
+
|
|
120
|
+
**Dementor** is intended only for lawful educational purposes: learning, testing
|
|
121
|
+
in your own lab, or assessments on systems where you have explicit written
|
|
122
|
+
authorization. You agree not to use this software to access, damage, interfere
|
|
123
|
+
with, or exfiltrate data from systems for which you do not have permission.
|
|
124
|
+
We make no promises about safety, completeness, or fitness for any purpose. Use
|
|
125
|
+
at your own risk. If you discover a vulnerability, please follow responsible
|
|
126
|
+
disclosure by using the private disclosing feature offered by GitHub.
|
|
@@ -56,4 +56,14 @@ Take a look at the [Documentation on GitHub-Pages](https://matrixeditor.github.i
|
|
|
56
56
|
|
|
57
57
|
## License
|
|
58
58
|
|
|
59
|
-
Distributed under the MIT License. See LICENSE for more information.
|
|
59
|
+
Distributed under the MIT License. See LICENSE for more information.
|
|
60
|
+
|
|
61
|
+
## Disclaimer
|
|
62
|
+
|
|
63
|
+
**Dementor** is intended only for lawful educational purposes: learning, testing
|
|
64
|
+
in your own lab, or assessments on systems where you have explicit written
|
|
65
|
+
authorization. You agree not to use this software to access, damage, interfere
|
|
66
|
+
with, or exfiltrate data from systems for which you do not have permission.
|
|
67
|
+
We make no promises about safety, completeness, or fitness for any purpose. Use
|
|
68
|
+
at your own risk. If you discover a vulnerability, please follow responsible
|
|
69
|
+
disclosure by using the private disclosing feature offered by GitHub.
|
|
@@ -186,10 +186,10 @@ UPnP = true
|
|
|
186
186
|
# These extra fields (e.g., `foo`, `bar`) will be passed to the final
|
|
187
187
|
# FilterObj instance and may be used for protocol-specific handling.
|
|
188
188
|
|
|
189
|
-
#
|
|
189
|
+
# Target = []
|
|
190
190
|
|
|
191
191
|
# Describes a list of hosts to *exclude* from poisoning (blacklist approach).
|
|
192
|
-
# Uses the same filter structure as
|
|
192
|
+
# Uses the same filter structure as Target.
|
|
193
193
|
|
|
194
194
|
# Ignore = []
|
|
195
195
|
|
|
@@ -205,16 +205,10 @@ UPnP = true
|
|
|
205
205
|
|
|
206
206
|
DuplicateCreds = true
|
|
207
207
|
|
|
208
|
-
#
|
|
209
|
-
#
|
|
210
|
-
|
|
211
|
-
#
|
|
212
|
-
|
|
213
|
-
# Custom path where the database file will be stored. If set, it overrides the
|
|
214
|
-
# workspace directory.
|
|
215
|
-
|
|
216
|
-
# Directory = "/path/to/directory/"
|
|
217
|
-
|
|
208
|
+
# Dialect = "sqlite"
|
|
209
|
+
# Driver = "pysqlite"
|
|
210
|
+
# Url = "sqlite:///:memory:"
|
|
211
|
+
# Path = "Dementor.db"
|
|
218
212
|
|
|
219
213
|
# =============================================================================
|
|
220
214
|
# mDNS
|
|
@@ -17,10 +17,11 @@
|
|
|
17
17
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
19
|
# SOFTWARE.
|
|
20
|
+
# pyright: reportUninitializedInstanceVariable=false
|
|
20
21
|
import asyncio
|
|
22
|
+
import typing
|
|
21
23
|
|
|
22
|
-
from typing import Any
|
|
23
|
-
|
|
24
|
+
from typing import Any
|
|
24
25
|
from pathlib import Path
|
|
25
26
|
|
|
26
27
|
from dementor.config.toml import TomlConfig, Attribute
|
|
@@ -28,6 +29,31 @@ from dementor.config.util import is_true
|
|
|
28
29
|
from dementor.paths import DEMENTOR_PATH
|
|
29
30
|
from dementor import config
|
|
30
31
|
|
|
32
|
+
if typing.TYPE_CHECKING:
|
|
33
|
+
from dementor.protocols import (
|
|
34
|
+
kerberos,
|
|
35
|
+
mdns,
|
|
36
|
+
netbios,
|
|
37
|
+
llmnr,
|
|
38
|
+
ldap,
|
|
39
|
+
smb,
|
|
40
|
+
smtp,
|
|
41
|
+
ftp,
|
|
42
|
+
http,
|
|
43
|
+
imap,
|
|
44
|
+
ipp,
|
|
45
|
+
mssql,
|
|
46
|
+
mysql,
|
|
47
|
+
pop3,
|
|
48
|
+
quic,
|
|
49
|
+
ssdp,
|
|
50
|
+
upnp,
|
|
51
|
+
x11,
|
|
52
|
+
)
|
|
53
|
+
from dementor.protocols.msrpc import rpc
|
|
54
|
+
from dementor.db.model import DementorDB
|
|
55
|
+
from dementor.db.connector import DatabaseConfig
|
|
56
|
+
|
|
31
57
|
|
|
32
58
|
class SessionConfig(TomlConfig):
|
|
33
59
|
_section_ = "Dementor"
|
|
@@ -64,14 +90,66 @@ class SessionConfig(TomlConfig):
|
|
|
64
90
|
]
|
|
65
91
|
|
|
66
92
|
# TODO: move into .pyi
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
93
|
+
if typing.TYPE_CHECKING:
|
|
94
|
+
workspace_path: str
|
|
95
|
+
extra_modules: list[str]
|
|
96
|
+
ipv6: str | None
|
|
97
|
+
ipv4: str | None
|
|
98
|
+
interface: str | None
|
|
99
|
+
protocols: dict[str, Any]
|
|
100
|
+
|
|
101
|
+
db: DementorDB
|
|
102
|
+
db_config: DatabaseConfig
|
|
103
|
+
krb5_config: kerberos.KerberosConfig
|
|
104
|
+
mdns_config: mdns.MDNSConfig
|
|
105
|
+
llmnr_config: llmnr.LLMNRConfig
|
|
106
|
+
netbiosns_config: netbios.NBTNSConfig
|
|
107
|
+
ldap_config: list[ldap.LDAPServerConfig]
|
|
108
|
+
smtp_servers: list[smtp.SMTPServerConfig]
|
|
109
|
+
smb_config: list[smb.SMBServerConfig]
|
|
110
|
+
ftp_config: list[ftp.FTPServerConfig]
|
|
111
|
+
proxy_config: http.ProxyAutoConfig
|
|
112
|
+
http_config: list[http.HTTPServerConfig]
|
|
113
|
+
winrm_config: list[http.HTTPServerConfig]
|
|
114
|
+
imap_config: list[imap.IMAPServerConfig]
|
|
115
|
+
ipp_config: ipp.IPPConfig
|
|
116
|
+
rpc_config: rpc.RPCConfig
|
|
117
|
+
mssql_config: mssql.MSSQLConfig
|
|
118
|
+
ssrp_config: mssql.SSRPConfig
|
|
119
|
+
mysql_config: mysql.MySQLConfig
|
|
120
|
+
pop3_config: list[pop3.POP3ServerConfig]
|
|
121
|
+
quic_config: quic.QuicServerConfig
|
|
122
|
+
ssdp_config: ssdp.SSDPConfig
|
|
123
|
+
upnp_config: upnp.UPNPConfig
|
|
124
|
+
x11_config: x11.X11Config
|
|
125
|
+
|
|
126
|
+
ntlm_challange: bytes
|
|
127
|
+
ntlm_ess: bool
|
|
128
|
+
analysis: bool
|
|
129
|
+
loop: asyncio.AbstractEventLoop
|
|
130
|
+
|
|
131
|
+
llmnr_enabled: bool
|
|
132
|
+
nbtns_enabled: bool
|
|
133
|
+
nbtds_enabled: bool
|
|
134
|
+
smtp_enabled: bool
|
|
135
|
+
smb_enabled: bool
|
|
136
|
+
ftp_enabled: bool
|
|
137
|
+
kdc_enabled: bool
|
|
138
|
+
ldap_enabled: bool
|
|
139
|
+
quic_enabled: bool
|
|
140
|
+
mdns_enabled: bool
|
|
141
|
+
http_enabled: bool
|
|
142
|
+
rpc_enabled: bool
|
|
143
|
+
winrm_enabled: bool
|
|
144
|
+
mssql_enabled: bool
|
|
145
|
+
ssrp_enabled: bool
|
|
146
|
+
imap_enabled: bool
|
|
147
|
+
pop3_enabled: bool
|
|
148
|
+
mysql_enabled: bool
|
|
149
|
+
x11_enabled: bool
|
|
150
|
+
ipp_enabled: bool
|
|
151
|
+
ssdp_enabled: bool
|
|
152
|
+
upnp_enabled: bool
|
|
75
153
|
|
|
76
154
|
def __init__(self) -> None:
|
|
77
155
|
super().__init__(config._get_global_config().get("Dementor", {}))
|
|
@@ -90,9 +168,6 @@ class SessionConfig(TomlConfig):
|
|
|
90
168
|
self.ntlm_challange = b"1337LEET"
|
|
91
169
|
self.ntlm_ess = True
|
|
92
170
|
|
|
93
|
-
# SMB configuration
|
|
94
|
-
self.smb_server_config = []
|
|
95
|
-
|
|
96
171
|
def is_bound_to_all(self) -> bool:
|
|
97
172
|
# REVISIT: this should raise an exception
|
|
98
173
|
return self.interface == "ALL"
|
|
@@ -17,11 +17,12 @@
|
|
|
17
17
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
19
|
# SOFTWARE.
|
|
20
|
-
from typing import NamedTuple, Callable, Any
|
|
20
|
+
from typing import NamedTuple, Callable, Any, TypeVar
|
|
21
21
|
|
|
22
22
|
from dementor.config.util import get_value
|
|
23
23
|
|
|
24
24
|
_LOCAL = object()
|
|
25
|
+
_T = TypeVar("_T", bound="TomlConfig")
|
|
25
26
|
|
|
26
27
|
|
|
27
28
|
class Attribute(NamedTuple):
|
|
@@ -36,7 +37,7 @@ class TomlConfig:
|
|
|
36
37
|
_section_: str | None
|
|
37
38
|
_fields_: list[Attribute]
|
|
38
39
|
|
|
39
|
-
def __init__(self, config: dict | None = None) -> None:
|
|
40
|
+
def __init__(self, config: dict[str, Any] | None = None) -> None:
|
|
40
41
|
for field in self._fields_:
|
|
41
42
|
self._set_field(
|
|
42
43
|
config or {},
|
|
@@ -62,8 +63,12 @@ class TomlConfig:
|
|
|
62
63
|
raise KeyError(f"Could not find config with key {key!r}")
|
|
63
64
|
|
|
64
65
|
@staticmethod
|
|
65
|
-
def build_config(cls_ty, section: str | None = None) ->
|
|
66
|
-
|
|
66
|
+
def build_config(cls_ty: type[_T], section: str | None = None) -> _T:
|
|
67
|
+
section_name = section or cls_ty._section_
|
|
68
|
+
if section_name is None:
|
|
69
|
+
raise ValueError("section cannot be None")
|
|
70
|
+
|
|
71
|
+
return cls_ty(get_value(section_name, key=None, default={}))
|
|
67
72
|
|
|
68
73
|
def _set_field(
|
|
69
74
|
self,
|
|
@@ -53,10 +53,10 @@ def is_true(value: str) -> bool:
|
|
|
53
53
|
|
|
54
54
|
|
|
55
55
|
class BytesValue:
|
|
56
|
-
def __init__(self, length=None) -> None:
|
|
57
|
-
self.length = length
|
|
56
|
+
def __init__(self, length: int | None = None) -> None:
|
|
57
|
+
self.length: int | None = length
|
|
58
58
|
|
|
59
|
-
def __call__(self, value) ->
|
|
59
|
+
def __call__(self, value: Any) -> bytes:
|
|
60
60
|
match value:
|
|
61
61
|
case None:
|
|
62
62
|
return secrets.token_bytes(self.length or 1)
|
|
@@ -78,7 +78,7 @@ def random_value(size: int) -> str:
|
|
|
78
78
|
return "".join(random.choice(string.ascii_letters) for _ in range(size))
|
|
79
79
|
|
|
80
80
|
|
|
81
|
-
def format_string(value: str, locals: dict | None = None) -> str:
|
|
81
|
+
def format_string(value: str, locals: dict[str, Any] | None = None) -> str:
|
|
82
82
|
config = _get_global_config()
|
|
83
83
|
try:
|
|
84
84
|
template = _SANDBOX.from_string(value)
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
|
|
21
|
+
_CLEARTEXT = "Cleartext"
|
|
22
|
+
_NO_USER = "<missing-user>"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def normalize_client_address(client: str) -> str:
|
|
26
|
+
return client.removeprefix("::ffff:")
|
|
27
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
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 sqlite3
|
|
21
|
+
from sqlalchemy import Engine, create_engine
|
|
22
|
+
|
|
23
|
+
from dementor.config.session import SessionConfig
|
|
24
|
+
from dementor.db.model import DementorDB, ModelBase
|
|
25
|
+
from dementor.log.logger import dm_logger
|
|
26
|
+
from dementor.config.toml import TomlConfig, Attribute as A
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class DatabaseConfig(TomlConfig):
|
|
30
|
+
_section_ = "DB"
|
|
31
|
+
_fields_ = [
|
|
32
|
+
A("db_raw_path", "Url", None),
|
|
33
|
+
A("db_path", "Path", "Dementor.db"),
|
|
34
|
+
A("db_duplicate_creds", "DuplicateCreds", False),
|
|
35
|
+
A("db_dialect", "Dialect", None),
|
|
36
|
+
A("db_driver", "Driver", None),
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def init_dementor_db(session: SessionConfig) -> Engine | None:
|
|
41
|
+
engine = init_engine(session)
|
|
42
|
+
if engine is not None:
|
|
43
|
+
ModelBase.metadata.create_all(engine)
|
|
44
|
+
return engine
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def init_engine(session: SessionConfig) -> Engine | None:
|
|
48
|
+
# based on dialect and driver configuration
|
|
49
|
+
raw_path = session.db_config.db_raw_path
|
|
50
|
+
if raw_path is None:
|
|
51
|
+
# fall back to constructing the path manually
|
|
52
|
+
dialect = session.db_config.db_dialect or "sqlite"
|
|
53
|
+
driver = session.db_config.db_driver or "pysqlite"
|
|
54
|
+
path = session.db_config.db_path
|
|
55
|
+
if not path:
|
|
56
|
+
return dm_logger.error("Database path not specified!")
|
|
57
|
+
|
|
58
|
+
if dialect == "sqlite":
|
|
59
|
+
if path != ":memory:":
|
|
60
|
+
path = f"/{session.resolve_path(path)}"
|
|
61
|
+
|
|
62
|
+
# see https://docs.sqlalchemy.org/en/20/core/engines.html#database-urls
|
|
63
|
+
raw_path = f"{dialect}+{driver}://{path}"
|
|
64
|
+
else:
|
|
65
|
+
sql_type, path = raw_path.split("://")
|
|
66
|
+
if sql_type.count("+") > 0:
|
|
67
|
+
dialect, driver = sql_type.split("+")
|
|
68
|
+
else:
|
|
69
|
+
dialect = sql_type
|
|
70
|
+
driver = "<default>"
|
|
71
|
+
|
|
72
|
+
if dialect != "sqlite":
|
|
73
|
+
first_element, *parts = path.split("/")
|
|
74
|
+
if "@" in first_element:
|
|
75
|
+
first_element = first_element.split("@")[1]
|
|
76
|
+
path = "***:***@" + "/".join([first_element] + list(parts))
|
|
77
|
+
|
|
78
|
+
dm_logger.debug("Using database [%s:%s] at: %s", dialect, driver, path)
|
|
79
|
+
return create_engine(raw_path, isolation_level="AUTOCOMMIT", future=True)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def create_db(session: SessionConfig) -> DementorDB:
|
|
83
|
+
# TODO: add support for custom database implementations
|
|
84
|
+
engine = init_engine(session)
|
|
85
|
+
if not engine:
|
|
86
|
+
raise Exception("Failed to create database engine")
|
|
87
|
+
return DementorDB(engine, session)
|