forked from open-webrtc-toolkit/owt-server-p2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerserver.js
154 lines (138 loc) · 4.56 KB
/
peerserver.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
// Copyright (C) <2015> Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
// 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 '4.2':
case '4.2.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=['owt-message'];
for (var i=0;i<forwardEvents.length;i++){
socket.on(forwardEvents[i],(function(i){
return function(data, ackCallback){
console.log('Received '+forwardEvents[i]+': '+JSON.stringify(data));
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);