forked from csfloat/inspect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (170 loc) · 6.66 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
const fs = require('fs'),
queue = new (require('./lib/queue'))(),
CONFIG = require('./config'),
utils = require('./lib/utils'),
InspectURL = require('./lib/inspect_url'),
botController = new (require('./lib/bot_controller'))(),
resHandler = require('./lib/res_handler'),
DB = new (require('./lib/db'))(CONFIG.database_url),
gameData = new (require('./lib/game_data'))(CONFIG.game_files_update_interval, CONFIG.enable_game_file_updates);
const errorMsgs = {
1: 'Improper Parameter Structure',
2: 'Invalid Inspect Link Structure',
3: 'You may only have one pending request at a time',
4: 'Valve\'s servers didn\'t reply in time',
5: 'Valve\'s servers appear to be offline, please try again later',
6: 'Something went wrong on our end, please try again'
};
if (CONFIG.logins.length == 0) {
console.log('There are no bot logins. Please add some in config.json');
process.exit(1);
}
// If the sentry folder doesn't exist, create it
if (!utils.isValidDir('sentry')) {
console.log('Creating sentry directory');
fs.mkdirSync('sentry');
}
for (let loginData of CONFIG.logins) {
botController.addBot(loginData, CONFIG.bot_settings);
}
const lookupHandler = function (params) {
// Check if the item is already in the DB
DB.getItemData(params)
.then((doc) => {
// If we got the result, just return it
if (doc) {
gameData.addAdditionalItemProperties(doc);
resHandler.respondFloatToUser(params, {'iteminfo': doc});
return;
}
// Check if there is a bot online to process this request
if (!botController.hasBotOnline()) {
resHandler.respondErrorToUser(params, {error: errorMsgs[5], code: 5}, 503);
return;
}
// If the flag is set, check if the user already has a request in the queue
if (!CONFIG.allow_simultaneous_requests && queue.isUserInQueue(params.ip)) {
resHandler.respondErrorToUser(params, {error: errorMsgs[3], code: 3}, 400);
return;
}
queue.addJob(params, CONFIG.bot_settings.max_attempts);
if (params.type === 'ws') {
resHandler.respondInfoToUser(params, {'msg': `Your request for ${params.a} is in the queue`});
}
})
.catch((err) => {
console.log(`getItemData Promise rejected: ${err.message}`);
resHandler.respondErrorToUser(params, {error: errorMsgs[6], code: 6}, 500);
});
};
// Setup and configure express
let app = require('express')();
app.get('/', function(req, res) {
// Allow some origins
if (CONFIG.allowed_origins.length > 0 && req.get('origin') != undefined) {
// check to see if its a valid domain
if (CONFIG.allowed_origins.indexOf(req.get('origin')) !== -1) {
res.header('Access-Control-Allow-Origin', req.get('origin'));
res.header('Access-Control-Allow-Methods', 'GET');
}
}
// Get and parse parameters
let thisLink;
if ('url' in req.query) thisLink = new InspectURL(req.query.url);
else if ('a' in req.query && 'd' in req.query && ('s' in req.query || 'm' in req.query)) thisLink = new InspectURL(req.query);
// Make sure the params are valid
if (!thisLink || !thisLink.getParams()) {
res.status(400).json({error: errorMsgs[2], code: 2});
return;
}
// Look it up
let params = thisLink.getParams();
params.ip = req.connection.remoteAddress;
params.type = 'http';
params.res = res;
lookupHandler(params);
});
let http_server = require('http').Server(app);
let https_server;
if (CONFIG.https.enable) {
const credentials = {
key: fs.readFileSync(CONFIG.https.key_path, 'utf8'),
cert: fs.readFileSync(CONFIG.https.cert_path, 'utf8'),
ca: fs.readFileSync(CONFIG.https.ca_path, 'utf8')
};
https_server = require('https').Server(credentials, app);
}
if (CONFIG.http.enable) {
http_server.listen(CONFIG.http.port);
console.log('Listening for HTTP on port: ' + CONFIG.http.port);
}
if (CONFIG.https.enable) {
https_server.listen(CONFIG.https.port);
console.log('Listening for HTTPS on port: ' + CONFIG.https.port);
}
if (CONFIG.socketio.enable) {
let io;
if (https_server) {
io = require('socket.io')(https_server);
console.log('Listening for HTTPS websocket connections on port: ' + CONFIG.https.port);
}
else {
// Fallback onto HTTP for socket.io
io = require('socket.io')(http_server);
console.log('Listening for HTTP websocket connections on port: ' + CONFIG.http.port);
}
if (CONFIG.socketio.origins) {
io.set('origins', CONFIG.socketio.origins);
}
io.on('connection', function(socket) {
socket.emit('joined');
if (botController.hasBotOnline() === false) {
socket.emit('errormessage', {error: errorMsgs[5], code: 5});
}
socket.on('lookup', function(link) {
link = new InspectURL(link);
let params = link.getParams();
if (link && params) {
params.ip = socket.request.connection.remoteAddress;
params.type = 'ws';
params.res = socket;
lookupHandler(params);
}
else {
socket.emit('errormessage', {error: errorMsgs[2], code: 2});
}
});
});
botController.on('ready', () => {
console.log('Telling WS Users that Valve is online');
io.emit('successmessage', {'msg': 'Valve\'s servers are online!'});
});
botController.on('unready', () => {
console.log('Telling WS Users that Valve is offline');
io.emit('errormessage', {error: errorMsgs[5], code: 5});
});
}
queue.process(CONFIG.logins.length, (job) => {
return new Promise((resolve, reject) => {
botController.lookupFloat(job.data)
.then((itemData) => {
console.log(`Received itemData for ${job.data.a}`);
// Save and remove the delay attribute
let delay = itemData.delay;
delete itemData.delay;
// add the item info to the DB
DB.insertItemData(itemData.iteminfo);
gameData.addAdditionalItemProperties(itemData.iteminfo);
resHandler.respondFloatToUser(job.data, itemData);
resolve(delay);
})
.catch(() => {
console.log(`Request Timeout for ${job.data.a}`);
reject();
});
});
});
queue.on('job failed', (job) => {
console.log(`Job Failed! S: ${job.data.s} A: ${job.data.a} D: ${job.data.d} M: ${job.data.m} IP: ${job.data.ip}`);
resHandler.respondErrorToUser(job.data, {error: errorMsgs[4], code: 4}, 500);
});