/** * Copyright JS Foundation and other contributors, http://js.foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ var ws = require("ws"); var log = require("@node-red/util").log; // TODO: separate module var Tokens; var Users; var Permissions; var server; var settings; var runtimeAPI; var wsServer; var activeConnections = []; var anonymousUser; var retained = {}; var heartbeatTimer; var lastSentTime; function init(_server,_settings,_runtimeAPI) { server = _server; settings = _settings; runtimeAPI = _runtimeAPI; Tokens = require("../auth/tokens"); Tokens.onSessionExpiry(handleSessionExpiry); Users = require("../auth/users"); Permissions = require("../auth/permissions"); } function handleSessionExpiry(session) { activeConnections.forEach(connection => { if (connection.token === session.accessToken) { connection.ws.send(JSON.stringify({auth:"fail"})); connection.ws.close(); } }) } function generateSession(length) { var c = "ABCDEFGHIJKLMNOPQRSTUZWXYZabcdefghijklmnopqrstuvwxyz1234567890"; var token = []; for (var i=0;i webSocketKeepAliveTime) { activeConnections.forEach(connection => connection.send("hb",lastSentTime)); } }, webSocketKeepAliveTime); }); } } function stop() { if (heartbeatTimer) { clearInterval(heartbeatTimer); heartbeatTimer = null; } if (wsServer) { wsServer.close(); wsServer = null; } } function addActiveConnection(connection) { activeConnections.push(connection); runtimeAPI.comms.addConnection({client: connection}); } function removeActiveConnection(connection) { for (var i=0;i