-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
47 lines (47 loc) · 1.84 KB
/
server.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
"use strict";
// Object.defineProperty(exports, "__esModule", { value: true });
const http_1 = require("http");
const url_1 = require("url");
const gamedig_1 = require("gamedig");
const maxAttempts = parseInt(process.env.MAX_ATTEMPTS, 10) || 1;
const socketTimeout = parseInt(process.env.SOCKET_TIMEOUT, 10) || 2000;
const attemptTimeout = parseInt(process.env.ATTEMPT_TIMEOUT, 10) || 10000;
const givenPortOnly = Boolean(process.env.GIVEN_PORT_ONLY) || false;
const listenUdpPort = parseInt(process.env.LISTEN_UDP_PORT, 10) || undefined;
const CACHE_MAX_AGE = parseInt(process.env.CACHE_MAX_AGE, 10) || 0;
const DBG = Boolean(process.env.DBG) || false;
(0, http_1.createServer)((req, res) => {
const q = (0, url_1.parse)(req.url, true).query;
if (DBG)
console.log((new Date()).toJSON() + ' %O', q);
if (q.type && q.host) {
const headers = {
'Content-Type': 'application/json',
'Cache-Control': 'max-age=' + String(CACHE_MAX_AGE)
};
(0, gamedig_1.query)({
type: String(q.type),
host: String(q.host),
port: parseInt(String(q.port), 10),
requestRules: Boolean(q.requestRules),
maxAttempts,
socketTimeout,
attemptTimeout,
givenPortOnly,
// @ts-ignore
listenUdpPort
}).then(data => {
res.writeHead(200, headers);
res.end(JSON.stringify(data, null, DBG ? 2 : null));
}).catch(err => {
if (DBG)
console.error('Error: %s', err.message);
res.writeHead(404, headers);
res.end(JSON.stringify({ error: err.message }, null, DBG ? 2 : null));
});
}
else {
res.writeHead(301, { 'Location': 'https://github.com/a-sync/gamedig.cloudno.de' });
res.end();
}
}).listen(80, '0.0.0.0');