forked from Unyxos/srcds_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (52 loc) · 1.92 KB
/
index.js
File metadata and controls
59 lines (52 loc) · 1.92 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
import express from 'express';
import module from './modules/index.js';
import SourceRcon from '@hellz.fr/sourcecon';
const app = express();
app.get('/', (req, res) => {
res.send('use /metrics?ip=<srcds ip>&port=<srcds port>&rconPassword=<rcon password>&game=<game>&sm=<boolean>&meta=<boolean>sp=<boolean> to get data');
});
app.get('/metrics', async (req, res) => {
const config = {
ip: req.query.ip,
port: req.query.port,
game: req.query.game,
rconPassword: req.query.rconPassword,
};
config.metamod = req.query.metamod === 'true' || false;
config.sourcemod = req.query.sourcemod === 'true' || false;
config.sourcepython = req.query.sourcepython === 'true' || false;
config.tags = req.query.tags || [];
if (
config.ip == null
|| config.port == null
|| config.rconPassword == null
|| config.game == null
) {
res.send('Missing parameter');
return;
}
if (!['csgo', 'cstrike', 'gmod', 'cs2'].includes(config.game)) {
res.send('Incorrect game value, currently supported games are : csgo, gmod, cstrike');
return;
}
try {
const client = new SourceRcon.default(config.ip, Number.parseInt(config.port));
console.log(`[${config.game} ${config.ip}:${config.port}] connecting`);
await client.connect();
console.log(`[${config.game} ${config.ip}:${config.port}] connected`);
await client.auth(config.rconPassword);
console.log(`[${config.game} ${config.ip}:${config.port}] auth`);
const response = await module.request(config, client);
console.log(`[${config.game} ${config.ip}:${config.port}] request`);
await module.send(config, response, res);
console.log(`[${config.game} ${config.ip}:${config.port}] send`);
} catch (e) {
if (e.code === 'EHOSTUNREACH') {
console.error(`Unreachable host : ${e.address}:${e.port}`);
} else {
console.error(e);
}
await module.send(config, null, res, true);
}
});
app.listen(9591);