2016-09-05 04:39:59 +00:00
|
|
|
// ===========================================
|
|
|
|
// REQUARKS WIKI - WebSocket Server
|
|
|
|
// 1.0.0
|
|
|
|
// Licensed under AGPLv3
|
|
|
|
// ===========================================
|
|
|
|
|
|
|
|
global.ROOTPATH = __dirname;
|
2016-09-29 01:58:18 +00:00
|
|
|
global.PROCNAME = 'WS';
|
2016-09-05 04:39:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Load Winston
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
var _isDebug = process.env.NODE_ENV === 'development';
|
2016-09-29 01:58:18 +00:00
|
|
|
global.winston = require('./lib/winston')(_isDebug);
|
2016-09-05 04:39:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Fetch internal handshake key
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
if(!process.argv[2] || process.argv[2].length !== 40) {
|
|
|
|
winston.error('[WS] Illegal process start. Missing handshake key.');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
global.internalAuth = require('./lib/internalAuth').init(process.argv[2]);;
|
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-09-25 04:08:49 +00:00
|
|
|
// Load global modules
|
2016-09-05 04:39:59 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
winston.info('[WS] WS Server is initializing...');
|
|
|
|
|
|
|
|
var appconfig = require('./models/config')('./config.yml');
|
2016-09-25 04:08:49 +00:00
|
|
|
let lcdata = require('./models/localdata').init(appconfig, 'ws');
|
2016-09-05 04:39:59 +00:00
|
|
|
|
|
|
|
global.entries = require('./models/entries').init(appconfig);
|
|
|
|
global.mark = require('./models/markdown');
|
|
|
|
global.search = require('./models/search').init(appconfig);
|
|
|
|
|
|
|
|
// ----------------------------------------
|
2016-09-25 04:08:49 +00:00
|
|
|
// Load local modules
|
2016-09-05 04:39:59 +00:00
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
var express = require('express');
|
|
|
|
var path = require('path');
|
|
|
|
var http = require('http');
|
|
|
|
var socketio = require('socket.io');
|
|
|
|
var moment = require('moment');
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Define Express App
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
global.app = express();
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Controllers
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
app.get('/', function(req, res){
|
2016-09-19 03:03:19 +00:00
|
|
|
res.send('Requarks Wiki WebSocket server');
|
2016-09-05 04:39:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Start WebSocket server
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
winston.info('[SERVER] Starting WebSocket server on port ' + appconfig.wsPort + '...');
|
|
|
|
|
|
|
|
app.set('port', appconfig.wsPort);
|
|
|
|
var server = http.Server(app);
|
|
|
|
var io = socketio(server);
|
|
|
|
|
|
|
|
server.on('error', (error) => {
|
2016-09-19 03:03:19 +00:00
|
|
|
if (error.syscall !== 'listen') {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (error.code) {
|
|
|
|
case 'EACCES':
|
|
|
|
console.error('Listening on port ' + appconfig.port + ' requires elevated privileges!');
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
case 'EADDRINUSE':
|
|
|
|
console.error('Port ' + appconfig.port + ' is already in use!');
|
|
|
|
process.exit(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw error;
|
|
|
|
}
|
2016-09-05 04:39:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(appconfig.wsPort, () => {
|
2016-09-19 03:03:19 +00:00
|
|
|
winston.info('[WS] WebSocket server started successfully! [RUNNING]');
|
2016-09-05 04:39:59 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
io.on('connection', (socket) => {
|
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
//-----------------------------------------
|
|
|
|
// SEARCH
|
|
|
|
//-----------------------------------------
|
|
|
|
|
2016-09-05 04:39:59 +00:00
|
|
|
socket.on('searchAdd', (data) => {
|
|
|
|
if(internalAuth.validateKey(data.auth)) {
|
|
|
|
search.add(data.content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
socket.on('searchDel', (data, cb) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
cb = cb || _.noop
|
2016-09-19 03:03:19 +00:00
|
|
|
if(internalAuth.validateKey(data.auth)) {
|
|
|
|
search.delete(data.entryPath);
|
|
|
|
}
|
|
|
|
});
|
2016-09-11 02:53:21 +00:00
|
|
|
|
2016-09-05 04:39:59 +00:00
|
|
|
socket.on('search', (data, cb) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
cb = cb || _.noop
|
2016-09-05 04:39:59 +00:00
|
|
|
search.find(data.terms).then((results) => {
|
|
|
|
cb(results);
|
|
|
|
});
|
2016-09-11 02:53:21 +00:00
|
|
|
});
|
2016-09-05 04:39:59 +00:00
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
//-----------------------------------------
|
|
|
|
// UPLOADS
|
|
|
|
//-----------------------------------------
|
|
|
|
|
|
|
|
socket.on('uploadsSetFolders', (data, cb) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
cb = cb || _.noop
|
2016-09-19 03:03:19 +00:00
|
|
|
if(internalAuth.validateKey(data.auth)) {
|
|
|
|
lcdata.setUploadsFolders(data.content);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('uploadsGetFolders', (data, cb) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
cb = cb || _.noop
|
2016-09-19 03:03:19 +00:00
|
|
|
cb(lcdata.getUploadsFolders());
|
|
|
|
});
|
2016-09-05 04:39:59 +00:00
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
socket.on('uploadsCreateFolder', (data, cb) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
cb = cb || _.noop
|
2016-09-29 01:58:18 +00:00
|
|
|
lcdata.createUploadsFolder(data.foldername).then((fldList) => {
|
|
|
|
cb(fldList);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-09-25 04:08:49 +00:00
|
|
|
socket.on('uploadsSetFiles', (data, cb) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
cb = cb || _.noop;
|
2016-09-25 04:08:49 +00:00
|
|
|
if(internalAuth.validateKey(data.auth)) {
|
|
|
|
lcdata.setUploadsFiles(data.content);
|
2016-10-09 05:26:25 +00:00
|
|
|
cb(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('uploadsAddFiles', (data, cb) => {
|
|
|
|
cb = cb || _.noop
|
|
|
|
if(internalAuth.validateKey(data.auth)) {
|
|
|
|
lcdata.addUploadsFiles(data.content);
|
|
|
|
cb(true);
|
2016-09-25 04:08:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
socket.on('uploadsGetImages', (data, cb) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
cb = cb || _.noop
|
2016-09-25 04:08:49 +00:00
|
|
|
cb(lcdata.getUploadsFiles('image', data.folder));
|
2016-09-19 03:03:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2016-09-05 04:39:59 +00:00
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Shutdown gracefully
|
|
|
|
// ----------------------------------------
|
|
|
|
|
|
|
|
process.on('disconnect', () => {
|
|
|
|
winston.warn('[WS] Lost connection to main server. Exiting... [' + moment().toISOString() + ']');
|
|
|
|
server.close();
|
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('exit', () => {
|
|
|
|
server.stop();
|
|
|
|
});
|