forked from homebridge/HAP-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSession.js
137 lines (128 loc) · 4.46 KB
/
Session.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
var encryption = require("./Encryption.js");
var hkdf = require("node-hkdf");
var net = require('net');
var server_fac = require("./Server.js");
HAPSession.prototype = {
updateEncryptTrafficStatus: function updateEncryptTrafficStatus() {
if (this.shouldEncrypt) {
this.isEncryptInUse = true;
}
},
sendHAPEvent: function sendHAPEvent(data) {
if (this.isEncryptInUse && (this.socket !== undefined)) {
var resultMsg = encryption.layer_encrypt(data,this.accessoryToControllerCount,this.accessoryToControllerKey);
try {
this.socket.write(resultMsg);
} catch (ex) {
console.log("An Error Occured when sending events,",ex);
}
}
},
setupSession: function setupSession(socket, hapPort) {
console.log("New Session", socket.remotePort);
//From HAP Server
var serviceSocket = new net.Socket();
serviceSocket.connect(parseInt(hapPort), "127.0.0.1", function () {
console.log("Server Connection Established", serviceSocket.localPort);
this.localPort = serviceSocket.localPort;
this.registerCallback(socket.remotePort, serviceSocket.localPort);
}.bind(this));
serviceSocket.on("data", function (data) {
if (this.isEncryptInUse) {
var resultMsg = encryption.layer_encrypt(data,this.accessoryToControllerCount,this.accessoryToControllerKey);
try {
socket.write(resultMsg);
} catch (ex) {
console.log("An Error Occured when sending data from server,",ex);
}
} else {
try {
socket.write(data);
} catch (ex) {
console.log("An Error Occured when sending data from server,",ex);
}
}
}.bind(this));
serviceSocket.on("close", function() {
console.log('Server Disconnected');
this.tcpServer.removeSession(this.localPort);
try {
socket.end();
} catch (ex) {
console.log("An Error Occured when closing the socket",ex);
}
this.socket = undefined;
}.bind(this));
serviceSocket.on("error", function(err){
console.log("An Error Occured on HAP side connection,",err);
this.tcpServer.removeSession(this.localPort);
socket.destroy();
serviceSocket.destroy();
}.bind(this));
//From iOS
socket.on('data', function (msg) {
this.updateEncryptTrafficStatus();
if (this.isEncryptInUse) {
var resultMsg = encryption.layer_decrypt(msg,this.controllerToAccessoryCount,this.controllerToAccessoryKey);
try {
serviceSocket.write(resultMsg);
} catch (ex) {
console.log("An Error Occured when sending data from server,",ex);
}
} else {
try {
serviceSocket.write(msg);
} catch (ex) {
console.log("An Error Occured when sending data from server,",ex);
}
}
}.bind(this));
socket.on("close", function() {
console.log('Client Disconnected');
this.tcpServer.removeSession(this.localPort);
try {
serviceSocket.end();
} catch (ex) {
console.log("An Error Occured when closing socket to HAP Server,",ex);
}
this.socket = undefined;
}.bind(this));
socket.on("error", function(err){
console.log("An Error Occured on client side connection,",err);
this.tcpServer.removeSession(this.localPort);
socket.destroy();
serviceSocket.destroy();
}.bind(this));
},
enableEncryptionForSession: function enableEncryptionForSession(sharedSec) {
this.shouldEncrypt = true;
this.sharedSec = sharedSec;
this.accessoryToControllerCount = {Value: 0};
this.controllerToAccessoryCount = {Value: 0};
var enc_salt = new Buffer("Control-Salt");
var info_read = new Buffer("Control-Read-Encryption-Key");
var info_write = new Buffer("Control-Write-Encryption-Key");
var output_key_read = hkdf.HKDF("sha512",enc_salt,sharedSec,info_read,32);
this.accessoryToControllerKey = output_key_read;
var output_key_write = hkdf.HKDF("sha512",enc_salt,sharedSec,info_write,32);
this.controllerToAccessoryKey = output_key_write;
}
}
function HAPSession(tcpServer, socket, hapPort, persistStore, accessoryInfo, callback) {
if (!(this instanceof HAPSession)) {
return new HAPSession(tcpServer, socket, persistStore, accessoryInfo, callback);
}
this.tcpServer = tcpServer;
this.socket = socket;
this.persistStore = persistStore;
this.shouldEncrypt = false;
this.isEncryptInUse = false;
this.registerCallback = callback;
this.setupSession(socket, hapPort);
}
function randomInt (low, high) {
return Math.floor(Math.random() * (high - low) + low);
}
module.exports = {
HAPSession: HAPSession
}