const express = require('express');
const app = express();
const port = 2428;
const server = app.listen(port, () => console.log(`ARNweb API running on port ${port}`))
var io = require('socket.io').listen(server, { path: '/chat/socket.io' })
var striptags = require('striptags');
var MarkdownIt = require('markdown-it'),
md = new MarkdownIt();
var users = [];
const os = require("os");
const si = require('systeminformation');
var sysInfo;
const gameQuery = require('game-server-query');
const fs = require('fs');
let rawdata = fs.readFileSync(__dirname + "/node_modules/game-server-query/games.json");
const games = JSON.parse(rawdata);
// Request documentation
app.get('/', (req, res) =>
res.sendFile(__dirname + "/index.html")
);
// Function for easy server broadcasts
function serverMsg(content) {
return {
'username': 'Server',
'mailHash': '470965550d3f019830c421f0535d9268',
'message': content
}
}
// Socket.io server side
io.on('connection', function (socket) {
// Send identification request to client
socket.emit('user_identify');
// Bind username and mailhash to client
socket.on('user_login', function (user) {
socket.username = user.username;
socket.mailHash = user.mailHash;
if (users.length > 0) {
socket.emit('message_receive', serverMsg(`Hello ${socket.username}, welcome to ARNweb Chat. The following people are currently online: ${users.join(', ')}`));
} else {
socket.emit('message_receive', serverMsg(`Hello ${socket.username}, welcome to ARNweb Chat. You are the only one here at the moment`));
}
io.emit('message_receive', serverMsg(`${socket.username} has connected`));
users.push(socket.username);
});
// Send disconnect broadcast to all clients
socket.on('disconnect', function () {
io.emit('message_receive', serverMsg(`${socket.username} has disconnected`));
users.splice(users.indexOf(socket.username), 1);
});
// Handle messages
socket.on('message_send', function (message) {
// Build message data for clients
var message = striptags(message, '
');
if (message !== '') {
message = md.render(message);
message = message.replace(/\r?\n/g, '
');
var msg = {
'username': socket.username,
'mailHash': socket.mailHash,
'message': message
}
// Send message to clients
io.emit('message_receive', msg);
} else {
socket.emit('message_receive', serverMsg(`Empty messages will not be sent.`));
}
});
});
// Chat socket and info
app.get('/chat/', function (req, res) {
// Server browser
res.json({
"status": "online",
"users": users
});
});
// System info
app.get('/system/', function (req, res) {
var time = new Date();
sysInfo = {
"hostname": os.hostname(),
"type": os.type(),
"load": os.loadavg(),
"time": si.time()
};
si.cpuTemperature(function (data) {
sysInfo["temp"] = data;
});
si.mem(function (data) {
sysInfo["mem"] = data;
res.json(sysInfo);
});
});
app.get('/css/:stylesheet', function (req, res) {
res.sendFile(__dirname + '/css/' + req.params.stylesheet);
})
// Game servers
app.get('/:type/:host', function (req, res) {
if (games[req.params.type] != undefined) {
gameQuery(
{
type: req.params.type,
host: req.params.host
},
function (state) {
res.json(state);
}
);
} else {
res.json({ "error": "Error: Game type unknown." });
}
});