Skip to content

Commit 017b06a

Browse files
committed
refactor!: Rewrite cookie logic; feat: Add CNM protocol verify; Add log system; fix: Variable length text cannot be primary key;
1 parent 4a39eae commit 017b06a

File tree

13 files changed

+121
-49
lines changed

13 files changed

+121
-49
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,9 @@ Services/Config/config.json
183183
# plugins
184184
Plugins/*/
185185

186+
# logs
187+
Logs/*.lock
188+
Logs/*.log.*
189+
186190
# version lock
187191
uv.lock

Logs/.gitkeep

Whitespace-only changes.

Models/database.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class UserDb(Base):
1111
__tablename__ = "user"
1212
user_id: Mapped[str] = mapped_column(VARCHAR(32), primary_key=True)
13-
username: Mapped[str] = mapped_column(TEXT, unique=True, nullable=False)
13+
username: Mapped[str] = mapped_column(VARCHAR(255), nullable=False)
1414
email: Mapped[str] = mapped_column(TEXT, nullable=False)
1515
password: Mapped[str] = mapped_column(TEXT, nullable=False)
1616
created_at: Mapped[datetime] = mapped_column(
@@ -22,5 +22,5 @@ class PwdDb(Base):
2222
__tablename__ = "src_pwd"
2323
__table_args__ = (PrimaryKeyConstraint("user_id", "source"),)
2424
user_id: Mapped[str] = mapped_column(VARCHAR(32), nullable=False)
25-
source: Mapped[str] = mapped_column(TEXT, nullable=False)
25+
source: Mapped[str] = mapped_column(VARCHAR(10), nullable=False)
2626
data: Mapped[str] = mapped_column(TEXT, nullable=False)

Models/plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def search(self, keyword: str) -> list[BaseComicInfo]:
2626
pass
2727

2828
@abstractmethod
29-
def album(self, album_id: str) -> list[ComicInfo]:
29+
def album(self, album_id: str) -> ComicInfo:
3030
pass
3131

3232

Models/user.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
from datetime import datetime
3-
from http.cookies import BaseCookie
43

54
from pydantic import BaseModel
65

@@ -14,24 +13,28 @@ class User(BaseModel):
1413

1514
class UserData:
1615
uid: str
17-
plugin_cookies: dict[str, BaseCookie[str]]
16+
plugin_cookies: dict[str, dict[str, str]] | None
1817

19-
def __init__(self, uid: str, plugin_cookies: dict[str, BaseCookie[str]]):
18+
def __init__(
19+
self, uid: str, plugin_cookies: dict[str, dict[str, str]] | None = None
20+
) -> None:
2021
self.uid = uid
2122
self.plugin_cookies = plugin_cookies
2223

23-
def set_src_cookies(self, src: str, cookies: BaseCookie[str]) -> None:
24-
self.plugin_cookies[src] = cookies
24+
def set_src_cookies(self, src: str, cookies: dict[str, str | None]) -> None:
25+
if self.plugin_cookies is None:
26+
self.plugin_cookies = dict()
27+
_cookies = {k: v for k, v in cookies.items() if v is not None}
28+
self.plugin_cookies[src] = _cookies
2529

26-
def get_src_cookies(self, src: str) -> BaseCookie[str]:
27-
if src in self.plugin_cookies:
28-
return self.plugin_cookies[src]
29-
else:
30-
return BaseCookie[str]()
30+
def get_src_cookies(self, src: str) -> dict[str, str]:
31+
return self.plugin_cookies.get(src, dict()) if self.plugin_cookies else dict()
3132

3233
def __str__(self):
33-
return json.dumps(
34-
{k: v.output(header="").strip() for k, v in self.plugin_cookies.items()}
34+
return (
35+
json.dumps(self.plugin_cookies, ensure_ascii=False)
36+
if self.plugin_cookies
37+
else "{}"
3538
)
3639

3740

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ For Red Hat / CentOS:
2929
sudo yum install python3-devel mysql-devel pkgconfig
3030
```
3131

32-
Restore the dependencies:
32+
Restore the dependencies(Include the plugins' dependencies):
3333

3434
```bash
35-
uv sync
35+
uv sync --all-packages
3636
```
3737

3838
Modify the configuration file located at `Services/Config/config.toml.sample` and rename it to `config.toml`.

Routers/core.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from fastapi import APIRouter
2+
23
from Models.response import BaseResponse, StandardResponse
34
from Services.Modulator.manager import plugin_manager
45

@@ -10,6 +11,12 @@ def get_status() -> StandardResponse[None]:
1011
return StandardResponse(message="pong")
1112

1213

14+
@core_router.get("/sources", response_model=BaseResponse[set[str]])
15+
def get_sources() -> StandardResponse[set[str]]:
16+
sources = plugin_manager.registered_source
17+
return StandardResponse[set[str]](data=sources)
18+
19+
1320
@core_router.get("/protocol", response_model=BaseResponse[str])
1421
def get_cnm_version() -> StandardResponse[str]:
15-
return StandardResponse[str](data=plugin_manager.cnm_version)
22+
return StandardResponse[str](data=plugin_manager.cnm_version.__str__())

Services/Config/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ class PluginConfig(BaseModel):
2929
strict_load: bool
3030

3131

32+
class LogConfig(BaseModel):
33+
log_level: str
34+
35+
3236
class Config(BaseModel):
3337
security: SecurityConfig
3438
database: DatabaseConfig
3539
email: EmailConfig
3640
plugin: PluginConfig
41+
log: LogConfig = LogConfig(log_level="INFO")
3742

3843
@classmethod
3944
def load(cls):

Services/Config/config.toml.sample

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ address =
1515
password =
1616

1717
[plugin]
18-
strict_load = false
18+
strict_load = false
19+
20+
# [log]
21+
# log_level = # Set to debug, info, warning, error, or critical

Services/Mail/mail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def _send_email(addr: str, subject: str, body: str):
3939

4040

4141
def send_captcha(addr: str, purpose: Purpose, ip: str) -> str:
42-
captcha = str(secure_rng.randrange(10000, 99999))
42+
captcha = str(secure_rng.randrange(100001, 999999))
4343
body = captcha_template.format(captcha=captcha, purpose=purpose.__str__(), ip=ip)
4444
_send_email(addr, purpose.__str__(), body)
4545
return captcha

0 commit comments

Comments
 (0)