-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
468 lines (437 loc) · 22.2 KB
/
main.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Set Group ID
var groupId = ''
// Websocket Reconnect
var reconnectInterval = 1000 * 5;
// Get group ID from environment
var groupIds = process.env.GROUP_ID || "";
if (groupIds.length > 0) { var groupId = groupIds.split('-')[0] }
// local only mode
const local_only = process.env.LOCAL_ONLY || false;
if (local_only && groupId.length === 0) {
throw ("Cannot run in local only mode without specifying group ID")
}
// Setup Logging
const { transports, createLogger, format } = require('winston');
const logger = createLogger({
format: format.combine(
format.timestamp(),
format.json()
),
'transports': [
new transports.Console({
level: process.env.LOG_LEVEL || 'info'
}),
new transports.File({ filename: 'error.log', level: 'error' }),
new transports.File({ filename: 'debug.log', level: 'debug' })
]
});
// Importing the required modules
const WebSocket = require('ws');
const fs = require('fs');
const https = require('https');
// Load Cert
const server = new https.createServer({
cert: fs.readFileSync('server.cert'),
key: fs.readFileSync('server.key')
});
// Server API URLS
lw_app_api_url = "wss://v1-linkplus-app.lightwaverf.com"
lw_hub_api_url = "wss://linkplus-pub-api.lightwaverf.com:443/sockets"
// Get Local IP
var ip = require("ip");
const { cli } = require('winston/lib/winston/config');
// Set up Wait
waitingResponse = [];
// Set up Websockets
webSockets = [];
// Create Hub Client
var connect_ws_lw = function () {
ws_lw = new WebSocket(lw_hub_api_url, {
// rejectUnauthorized: false
});
ws_lw.on('open', function () {
ws_lw.id = "LWHUBAPI"
AppClients.push(ws_lw)
logger.info("Hub API: Connected");
if (typeof webSockets['hub'] !== 'undefined' && webSockets['hub']) {
webSockets['hub'].close();
logger.info("Hub API: Forcing Hub to reconnect")
}
// if (hub_auth) { // If connection is re-established re-send initial hub auth
// logger.info("Hub Api: Re-authenticating with Hub API")
// logger.debug(`Hub API: sending ${hub_auth} to LW`)
// ws_lw.send(hub_auth)
// }
});
ws_lw.on('error', function (event) {
logger.error("Hub API: Socket Error");
logger.debug("Hub API:", event)
});
ws_lw.on('close', function () {
logger.error("Hub API: Socket Closed");
setTimeout(connect_ws_lw, reconnectInterval);
});
};
connect_ws_lw();
// Auth Variables
var lw_is_auth = false
var hub_auth = ""
var app_auth = ""
var connect_lw_api = function () {
ws_lw_app = new WebSocket(lw_app_api_url, {
rejectUnauthorized: false
});
ws_lw_app.on('open', function () {
ws_lw_app.id = "LWAPI"
AppClients.push(ws_lw_app)
logger.info("App API: Connected");
lw_is_auth = false
if (app_auth) { // If socket is reconnected, attempt to re-auth with saved json
logger.info("App Api: Re-authenticating with App API")
ws_lw_app.send(app_auth)
}
});
ws_lw_app.on('error', function (event) {
logger.info("App API: Socket Error");
logger.debug("App API:", event)
});
ws_lw_app.on('close', function () {
logger.info("App API: Socket Closed");
setTimeout(connect_lw_api, reconnectInterval);
});
};
connect_lw_api();
var webSockets = {} // userID: webSocket
// Creating a new websocket server
const wss = new WebSocket.Server({
server: server
})
AppClients = [];
wss.getUniqueID = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4();
};
// Creating connection using websocket
wss.on("connection", (ws, req) => {
ws.id = wss.getUniqueID();
if (req.url == '/sockets') { // if hub connects, open new connection to LW
ws_lw.addEventListener('message', function (event) { // Messages from Hub API
const messageBody = JSON.parse(event.data);
switch (messageBody.operation) {
case 'write':
logger.debug(`Hub Api: LW has sent us: ${event.data}`)
featureId = messageBody.items[0].payload.featureId
value = messageBody.items[0].payload.value
logger.info(`Hub API: LW has requested Feature ${featureId} to be set to ${value}`)
messageBody.transactionId = messageBody.items[0].itemId
sendtoHub('LWHUBAPI', messageBody)
break;
case 'read':
logger.debug(`Hub Api: LW has sent us: ${event.data}`)
featureId = messageBody.items[0].payload.featureId
logger.info(`Hub API: LW has requested read on Feature ${featureId}`)
messageBody.transactionId = messageBody.items[0].itemId
sendtoHub('LWHUBAPI', messageBody)
break;
case 'authenticate':
logger.debug(`Hub API: LW has sent us: ${event.data}`);
if (messageBody.items[0].success == true) {
logger.info("Hub API: Hub Successfully Authenticated")
}
else {
logger.error("Hub API: Hub Authentication Failed")
}
sendtoHub('LWHUBAPI', messageBody)
break;
default:
logger.info(`Hub API: LW has sent us: ${event.data}`);
sendtoHub('LWHUBAPI', messageBody)
break;
}
});
webSockets['hub'] = ws
logger.info(`Hub: New Hub Connection from ${ws._socket.remoteAddress}`);
}
if (req.url == '/') {
ws_lw_app.addEventListener('message', function (event) { // Messages from LW API
const messageBody = JSON.parse(event.data);
switch (messageBody.operation) {
case 'authenticate':
logger.debug(`App Api: LW App has sent us: ${event.data}`)
if (messageBody.items[0].success == true) { // Successfully authed with LW API
lw_is_auth = true
logger.info("App Api: Successfully Authenticated with LW")
// Lets request groups from LW rather than waiting for them (if we restart the local API, the client doesnt request them)
if (groupId.length === 0) {
var group_json = '{"class":"user","operation":"rootGroups","version":1,"senderId":"29db9beb-fb3f-475c-929c-68eaa21ea80e","transactionId":"a","direction":"request","items":[{"itemId":0,"payload":{}}]}'
sendtoLW(0, JSON.parse(group_json))
}
} else if (messageBody.items[0]["error"]["code"] == "200") {
logger.info("App Api: Already authenticated")
} else {
logger.info("App Api: Authentication Failed")
}
if (typeof waitingResponse[messageBody.transactionId] !== 'undefined') {
logger.info(`App Api: Received response from LW App for transaction ${messageBody.transactionId} sending to client ${waitingResponse[messageBody.transactionId]}`)
authClient(waitingResponse[messageBody.transactionId])
sendtoClient(waitingResponse[messageBody.transactionId], messageBody)
} else {
logger.debug(`Hub: Received unexpected response ${event.data}`)
}
break;
case 'rootGroups':
logger.debug(`App Api: LW App has sent us: ${event.data}`)
groupIds = messageBody.items[0].payload.groupIds[0]
groupId = groupIds.split('-')[0]
logger.info(`App Api: Group ID ${groupId} received`)
if (messageBody.transactionId != "a") {
if (typeof waitingResponse[messageBody.transactionId] !== 'undefined') {
logger.info(`App Api: Received response from LW App for transaction ${messageBody.transactionId} sending to client ${waitingResponse[messageBody.transactionId]}`)
sendtoClient(waitingResponse[messageBody.transactionId], messageBody)
} else {
logger.debug(`App Api: Received unexpected response ${event.data}`)
}
}
break;
case 'event': // only send the event to the clients if we don't have a groupID (shouldnt happen now)
if (groupId.length === 0) {
logger.debug(`Hub API: LW has sent us: ${event.data}`);
sendAll(messageBody)
}
break;
case 'read':
if (messageBody.class == 'group') { //response to a group request
if (messageBody.transactionId != 0) {
if (typeof waitingResponse[messageBody.transactionId] !== 'undefined') {
logger.info(`App Api: Received response from LW App for transaction ${messageBody.transactionId} sending to client ${waitingResponse[messageBody.transactionId]}`)
sendtoClient(waitingResponse[messageBody.transactionId], messageBody)
} else {
logger.debug(`App Api: Received unexpected response ${event.data}`)
}
}
}
break;
default:
if ((messageBody.items[0].success == false) && (messageBody.items[0].error.code == "200")) {
lw_is_auth = false
logger.error("App Api: Error, not authenticated with LW")
}
logger.error(`App Api: LW has sent us unhandled data: ${event.data}`);
break;
}
});
AppClients.push(ws);
logger.info(`New Application Client Connected ${ws._socket.remoteAddress} ${ws.id}`);
}
// Client sending message
ws.on("message", data => {
const messageBody = JSON.parse(data);
const operation = messageBody.operation
if (req.url == '/sockets') { // Message from Hub
logger.debug(`Hub: Hub has sent us: ${data}`)
switch (operation) {
case 'authenticate':
hub_auth = data
logger.info("Hub: Authenticating with Lightwave")
logger.debug(`Hub: Sending ${data} to LW`)
ws_lw.send(data)
break;
case 'event':
sendtoClient('LWHUBAPI', messageBody) // Send the original event to the HUB API
messageBody.items.forEach(element => {
logger.info(`Event received from Hub - Feature ${element['payload']['featureId']} - Value ${element['payload']['value']} `)
var response = {}
response['version'] = messageBody.version
response['senderId'] = messageBody.senderId
response['transactionId'] = messageBody.transactionId
response['direction'] = 'notification'
response['class'] = messageBody.class
response['operation'] = messageBody.operation
response['items'] = [];
element['payload']['featureId'] = groupId + '-' + element['payload']['featureId'] + '-' + messageBody.senderId + '+1'
element['success'] = 'true'
response['items'].push(element)
responsejson = JSON.stringify(response);
if (groupId) { // can't send event without the GroupID so we'll need to get LW to do it
sendAll(responsejson)
}
});
break;
case 'write': // response from hub for feature write
clientId = messageBody.items[0].itemId
if (typeof waitingResponse[clientId] !== 'undefined') {
logger.info(`Hub: Received response from hub for transaction ${clientId} sending to client ${waitingResponse[clientId]}`)
// HA expects the transaction ID to be the same as itemId
if (waitingResponse[clientId] != 'LWHUBAPI') { messageBody.transactionId = messageBody.items[0].itemId }
sendtoClient(waitingResponse[clientId], messageBody)
} else {
logger.debug(`Hub: Received unexpected response ${data}`)
}
break;
case 'read':
clientId = messageBody.items[0].itemId
if (typeof waitingResponse[clientId] !== 'undefined') {
logger.info(`Hub: Received response from hub for transaction ${clientId} sending to client ${waitingResponse[clientId]}`)
// HA expects the transaction ID to be the same as itemId
if (waitingResponse[clientId] != 'LWHUBAPI') { messageBody.transactionId = messageBody.items[0].itemId }
sendtoClient(waitingResponse[clientId], messageBody)
} else {
logger.debug(`Hub: Received unexpected response ${data}`)
}
break;
default:
logger.error(`Hub: Unhandled operation: ${data}`)
sendtoClient('LWHUBAPI', messageBody)
break;
}
}
if (req.url == '/') { // Message from HA
switch (operation) {
case 'authenticate': // proxy this if not local mode or already authd
logger.info('App Client: Requested Authenticate')
logger.debug(`App Client Message: ${data}`)
app_auth = data
if (lw_is_auth == false && local_only == false) {
sendtoLW(ws.id, messageBody)
} else { // tell the client it's authorised
auth_json = '{"version":1,"senderId":"1.ip=10=192=22=140*eu=west=1*compute*internal=82149","direction":"response","source":"_channel","items":[{"itemId":0,"success":true,"payload":{"workerUniqueId":"ip=10=192=22=140*eu=west=1*compute*internal=82149","serverName":"i-0b3c24bf89033f71e","handlerId":"user.7aa9ada8-9914-4f8f-9bd4-80f85593a54b.eb0d95dc-83f5-4b3c-9691-dcdbe9315987"}}],"class":"user","operation":"authenticate","transactionId":1}';
auth_json_parsed = JSON.parse(auth_json);
auth_json_parsed.transactionId = messageBody.transactionId
auth_json_parsed.items[0].itemId = messageBody.transactionId
auth_json_parsed.senderId = "1.ip=" + ip.address()
auth_json_parsed.items[0].payload.workerUniqueId = ip.address()
handlerId = auth_json_parsed.items[0].payload.handlerId
handlerId = handlerId.split(".")[0] + '.' + handlerId.split(".")[1] + '.' + messageBody.items[0].payload.clientDeviceId
auth_json_parsed.items[0].payload.handlerId = handlerId
response_json = JSON.stringify(auth_json_parsed);
logger.info('App: Sending Auth Response to App')
logger.debug(`App: Auth Response ${response_json}`)
authClient(ws.id)^
sendtoClient(ws.id, auth_json_parsed)
}
break;
case 'read': // HA requesting state
if (messageBody.class == 'feature') { // Send feature request direct to hub
logger.debug(`App: App has sent us: ${data}`)
// extract featureId from JSON
featureId = messageBody.items[0].payload.featureId.split('-')[1]
// Get the HUB Id from JSON
hub = messageBody.items[0].payload.featureId.split('-')[2].substring(0, messageBody.items[0].payload.featureId.split('-')[2].length - 2)
logger.info(`App: App has requested read on function ${featureId} Transaction ${messageBody.transactionId}`)
// Replace featureId
messageBody.items[0].payload.featureId = parseInt(featureId)
sendtoHub(ws.id, messageBody)
} else if (messageBody.class == 'group') {
sendtoLW(ws.id, messageBody)
}
break;
case 'rootGroups': // proxy this if we've not already got the group ID
if (groupId.length === 0) {
sendtoLW(ws.id, messageBody)
} else {
rootgroups_json = '{"version":1,"senderId":"1.ip=10=192=21=210*eu=west=1*compute*internal=23804","direction":"response","source":"_channel","items":[{"itemId":1,"success":true,"payload":{"groupIds":[""],"rootGroups":[{"rootGroupId":"","name":"My Group"}]}}],"class":"user","operation":"rootGroups","transactionId":1}';
rootgroups_json_parsed = JSON.parse(rootgroups_json);
rootgroups_json_parsed.transactionId = messageBody.transactionId
rootgroups_json_parsed.items[0].itemId = messageBody.transactionId
rootgroups_json_parsed.senderId = "1.ip=" + ip.address()
rootgroups_json_parsed.items[0].payload.groupIds[0] = groupIds
rootgroups_json_parsed.items[0].payload.rootGroups[0].rootGroupId = groupIds
response_json = JSON.stringify(rootgroups_json_parsed);
logger.info('App: Sending Root Groups to App')
logger.debug(`App: Root Groups ${response_json}`)
sendtoClient(ws.id, rootgroups_json_parsed)
}
break;
case 'write': // send direct to hub, bypassing Lightwave
// get Hub ID from feature
hub = messageBody.items[0].payload.featureId.split('-')[2].substring(0, messageBody.items[0].payload.featureId.split('-')[2].length - 2)
message = messageBody
// Only send the feature ID, not the groups or hub ID
message.items[0].payload.featureId = parseInt(message.items[0].payload.featureId.split("-")[1])
// We don't want to send the request if the hub isnt connected
sendtoHub(ws.id, message)
logger.info(`App: App has requested write on feature ${message.items[0].payload.featureId} Transaction ${messageBody.transactionId}`)
logger.debug(`App: App has sent us: ${data}`)
break;
default:
logger.info(`App: App has sent us: ${data}`)
break;
}
}
});
// handling what to do when clients disconnects from server
ws.on("close", () => {
logger.info(`Client ${ws.id} has disconnected`);
RemoveClient(ws.id)
});
// handling client connection error
ws.onerror = function () {
logger.info("Some Error occurred")
}
});
server.listen(443);
logger.info(`The WebSocket server is running on port ${server.address().port}`)
function RemoveClient(clientId) {
if (webSockets['hub'].id == clientId) {
webSockets = []
} else {
for (var i = 0; i < AppClients.length; i++) {
if (AppClients[i].id == clientId) {
AppClients.splice(i, 1)
}
}
}
}
function authClient(clientId) {
var success = false
for (var i = 0; i < AppClients.length; i++) {
if (AppClients[i].id == clientId) {
logger.debug(`Client: Marking ${clientId} as auth'd`)
AppClients[i].is_auth = true;
success = true
}
}
if (success != true) {
logger.error(`App: Client with ID ${clientId} not found`)
}
}
function sendAll(message) {
for (var i = 0; i < AppClients.length; i++) {
if ((AppClients[i].id != 'LWAPI') && (AppClients[i].id != 'LWHUBAPI') && (AppClients[i].is_auth == true)) {
logger.debug(`Broadcast: Sending ${message} to ${AppClients[i].id}`)
AppClients[i].send(message);
}
}
}
function sendtoClient(clientId, message) {
var success = false
for (var i = 0; i < AppClients.length; i++) {
if (AppClients[i].id == clientId) {
logger.debug(`Client: Sending ${JSON.stringify(message)} to ${clientId}`)
AppClients[i].send(JSON.stringify(message));
success = true
}
}
if (success != true) {
logger.error(`App: Client with ID ${clientId} not found`)
}
}
function sendtoLW(clientId, message) {
waitingResponse[message.transactionId] = clientId
sendtoClient("LWAPI", message)
}
function sendtoHub(clientId, message) {
waitingResponse[message.transactionId] = clientId
if (webSockets['hub']) {
logger.debug(`Hub: Sending ${JSON.stringify(message)} to ${clientId}`)
webSockets['hub'].send(JSON.stringify(message))
} else {
logger.error("Hub: Error Hub not connected")
}
}
process.on('SIGINT', function () {
logger.info("Caught interrupt signal");
process.exit();
});