forked from tdjsnelling/sqtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannounce.js
More file actions
188 lines (160 loc) · 5.35 KB
/
Copy pathannounce.js
File metadata and controls
188 lines (160 loc) · 5.35 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import qs from "qs";
import bencode from "bencode";
import User from "../schema/user";
import Torrent from "../schema/torrent";
import Progress from "../schema/progress";
import { getUserRatio } from "../utils/ratio";
import { getUserHitNRuns } from "../utils/hitnrun";
export const BYTES_GB = 1e9;
export const binaryToHex = (b) => Buffer.from(b, "binary").toString("hex");
export const hexToBinary = (h) => Buffer.from(h, "hex").toString("binary");
const handleAnnounce = async (req, res) => {
const userId = req.originalUrl.split("/")[2];
req.userId = userId;
console.log(`[DEBUG] userId: ${userId}`);
const user = await User.findOne({ uid: userId }).lean();
// if the uid does not match a registered user, deny announce
if (!user) {
const response = bencode.encode({
"failure reason": "Announce denied: you are not registered.",
});
res.send(response);
return;
}
// if the users email is not verified, deny announce
if (!user.emailVerified) {
const response = bencode.encode({
"failure reason": "Announce denied: email address must be verified.",
});
res.send(response);
return;
}
const q = req.url.split("?")[1];
const params = qs.parse(q, { decoder: unescape });
const infoHash = binaryToHex(params.info_hash);
console.log(`[DEBUG] query: ${JSON.stringify(params)}`);
console.log(`[DEBUG] infoHash: ${infoHash}`);
const torrent = await Torrent.findOne({ infoHash }).lean();
// if torrent info hash is not in the database, deny announce
if (!torrent) {
const response = bencode.encode({
"failure reason":
"Announce denied: cannot announce a torrent that has not been uploaded.",
});
res.send(response);
return;
}
const { ratio } = await getUserRatio(user._id);
const hitnruns = await getUserHitNRuns(user._id);
console.log(`[DEBUG] user ratio: ${ratio}`);
console.log(`[DEBUG] user hit'n'runs: ${hitnruns}`);
// if users ratio is below the minimum threshold, and they are trying to download, deny announce
if (
Number(process.env.SQ_MINIMUM_RATIO) !== -1 &&
ratio < Number(process.env.SQ_MINIMUM_RATIO) &&
ratio !== -1 &&
Number(params.left > 0)
) {
const response = bencode.encode({
"failure reason": `Announce denied: Ratio is below minimum threshold ${process.env.SQ_MINIMUM_RATIO}.`,
peers: [],
peers6: [],
});
res.send(response);
return;
}
// if user has committed more than the allowed number of hit'n'runs, and they are trying to download, deny announce
if (
Number(process.env.SQ_MAXIMUM_HIT_N_RUNS) !== -1 &&
hitnruns >= Number(process.env.SQ_MAXIMUM_HIT_N_RUNS) &&
Number(params.left > 0)
) {
const response = bencode.encode({
"failure reason": `Announce denied: You have committed ${process.env.SQ_MAXIMUM_HIT_N_RUNS} or more hit'n'runs.`,
peers: [],
peers6: [],
});
res.send(response);
return;
}
const uploaded = Number(params.uploaded);
const downloaded = params.event === "started" ? 0 : Number(params.downloaded);
const prevProgressRecord = await Progress.findOne({
userId: user._id,
infoHash,
}).lean();
const alreadyUploadedSession = prevProgressRecord?.uploaded?.session ?? 0;
const uploadDeltaSession =
uploaded >= alreadyUploadedSession ? uploaded - alreadyUploadedSession : 0;
const alreadyDownloadedSession = prevProgressRecord?.downloaded?.session ?? 0;
const downloadDeltaSession =
downloaded >= alreadyDownloadedSession
? downloaded - alreadyDownloadedSession
: 0;
const [sumUploaded] = await Progress.aggregate([
{
$match: {
userId: user._id,
},
},
{
$group: {
_id: "uploaded",
bytes: { $sum: "$uploaded.total" },
},
},
]);
const { bytes } = sumUploaded ?? { bytes: 0 };
const nextGb = Math.max(Math.ceil((bytes + 1) / BYTES_GB), 1);
const currentGb = nextGb - 1;
const gbAfterUpload = Math.floor((bytes + uploadDeltaSession) / BYTES_GB);
if (gbAfterUpload >= nextGb) {
const deltaGb = gbAfterUpload - currentGb;
await User.findOneAndUpdate(
{ _id: user._id },
{ $inc: { bonusPoints: deltaGb * process.env.SQ_BP_EARNED_PER_GB } }
);
}
await Progress.findOneAndUpdate(
{ userId: user._id, infoHash },
{
$set: {
userId: user._id,
infoHash,
uploaded: {
session: uploaded,
total:
(prevProgressRecord?.uploaded?.total ?? 0) + uploadDeltaSession,
},
left: Number(params.left),
},
},
{ upsert: true }
);
await Progress.findOneAndUpdate(
{ userId: user._id, infoHash },
{
$set: {
userId: user._id,
infoHash,
downloaded: {
session:
torrent.freeleech || process.env.SQ_SITE_WIDE_FREELEECH === true
? prevProgressRecord?.downloaded?.session ?? 0
: downloaded,
total:
torrent.freeleech || process.env.SQ_SITE_WIDE_FREELEECH === true
? prevProgressRecord?.downloaded?.total ?? 0
: (prevProgressRecord?.downloaded?.total ?? 0) +
downloadDeltaSession,
},
left: Number(params.left),
},
},
{ upsert: true }
);
if (params.event === "completed") {
await Torrent.findOneAndUpdate({ infoHash }, { $inc: { downloads: 1 } });
}
};
export default handleAnnounce;