-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
304 lines (275 loc) · 8.22 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'use strict'
const path = require('path');
const fs = require('fs');
const fastify = require('fastify');
const fastify_static = require('fastify-static');
const fastify_ws = require('fastify-ws');
//Server
const version = "0.9.2";
const settings = require('./settings.json');
var Clexi = function(customSettings){
var ClexiServer = {};
if (!customSettings) customSettings = {};
var port = customSettings.port || settings.port || 8443;
var hostname = customSettings.hostname || settings.hostname || "localhost"; //default: 127.0.0.1, all: 0.0.0.0
var serverId = customSettings.id || settings.id || "clexi-123";
var idIsPassword = customSettings.idIsPassword || settings.idIsPassword || false;
var sslCertPath = customSettings.sslCertPath || path.join(__dirname, "ssl");
var wwwPath = customSettings.wwwPath || path.join(__dirname, "www");
var defaultXtensionsPath = path.join(__dirname, 'xtensions');
var customXtensionsPath = customSettings.customXtensionsPath || defaultXtensionsPath;
var customXtensions = customSettings.customXtensions || [];
var server_options = {
//http2: true,
};
if (customSettings.logger){
server_options.logger = customSettings.logger;
}else if (settings.logger){
server_options.logger = settings.logger;
}else{
server_options.logger = {
level: 'info'
};
}
var isLogLevelDebugOrTrace = (server_options.logger.level && (server_options.logger.level == "debug" || server_options.logger.level == "trace"));
var useSsl = (customSettings.ssl != undefined)? customSettings.ssl : settings.ssl;
if (useSsl){
server_options.https = {
key: fs.readFileSync(path.join(sslCertPath, 'key.pem')),
cert: fs.readFileSync(path.join(sslCertPath, 'certificate.pem'))
};
}
const server = fastify(server_options);
ClexiServer.fastify = server;
//Plugins
server.register(fastify_static, {
root: wwwPath,
redirect: false //for fastify 2 security - see: https://github.com/advisories/GHSA-p6vg-p826-qp3v
});
server.register(fastify_ws);
//Xtensions
var xtensions = {};
function loadXtensions(){
let n = 0;
if (settings.xtensions && (settings.xtensions instanceof Array)){
//Load default extensions
n += loadXtensionsArray(settings.xtensions, defaultXtensionsPath);
}
if (customXtensions && (customXtensions instanceof Array)){
//Load custom extensions
n += loadXtensionsArray(customXtensions, customXtensionsPath);
//NOTE: will overwrite extension if they have same name
}
console.log('CLEXI Xtensions loaded: ' + n);
}
function loadXtensionsArray(xtensionNames, xtensionsPath){
let n = 0;
for (var i=0; i<xtensionNames.length; i++){
n++;
let xName = xtensionNames[i];
let X = require(path.join(xtensionsPath, xName));
xtensions[xName] = new X(
function(msg){
//On start
msg.type = xName;
server.log.info(JSON.stringify(msg, null, ' '));
//EVENT OUTPUT TO CLIENT
}, function(msg){
//On event
msg.type = xName;
if (isLogLevelDebugOrTrace){
server.log.debug('Broadcasting event: ' + msg.type);
server.log.trace(JSON.stringify(msg, null, ' '));
}
broadcast(msg);
}, function(error){
//On error
error.type = xName;
server.log.error(JSON.stringify(error, null, ' '));
broadcast(error);
}
);
}
return n;
}
function getXtensionsInfo(){
var info = {};
Object.keys(xtensions).forEach(function(name) {
info[name] = { active: true }; //TODO: one could add interface information here for each xtension
});
return info;
}
//Broadcast to receiver or all
function broadcast(data){
if (data.receiver){
//single receiver?
var client = data.receiver;
delete data.receiver; //remove object before sending
if (!idIsPassword || (idIsPassword && client.authState)){
if (typeof data === "object"){
client.send(JSON.stringify(data));
}else{
client.send(data);
}
}else{
server.log.error("Tried to broadcast to unauthorized client. Client should be disconnected already!");
client.terminate();
}
}else{
//broadcast to all
server.ws.clients.forEach(function each(client){
if (client.readyState === 1) { //WebSocket.OPEN should be 1
if (!idIsPassword || (idIsPassword && client.authState)){
if (typeof data === "object"){
client.send(JSON.stringify(data));
}else{
client.send(data);
}
}else{
server.log.error("Tried to broadcast to unauthorized client. Client should be disconnected already!");
client.terminate();
}
}
});
}
}
//Server routes:
//Ping
server.get('/ping', function(request, reply) {
var d = {
v: version,
id: serverId
}
if (idIsPassword){
d.id = "[SECRET]";
}
reply.send(d);
});
//CLEXI HTTP-Events
server.get('/event/:name', function(request, reply){
if (idIsPassword){
if (request.headers['clexi-id'] == serverId){
sendHttpEventToXtension(request, reply, "GET");
}else{
reply.code(401);
reply.send();
}
}else{
sendHttpEventToXtension(request, reply, "GET");
}
});
server.post('/event/:name', function(request, reply){
if (idIsPassword){
if (request.headers['clexi-id'] == serverId){
sendHttpEventToXtension(request, reply, "POST");
}else{
reply.code(401);
reply.send();
}
}else{
sendHttpEventToXtension(request, reply, "POST");
}
});
function sendHttpEventToXtension(request, reply, method){
if (xtensions["clexi-http-events"]){
//the http-event extension is active
xtensions["clexi-http-events"].event(method, request);
reply.code(204);
reply.send();
}else{
//no http-event extension support
reply.code(404);
reply.send();
}
}
//Run the server
ClexiServer.start = function(){
server.listen(port, hostname, function(err, address){
if (err){
server.log.error(err);
process.exit(1);
}
console.log(`Local date/time at start: ${new Date().toLocaleString()}`);
console.log(`Server with ID '${serverId}' running at: ${address}`);
console.log(`Hostname: ${hostname} - SSL: ${useSsl}`);
server.log.info(`Server running at ${address}`);
//Websocket interface
server.ws.on('connection', function(socket, request){
server.log.info('Client connected.');
//CLIENT INPUT
socket.on('message', function(msg){
let msgObj = JSON.parse(msg);
//Handle extensions input
if (msgObj.type && xtensions[msgObj.type]){
server.log.info('Calling xtensions: ' + msgObj.type);
let response = xtensions[msgObj.type].input(msgObj, socket);
if (response){
socket.send(JSON.stringify({
response: response,
type: msgObj.type,
id: msgObj.id
}));
}
//Welcome
}else if (msgObj.type && msgObj.type == "welcome"){
//check server ID
if (msgObj.data && msgObj.data.server_id && msgObj.data.server_id == serverId){
socket.authState = true;
}
if (!idIsPassword){
socket.send(JSON.stringify({
type: "welcome",
code: 200,
info: {
id: serverId,
version: ("CLEXI Node.js server v" + version),
xtensions: getXtensionsInfo()
}
}));
}else if (idIsPassword && socket.authState){
socket.send(JSON.stringify({
type: "welcome",
code: 200,
info: {
version: ("CLEXI Node.js server v" + version),
xtensions: getXtensionsInfo()
}
}));
}else{
socket.send(JSON.stringify({
type: "welcome",
code: 401,
info: {
msg: "not authorized",
version: ("CLEXI Node.js server v" + version)
}
}));
//socket.terminate(); //the client should gracefully disconnect O_O
}
//undefined
}else{
socket.send(JSON.stringify({
response: ("Unknown message type: " + msgObj.type),
type: "undefined"
}));
}
});
socket.on('close', function(){
server.log.info('Client disconnected.');
});
socket.on('error', function(e){
server.log.error(`Client error: ${e.message}`);
});
});
//Load extensions
loadXtensions();
});
}
ClexiServer.stop = server.close;
return ClexiServer;
}
//Run server when called directly
if (require.main === module){
Clexi().start();
}
module.exports = Clexi;