forked from Unyxos/srcds_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (36 loc) · 1014 Bytes
/
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
import express from 'express';
import { connect } from 'working-rcon';
import module from './modules/index.js';
const app = express();
app.get('/', (req, res) => {
res.send('use /metrics?ip=<srcds ip>&port=<srcds port>&rconPassword=<rcon password> to get data');
});
app.get('/metrics', async (req, res) => {
const config = {
ip: req.query.ip,
port: req.query.port,
rconPassword: req.query.rconPassword,
};
if (
config.ip == null
|| config.port == null
|| config.rconPassword == null
) {
res.send('Missing parameter');
return;
}
try {
const client = await connect(config.ip, config.port, config.rconPassword, 5000);
const response = await module.request(config, client);
await module.send(config, response, res);
await client.disconnect();
} 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);