-
Notifications
You must be signed in to change notification settings - Fork 65
/
torrent.ts
164 lines (150 loc) · 4.48 KB
/
torrent.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import fs, { promises as fsPromises } from "fs";
import parseTorrent, { Metafile } from "parse-torrent";
import path from "path";
import { concat } from "simple-get";
import { inspect } from "util";
import db from "./db.js";
import { CrossSeedError } from "./errors.js";
import { logger } from "./logger.js";
import { getRuntimeConfig, NonceOptions } from "./runtimeConfig.js";
import { createSearcheeFromTorrentFile, Searchee } from "./searchee.js";
import { ok, stripExtension } from "./utils.js";
export interface TorrentLocator {
infoHash?: string;
name?: string;
}
export async function parseTorrentFromFilename(
filename: string
): Promise<Metafile> {
const data = await fsPromises.readFile(filename);
return parseTorrent(data);
}
export async function parseTorrentFromURL(url: string): Promise<Metafile> {
let response;
try {
response = await new Promise((resolve, reject) => {
concat({ url, followRedirects: false }, (err, res, data) => {
if (err) return reject(err);
res.data = data;
return resolve(res);
});
});
} catch (e) {
logger.error(`failed to access ${url}`);
logger.debug(e);
return null;
}
if (response.statusCode < 200 || response.statusCode >= 300) {
if (
response.statusCode >= 300 &&
response.statusCode < 400 &&
response.headers.location?.startsWith("magnet:")
) {
logger.error(`Unsupported: magnet link detected at ${url}`);
return null;
} else {
logger.error(
`error downloading torrent at ${url}: ${response.statusCode} ${response.statusMessage}`
);
logger.debug("response: %s", response.data);
logger.debug("headers: %s", response.headers);
return null;
}
}
try {
return parseTorrent(response.data);
} catch (e) {
logger.error(`invalid torrent contents at ${url}`);
logger.debug(e);
return null;
}
}
export function saveTorrentFile(
tracker: string,
tag = "",
info: Metafile,
nonceOptions: NonceOptions
): void {
const { outputDir: runtimeConfigOutputDir } = getRuntimeConfig();
const { outputDir = runtimeConfigOutputDir } = nonceOptions;
const buf = parseTorrent.toTorrentFile(info);
const name = stripExtension(info.name);
const filename = `[${tag}][${tracker}]${name}.torrent`;
fs.writeFileSync(path.join(outputDir, filename), buf, { mode: 0o644 });
}
export async function findAllTorrentFilesInDir(
torrentDir: string
): Promise<string[]> {
return (await fsPromises.readdir(torrentDir))
.filter((fn) => path.extname(fn) === ".torrent")
.sort()
.map((fn) => path.resolve(path.join(torrentDir, fn)));
}
export async function indexNewTorrents(): Promise<void> {
const { torrentDir } = getRuntimeConfig();
const dirContents = await findAllTorrentFilesInDir(torrentDir);
// index new torrents in the torrentDir
for (const filepath of dirContents) {
const doesAlreadyExist = db.data.indexedTorrents.find(
(e) => e.filepath === filepath
);
if (!doesAlreadyExist) {
let meta;
try {
meta = await parseTorrentFromFilename(filepath);
} catch (e) {
logger.error(`Failed to parse ${filepath}`);
logger.debug(e);
continue;
}
db.data.indexedTorrents.push({
filepath,
infoHash: meta.infoHash,
name: meta.name,
});
}
}
// clean up torrents that no longer exist in the torrentDir
db.data.indexedTorrents = db.data.indexedTorrents.filter((e) =>
dirContents.includes(e.filepath)
);
db.write();
}
export function getInfoHashesToExclude(): string[] {
return db.data.indexedTorrents.map((t) => t.infoHash);
}
export async function validateTorrentDir(): Promise<void> {
const { torrentDir } = getRuntimeConfig();
try {
await fsPromises.readdir(torrentDir);
} catch (e) {
throw new CrossSeedError(`Torrent dir ${torrentDir} is invalid`);
}
}
export async function loadTorrentDirLight(): Promise<Searchee[]> {
const { torrentDir } = getRuntimeConfig();
return Promise.all(
fs
.readdirSync(torrentDir)
.filter((fn) => path.extname(fn) === ".torrent")
.sort()
.map(createSearcheeFromTorrentFile)
).then((searcheeResults) => searcheeResults.filter(ok));
}
export async function getTorrentByCriteria(
criteria: TorrentLocator
): Promise<Metafile> {
await indexNewTorrents();
const findResult = db.data.indexedTorrents.find(
(e) =>
(!criteria.infoHash || criteria.infoHash === e.infoHash) &&
(!criteria.name || criteria.name === e.name)
);
if (findResult === undefined) {
const message = `could not find a torrent with the criteria ${inspect(
criteria
)}`;
throw new Error(message);
}
return parseTorrentFromFilename(findResult.filepath);
}