2019-07-04 12:21:37 +02:00
|
|
|
const fs = require('fs');
|
2019-09-03 08:30:47 +02:00
|
|
|
const express = require('express');
|
2019-09-19 12:28:14 +02:00
|
|
|
const bodyParser = require('body-parser');
|
2019-07-04 12:21:37 +02:00
|
|
|
|
|
|
|
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
|
|
|
|
|
2019-08-29 11:35:53 +02:00
|
|
|
const pg = require('postgresupdater')(
|
|
|
|
config.database.host,
|
|
|
|
config.database.port,
|
|
|
|
config.database.user,
|
|
|
|
config.database.password,
|
|
|
|
config.database.database
|
|
|
|
);
|
2019-07-04 12:21:37 +02:00
|
|
|
|
2019-09-03 08:30:47 +02:00
|
|
|
const app = express();
|
2019-08-29 11:35:53 +02:00
|
|
|
|
2019-09-26 15:59:24 +02:00
|
|
|
const tryParseJSON = function(text) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(text);
|
|
|
|
} catch {
|
|
|
|
console.log('invalid json');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-26 15:07:56 +02:00
|
|
|
app.use(bodyParser.text({ type: '*/*' }));
|
2019-09-03 08:30:47 +02:00
|
|
|
|
|
|
|
app.post('/', (req, res, next) => {
|
2019-09-26 15:59:24 +02:00
|
|
|
const json = tryParseJSON(req.body);
|
|
|
|
if (typeof req.query.json != 'undefined') {
|
2020-01-21 15:59:40 +01:00
|
|
|
//json mod requests
|
2019-09-26 15:59:24 +02:00
|
|
|
} else {
|
|
|
|
pg.query(
|
|
|
|
`INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`,
|
|
|
|
[json.app, json.type, json.client, json.message, json.misc, json.stack]
|
|
|
|
);
|
2019-09-03 08:30:47 +02:00
|
|
|
}
|
|
|
|
res.status(201).end();
|
|
|
|
next();
|
|
|
|
});
|
2019-07-04 12:21:37 +02:00
|
|
|
|
2019-09-26 15:07:56 +02:00
|
|
|
app.get('/', async (req, res, next) => {
|
2019-09-03 12:24:50 +02:00
|
|
|
if (typeof req.query.json != 'undefined') {
|
2020-01-21 15:59:40 +01:00
|
|
|
if (
|
|
|
|
config.restrictClients &&
|
|
|
|
!config.allowedClients.includes(req.connection.remoteAddress)
|
|
|
|
) {
|
|
|
|
console.log('blocked request from ' + req.connection.remoteAddress);
|
|
|
|
res.status(403).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-03 08:30:47 +02:00
|
|
|
const data = await pg.query(
|
2019-09-26 15:59:24 +02:00
|
|
|
`SELECT "ID", "Timestamp", "Type", "App", "Client", "Message", "Misc", "Stack" FROM "Log"`
|
2019-09-03 08:30:47 +02:00
|
|
|
);
|
|
|
|
const rows = [];
|
2019-09-26 15:59:24 +02:00
|
|
|
const headings = data.rows.length > 0 ? Object.keys(data.rows[0]) : [];
|
|
|
|
const hiddenData = [0];
|
2019-09-03 08:30:47 +02:00
|
|
|
for (let row of data.rows) {
|
|
|
|
rows.push(Object.values(row));
|
|
|
|
}
|
|
|
|
res
|
|
|
|
.status(200)
|
|
|
|
.type('application/json')
|
2019-09-26 15:59:24 +02:00
|
|
|
.end(
|
|
|
|
JSON.stringify({ headings: headings, data: rows, hidden: hiddenData })
|
|
|
|
);
|
2020-01-21 15:59:40 +01:00
|
|
|
try {
|
|
|
|
const clearLogInterval = Number.parseInt(config.clearLogAfterDays);
|
|
|
|
if (clearLogInterval > 0)
|
|
|
|
await pg.query(`DELETE FROM "Log" WHERE "Timestamp" < now() - interval '${clearLogInterval} days'`);
|
|
|
|
} catch (e) {}
|
2019-09-03 12:24:50 +02:00
|
|
|
} else {
|
2019-09-26 15:07:56 +02:00
|
|
|
next();
|
2019-09-03 08:30:47 +02:00
|
|
|
}
|
2019-07-04 12:21:37 +02:00
|
|
|
});
|
|
|
|
|
2019-09-26 15:07:56 +02:00
|
|
|
app.use(express.static('html'));
|
|
|
|
|
2019-09-03 08:30:47 +02:00
|
|
|
app.listen(config.port);
|
2019-07-04 12:21:37 +02:00
|
|
|
console.log('server listening on', config.port);
|