This repository has been archived by the owner on Aug 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
405 lines (331 loc) · 12.9 KB
/
index.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
const optionDefinitions = [
{ name: 'config', alias: 'c', type: String, defaultValue: './config' } // base file path directory
];
const args = require('command-line-args')(optionDefinitions);
const config = require(args.config);
const vdf = require('simple-vdf');
const rateLimit = require("express-rate-limit");
const fs = require('fs');
const express = require('express');
const fetch = require('node-fetch');
const { Pool } = require('pg');
const ItemParser = require('./item_parser');
const Counter = require('./counter');
const ProfileFetcher = require('./profile_fetcher');
const rp = require('request-promise');
const app = express();
const pool = new Pool({
connectionString: config.connectionString,
});
const profileFetcher = new ProfileFetcher(pool, config.steam_api_keys, config.steam_cache_expiring_ms);
let itemParser;
const itemUrl = `https://raw.githubusercontent.com/SteamDatabase/GameTracking-CSGO/master/csgo/scripts/items/items_game.txt`;
const englishUrl = `https://raw.githubusercontent.com/SteamDatabase/GameTracking-CSGO/master/csgo/resource/csgo_english.txt`;
async function updateItems() {
try {
const resp = await fetch(itemUrl);
const data = await resp.text();
fs.writeFile('items_game.txt', data, () => {
console.log('Saved items_game.txt');
});
const langResp = await fetch(englishUrl);
const langData = await langResp.text();
fs.writeFile('csgo_english.txt', langData, () => {
console.log('Saved csgo_english.txt');
});
itemParser = new ItemParser(vdf.parse(data).items_game, vdf.parse(langData).lang.Tokens);
} catch (e) {
console.error(e);
}
}
if (fs.existsSync('items_game.txt') && fs.existsSync('items_game.txt')) {
const itemsGame = fs.readFileSync('items_game.txt', 'utf8');
itemParser = new ItemParser(vdf.parse(itemsGame)['items_game']);
const english = fs.readFileSync('csgo_english.txt', 'utf8');
itemParser = new ItemParser(vdf.parse(itemsGame).items_game, vdf.parse(english).lang.Tokens);
} else {
updateItems();
}
setInterval(() => updateItems(), config.file_update_interval);
if (config.trust_proxy) {
app.enable('trust proxy');
}
config.allowed_regex_origins = config.allowed_regex_origins || [];
config.allowed_origins = config.allowed_origins || [];
const allowedRegexOrigins = config.allowed_regex_origins.map((origin) => new RegExp(origin));
function EnsureOrigin(req, res, next) {
// Origin whitelist
if ((config.allowed_origins.length > 0 || config.allowed_regex_origins.length > 0)) {
// check to see if its a valid domain
const allowed = config.allowed_origins.indexOf(req.get('origin')) > -1 ||
allowedRegexOrigins.findIndex((reg) => reg.test(req.get('origin'))) > -1;
if (allowed) {
res.header('Access-Control-Allow-Origin', req.get('origin'));
res.header('Access-Control-Allow-Methods', 'GET');
next();
} else {
res.status(400).json({error: 'Invalid request'});
}
} else {
res.header('Access-Control-Allow-Origin', req.get('origin'));
res.header('Access-Control-Allow-Methods', 'GET');
next();
}
}
app.use(EnsureOrigin);
app.get('/items', (req, res) => {
if (itemParser) {
res.json(itemParser.getFullResponse());
} else {
res.status(500).json({error: 'Item response is not initialized, new csgo update?'});
}
});
const counter = new Counter(pool);
app.get('/count', (req, res) => {
const countData = counter.get();
res.setHeader("Cache-Control", `public, s-maxage=${Math.ceil(counter.counterUpdateInterval/1000-
(Date.now()/1000-countData.lastUpdate))}`);
res.json(countData);
});
function isInt(i) {
return !isNaN(parseInt(i));
}
/*
Possible URL Query Params
defIndex: Weapon index
paintIndex: Paint index
order: 1 for asc, -1 for desc
stattrak: true/false
souvenir: true/false
paintseed: 0-999
rarity: 1-7
min: 0-1
max: 0-1
stickers: [{i: stickerId, s: slot number}]
start: 0-5000
*/
function buildQuery(params) {
const conditions = [], values = [];
let conditionIndex = 0;
if (params.defIndex && isInt(params.defIndex)) {
conditions.push(`defindex = $${++conditionIndex}`);
values.push(params.defIndex);
}
if (params.paintIndex && isInt(params.paintIndex)) {
conditions.push(`paintindex = $${++conditionIndex}`);
values.push(params.paintIndex);
}
if (params.stattrak) {
conditions.push(`stattrak = $${++conditionIndex}`);
values.push(params.stattrak === 'true');
}
if (params.souvenir) {
conditions.push(`souvenir = $${++conditionIndex}`);
values.push(params.souvenir === 'true');
}
if (params.paintSeed && isInt(params.paintSeed)) {
conditions.push(`paintseed = $${++conditionIndex}`);
values.push(params.paintSeed);
}
if (params.min) {
const min = parseFloat(params.min);
if (min >= 0.0 && min <= 1.0) {
const buf = Buffer.alloc(4);
buf.writeFloatBE(min, 0);
const intMin = buf.readInt32BE(0);
conditions.push(`paintwear >= $${++conditionIndex}`);
values.push(intMin);
}
}
if (params.max) {
const max = parseFloat(params.max);
if (max >= 0.0 && max <= 1.0) {
const buf = Buffer.alloc(4);
buf.writeFloatBE(max, 0);
const intMax = buf.readInt32BE(0);
conditions.push(`paintwear <= $${++conditionIndex}`);
values.push(intMax);
}
}
if (params.rarity && isInt(params.rarity)) {
conditions.push(`rarity = $${++conditionIndex}`);
values.push(params.rarity);
}
if (params.stickers) {
try {
const stickers = [];
const uniqueIds = new Set();
const inputStickers = JSON.parse(params.stickers);
for (const s of inputStickers) {
if (!s.i) continue;
const sticker = {
i: parseInt(s.i)
};
uniqueIds.add(s.i);
if (s.s !== undefined) {
sticker.s = parseInt(s.s);
}
stickers.push(sticker);
}
// This seems to force postgres to use the i_stickers index if > 1 stickers, otherwise it sometimes uses
// the i_paintwear index and filters rows which is substantially slower if we put it all in one array
for (const s of stickers) {
conditions.push(`stickers @> $${++conditionIndex}`);
values.push(JSON.stringify([s]));
}
// Yes, I know this seems strange, why add the same value twice if there's only one sticker?
// This seems to get postgres' query planner to use the i_stickers index instead of i_paintwear
// when there's only one sticker, which is way faster
if (stickers.length === 1) {
conditions.push(`stickers @> $${++conditionIndex}`);
values.push(JSON.stringify([stickers[0]]));
}
let totalDuplicates = 0;
// Add duplicate property (allows us to use the index to search sticker dupes)
for (const sticker of stickers) {
const matching = stickers.filter((s) => s.i === sticker.i);
if (matching.length > 1 && !matching.find((s) => s.d > 1)) {
sticker.d = matching.length;
totalDuplicates += sticker.d;
}
}
// Patch to ensure that if a user wants to search 2 of a same sticker, we'd also include guns with 2 or more
// of the same one
// Unfortunately the DB is designed to only store the highest amount of one sticker
for (const sticker of stickers) {
if (inputStickers.length === 1) continue;
if (!sticker.d) continue;
const possibleExtra = 5 - uniqueIds.size + 1 - sticker.d;
const conds = [];
// Amount of possible stickers
for (let i = 0; i < possibleExtra+1; i++) {
conds.push(`stickers @> $${++conditionIndex}`);
values.push(JSON.stringify([{i: sticker.i, d: sticker.d+i}]));
}
conditions.push(`(${conds.join(' OR ')})`);
}
} catch (e) {
console.error(e);
}
}
const start = (params.start && parseInt(params.start) < 5000 && parseInt(params.start) >= 0) ?
parseInt(params.start) : 0;
const statement = `SELECT * FROM items ${conditions.length > 0 ? 'WHERE' : ''} ${conditions.join(' AND ')}
ORDER BY paintwear ${params.order === '-1' ? 'DESC' : ''} LIMIT ${config.max_query_items || 200}
OFFSET ${start}`;
return {
text: statement,
values
}
}
/*
Converts the given unsigned 64 bit integer into a signed 64 bit integer
*/
function unsigned64ToSigned(num) {
const mask = 1n << 63n;
return (BigInt(num)^mask) - mask;
}
/*
Converts the given signed 64 bit integer into an unsigned 64 bit integer
*/
function signed64ToUnsigned(num) {
const mask = 1n << 63n;
return (BigInt(num)+mask) ^ mask;
}
function isSteamId64(id) {
id = BigInt(id);
const universe = id >> 56n;
if (universe > 5n) return false;
const instance = (id >> 32n) & (1n << 20n)-1n;
// There are currently no documented instances above 4, but this is for good measure
return instance <= 32n;
}
const searchLimiter = rateLimit({
windowMs: config.search_rate_window || 2 * 60 * 60 * 1000, // 2 hours
max: config.search_rate_limit || 120,
headers: false,
handler: function (req, res) {
res.status(429).json({error: `Rate limit exceeded, please try again later`});
}
});
async function captchaVerifier(req, res, next) {
if (!config.recaptcha || !config.recaptcha.enable) {
next();
return;
}
if (!req.query.token) {
res.status(400).json({error: 'Failed to include token'});
return;
}
try {
const response = await rp({
url: `https://www.google.com/recaptcha/api/siteverify?secret=${config.recaptcha.secret}&response=${req.query.token}&remoteip=${req.ip}`,
json: true
});
if (response.success === false || response.action !== config.recaptcha.action ||
response.score < config.recaptcha.minScore) {
res.status(400).json({error: `Failed to verify recaptcha, please try again or remove ad blockers`});
} else {
next();
}
} catch (e) {
console.error(`Failed to verify recaptcha: ${e.toString()}`);
res.status(400).json({error: `Failed to verify recaptcha, please try again or remove ad blockers`});
}
}
app.get('/search', [searchLimiter, captchaVerifier], async (req, res) => {
const query = buildQuery(req.query);
try {
const results = await pool.query(query);
let rows = results.rows.map((row) => {
const buf = Buffer.alloc(4);
buf.writeInt32BE(row.paintwear, 0);
const floatvalue = buf.readFloatBE(0);
const a = signed64ToUnsigned(row.a).toString();
const d = signed64ToUnsigned(row.d).toString();
const ms = signed64ToUnsigned(row.ms).toString();
let m = '0', s = '0';
if (isSteamId64(ms)){
s = ms;
} else {
m = ms;
}
return {
s,
a,
d,
m,
floatvalue,
props: row.props,
souvenir: row.souvenir,
stattrak: row.stattrak,
stickers: row.stickers,
updated: row.updated,
paintseed: row.paintseed,
defIndex: row.defindex,
paintIndex: row.paintindex,
}
});
if (profileFetcher.canFetch()) {
const steamIds = [...new Set(rows.filter(e => e.s != '0').map(e => e.s))];
const profiles = await profileFetcher.getProfilesForSteamIds(steamIds);
rows = rows.map(row => {
const profile = profiles[row.s];
if (profile) {
row.avatar = profile.avatar;
row.personaState = profile.personastate;
// Patch to pass state if they are in-game
if (profile.gameextrainfo) {
row.personaState = 100;
}
}
return row;
});
}
res.json(rows);
} catch (e) {
console.error(e);
res.status(400).json({error: 'Something went wrong'});
}
});
app.listen(config.port, () => console.log(`Listening on Port ${config.port}`));