The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
feat(bt): 加速 magnet 元数据获取
XiaoYouChR060eb532b788
diff --git a/features/bittorrent_pack/pack.py b/features/bittorrent_pack/pack.pyindex 2c923a7f..a7b0db05 100644--- a/features/bittorrent_pack/pack.py+++ b/features/bittorrent_pack/pack.py@@ -124,7 +124,7 @@ class BitTorrentPack(FeaturePack):return [UriScheme("magnet", "Magnet")]async def activate(self):- pass+ btSession.open()async def deactivate(self):await btSession.close()diff --git a/features/bittorrent_pack/session.py b/features/bittorrent_pack/session.pyindex f05d8b38..48f4f096 100644--- a/features/bittorrent_pack/session.py+++ b/features/bittorrent_pack/session.py@@ -31,6 +31,20 @@ _STATE_TEXT = {"queued_for_checking": "等待校验",}+DHT_BOOTSTRAP_NODES = (+ "router.bittorrent.com:6881, router.utorrent.com:6881, "+ "dht.transmissionbt.com:6881, dht.libtorrent.org:25401"+)++ALERT_MASK = (+ lt.alert.category_t.error_notification+ | lt.alert.category_t.port_mapping_notification+ | lt.alert.category_t.storage_notification+ | lt.alert.category_t.status_notification+)++DHT_STATE_FILE = "bt_dht_state.dat"+_ERROR_ALERTS = (lt.file_error_alert,lt.metadata_failed_alert,@@ -110,9 +124,12 @@ class BTSession(QObject):# ── public interface ──+ def open(self) -> None:+ self._ensureSession()+async def run(self, taskId: str, params: TorrentParams,onProgress: Callable[[TorrentProgress], None] | None = None) -> bytes:- self._open()+ self._ensureSession()handle = self._addTorrent(params)handle.resume()@@ -148,7 +165,7 @@ class BTSession(QObject):return self._resumeCache.pop(taskId, b"")async def fetchMetadata(self, magnetUri: str, trackers: list[str]) -> bytes:- self._open()+ self._ensureSession()params = lt.parse_magnet_uri(magnetUri)params.trackers = list(dict.fromkeys(@@ -196,6 +213,7 @@ class BTSession(QObject):return self._resumeCache.pop(taskId, None)async def close(self) -> None:+ self._saveDhtState()waiters = []for entry in list(self._active.values()):try:@@ -239,10 +257,11 @@ class BTSession(QObject):# ── session lifecycle ──- def _open(self) -> None:+ def _ensureSession(self) -> None:if self._session is None:from app.config.constants import VERSION- self._session = lt.session({+ params = self._loadDhtState()+ params.settings = {"user_agent": f"GhostDownloader/{VERSION} libtorrent/{lt.__version__}","listen_interfaces": f"0.0.0.0:{bittorrentConfig.listenPort.value}","connections_limit": bittorrentConfig.maxConnections.value,@@ -250,14 +269,44 @@ class BTSession(QObject):"upload_rate_limit": int(bittorrentConfig.maxUploadSpeed.value),"enable_dht": bittorrentConfig.enableDht.value,"enable_lsd": bittorrentConfig.enableLsd.value,+ "dht_bootstrap_nodes": DHT_BOOTSTRAP_NODES,"announce_to_all_trackers": True,"announce_to_all_tiers": True,- "alert_mask": lt.alert.category_t.all_categories,+ "enable_upnp": True,+ "enable_natpmp": True,+ "alert_mask": ALERT_MASK,**self._proxySettings(),- })+ }+ self._session = lt.session(params)if self._poller is None or self._poller.done():self._poller = asyncio.get_running_loop().create_task(self._supervise())+ def _loadDhtState(self) -> lt.session_params:+ from app.config.paths import APP_DATA_DIR+ path = Path(APP_DATA_DIR) / DHT_STATE_FILE+ if path.exists():+ try:+ return lt.read_session_params(+ path.read_bytes(), lt.save_state_flags_t.save_dht_state+ )+ except Exception as e:+ logger.warning("加载 DHT 状态失败: {}", e)+ return lt.session_params()++ def _saveDhtState(self) -> None:+ if self._session is None:+ return+ from app.config.paths import APP_DATA_DIR+ try:+ state = self._session.save_state(+ lt.save_state_flags_t.save_dht_state+ )+ (Path(APP_DATA_DIR) / DHT_STATE_FILE).write_bytes(+ lt.bencode(state)+ )+ except Exception as e:+ logger.warning("保存 DHT 状态失败: {}", e)+# ── torrent management ──def _addTorrent(self, params: TorrentParams) -> lt.torrent_handle: