萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

Node.js 實戰:解析並清洗 Torrent 種子文件中的敏感信息

背景與動機

BT 種子文件本質是 Bencode 編碼的元數據索引,包含 Tracker 信息與文件結構。隨著「淨網行動」推進,雲存儲、下載工具開始攔截包含敏感關鍵詞的種子。本文示範如何用 Node.js + parse-torrent 庫解析種子,清洗 name 與 files[].path 中的敏感信息,並重新生成「乾淨」種子。

種子結構速覽

{
  announce: 'https://tracker.example.com/announce',
  info: {
    name: '敏感影片標題',           // ← 需清洗
    'name.utf-8': ,
    pieceLength: 262144,
    pieces: ,
    files: [
      { path: ['敏感資料夾', '敏感文件名.mp4'], length: 123456789, ... }, // ← path 需清洗
      ...
    ]
  }
}

環境準備

npm init -y
npm install parse-torrent md5-file  # md5-file 用於生成決定性哈希名

核心清洗腳本

const fs = require('fs');
const parseTorrent = require('parse-torrent');
const md5 = require('md5-file'); // 或使用 crypto.createHash('md5')

// 讀取並解析種子
const torrentBuffer = fs.readFileSync('input.torrent');
const torrent = parseTorrent(torrentBuffer);

// 清洗邏輯
function cleanInfo(info) {
  // 1. 清洗種子名稱
  if (info.name) {
    const hash = require('crypto').createHash('md5').update(info.name).digest('hex');
    info.name = hash;
  }
  if (info['name.utf-8']) {
    const hash = require('crypto').createHash('md5').update(info['name.utf-8']).digest('hex');
    info['name.utf-8'] = Buffer.from(hash);
  }

  // 2. 清洗文件路徑
  if (info.files && Array.isArray(info.files)) {
    info.files.forEach(file => {
      ['path', 'path.utf-8'].forEach(key => {
        if (file[key] && Array.isArray(file[key])) {
          file[key] = file[key].map(p => {
            const str = Buffer.isBuffer(p) ? p.toString() : p;
            const dotIndex = str.lastIndexOf('.');
            const ext = dotIndex > -1 ? str.slice(dotIndex) : '';
            const base = dotIndex > -1 ? str.slice(0, dotIndex) : str;
            const hash = require('crypto').createHash('md5').update(base).digest('hex');
            return Buffer.from(hash + ext); // 保留副檔名
          });
        }
      });
    });
  }
  return info;
}

// 執行清洗
const cleanedInfo = cleanInfo(torrent.info);

// 重新生成種子文件
const cleanedTorrent = parseTorrent.toTorrentFile({ info: cleanedInfo });
fs.writeFileSync('cleaned_' + cleanedInfo.name + '.torrent', cleanedTorrent);

console.log('清洗完成,輸出:', 'cleaned_' + cleanedInfo.name + '.torrent');

關鍵技術點解析

1. Bencode 編碼與 parse-torrent

parse-torrent 內部處理了 Bencode 解碼/編碼,直接返回 JS 物件操作,極大降低門檻。

2. Buffer 處理

種子中 name.utf-8、path.utf-8 為 Buffer 類型,清洗時需 toString() 處理,寫回時轉回 Buffer.from()。

3. 保留副檔名的重要性

播放器、瀏覽器依賴副檔名識別媒體類型,清洗時務必保留最後一個點後的內容(如 .mp4、.mkv)。

4. 確定性哈希

使用 MD5(或 SHA1)而非隨機字串,確保同一原始名稱始終映射到同一哈希,便於去重、校驗。

實戰驗證流程

  1. 準備一個被某雲盤攔截的種子 blocked.torrent
  2. 運行腳本:node clean.js blocked.torrent
  3. 獲得 cleaned_xxx.torrent
  4. 上傳至雲盤離線下載,觀察是否通過審核

雲盤攔截截圖

清洗後成功截圖

擴展:批量清洗目錄下所有種子

const glob = require('glob');

glob('torrents/*.torrent', (err, files) => {
  files.forEach(file => {
    const buf = fs.readFileSync(file);
    const torrent = parseTorrent(buf);
    const cleaned = cleanInfo(torrent.info);
    const outBuf = parseTorrent.toTorrentFile({ info: cleaned });
    fs.writeFileSync('cleaned/' + cleaned.name + '.torrent', outBuf);
  });
});

法律與道德提示

本技術旨在保護隱私、移除惡意廣告注入、合規歸檔,不得用於傳播違法內容、規避版權保護或傳播有害信息。請遵守當地法律法規,尊重知識產權。

總結

Node.js + parse-torrent 讓種子解析與重構變得極其簡單。核心清洗邏輯聚焦於 info.name 與 info.files[].path 兩個敏感字段,通過確定性哈希保留副檔名實現「去敏而不破壞結構」。此技術同樣適用於磁力鏈接解析、種子批量重命名、PT 站種子合規檢測等場景。

copyright © 萬盛學電腦網 all rights reserved