-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermes.js
315 lines (220 loc) · 9.16 KB
/
hermes.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
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var socketIo = require('socket.io');
var socketio_jwt = require('socketio-jwt');
var cors = require('cors');
var crypto = require('crypto');
var jwt = require('jsonwebtoken');
var unirest = require('unirest');
// Hash
var hrTime = process.hrtime()
var microtime = hrTime[0] * 1000000 + hrTime[1] / 1000;
var salt = 'SDAF34$@%%fdf34$3234&!!fdk00912ljsadjfvmASDFcasdakjedwer324rsdf!!&&****%$sdcsc@#3sfdlkfdsfsdf'+microtime;
var hash = crypto.createHash('md5').update(salt).digest('hex');
var jwt_secret = hash;
// Basic login auth. You can change it to auth with something else if you like.
var authUser = 'hermes';
var authPass = 'messengergod007';
// Local data storage. Push clients into the array.
var client_data = { clients : [] };
// Express stuff
var app = express();
app.use( bodyParser.json() );
app.use( bodyParser.urlencoded({ extended: true }) );
app.use( express.static(__dirname + '/') );
app.use( cors() );
// Login route to handle authentication. Returns jwt token.
app.post('/login', function (req, res) {
var profile = {
project: 'Hermes',
id: microtime
};
var username = req.body.username;
var password = req.body.password;
if( typeof username === 'undefined' || typeof password === 'undefined' ){
res.status(401).send('Unauthorized');
return;
}
// Send login request optional. Coming soonish.
if( !username.length || !password.length ){
res.status(401).send('Unauthorized');
return;
} else {
// API auth - Authenticate with your API
unirest.post('https://api.walkerfirst.com/v1/Bf65GHop3WEssdf56z/json/auth/login')
.headers({ 'Accept': 'application/json' })
.type('json')
.send( { "username" : username, "password" : password } )
.end(function (response) {
var api_status = (response.body.api_status);
if( api_status === 'success' ){
var token = jwt.sign(profile, jwt_secret, { expiresInMinutes: 60*10 });
res.json({token: token});
} else {
res.status(401).send('Unauthorized');
return;
}
});
// Basic auth - Authenticate with local username and password vars
/*
if( ( username === authUser ) && ( password === authPass ) ){
// We are sending the profile inside the token
var token = jwt.sign(profile, jwt_secret, { expiresInMinutes: 60*10 });
res.json({token: token});
} else {
res.status(401).send('Unauthorized');
return;
}*/
}
});
function extend(target) {
var sources = [].slice.call(arguments, 1);
sources.forEach(function (source) {
for (var prop in source) {
target[prop] = source[prop];
}
});
return target;
}
function Merge(obj1, obj2) {
for (var p in obj2) {
try {
if ( obj2[p].constructor==Object ) {
obj1[p] = Merge(obj1[p], obj2[p]);
} else {
obj1[p] = obj2[p];
}
} catch(e) {
obj1[p] = obj2[p];
}
}
return obj1;
}
// Create the server
var server = http.createServer(app);
var sio = socketIo.listen(server);
// Use the auth token for socket.io
sio.use(socketio_jwt.authorize({
secret: jwt_secret,
handshake: true
}));
sio.sockets.on('connection', function (socket) {
// The app namespace
var nsp = sio.of(socket.nsp.name);
// App space is used to isolate trafic between apps using the messenger service.
var appSpace = 'default';
console.info('New client connected (id=' + socket.id + ').');
// Add clients to the clients object for future use
client_data.clients.push( { id : socket.id } );
// Adds client data from the client
socket.on('init', function ( clientObj ) {
if( typeof clientObj !== 'undefined' || clientObj !== null ){
if( (typeof clientObj === 'object') || (typeof clientObj == 'string' || clientObj instanceof String) ){
// If something like PHP submits the object sometimes it's seen as a string.
if( typeof clientObj == 'string' || clientObj instanceof String ){
clientObj = JSON.parse(clientObj);
}
var newObj = client_data.clients.filter(function( obj ) {
if(obj.id === socket.id){
obj = Merge(obj, clientObj);
return obj;
}
});
client_data.clients = client_data.clients.filter(function( objj ) {
return objj.id !== socket.id;
});
// Save the connected client to the data object
client_data.clients.push( newObj[0] );
// Send the session id to the connected user for future use
var cObj = { client_id : socket.id };
sendObj = obj2 = extend({}, cObj, client_data);
sio.sockets.connected[socket.id].emit('client_init', sendObj );
//nsp.to(socket.id).emit( 'client_init', sendObj );
// Broadcast to all clients that a client connected
var connClientObj = client_data.clients.filter(function( obj ) {
return obj.id === socket.id;
});
// Join the app space
appSpace = (clientObj.app_space)? clientObj.app_space : 'default' ;
socket.join(appSpace);
if( appSpace === "default" ){
//nsp.emit('client_connected', connClientObj[0]);
socket.broadcast.emit('client_connected', connClientObj[0]);
} else {
socket.in(appSpace).emit('client_connected', connClientObj[0]);
}
} // END if
} // END if
}); // END socket.on init
// Relay messages from the sender to all other clients
socket.on('messenger', function ( obj ) {
if( typeof obj == 'string' || obj instanceof String ){
obj = JSON.parse(obj);
}
var fromData = client_data.clients.filter(function( objj ) {
return objj.id === socket.id;
});
var fromObj = { from : fromData[0] };
obj2 = extend({}, obj, fromObj);
// This sends to everyone including the sender
//io.emit('message', msg);
// This sends to everyone but the sender
if( appSpace === "default" ){
socket.broadcast.emit('messenger', obj2);
} else {
socket.in(appSpace).emit('messenger', obj2);
}
});
// Send message object to one specific client
socket.on('messenger_to', function ( obj ) {
if( typeof obj == 'string' || obj instanceof String ){
obj = JSON.parse(obj);
}
id = obj.to;
var fromData = client_data.clients.filter(function( objj ) {
return objj.id === socket.id;
});
var fromObj = { from : fromData[0] };
obj2 = extend({}, obj, fromObj);
// Check if exists
if( sio.sockets.connected[id] !== undefined ){
sio.sockets.connected[id].emit('messenger', obj2);
}
});
// Send message object to one specific client by OPRID
socket.on('messenger_to_oprid', function ( obj ) {
if( typeof obj == 'string' || obj instanceof String ){
obj = JSON.parse(obj);
}
var getClient = client_data.clients.filter(function( objj ) {
return objj.oprid.toUpperCase() === obj.oprid.toUpperCase();
});
var clientObj = getClient[0];
if( sio.sockets.connected[clientObj.id] !== undefined ){
sio.sockets.connected[clientObj.id].emit('messenger', obj);
}
});
// Client disconnection actions
socket.on('disconnect', function () {
var dis = client_data.clients.filter(function( obj ) {
return obj.id === socket.id;
});
client_data.clients = client_data.clients.filter(function( objj ) {
return objj.id !== socket.id;
});
if( appSpace === "default" ){
socket.broadcast.emit('client_disconnected', dis[0] );
} else {
socket.in(appSpace).emit('client_disconnected', dis[0] );
}
console.info('Client disconnected (id=' + socket.id + ').');
});
socket.on('error', function () {
// log error somewhere
});
}); // END on connection
// Start the server
server.listen(80, function () {
console.log('Hermes server started.');
});