forked from tdjsnelling/sqtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.js
More file actions
47 lines (43 loc) · 1.24 KB
/
Copy pathroutes.js
File metadata and controls
47 lines (43 loc) · 1.24 KB
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
import parseHttpRequest from "bittorrent-tracker/lib/server/parse-http";
import bencode from "bencode";
import handleAnnounce from "./announce";
const createTrackerRoute = (action, onRequest) => async (req, res) => {
if (action === "announce") {
await handleAnnounce(req, res);
if (res.writableEnded) return;
}
// bittorrent-tracker will only parse a single IP in x-forwarded-for, and will
// fail if it includes a comma-separated list
if (req.headers["x-forwarded-for"]) {
req.headers["x-forwarded-for"] =
req.headers["x-forwarded-for"].split(",")[0];
}
let params;
try {
params = parseHttpRequest(req, { action, trustProxy: true });
params.httpReq = req;
params.httpRes = res;
} catch (err) {
res.end(
bencode.encode({
"failure reason": err.message,
})
);
}
onRequest(params, (err, response) => {
let finalResponse = response;
delete finalResponse.action;
if (err) {
finalResponse = {
"failure reason": err.message,
};
} else {
if (action === "announce") {
finalResponse["interval"] = 30;
finalResponse["min interval"] = 30;
}
}
res.end(bencode.encode(finalResponse));
});
};
export default createTrackerRoute;