|
1 | 1 | import bencode from 'bencode' |
2 | 2 | import crypto from 'crypto' |
3 | | -import fetch from 'node-fetch' |
4 | 3 | import mongoose from 'mongoose' |
5 | | -import qs from 'qs' |
6 | 4 | import slugify from 'slugify' |
7 | 5 | import Torrent from '../schema/torrent' |
8 | 6 | import User from '../schema/user' |
9 | 7 | import Comment from '../schema/comment' |
10 | | -import { hexToBinary } from '../middleware/announce' |
| 8 | +import { tracker } from '../index' |
11 | 9 |
|
12 | | -export const embellishTorrentsWithTrackerScrape = async (torrents) => { |
| 10 | +export const embellishTorrentsWithTrackerScrape = async (tracker, torrents) => { |
13 | 11 | if (!torrents.length) return [] |
14 | 12 |
|
15 | 13 | try { |
16 | | - const infoHashes = torrents.map((torrent) => hexToBinary(torrent.infoHash)) |
17 | | - const query = qs.stringify( |
18 | | - { info_hash: infoHashes }, |
19 | | - { encoder: escape, indices: false } |
20 | | - ) |
21 | | - |
22 | | - const trackerRes = await fetch( |
23 | | - `${process.env.SQ_TRACKER_URL}/scrape?${query}` |
24 | | - ) |
25 | | - |
26 | | - if (!trackerRes.ok) { |
27 | | - const body = await trackerRes.text() |
28 | | - throw new Error( |
29 | | - `[DEBUG] Error performing tracker scrape: ${trackerRes.status} ${body}` |
30 | | - ) |
31 | | - } |
32 | | - |
33 | | - const bencoded = await trackerRes.arrayBuffer() |
34 | | - const scrape = bencode.decode(bencoded) |
35 | | - |
36 | 14 | return torrents.map((torrent) => { |
37 | | - const scrapeForInfoHash = |
38 | | - scrape.files[Buffer.from(hexToBinary(torrent.infoHash), 'binary')] |
| 15 | + const torrentFromTracker = tracker.torrents[torrent.infoHash] |
39 | 16 | return { |
40 | 17 | ...torrent, |
41 | | - seeders: scrapeForInfoHash?.complete || 0, |
42 | | - leechers: scrapeForInfoHash?.incomplete || 0, |
| 18 | + seeders: torrentFromTracker?.complete || 0, |
| 19 | + leechers: torrentFromTracker?.incomplete || 0, |
43 | 20 | } |
44 | 21 | }) |
45 | 22 | } catch (e) { |
@@ -169,7 +146,7 @@ export const downloadTorrent = async (req, res) => { |
169 | 146 | res.end() |
170 | 147 | } |
171 | 148 |
|
172 | | -export const fetchTorrent = async (req, res) => { |
| 149 | +export const fetchTorrent = (tracker) => async (req, res) => { |
173 | 150 | const { infoHash } = req.params |
174 | 151 |
|
175 | 152 | try { |
@@ -264,9 +241,10 @@ export const fetchTorrent = async (req, res) => { |
264 | 241 |
|
265 | 242 | if (torrent.anonymous) delete torrent.uploadedBy |
266 | 243 |
|
267 | | - const [embellishedTorrent] = await embellishTorrentsWithTrackerScrape([ |
268 | | - torrent, |
269 | | - ]) |
| 244 | + const [embellishedTorrent] = await embellishTorrentsWithTrackerScrape( |
| 245 | + tracker, |
| 246 | + [torrent] |
| 247 | + ) |
270 | 248 |
|
271 | 249 | res.json(embellishedTorrent) |
272 | 250 | } catch (e) { |
@@ -309,6 +287,7 @@ export const getTorrentsPage = async ({ |
309 | 287 | category, |
310 | 288 | tag, |
311 | 289 | userId, |
| 290 | + tracker, |
312 | 291 | }) => { |
313 | 292 | const torrents = await Torrent.aggregate([ |
314 | 293 | { |
@@ -442,31 +421,32 @@ export const getTorrentsPage = async ({ |
442 | 421 | ]) |
443 | 422 |
|
444 | 423 | return { |
445 | | - torrents: await embellishTorrentsWithTrackerScrape(torrents), |
| 424 | + torrents: await embellishTorrentsWithTrackerScrape(tracker, torrents), |
446 | 425 | ...count, |
447 | 426 | } |
448 | 427 | } |
449 | 428 |
|
450 | | -export const listLatest = async (req, res) => { |
| 429 | +export const listLatest = (tracker) => async (req, res) => { |
451 | 430 | let { count } = req.query |
452 | 431 | count = parseInt(count) || 25 |
453 | 432 | count = Math.min(count, 100) |
454 | 433 | try { |
455 | | - const { torrents } = await getTorrentsPage({ limit: count }) |
| 434 | + const { torrents } = await getTorrentsPage({ limit: count, tracker }) |
456 | 435 | res.json(torrents) |
457 | 436 | } catch (e) { |
458 | 437 | res.status(500).send(e.message) |
459 | 438 | } |
460 | 439 | } |
461 | 440 |
|
462 | | -export const searchTorrents = async (req, res) => { |
| 441 | +export const searchTorrents = (tracker) => async (req, res) => { |
463 | 442 | const { query, category, tag, page } = req.query |
464 | 443 | try { |
465 | 444 | const torrents = await getTorrentsPage({ |
466 | 445 | skip: page ? parseInt(page) : 0, |
467 | 446 | query: decodeURIComponent(query), |
468 | 447 | category, |
469 | 448 | tag, |
| 449 | + tracker, |
470 | 450 | }) |
471 | 451 | res.json(torrents) |
472 | 452 | } catch (e) { |
|
0 commit comments