UNPKG

iot-sol

Version:

Intel(r) IoT Services Orchestration Layer - HTML5 IDE + Node.js middleware to create and host distributed IoT Apps in minutes

207 lines (187 loc) 7.88 kB
/****************************************************************************** Copyright (c) 2016, Intel Corporation Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *****************************************************************************/ /* * Copyright © 2015 Intel Corporation. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // Prepare for web server var fs = require("fs"); var path = require("path"); var url = require('url'); var config = require('./config'); var account = require('./vendermodule'); var dirname = __dirname || path.dirname(fs.readlinkSync('/proc/self/exe')); var httpsOptions = { key: fs.readFileSync(path.resolve(dirname, 'cert/key.pem')).toString(), cert: fs.readFileSync(path.resolve(dirname, 'cert/cert.pem')).toString() }; var app = require('express')(); var server = app.listen(config.port.plain); var servers = require("https").createServer(httpsOptions, app).listen(config.port.secured); var io = require('socket.io').listen(server); var ios = require('socket.io').listen(servers); var sessionMap = {}; // Key is uid, and value is session object. // Check user's token from partner function validateUser(token, successCallback, failureCallback){ // TODO: Should check token first, replace this block when engagement with different partners. if(token){ account.authentication(token,function(uid){ successCallback(uid); },function(){ console.log('Account system return false.'); failureCallback(0); }); } else failureCallback(0); } function disconnectClient(uid){ if(sessionMap[uid]!==undefined){ var session=sessionMap[uid]; session.emit('server-disconnect'); session.disconnect(); console.log('Force disconnected '+uid); } } function createUuid(){ return 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8); return v.toString(16); }); } function emitChatEvent(targetUid, eventName, message, successCallback, failureCallback){ if(sessionMap[targetUid]){ sessionMap[targetUid].emit(eventName,message); if(successCallback) successCallback(); } else if(failureCallback) failureCallback(2201); } function authorization(socket, next){ var query=url.parse(socket.request.url,true).query; var token=query.token; var clientVersion=query.clientVersion; var clientType=query.clientType; switch(clientVersion){ case '2.0': case '1.5': case '1.1': case '2.1': case '2.0.1': case '2.1.1': // socket.user stores session related information. if(token){ validateUser(token, function(uid){ // Validate user's token successfully. socket.user={id:uid}; console.log(uid+' authentication passed.'); },function(error){ // Invalid login. console.log('Authentication failed.'); next(); }); }else{ socket.user=new Object(); socket.user.id=createUuid()+'@anonymous'; console.log('Anonymous user: '+socket.user.id); } next(); break; default: next(new Error('2103')); console.log('Unsupported client. Client version: '+query.clientVersion); break; } } function onConnection(socket){ // Disconnect previous session if this user already signed in. var uid=socket.user.id; disconnectClient(uid); sessionMap[uid]=socket; socket.emit('server-authenticated',{uid:uid}); // Send current user's id to client. console.log('A new client has connected. Online user number: '+Object.keys(sessionMap).length); socket.on('disconnect',function(){ if(socket.user){ var uid=socket.user.id; // Delete session if(socket===sessionMap[socket.user.id]){ delete sessionMap[socket.user.id]; } console.log(uid+' has disconnected. Online user number: '+Object.keys(sessionMap).length); } }); // Forward events var forwardEvents=['chat-invitation','chat-accepted','stream-type','chat-negotiation-needed','chat-negotiation-accepted','chat-stopped','chat-denied','chat-signal']; for (var i=0;i<forwardEvents.length;i++){ socket.on(forwardEvents[i],(function(i){ return function(data, ackCallback){ console.log('Received '+forwardEvents[i]); data.from=socket.user.id; var to=data.to; delete data.to; emitChatEvent(to,forwardEvents[i],data,function(){ if(ackCallback) ackCallback(); },function(errorCode){ if(ackCallback) ackCallback(errorCode); }); }; })(i)); } } function listen(io) { io.use(authorization); io.on('connection',onConnection); } listen(io); listen(ios); // Signaling server only allowed to be connected with Socket.io. // If a client try to connect it with any other methods, server returns 405. app.get('*', function(req, res, next) { res.send(405, 'WebRTC signaling server. Please connect it with Socket.IO.'); }); console.info('Listening port: ' + config.port.plain + '/' + config.port.secured);