forked from PraatApparaat/NodeJS-Seedlink-Stations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
411 lines (294 loc) · 10.1 KB
/
index.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* nodejs-seedlink-stations-proxy
*
* NodeJS application for reading the available
* stations from arbitrary Seedlink servers.
*
* Copyright ORFEUS Data Center
* Author: Mathijs Koymans
* Licensed under MIT
*
*/
"use strict";
// Native modules
const network = require("net");
const http = require("http");
const url = require("url");
const querystring = require("querystring");
const path = require("path");
const fs = require("fs");
// Global container for Stations
var SeedlinkStationProxy = function(configuration, callback) {
/* class SeedlinkStationProxy
* NodeJS proxy for getting Seedlink station information
*/
function HTTPError(response, statusCode, message) {
/* function HTTPError
* Writes HTTP reponse to the client
*/
response.writeHead(statusCode, {"Content-Type": "text/plain"});
response.write(message);
response.end();
}
function EnableCORS(response) {
/* function EnableCORS
* Enables the cross origin headers
*/
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET");
}
this.configuration = configuration;
this.logger = this.setupLogger();
this.cachedStations = new Object();
// Create the HTTP server
const Server = http.createServer(function(request, response) {
// Enable CORS headers when required
if(this.configuration.__CORS__) {
EnableCORS(response);
}
var uri = url.parse(request.url);
var initialized = Date.now();
// Only root path is supported
if(uri.pathname !== "/") {
return HTTPError(response, 404, "Method not supported.")
}
var queryObject = querystring.parse(uri.query);
if(!queryObject.host) {
return HTTPError(response, 400, "Host parameter is required.");
}
// Get the comma delimited of requested servers
var servers = queryObject.host.split(",").map(this.parseHost);
if(servers.length === 0) {
return HTTPError(response, 204);
}
// Check user input
try {
Object.keys(queryObject).forEach(this.validateParameters);
servers.forEach(this.validateServer);
} catch(exception) {
if(this.configuration.__DEBUG__) {
return HTTPError(response, 400, exception.stack);
} else {
return HTTPError(response, 400, exception.message);
}
}
// Write information to logfile
response.on("finish", function() {
this.logger.write(JSON.stringify({
"timestamp": new Date().toISOString(),
"method": request.method,
"query": uri.query,
"path": uri.pathname,
"client": request.headers["x-forwarded-for"] || request.connection.remoteAddress,
"agent": request.headers["user-agent"] || null,
"statusCode": response.statusCode,
"type": "HTTP Request",
"msRequestTime": (Date.now() - initialized)
}) + "\n");
}.bind(this));
// Make queries to the servers (or read from cache)
this.readSeedlinkStations(servers, function(data) {
response.writeHead(200, {"Content-Type": "application/json"});
response.write(JSON.stringify(data));
response.end();
});
}.bind(this));
// Get process environment variables (Docker)
var host = process.env.SERVICE_HOST || this.configuration.HOST;
var port = Number(process.env.SERVICE_PORT) || this.configuration.PORT;
// Listen to incoming HTTP connections
Server.listen(this.configuration.PORT, this.configuration.HOST, function() {
callback(configuration.__NAME__, host, port);
});
}
SeedlinkStationProxy.prototype.parseHost = function(x) {
/* function SeedlinkStationProxy.parseHost
* Parses host:port to usable object
*/
const DEFAULT_SEEDLINK_PORT = 18000;
var [host, port] = x.split(":");
port = Number(port) || DEFAULT_SEEDLINK_PORT;
return {
"url": host + ":" + port,
"host": host,
"port": port
}
}
SeedlinkStationProxy.prototype.setupLogger = function() {
/* function SeedlinkStationProxy.setupLogger
* Sets up log directory and file for logging
*/
// Create the log directory if it does not exist
fs.existsSync(path.join(__dirname, "logs")) || fs.mkdirSync(path.join(__dirname, "logs"));
return fs.createWriteStream(path.join(__dirname, "logs", "service.log"), {"flags": "a"});
}
SeedlinkStationProxy.prototype.validateParameters = function(key) {
/* function SeedlinkStationProxy.validateParameters
* Validates whether a key is allowed;
*/
const ALLOWED_PARAMETERS = [
"host"
];
if(!ALLOWED_PARAMETERS.includes(key)) {
throw new Error("Key " + key + " is not supported.");
}
}
SeedlinkStationProxy.prototype.validateServer = function(server) {
/* function SeedlinkStationProxy.validateServer
* Validates the submitted server port
*/
// Check if port is integer
if(!Number.isInteger(server.port)) {
throw new Error("A submitted port is non-numerical.");
}
// Check the submitted port
if(server.port < 0 || server.port >= (1 << 16)) {
throw new Error("A submitted port is outside the valid range.");
}
}
SeedlinkStationProxy.prototype.readSeedlinkStations = function(servers, callback) {
/* function SeedlinkStationProxy.readSeedlinkStations
* Concurrently but asynchronously updates the cache
*/
// Collect the results for all servers
var results = new Array();
var next;
// Asynchronously but concurrently get the data
(next = function() {
var server = servers.pop();
this.checkSeedlink(server, function(result) {
// Update the information in the hashMap
if(result.error === null) {
this.cachedStations[server.url] = result;
}
results.push(result);
// Proceed to the next requested server
if(servers.length) {
return next();
}
callback(results);
}.bind(this));
}.bind(this))();
}
SeedlinkStationProxy.prototype.isCached = function(host) {
/* function isCached
* Returns boolean whether a seedlink server is cached
*/
var cachedValue = this.cachedStations[host];
return cachedValue.requested > (Date.now() - this.configuration.REFRESH_INTERVAL_MS);
}
SeedlinkStationProxy.prototype.newRequestData = function(server) {
/* function SeedlinkStationProxy.newRequestData
* Returns new default object for request metadata/data
*/
return {
"server": server,
"stations": new Array(),
"error": null,
"version": null,
"identifier": null,
"connected": false,
"requested": Date.now()
}
}
SeedlinkStationProxy.prototype.checkSeedlink = function(server, callback) {
var finish = function(socket, host, callback, requestData) {
/* function finish
* Function to be bound and called per request
*/
// Destroy the socket
socket.destroy();
// Callback with particular request data
callback(requestData);
}
/* Function checkSeedlink
* Checks if Seedlink is present
* Returns all metadata: networks, stations, sites
*/
// Constants
const CRNL = "\r\n";
const CAT_COMMAND = "CAT" + CRNL;
const HELLO_COMMAND = "HELLO" + CRNL;
// Extract the Seedlink server URL
var url = server.url;
// If the host is still in the cache
if(this.cachedStations.hasOwnProperty(url) && this.isCached(url)) {
return callback(this.cachedStations[url]);
}
// Metadata for the request to be filled
var requestData = this.newRequestData(server);
// Create a new TCP socket and empty buffer
const socket = new network.Socket()
var buffer = new Buffer(0);
// Bind some parameters to the finish function
finish = finish.bind(this, socket, url, callback);
// When the connection is established write HELLO to Seedlink
socket.connect(server.port, server.host, function() {
socket.write(HELLO_COMMAND);
});
// Data is written over the socket
socket.on("data", function(data) {
requestData.connected = true;
// Extend the buffer with new data
buffer = Buffer.concat([buffer, data]);
// Get the Seedlink version
if(requestData.version === null && buffer.toString().split(CRNL).length === 3) {
// Extract the version
var [version, identifier] = buffer.toString().split(CRNL);
requestData.version = version;
requestData.identifier = identifier;
// Reset the buffer for the next request
buffer = new Buffer(0);
// Info was requested: proceed with the CAT command
return socket.write(CAT_COMMAND);
}
// If the command was not implemented (e.g. IRIS ringserver)
if(buffer.toString() === "CAT command not implemented" + CRNL) {
requestData.error = "CATNOTIMPLEMENTED";
finish(requestData);
}
// End of the Seedlink response
if(buffer.lastIndexOf("\nEND") === buffer.length - 4) {
requestData.stations = this.parseBuffer(buffer);
finish(requestData);
}
}.bind(this));
// An error occured connecting to Seedlink
socket.on("error", function() {
requestData.error = "ECONNREFUSED";
finish(requestData);
});
// Propagate timeout to error
socket.on("timeout", function() {
socket.emit("error");
});
// Set Timout in milliseconds
socket.setTimeout(this.configuration.SOCKET.TIMEOUT);
}
SeedlinkStationProxy.prototype.parseBuffer = function(buffer) {
/* function SeedlinkStationProxy.parseBuffer
* Extracts network, station information from Seedlink CAT response
*/
// Split by line and map result
return buffer.slice(0, buffer.lastIndexOf("\nEND")).toString().split("\n").map(this.parseResponse);
}
SeedlinkStationProxy.prototype.parseResponse = function(x) {
/* function SeedlinkStationProxy.parseResponse
* Extracts network, station, site from the Seedlink response
*/
return {
"network": x.slice(0, 2).trim(),
"station": x.slice(3, 8).trim(),
"site": x.slice(9, x.length).trim()
}
}
// Expose the class
module.exports = SeedlinkStationProxy;
// Start the NodeJS Seedlink Server
if(require.main === module) {
const CONFIG = require("./config");
// Start up the WFCatalog
new module.exports(CONFIG, function(name, host, port) {
console.log(name + " microservice has been started on " + host + ":" + port);
});
}