-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.js
38 lines (32 loc) · 914 Bytes
/
tracker.js
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
import { Server } from "bittorrent-tracker";
import { checkTorrentIsTracked } from "./databaseOperations";
import { ERROR, INFO } from "./utils";
const { CAN_TRACKER_PORT: PORT } = process.env;
function filterTorrents(infoHash, params, cb) {
return checkTorrentIsTracked(infoHash)
.then(isTracked => {
if (isTracked) {
cb(null);
} else {
cb(new Error("Untracked torrent file"));
}
})
.catch(err => {
DEBUG("Error responding to announce!", err);
cb(new Error("Server error"));
});
}
export function startServer() {
const server = new Server({
filter: filterTorrents,
trustProxy: false
});
server.on("error", err => {
ERROR(err);
});
server.on("listening", () => {
INFO("listening on http port:", server.http.address().port);
INFO("listening on udp port:", server.udp.address().port);
});
server.listen(PORT);
}