-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
240 lines (196 loc) · 8.74 KB
/
app.js
File metadata and controls
240 lines (196 loc) · 8.74 KB
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
/**************************************************************************
* Required files
**************************************************************************/
var http = require('http'),
util = require('util'),
express = require('express'),
httpAuth = require('http-auth'),
io = require("socket.io"),
uuid = require('node-uuid');
var config = require('./config/config').configure();
var usersM = require('./model/users.js'),
channelsM = require('./model/channels.js');
var Commands = require('./lib/commands.js'),
Wordings = require('./lib/wordings.js');
// Manager = require('./lib/manager.js');
var A = require('./vendor/array.js');
var users = {};
/**************************************************************************
* Initialize
* Express + Socket.io
**************************************************************************/
var app = express.createServer();
app.listen(config.port, config.host);
app.configure(function(){
app.use(express.bodyParser());
});
var expressAuth = httpAuth({
authType: "digest",
authRealm: "realm",
authFile: __dirname + '/users.htpasswd'
});
var socketServer = io.listen(app);
/**************************************************************************
* Express
**************************************************************************/
app.get('/', function(req, res) {
res.contentType('text');
res.send(Wordings.welcomeMessage);
});
/**
* Receive push from API, and send it into the channel asked
*/
app.post('/api/send_push', function(req, res) {
expressAuth.apply(req, res, function(username) {
res.contentType('json');
var channel = req.body.channel,
event = req.body.event,
data = req.body.data || {};
// util.debug(JSON.stringify({'channel':channel, 'event':event, 'data':data}));
if (channel != undefined && event != undefined) {
socketServer.sockets.in(channel).send(JSON.stringify({'channel' : channel, 'event' : event, 'data' : data}));
res.send(JSON.stringify({'msg':'Push sended'}), 200);
}
else {
res.send(JSON.stringify({'msg':'Push not sended. Channel or event is missing.'}), 400);
}
});
});
/**
* Verify authenticity of socket
*/
app.post('/api/authenticate', function(req, res) {
expressAuth.apply(req, res, function(username) {
res.contentType('json');
var socket_id = req.body.socket_id || '',
user_uuid = req.body.user_uuid || '',
authenticate = req.body.authenticate ? true : false,
channels = req.body.channels || [];
// util.debug(JSON.stringify({'socket_id':socket_id, 'authenticate':authenticate, 'user_uuid':user_uuid, 'channels':channels}));
var client = socketServer.sockets.sockets[socket_id];
if (authenticate === true && client != undefined) {
if (users[user_uuid] == undefined) users[user_uuid] = []
users[user_uuid].push(socket_id);
client.user.authentify = true;
client.user.uuid = user_uuid;
client.user.channels = channels;
client.send(JSON.stringify({'msg':'You are now authenticate','data':{'auth':true, 'channels':channels}}));
client.join("broadcast");
for (var i = channels.length - 1; i >= 0; i--) {
// Subscribe the user in the channel
client.join(channels[i]);
};
res.send(JSON.stringify({'auth':true}), 200);
}
else
{
res.send(JSON.stringify({'auth':false, 'msg':'Undefined socket'}), 401);
}
});
});
/**
* Subscribe socket session on new channels
*/
app.post('/api/channels/subscribe', function(req, res) {
expressAuth.apply(req, res, function(username) {
res.contentType('json');
var user_uuid = req.body.user_uuid || '',
new_channels = req.body.channels || [];
util.debug("Subscribe" + JSON.stringify({'user_uuid':user_uuid, 'channels':new_channels}));
var user_sockets = users[user_uuid];
if (user_sockets != undefined)
{
for (var i = user_sockets.length - 1; i >= 0; i--)
{
var client = socketServer.sockets.sockets[(user_sockets[i])];
if (client != undefined && client.user.authentify === true)
{
client.user.channels = client.user.channels.concat(new_channels);
for (var i = new_channels.length - 1; i >= 0; i--) {
// Subscribe the user in the channel
client.join(new_channels[i]);
};
}
};
// util.debug(JSON.stringify({'users':users, 'socket_id':user_sockets[i], 'client.user':client.user.channels}));
res.send(JSON.stringify({'subscribe':true}), 200);
}
else
{
res.send(JSON.stringify({'subscribe':false, 'msg':'Undefined user_uuid'}), 401);
}
});
});
/**
* Unsubscribe socket session on new channels
*/
app.post('/api/channels/unsubscribe', function(req, res) {
expressAuth.apply(req, res, function(username) {
res.contentType('json');
var user_uuid = req.body.user_uuid || '',
channels_to_remove = req.body.channels || [];
util.debug("Unsubscribe" + JSON.stringify({'user_uuid':user_uuid, 'channels':channels_to_remove}));
var user_sockets = users[user_uuid];
if (user_sockets != undefined)
{
for (var i = user_sockets.length - 1; i >= 0; i--)
{
var client = socketServer.sockets.sockets[user_sockets[i]];
if (client != undefined && client.user.authentify === true)
{
var channels = client.user.channels;
for (var i = channels_to_remove.length - 1; i >= 0; i--) {
channels.remove(channels_to_remove[i]);
// Unsubscribe the user in the channel
client.leave(channels_to_remove[i]);
};
}
};
// util.debug(JSON.stringify({'users':users, 'socket_id':user_sockets[i], 'client.user':client.user.channels}));
res.send(JSON.stringify({'unsubscribe':true}), 200);
}
else
{
res.send(JSON.stringify({'unsubscribe':false, 'msg':'Undefined user_uuid'}), 401);
}
});
});
/**************************************************************************
* Socket.io
**************************************************************************/
// assuming io is the Socket.IO server object
// Resolve bug with Heroku
// socketServer.configure(function () {
// socketServer.set("transports", ["websocket"]);
// socketServer.set("polling duration", config.pollingDuration);
// });
socketServer.sockets.on("connection", function(client) {
verifAuthentication = function(client) {
if (!client.user.authentify) {
client.user.send(JSON.stringify({'msg': 'you have not authentified. Bye Bye !'}));
client.disconnect();
}
}
// Initialize client
client.user = new User(client);
// TODO verif identity with OAuth provider. Give just 30 seconds for the client, after, disconnect it.
client.user.send(JSON.stringify({'msg': 'you have '+config.authenticationTimeout+' seconds for confirm your identity. Please authentify you on API', 'data':{'socket_id':client.sessionid}}));
setTimeout(verifAuthentication, config.authenticationTimeout*1000, client);
client.on("message", function(data) {
client.send(JSON.stringify({'msg': 'you can\'t speak'}));
});
client.on("disconnect", function() {
if (users[client.user.uuid] != undefined) users[client.user.uuid].remove(client.sessionid);
// Commands.leave(client.user, {});
});
});
/*
// HTTP Client example
var httpClient = http.createClien(80, 'api.dev.trackline-project.net'); // Place into config file
var request = client.request('POST', '/pusher/token_verification');
request.end();
request.on('response', function(response) {
// Verif if identifiants is okey
});
*/
util.puts('-- Server running on '+config.host+':'+config.port);