Skip to content

The only downloader you need. 下载器的集大成者。

feat(wrapped-link): 支持迅雷、快车、QQ 旋风链接

XiaoYouChR2038139e41a8
diff --git a/GLOSSARY.md b/GLOSSARY.md
index 201744b9..afb0e35f 100644
--- a/GLOSSARY.md
+++ b/GLOSSARY.md
@@ -104,6 +104,11 @@ FeaturePack 提供的能力,将 Task Options 转为 Task。声明优先级和
Task 加入失败(如 Output Folder 无法创建)时退回 Task Draft,由用户修改后重新确认。
_Avoid_: pending task、unconfirmed task
+**Wrapped Link**:
+把另一条可下载链接编码包裹起来的链接:迅雷(thunder)、QQ 旋风(qqdl)、快车(flashget)。解出的链接称 Inner Link。
+在任何入口,Wrapped Link 的表现都与直接给出它的 Inner Link 相同;Task 只记 Inner Link。只解一层。
+_Avoid_: 加密链接(Base64 不是加密)、专用链、转换
+
**Resource**:
浏览器扩展捕获的可下载物。由 Browser Service 转换为 Task Options 进入任务创建流程。
_Avoid_: 与泛义"资源"混淆
diff --git a/app/models/pack.py b/app/models/pack.py
index 81bf97a7..7af12c72 100644
--- a/app/models/pack.py
+++ b/app/models/pack.py
@@ -43,6 +43,7 @@ class TaskParser:
priority: int = 100
pack: FeaturePack | None = None
delegate: Callable[[TaskOptions], Awaitable[Task]] | None = None
+ delegateProbe: Callable[[TaskOptions], Awaitable[TaskOptions]] | None = None
def match(self, options: TaskOptions) -> bool:
raise NotImplementedError
diff --git a/app/services/feature_service.py b/app/services/feature_service.py
index bb490a76..bee6348e 100644
--- a/app/services/feature_service.py
+++ b/app/services/feature_service.py
@@ -62,6 +62,7 @@ class FeatureService:
parser = ParserClass()
parser.pack = pack
parser.delegate = self.parse
+ parser.delegateProbe = self.probe
self._parsers.append(parser)
self._parsers.sort(key=lambda p: p.priority)
diff --git a/features/wrapped_link_pack/config.py b/features/wrapped_link_pack/config.py
new file mode 100644
index 00000000..7383e58c
--- /dev/null
+++ b/features/wrapped_link_pack/config.py
@@ -0,0 +1,9 @@
+from app.config.cfg import ConfigItem, BoolValidator
+from app.models.pack import PackConfig
+
+
+class WrappedLinkConfig(PackConfig):
+ associateUriSchemes = ConfigItem("WrappedLink", "AssociateUriSchemes", False, BoolValidator())
+
+
+wrappedLinkConfig = WrappedLinkConfig()
diff --git a/features/wrapped_link_pack/i18n.py b/features/wrapped_link_pack/i18n.py
new file mode 100644
index 00000000..21fc4141
--- /dev/null
+++ b/features/wrapped_link_pack/i18n.py
@@ -0,0 +1,4 @@
+from app.i18n import N
+
+N("TaskErrors", "无法解析该链接")
+N("TaskErrors", "不支持多层包裹的链接")
diff --git a/features/wrapped_link_pack/manifest.toml b/features/wrapped_link_pack/manifest.toml
new file mode 100644
index 00000000..8617d084
--- /dev/null
+++ b/features/wrapped_link_pack/manifest.toml
@@ -0,0 +1,5 @@
+[pack]
+entry = "pack.py"
+class = "WrappedLinkPack"
+version = "1.0.0"
+gdMinVersion = "4.4.7"
diff --git a/features/wrapped_link_pack/pack.py b/features/wrapped_link_pack/pack.py
new file mode 100644
index 00000000..63ad7182
--- /dev/null
+++ b/features/wrapped_link_pack/pack.py
@@ -0,0 +1,64 @@
+from __future__ import annotations
+
+import base64
+import binascii
+from dataclasses import replace
+from urllib.parse import urlparse
+
+from app.i18n import N
+from app.models.pack import FeaturePack, TaskParser, UriScheme
+from app.models.task import Task, TaskError, TaskOptions
+from .config import wrappedLinkConfig
+
+SCHEMES = {"thunder", "flashget", "qqdl"}
+
+
+def toInnerUrl(url: str) -> str:
+ scheme, _, payload = url.strip().partition("://")
+ payload = payload.split("&", 1)[0].rstrip("/")
+ try:
+ raw = base64.b64decode(payload + "=" * (-len(payload) % 4))
+ try:
+ text = raw.decode("utf-8")
+ except UnicodeDecodeError:
+ text = raw.decode("gbk")
+ except (binascii.Error, UnicodeDecodeError) as e:
+ raise TaskError("无法解析该链接") from e
+
+ match scheme.lower():
+ case "thunder":
+ if not (text.startswith("AA") and text.endswith("ZZ")):
+ raise TaskError("无法解析该链接")
+ text = text[2:-2]
+ case "flashget":
+ text = text.removeprefix("[FLASHGET]").removesuffix("[FLASHGET]").removesuffix("[/FLASHGET]")
+
+ innerScheme = urlparse(text.strip()).scheme.lower()
+ if innerScheme in SCHEMES:
+ raise TaskError("不支持多层包裹的链接")
+ if not innerScheme:
+ raise TaskError("无法解析该链接")
+ return text.strip()
+
+
+class WrappedLinkParser(TaskParser):
+ priority = 10
+
+ def match(self, options: TaskOptions) -> bool:
+ return urlparse(options.url.strip()).scheme.lower() in SCHEMES
+
+ async def parse(self, options: TaskOptions) -> Task:
+ return await self.delegate(replace(options, url=toInnerUrl(options.url)))
+
+ async def probe(self, options: TaskOptions) -> TaskOptions:
+ return await self.delegateProbe(replace(options, url=toInnerUrl(options.url)))
+
+
+class WrappedLinkPack(FeaturePack):
+ packId = "wrapped_link"
+ capturedLinks = N("WrappedLinkPack", "迅雷、快车、QQ 旋风链接")
+ config = wrappedLinkConfig
+ parsers = [WrappedLinkParser]
+
+ def uriSchemes(self) -> list[UriScheme]:
+ return [UriScheme("thunder", "迅雷"), UriScheme("flashget", "快车"), UriScheme("qqdl", "QQ 旋风")]
diff --git a/scripts/Ghost-Downloader-3.iss b/scripts/Ghost-Downloader-3.iss
index fae04338..84f48497 100644
--- a/scripts/Ghost-Downloader-3.iss
+++ b/scripts/Ghost-Downloader-3.iss
@@ -72,6 +72,9 @@ Root: HKCU; Subkey: "Software\Classes\magnet"; Flags: dontcreatekey uninsdeletek
Root: HKCU; Subkey: "Software\Classes\ed2k"; Flags: dontcreatekey uninsdeletekey
Root: HKCU; Subkey: "Software\Classes\ftp"; Flags: dontcreatekey uninsdeletekey
Root: HKCU; Subkey: "Software\Classes\ftps"; Flags: dontcreatekey uninsdeletekey
+Root: HKCU; Subkey: "Software\Classes\thunder"; Flags: dontcreatekey uninsdeletekey
+Root: HKCU; Subkey: "Software\Classes\flashget"; Flags: dontcreatekey uninsdeletekey
+Root: HKCU; Subkey: "Software\Classes\qqdl"; Flags: dontcreatekey uninsdeletekey
[UninstallDelete]
Type: filesandordirs; Name: "{localappdata}\GhostDownloader"
diff --git a/scripts/deploy.py b/scripts/deploy.py
index 2dc1a78f..d733d30f 100644
--- a/scripts/deploy.py
+++ b/scripts/deploy.py
@@ -254,7 +254,7 @@ def patchInfoPlist() -> None:
plist["NSDownloadsFolderUsageDescription"] = "Ghost Downloader 需要访问下载文件夹以管理和删除下载的文件。"
plist["CFBundleURLTypes"] = [{
"CFBundleURLName": DESKTOP_ID,
- "CFBundleURLSchemes": ["ghostdownloader", "magnet", "ed2k", "ftp", "ftps"],
+ "CFBundleURLSchemes": ["ghostdownloader", "magnet", "ed2k", "ftp", "ftps", "thunder", "flashget", "qqdl"],
}]
with open(plistPath, "wb") as f:
plistlib.dump(plist, f)
diff --git a/tests/wrapped_link_pack/__init__.py b/tests/wrapped_link_pack/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/wrapped_link_pack/test_wrapped_link.py b/tests/wrapped_link_pack/test_wrapped_link.py
new file mode 100644
index 00000000..f1bf391e
--- /dev/null
+++ b/tests/wrapped_link_pack/test_wrapped_link.py
@@ -0,0 +1,74 @@
+from __future__ import annotations
+
+import base64
+
+import pytest
+
+from app.models.pack import PackServices
+from app.models.task import TaskError, TaskOptions
+from http_pack.pack import HttpPack
+from http_pack.task import HttpTask
+from tests.helpers import buildFileContent, buildRangeHandler
+from wrapped_link_pack.pack import WrappedLinkPack, toInnerUrl
+
+INNER = "https://example.com/files/a.zip?sign=1&expires=2"
+
+
+def wrap(scheme: str, text: bytes) -> str:
+ return f"{scheme}://{base64.b64encode(text).decode()}"
+
+
[email protected](params=["desktop", "android"])
+def engine(startEngine, request):
+ return startEngine(request.param)
+
+
+def features(engine):
+ services = PackServices(coroutineRunner=engine.coroutineRunner, speedMeter=engine.speedMeter)
+ for pack in (HttpPack(services), WrappedLinkPack(services)):
+ engine.featureService.register(pack)
+ return engine.featureService
+
+
+ pytest.param(wrap("thunder", f"AA{INNER}ZZ".encode()), id="thunder"),
+ pytest.param(wrap("Thunder", f"AA{INNER}ZZ".encode()).rstrip("=") + "/", id="thunder-unpadded"),
+ pytest.param(wrap("flashget", f"[FLASHGET]{INNER}[FLASHGET]".encode()) + "&abc", id="flashget"),
+ pytest.param(wrap("qqdl", INNER.encode()), id="qqdl"),
+])
+def test_to_inner_url_unwraps_link(link):
+ assert toInnerUrl(link) == INNER
+
+
+def test_to_inner_url_reads_gbk_link():
+ assert toInnerUrl(wrap("thunder", b"AA" + "https://example.com/中文.zip".encode("gbk") + b"ZZ")) == \
+ "https://example.com/中文.zip"
+
+
+ pytest.param(wrap("thunder", b"AA" + wrap("qqdl", INNER.encode()).encode() + b"ZZ"), id="nested"),
+ pytest.param("thunder://!!!", id="not-base64"),
+ pytest.param(wrap("thunder", INNER.encode()), id="missing-marker"),
+])
+def test_to_inner_url_rejects_unknown_link(link):
+ with pytest.raises(TaskError):
+ toInnerUrl(link)
+
+
+async def test_parse_wrapped_link_adds_task_of_inner_link(features, startServer):
+ url = await startServer(buildRangeHandler(buildFileContent(100)))
+
+ task = await features.parse(TaskOptions(url=wrap("thunder", f"AA{url}ZZ".encode())))
+
+ assert isinstance(task, HttpTask)
+ assert task.url == url
+
+
+async def test_probe_wrapped_link_relinks_to_inner_link(features, startServer):
+ url = await startServer(buildRangeHandler(buildFileContent(100)))
+
+ probed = await features.probe(TaskOptions(url=wrap("qqdl", url.encode())))
+
+ assert probed.url == url
+ assert probed.file.fileSize == 100