@homebridge/hap-nodejs 2.2.4-beta.7 → 2.3.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -17
- package/dist/accessories/AirConditioner_accessory.js +27 -29
- package/dist/accessories/AirConditioner_accessory.js.map +1 -1
- package/dist/accessories/AppleTVRemote_accessory.js +63 -76
- package/dist/accessories/AppleTVRemote_accessory.js.map +1 -1
- package/dist/accessories/Camera_accessory.js +151 -134
- package/dist/accessories/Camera_accessory.js.map +1 -1
- package/dist/accessories/Fan_accessory.js +18 -21
- package/dist/accessories/Fan_accessory.js.map +1 -1
- package/dist/accessories/GarageDoorOpener_accessory.js +33 -35
- package/dist/accessories/GarageDoorOpener_accessory.js.map +1 -1
- package/dist/accessories/Light-AdaptiveLighting_accessory.js +41 -44
- package/dist/accessories/Light-AdaptiveLighting_accessory.js.map +1 -1
- package/dist/accessories/Light_accessory.js +32 -34
- package/dist/accessories/Light_accessory.js.map +1 -1
- package/dist/accessories/Lock_accessory.js +24 -26
- package/dist/accessories/Lock_accessory.js.map +1 -1
- package/dist/accessories/MotionSensor_accessory.js +13 -15
- package/dist/accessories/MotionSensor_accessory.js.map +1 -1
- package/dist/accessories/Outlet_accessory.js +20 -22
- package/dist/accessories/Outlet_accessory.js.map +1 -1
- package/dist/accessories/SmartSpeaker_accessory.js +18 -20
- package/dist/accessories/SmartSpeaker_accessory.js.map +1 -1
- package/dist/accessories/Sprinkler_accessory.js +36 -38
- package/dist/accessories/Sprinkler_accessory.js.map +1 -1
- package/dist/accessories/TV_accessory.js +43 -45
- package/dist/accessories/TV_accessory.js.map +1 -1
- package/dist/accessories/TemperatureSensor_accessory.js +12 -14
- package/dist/accessories/TemperatureSensor_accessory.js.map +1 -1
- package/dist/accessories/Wi-FiRouter_accessory.d.ts +1 -1
- package/dist/accessories/Wi-FiRouter_accessory.d.ts.map +1 -1
- package/dist/accessories/Wi-FiRouter_accessory.js +9 -12
- package/dist/accessories/Wi-FiRouter_accessory.js.map +1 -1
- package/dist/accessories/Wi-FiSatellite_accessory.d.ts +1 -1
- package/dist/accessories/Wi-FiSatellite_accessory.d.ts.map +1 -1
- package/dist/accessories/Wi-FiSatellite_accessory.js +11 -14
- package/dist/accessories/Wi-FiSatellite_accessory.js.map +1 -1
- package/dist/accessories/gstreamer-audioProducer.d.ts +3 -3
- package/dist/accessories/gstreamer-audioProducer.d.ts.map +1 -1
- package/dist/accessories/gstreamer-audioProducer.js +38 -38
- package/dist/accessories/gstreamer-audioProducer.js.map +1 -1
- package/dist/accessories/types.d.ts +63 -63
- package/dist/accessories/types.d.ts.map +1 -1
- package/dist/accessories/types.js +83 -87
- package/dist/accessories/types.js.map +1 -1
- package/dist/hap.d.ts +50 -0
- package/dist/hap.d.ts.map +1 -0
- package/dist/hap.js +49 -0
- package/dist/hap.js.map +1 -0
- package/dist/index.d.ts +3 -49
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -53
- package/dist/index.js.map +1 -1
- package/dist/internal-types.d.ts +1 -1
- package/dist/internal-types.d.ts.map +1 -1
- package/dist/internal-types.js +19 -19
- package/dist/internal-types.js.map +1 -1
- package/dist/lib/Accessory.d.ts +37 -37
- package/dist/lib/Accessory.d.ts.map +1 -1
- package/dist/lib/Accessory.js +286 -280
- package/dist/lib/Accessory.js.map +1 -1
- package/dist/lib/Advertiser.d.ts +6 -6
- package/dist/lib/Advertiser.d.ts.map +1 -1
- package/dist/lib/Advertiser.js +143 -139
- package/dist/lib/Advertiser.js.map +1 -1
- package/dist/lib/Bridge.d.ts +1 -1
- package/dist/lib/Bridge.d.ts.map +1 -1
- package/dist/lib/Bridge.js +2 -6
- package/dist/lib/Bridge.js.map +1 -1
- package/dist/lib/Characteristic.d.ts +40 -41
- package/dist/lib/Characteristic.d.ts.map +1 -1
- package/dist/lib/Characteristic.js +290 -240
- package/dist/lib/Characteristic.js.map +1 -1
- package/dist/lib/HAPServer.d.ts +31 -30
- package/dist/lib/HAPServer.d.ts.map +1 -1
- package/dist/lib/HAPServer.js +245 -247
- package/dist/lib/HAPServer.js.map +1 -1
- package/dist/lib/Service.d.ts +21 -22
- package/dist/lib/Service.d.ts.map +1 -1
- package/dist/lib/Service.js +60 -68
- package/dist/lib/Service.js.map +1 -1
- package/dist/lib/camera/RTPProxy.d.ts +2 -1
- package/dist/lib/camera/RTPProxy.d.ts.map +1 -1
- package/dist/lib/camera/RTPProxy.js +35 -37
- package/dist/lib/camera/RTPProxy.js.map +1 -1
- package/dist/lib/camera/RTPStreamManagement.d.ts +34 -33
- package/dist/lib/camera/RTPStreamManagement.d.ts.map +1 -1
- package/dist/lib/camera/RTPStreamManagement.js +184 -137
- package/dist/lib/camera/RTPStreamManagement.js.map +1 -1
- package/dist/lib/camera/RecordingManagement.d.ts +18 -17
- package/dist/lib/camera/RecordingManagement.d.ts.map +1 -1
- package/dist/lib/camera/RecordingManagement.js +189 -164
- package/dist/lib/camera/RecordingManagement.js.map +1 -1
- package/dist/lib/camera/index.d.ts +3 -3
- package/dist/lib/camera/index.d.ts.map +1 -1
- package/dist/lib/camera/index.js +3 -6
- package/dist/lib/camera/index.js.map +1 -1
- package/dist/lib/controller/AdaptiveLightingController.d.ts +8 -8
- package/dist/lib/controller/AdaptiveLightingController.d.ts.map +1 -1
- package/dist/lib/controller/AdaptiveLightingController.js +112 -99
- package/dist/lib/controller/AdaptiveLightingController.js.map +1 -1
- package/dist/lib/controller/CameraController.d.ts +21 -20
- package/dist/lib/controller/CameraController.d.ts.map +1 -1
- package/dist/lib/controller/CameraController.js +92 -89
- package/dist/lib/controller/CameraController.js.map +1 -1
- package/dist/lib/controller/Controller.d.ts +1 -1
- package/dist/lib/controller/Controller.d.ts.map +1 -1
- package/dist/lib/controller/Controller.js +5 -8
- package/dist/lib/controller/Controller.js.map +1 -1
- package/dist/lib/controller/DoorbellController.d.ts +4 -4
- package/dist/lib/controller/DoorbellController.d.ts.map +1 -1
- package/dist/lib/controller/DoorbellController.js +9 -13
- package/dist/lib/controller/DoorbellController.js.map +1 -1
- package/dist/lib/controller/RemoteController.d.ts +40 -39
- package/dist/lib/controller/RemoteController.d.ts.map +1 -1
- package/dist/lib/controller/RemoteController.js +213 -186
- package/dist/lib/controller/RemoteController.js.map +1 -1
- package/dist/lib/controller/index.d.ts +5 -5
- package/dist/lib/controller/index.d.ts.map +1 -1
- package/dist/lib/controller/index.js +5 -8
- package/dist/lib/controller/index.js.map +1 -1
- package/dist/lib/datastream/DataStreamManagement.d.ts +2 -2
- package/dist/lib/datastream/DataStreamManagement.d.ts.map +1 -1
- package/dist/lib/datastream/DataStreamManagement.js +36 -35
- package/dist/lib/datastream/DataStreamManagement.js.map +1 -1
- package/dist/lib/datastream/DataStreamParser.d.ts +1 -0
- package/dist/lib/datastream/DataStreamParser.d.ts.map +1 -1
- package/dist/lib/datastream/DataStreamParser.js +53 -69
- package/dist/lib/datastream/DataStreamParser.js.map +1 -1
- package/dist/lib/datastream/DataStreamServer.d.ts +24 -23
- package/dist/lib/datastream/DataStreamServer.d.ts.map +1 -1
- package/dist/lib/datastream/DataStreamServer.js +151 -154
- package/dist/lib/datastream/DataStreamServer.js.map +1 -1
- package/dist/lib/datastream/index.d.ts +3 -3
- package/dist/lib/datastream/index.d.ts.map +1 -1
- package/dist/lib/datastream/index.js +3 -6
- package/dist/lib/datastream/index.js.map +1 -1
- package/dist/lib/definitions/CharacteristicDefinitions.d.ts +1 -1
- package/dist/lib/definitions/CharacteristicDefinitions.d.ts.map +1 -1
- package/dist/lib/definitions/CharacteristicDefinitions.js +1103 -1288
- package/dist/lib/definitions/CharacteristicDefinitions.js.map +1 -1
- package/dist/lib/definitions/ServiceDefinitions.d.ts +1 -1
- package/dist/lib/definitions/ServiceDefinitions.d.ts.map +1 -1
- package/dist/lib/definitions/ServiceDefinitions.js +644 -722
- package/dist/lib/definitions/ServiceDefinitions.js.map +1 -1
- package/dist/lib/definitions/generate-definitions.d.ts +2 -2
- package/dist/lib/definitions/generate-definitions.d.ts.map +1 -1
- package/dist/lib/definitions/generate-definitions.js +241 -241
- package/dist/lib/definitions/generate-definitions.js.map +1 -1
- package/dist/lib/definitions/generator-configuration.d.ts +1 -1
- package/dist/lib/definitions/generator-configuration.d.ts.map +1 -1
- package/dist/lib/definitions/generator-configuration.js +572 -309
- package/dist/lib/definitions/generator-configuration.js.map +1 -1
- package/dist/lib/definitions/index.d.ts +2 -2
- package/dist/lib/definitions/index.d.ts.map +1 -1
- package/dist/lib/definitions/index.js +2 -5
- package/dist/lib/definitions/index.js.map +1 -1
- package/dist/lib/gen/HomeKit.js +1 -2
- package/dist/lib/gen/HomeKit.js.map +1 -1
- package/dist/lib/model/AccessoryInfo.d.ts +4 -3
- package/dist/lib/model/AccessoryInfo.d.ts.map +1 -1
- package/dist/lib/model/AccessoryInfo.js +53 -56
- package/dist/lib/model/AccessoryInfo.js.map +1 -1
- package/dist/lib/model/ControllerStorage.d.ts +3 -3
- package/dist/lib/model/ControllerStorage.d.ts.map +1 -1
- package/dist/lib/model/ControllerStorage.js +31 -30
- package/dist/lib/model/ControllerStorage.js.map +1 -1
- package/dist/lib/model/HAPFileStorage.d.ts.map +1 -1
- package/dist/lib/model/HAPFileStorage.js +71 -50
- package/dist/lib/model/HAPFileStorage.js.map +1 -1
- package/dist/lib/model/HAPStorage.d.ts +1 -1
- package/dist/lib/model/HAPStorage.d.ts.map +1 -1
- package/dist/lib/model/HAPStorage.js +11 -14
- package/dist/lib/model/HAPStorage.js.map +1 -1
- package/dist/lib/model/IdentifierCache.d.ts +1 -1
- package/dist/lib/model/IdentifierCache.d.ts.map +1 -1
- package/dist/lib/model/IdentifierCache.js +23 -27
- package/dist/lib/model/IdentifierCache.js.map +1 -1
- package/dist/lib/tv/AccessControlManagement.d.ts +6 -6
- package/dist/lib/tv/AccessControlManagement.d.ts.map +1 -1
- package/dist/lib/tv/AccessControlManagement.js +23 -24
- package/dist/lib/tv/AccessControlManagement.js.map +1 -1
- package/dist/lib/util/checkName.d.ts +2 -2
- package/dist/lib/util/checkName.d.ts.map +1 -1
- package/dist/lib/util/checkName.js +8 -14
- package/dist/lib/util/checkName.js.map +1 -1
- package/dist/lib/util/clone.d.ts.map +1 -1
- package/dist/lib/util/clone.js +1 -5
- package/dist/lib/util/clone.js.map +1 -1
- package/dist/lib/util/color-utils.d.ts +1 -1
- package/dist/lib/util/color-utils.d.ts.map +1 -1
- package/dist/lib/util/color-utils.js +1608 -410
- package/dist/lib/util/color-utils.js.map +1 -1
- package/dist/lib/util/eventedhttp.d.ts +23 -22
- package/dist/lib/util/eventedhttp.d.ts.map +1 -1
- package/dist/lib/util/eventedhttp.js +127 -122
- package/dist/lib/util/eventedhttp.js.map +1 -1
- package/dist/lib/util/hapCrypto.d.ts +2 -1
- package/dist/lib/util/hapCrypto.d.ts.map +1 -1
- package/dist/lib/util/hapCrypto.js +72 -59
- package/dist/lib/util/hapCrypto.js.map +1 -1
- package/dist/lib/util/hapStatusError.d.ts +1 -1
- package/dist/lib/util/hapStatusError.d.ts.map +1 -1
- package/dist/lib/util/hapStatusError.js +5 -9
- package/dist/lib/util/hapStatusError.js.map +1 -1
- package/dist/lib/util/net-utils.js +13 -19
- package/dist/lib/util/net-utils.js.map +1 -1
- package/dist/lib/util/once.js +3 -6
- package/dist/lib/util/once.js.map +1 -1
- package/dist/lib/util/promise-utils.d.ts +1 -1
- package/dist/lib/util/promise-utils.d.ts.map +1 -1
- package/dist/lib/util/promise-utils.js +9 -11
- package/dist/lib/util/promise-utils.js.map +1 -1
- package/dist/lib/util/request-util.d.ts +3 -3
- package/dist/lib/util/request-util.d.ts.map +1 -1
- package/dist/lib/util/request-util.js +14 -23
- package/dist/lib/util/request-util.js.map +1 -1
- package/dist/lib/util/time.d.ts +1 -0
- package/dist/lib/util/time.d.ts.map +1 -1
- package/dist/lib/util/time.js +6 -11
- package/dist/lib/util/time.js.map +1 -1
- package/dist/lib/util/tlv.d.ts +1 -0
- package/dist/lib/util/tlv.d.ts.map +1 -1
- package/dist/lib/util/tlv.js +38 -44
- package/dist/lib/util/tlv.js.map +1 -1
- package/dist/lib/util/uuid.d.ts +1 -0
- package/dist/lib/util/uuid.d.ts.map +1 -1
- package/dist/lib/util/uuid.js +26 -38
- package/dist/lib/util/uuid.js.map +1 -1
- package/dist/types.d.ts +24 -24
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +3 -5
- package/dist/types.js.map +1 -1
- package/package.json +49 -42
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
const DataStreamParser_1 = require("./DataStreamParser");
|
|
12
|
-
const debug = (0, debug_1.default)("HAP-NodeJS:DataStream:Server");
|
|
1
|
+
/* global NodeJS */
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import { Buffer } from 'node:buffer';
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
import { EventEmitter, EventEmitter as NodeEventEmitter } from 'node:events';
|
|
6
|
+
import net from 'node:net';
|
|
7
|
+
import createDebug from 'debug';
|
|
8
|
+
import * as hapCrypto from '../util/hapCrypto.js';
|
|
9
|
+
import { DataStreamParser, DataStreamReader, DataStreamWriter, Int64 } from './DataStreamParser.js';
|
|
10
|
+
const debug = createDebug('HAP-NodeJS:DataStream:Server');
|
|
13
11
|
/**
|
|
14
12
|
* @group HomeKit Data Streams (HDS)
|
|
15
13
|
*/
|
|
16
|
-
|
|
14
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
15
|
+
export var Protocols;
|
|
17
16
|
(function (Protocols) {
|
|
18
17
|
Protocols["CONTROL"] = "control";
|
|
19
18
|
Protocols["TARGET_CONTROL"] = "targetControl";
|
|
20
19
|
Protocols["DATA_SEND"] = "dataSend";
|
|
21
|
-
})(Protocols || (
|
|
20
|
+
})(Protocols || (Protocols = {}));
|
|
22
21
|
/**
|
|
23
22
|
* @group HomeKit Data Streams (HDS)
|
|
24
23
|
*/
|
|
25
|
-
|
|
24
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
25
|
+
export var Topics;
|
|
26
26
|
(function (Topics) {
|
|
27
27
|
// control
|
|
28
28
|
Topics["HELLO"] = "hello";
|
|
@@ -33,11 +33,11 @@ var Topics;
|
|
|
33
33
|
Topics["DATA"] = "data";
|
|
34
34
|
Topics["ACK"] = "ack";
|
|
35
35
|
Topics["CLOSE"] = "close";
|
|
36
|
-
})(Topics || (
|
|
36
|
+
})(Topics || (Topics = {}));
|
|
37
37
|
/**
|
|
38
38
|
* @group HomeKit Data Streams (HDS)
|
|
39
39
|
*/
|
|
40
|
-
var HDSStatus;
|
|
40
|
+
export var HDSStatus;
|
|
41
41
|
(function (HDSStatus) {
|
|
42
42
|
// noinspection JSUnusedGlobalSymbols
|
|
43
43
|
HDSStatus[HDSStatus["SUCCESS"] = 0] = "SUCCESS";
|
|
@@ -47,11 +47,11 @@ var HDSStatus;
|
|
|
47
47
|
HDSStatus[HDSStatus["PAYLOAD_ERROR"] = 4] = "PAYLOAD_ERROR";
|
|
48
48
|
HDSStatus[HDSStatus["MISSING_PROTOCOL"] = 5] = "MISSING_PROTOCOL";
|
|
49
49
|
HDSStatus[HDSStatus["PROTOCOL_SPECIFIC_ERROR"] = 6] = "PROTOCOL_SPECIFIC_ERROR";
|
|
50
|
-
})(HDSStatus || (
|
|
50
|
+
})(HDSStatus || (HDSStatus = {}));
|
|
51
51
|
/**
|
|
52
52
|
* @group HomeKit Data Streams (HDS)
|
|
53
53
|
*/
|
|
54
|
-
var HDSProtocolSpecificErrorReason;
|
|
54
|
+
export var HDSProtocolSpecificErrorReason;
|
|
55
55
|
(function (HDSProtocolSpecificErrorReason) {
|
|
56
56
|
// noinspection JSUnusedGlobalSymbols
|
|
57
57
|
HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["NORMAL"] = 0] = "NORMAL";
|
|
@@ -64,26 +64,26 @@ var HDSProtocolSpecificErrorReason;
|
|
|
64
64
|
HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["BAD_DATA"] = 7] = "BAD_DATA";
|
|
65
65
|
HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["PROTOCOL_ERROR"] = 8] = "PROTOCOL_ERROR";
|
|
66
66
|
HDSProtocolSpecificErrorReason[HDSProtocolSpecificErrorReason["INVALID_CONFIGURATION"] = 9] = "INVALID_CONFIGURATION";
|
|
67
|
-
})(HDSProtocolSpecificErrorReason || (
|
|
67
|
+
})(HDSProtocolSpecificErrorReason || (HDSProtocolSpecificErrorReason = {}));
|
|
68
68
|
/**
|
|
69
69
|
* An error indicating a protocol level HDS error.
|
|
70
70
|
* E.g. it may be used to encode a {@link HDSStatus.PROTOCOL_SPECIFIC_ERROR} in the {@link Protocols.DATA_SEND} protocol.
|
|
71
71
|
* @group HomeKit Data Streams (HDS)
|
|
72
72
|
*/
|
|
73
|
-
class HDSProtocolError extends Error {
|
|
73
|
+
export class HDSProtocolError extends Error {
|
|
74
74
|
reason;
|
|
75
75
|
/**
|
|
76
76
|
* Initializes a new `HDSProtocolError`
|
|
77
77
|
* @param reason - The {@link HDSProtocolSpecificErrorReason}.
|
|
78
|
-
*
|
|
78
|
+
* Values MUST NOT be {@link HDSProtocolSpecificErrorReason.NORMAL}.
|
|
79
79
|
*/
|
|
80
80
|
constructor(reason) {
|
|
81
|
-
super(
|
|
82
|
-
(
|
|
81
|
+
super(`HDSProtocolError: ${reason}`);
|
|
82
|
+
assert(reason !== HDSProtocolSpecificErrorReason.NORMAL, 'Cannot initialize a HDSProtocolError with NORMAL!');
|
|
83
83
|
this.reason = reason;
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
|
-
|
|
86
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
87
87
|
var ServerState;
|
|
88
88
|
(function (ServerState) {
|
|
89
89
|
ServerState[ServerState["UNINITIALIZED"] = 0] = "UNINITIALIZED";
|
|
@@ -91,6 +91,7 @@ var ServerState;
|
|
|
91
91
|
ServerState[ServerState["LISTENING"] = 2] = "LISTENING";
|
|
92
92
|
ServerState[ServerState["CLOSING"] = 3] = "CLOSING";
|
|
93
93
|
})(ServerState || (ServerState = {}));
|
|
94
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
94
95
|
var ConnectionState;
|
|
95
96
|
(function (ConnectionState) {
|
|
96
97
|
ConnectionState[ConnectionState["UNIDENTIFIED"] = 0] = "UNIDENTIFIED";
|
|
@@ -102,16 +103,18 @@ var ConnectionState;
|
|
|
102
103
|
/**
|
|
103
104
|
* @group HomeKit Data Streams (HDS)
|
|
104
105
|
*/
|
|
105
|
-
|
|
106
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
107
|
+
export var MessageType;
|
|
106
108
|
(function (MessageType) {
|
|
107
109
|
MessageType[MessageType["EVENT"] = 1] = "EVENT";
|
|
108
110
|
MessageType[MessageType["REQUEST"] = 2] = "REQUEST";
|
|
109
111
|
MessageType[MessageType["RESPONSE"] = 3] = "RESPONSE";
|
|
110
|
-
})(MessageType || (
|
|
112
|
+
})(MessageType || (MessageType = {}));
|
|
111
113
|
/**
|
|
112
114
|
* @group HomeKit Data Streams (HDS)
|
|
113
115
|
*/
|
|
114
|
-
|
|
116
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
117
|
+
export var DataStreamServerEvent;
|
|
115
118
|
(function (DataStreamServerEvent) {
|
|
116
119
|
/**
|
|
117
120
|
* This event is emitted when a new client socket is received. At this point we have no idea to what
|
|
@@ -122,23 +125,23 @@ var DataStreamServerEvent;
|
|
|
122
125
|
* This event is emitted when the socket of a connection gets closed.
|
|
123
126
|
*/
|
|
124
127
|
DataStreamServerEvent["CONNECTION_CLOSED"] = "connection-closed";
|
|
125
|
-
})(DataStreamServerEvent || (
|
|
128
|
+
})(DataStreamServerEvent || (DataStreamServerEvent = {}));
|
|
126
129
|
/**
|
|
127
130
|
* DataStreamServer which listens for incoming tcp connections and handles identification of new connections
|
|
128
131
|
* @group HomeKit Data Streams (HDS)
|
|
129
132
|
*/
|
|
130
|
-
// eslint-disable-next-line
|
|
131
|
-
class DataStreamServer extends
|
|
132
|
-
static version =
|
|
133
|
+
// eslint-disable-next-line ts/no-unsafe-declaration-merging
|
|
134
|
+
export class DataStreamServer extends EventEmitter {
|
|
135
|
+
static version = '1.0';
|
|
133
136
|
state = 0 /* ServerState.UNINITIALIZED */;
|
|
134
|
-
static accessoryToControllerInfo = Buffer.from(
|
|
135
|
-
static controllerToAccessoryInfo = Buffer.from(
|
|
137
|
+
static accessoryToControllerInfo = Buffer.from('HDS-Read-Encryption-Key');
|
|
138
|
+
static controllerToAccessoryInfo = Buffer.from('HDS-Write-Encryption-Key');
|
|
136
139
|
tcpServer;
|
|
137
140
|
tcpPort;
|
|
138
141
|
preparedSessions = [];
|
|
139
142
|
connections = [];
|
|
140
143
|
removeListenersOnceClosed = false;
|
|
141
|
-
internalEventEmitter = new
|
|
144
|
+
internalEventEmitter = new NodeEventEmitter(); // used for message event and message request handlers
|
|
142
145
|
constructor() {
|
|
143
146
|
super();
|
|
144
147
|
}
|
|
@@ -152,7 +155,7 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
152
155
|
* @param handler - function to be called for every occurring event
|
|
153
156
|
*/
|
|
154
157
|
onEventMessage(protocol, event, handler) {
|
|
155
|
-
this.internalEventEmitter.on(protocol
|
|
158
|
+
this.internalEventEmitter.on(`${protocol}-e-${event}`, handler);
|
|
156
159
|
return this;
|
|
157
160
|
}
|
|
158
161
|
/**
|
|
@@ -163,7 +166,7 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
163
166
|
* @param handler - registered event handler
|
|
164
167
|
*/
|
|
165
168
|
removeEventHandler(protocol, event, handler) {
|
|
166
|
-
this.internalEventEmitter.removeListener(protocol
|
|
169
|
+
this.internalEventEmitter.removeListener(`${protocol}-e-${event}`, handler);
|
|
167
170
|
return this;
|
|
168
171
|
}
|
|
169
172
|
/**
|
|
@@ -176,7 +179,7 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
176
179
|
* @param handler - function to be called for every occurring request
|
|
177
180
|
*/
|
|
178
181
|
onRequestMessage(protocol, request, handler) {
|
|
179
|
-
this.internalEventEmitter.on(protocol
|
|
182
|
+
this.internalEventEmitter.on(`${protocol}-r-${request}`, handler);
|
|
180
183
|
return this;
|
|
181
184
|
}
|
|
182
185
|
/**
|
|
@@ -187,20 +190,20 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
187
190
|
* @param handler - registered request handler
|
|
188
191
|
*/
|
|
189
192
|
removeRequestHandler(protocol, request, handler) {
|
|
190
|
-
this.internalEventEmitter.removeListener(protocol
|
|
193
|
+
this.internalEventEmitter.removeListener(`${protocol}-r-${request}`, handler);
|
|
191
194
|
return this;
|
|
192
195
|
}
|
|
193
196
|
prepareSession(connection, controllerKeySalt, callback) {
|
|
194
|
-
debug(
|
|
195
|
-
const accessoryKeySalt =
|
|
197
|
+
debug('Preparing for incoming HDS connection from %s', connection.sessionID);
|
|
198
|
+
const accessoryKeySalt = crypto.randomBytes(32);
|
|
196
199
|
const salt = Buffer.concat([controllerKeySalt, accessoryKeySalt]);
|
|
197
|
-
const accessoryToControllerEncryptionKey = hapCrypto.HKDF(
|
|
198
|
-
const controllerToAccessoryEncryptionKey = hapCrypto.HKDF(
|
|
200
|
+
const accessoryToControllerEncryptionKey = hapCrypto.HKDF('sha512', salt, connection.encryption.sharedSecret, DataStreamServer.accessoryToControllerInfo, 32);
|
|
201
|
+
const controllerToAccessoryEncryptionKey = hapCrypto.HKDF('sha512', salt, connection.encryption.sharedSecret, DataStreamServer.controllerToAccessoryInfo, 32);
|
|
199
202
|
const preparedSession = {
|
|
200
|
-
connection
|
|
201
|
-
accessoryToControllerEncryptionKey
|
|
202
|
-
controllerToAccessoryEncryptionKey
|
|
203
|
-
accessoryKeySalt
|
|
203
|
+
connection,
|
|
204
|
+
accessoryToControllerEncryptionKey,
|
|
205
|
+
controllerToAccessoryEncryptionKey,
|
|
206
|
+
accessoryKeySalt,
|
|
204
207
|
connectTimeout: setTimeout(() => this.timeoutPreparedSession(preparedSession), 10000),
|
|
205
208
|
};
|
|
206
209
|
preparedSession.connectTimeout.unref();
|
|
@@ -215,7 +218,7 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
215
218
|
});
|
|
216
219
|
}
|
|
217
220
|
timeoutPreparedSession(preparedSession) {
|
|
218
|
-
debug(
|
|
221
|
+
debug('Prepared HDS session timed out out since no connection was opened for 10 seconds (%s)', preparedSession.connection.sessionID);
|
|
219
222
|
const index = this.preparedSessions.indexOf(preparedSession);
|
|
220
223
|
if (index >= 0) {
|
|
221
224
|
this.preparedSessions.splice(index, 1);
|
|
@@ -225,42 +228,42 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
225
228
|
checkTCPServerEstablished(preparedSession, callback) {
|
|
226
229
|
switch (this.state) {
|
|
227
230
|
case 0 /* ServerState.UNINITIALIZED */:
|
|
228
|
-
debug(
|
|
229
|
-
this.tcpServer =
|
|
230
|
-
this.tcpServer.once(
|
|
231
|
-
this.tcpServer.on(
|
|
232
|
-
this.tcpServer.on(
|
|
231
|
+
debug('Starting up TCP server.');
|
|
232
|
+
this.tcpServer = net.createServer();
|
|
233
|
+
this.tcpServer.once('listening', this.listening.bind(this, preparedSession, callback));
|
|
234
|
+
this.tcpServer.on('connection', this.onConnection.bind(this));
|
|
235
|
+
this.tcpServer.on('close', this.closed.bind(this));
|
|
233
236
|
this.tcpServer.listen();
|
|
234
237
|
this.state = 1 /* ServerState.BINDING */;
|
|
235
238
|
break;
|
|
236
239
|
case 1 /* ServerState.BINDING */:
|
|
237
|
-
debug(
|
|
238
|
-
this.tcpServer.once(
|
|
240
|
+
debug('TCP server already running. Waiting for it to bind.');
|
|
241
|
+
this.tcpServer.once('listening', this.listening.bind(this, preparedSession, callback));
|
|
239
242
|
break;
|
|
240
243
|
case 2 /* ServerState.LISTENING */:
|
|
241
|
-
debug(
|
|
244
|
+
debug('Instructing client to connect to already running TCP server');
|
|
242
245
|
preparedSession.port = this.tcpPort;
|
|
243
246
|
callback();
|
|
244
247
|
break;
|
|
245
248
|
case 3 /* ServerState.CLOSING */:
|
|
246
|
-
debug(
|
|
247
|
-
this.tcpServer.once(
|
|
249
|
+
debug('TCP socket is currently closing. Trying again when server is fully closed and opening a new one then.');
|
|
250
|
+
this.tcpServer.once('close', () => setTimeout(() => this.checkTCPServerEstablished(preparedSession, callback), 10));
|
|
248
251
|
break;
|
|
249
252
|
}
|
|
250
253
|
}
|
|
251
254
|
listening(preparedSession, callback) {
|
|
252
255
|
this.state = 2 /* ServerState.LISTENING */;
|
|
253
256
|
const address = this.tcpServer.address();
|
|
254
|
-
if (address && typeof address !==
|
|
257
|
+
if (address && typeof address !== 'string') { // address is only typeof string when listening to a pipe or unix socket
|
|
255
258
|
this.tcpPort = address.port;
|
|
256
259
|
preparedSession.port = address.port;
|
|
257
|
-
debug(
|
|
260
|
+
debug('TCP server is now listening for new data stream connections on port %s', address.port);
|
|
258
261
|
callback();
|
|
259
262
|
}
|
|
260
263
|
}
|
|
261
264
|
onConnection(socket) {
|
|
262
|
-
debug(
|
|
263
|
-
// eslint-disable-next-line
|
|
265
|
+
debug('[%s] New DataStream connection was established', socket.remoteAddress);
|
|
266
|
+
// eslint-disable-next-line ts/no-use-before-define
|
|
264
267
|
const connection = new DataStreamConnection(socket);
|
|
265
268
|
connection.on("identification" /* DataStreamConnectionEvent.IDENTIFICATION */, this.handleSessionIdentification.bind(this, connection));
|
|
266
269
|
connection.on("handle-message-globally" /* DataStreamConnectionEvent.HANDLE_MESSAGE_GLOBALLY */, this.handleMessageGlobally.bind(this, connection));
|
|
@@ -269,7 +272,7 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
269
272
|
this.emit("connection-opened" /* DataStreamServerEvent.CONNECTION_OPENED */, connection);
|
|
270
273
|
}
|
|
271
274
|
handleSessionIdentification(connection, firstFrame, callback) {
|
|
272
|
-
let identifiedSession
|
|
275
|
+
let identifiedSession;
|
|
273
276
|
for (let i = 0; i < this.preparedSessions.length; i++) {
|
|
274
277
|
const preparedSession = this.preparedSessions[i];
|
|
275
278
|
// if we successfully decrypt the first frame with this key we know to which session this connection belongs
|
|
@@ -280,7 +283,7 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
280
283
|
}
|
|
281
284
|
callback(identifiedSession);
|
|
282
285
|
if (identifiedSession) {
|
|
283
|
-
debug(
|
|
286
|
+
debug('[%s] Connection was successfully identified (linked with sessionId: %s)', connection.remoteAddress, identifiedSession.connection.sessionID);
|
|
284
287
|
const index = this.preparedSessions.indexOf(identifiedSession);
|
|
285
288
|
if (index >= 0) {
|
|
286
289
|
this.preparedSessions.splice(index, 1);
|
|
@@ -292,19 +295,19 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
292
295
|
// on the other hand the keepAlive should handle that also :thinking:
|
|
293
296
|
}
|
|
294
297
|
else { // we looped through all session and didn't find anything
|
|
295
|
-
debug(
|
|
298
|
+
debug('[%s] Could not identify connection. Terminating.', connection.remoteAddress);
|
|
296
299
|
connection.close(); // disconnecting since first message was not a valid hello
|
|
297
300
|
}
|
|
298
301
|
}
|
|
299
302
|
handleMessageGlobally(connection, message) {
|
|
300
|
-
|
|
301
|
-
let separator =
|
|
303
|
+
assert.notStrictEqual(message.type, 3 /* MessageType.RESPONSE */); // responses can't physically get here
|
|
304
|
+
let separator = '';
|
|
302
305
|
const args = [];
|
|
303
306
|
if (message.type === 1 /* MessageType.EVENT */) {
|
|
304
|
-
separator =
|
|
307
|
+
separator = '-e-';
|
|
305
308
|
}
|
|
306
309
|
else if (message.type === 2 /* MessageType.REQUEST */) {
|
|
307
|
-
separator =
|
|
310
|
+
separator = '-r-';
|
|
308
311
|
args.push(message.id);
|
|
309
312
|
}
|
|
310
313
|
args.push(message.message);
|
|
@@ -314,16 +317,16 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
314
317
|
}
|
|
315
318
|
catch (error) {
|
|
316
319
|
hadListeners = true;
|
|
317
|
-
debug(
|
|
320
|
+
debug('[%s] Error occurred while dispatching handler for HDS message: %o', connection.remoteAddress, message);
|
|
318
321
|
debug(error.stack);
|
|
319
322
|
}
|
|
320
323
|
if (!hadListeners) {
|
|
321
|
-
debug(
|
|
324
|
+
debug('[%s] WARNING no handler was found for message: %o', connection.remoteAddress, message);
|
|
322
325
|
connection.respondToUnhandledRequest(message);
|
|
323
326
|
}
|
|
324
327
|
}
|
|
325
328
|
connectionClosed(connection) {
|
|
326
|
-
debug(
|
|
329
|
+
debug('[%s] DataStream connection closed', connection.remoteAddress);
|
|
327
330
|
this.connections.splice(this.connections.indexOf(connection), 1);
|
|
328
331
|
this.emit("connection-closed" /* DataStreamServerEvent.CONNECTION_CLOSED */, connection);
|
|
329
332
|
this.checkCloseable();
|
|
@@ -333,7 +336,7 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
333
336
|
}
|
|
334
337
|
checkCloseable() {
|
|
335
338
|
if (this.connections.length === 0 && this.preparedSessions.length === 0 && this.state < 3 /* ServerState.CLOSING */) {
|
|
336
|
-
debug(
|
|
339
|
+
debug('Last connection disconnected. Closing the server now.');
|
|
337
340
|
this.state = 3 /* ServerState.CLOSING */;
|
|
338
341
|
this.tcpServer.close();
|
|
339
342
|
}
|
|
@@ -358,11 +361,11 @@ class DataStreamServer extends events_1.EventEmitter {
|
|
|
358
361
|
this.state = 0 /* ServerState.UNINITIALIZED */;
|
|
359
362
|
}
|
|
360
363
|
}
|
|
361
|
-
exports.DataStreamServer = DataStreamServer;
|
|
362
364
|
/**
|
|
363
365
|
* @group HomeKit Data Streams (HDS)
|
|
364
366
|
*/
|
|
365
|
-
|
|
367
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
368
|
+
export var DataStreamConnectionEvent;
|
|
366
369
|
(function (DataStreamConnectionEvent) {
|
|
367
370
|
/**
|
|
368
371
|
* This event is emitted when the first HDSFrame is received from a new connection.
|
|
@@ -379,35 +382,35 @@ var DataStreamConnectionEvent;
|
|
|
379
382
|
* This event is emitted when the socket of the connection was closed.
|
|
380
383
|
*/
|
|
381
384
|
DataStreamConnectionEvent["CLOSED"] = "closed";
|
|
382
|
-
})(DataStreamConnectionEvent || (
|
|
385
|
+
})(DataStreamConnectionEvent || (DataStreamConnectionEvent = {}));
|
|
383
386
|
/**
|
|
384
387
|
* @group HomeKit Data Streams (HDS)
|
|
385
388
|
*/
|
|
386
|
-
|
|
389
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
390
|
+
export var HDSConnectionErrorType;
|
|
387
391
|
(function (HDSConnectionErrorType) {
|
|
388
392
|
HDSConnectionErrorType[HDSConnectionErrorType["ILLEGAL_STATE"] = 1] = "ILLEGAL_STATE";
|
|
389
393
|
HDSConnectionErrorType[HDSConnectionErrorType["CLOSED_SOCKET"] = 2] = "CLOSED_SOCKET";
|
|
390
394
|
HDSConnectionErrorType[HDSConnectionErrorType["MAX_PAYLOAD_LENGTH"] = 3] = "MAX_PAYLOAD_LENGTH";
|
|
391
|
-
})(HDSConnectionErrorType || (
|
|
395
|
+
})(HDSConnectionErrorType || (HDSConnectionErrorType = {}));
|
|
392
396
|
/**
|
|
393
397
|
* @group HomeKit Data Streams (HDS)
|
|
394
398
|
*/
|
|
395
|
-
class HDSConnectionError extends Error {
|
|
399
|
+
export class HDSConnectionError extends Error {
|
|
396
400
|
type;
|
|
397
401
|
constructor(message, type) {
|
|
398
402
|
super(message);
|
|
399
403
|
this.type = type;
|
|
400
404
|
}
|
|
401
405
|
}
|
|
402
|
-
exports.HDSConnectionError = HDSConnectionError;
|
|
403
406
|
/**
|
|
404
407
|
* DataStream connection which holds any necessary state information, encryption and decryption keys, manages
|
|
405
408
|
* protocol handlers and also handles sending and receiving of data stream frames.
|
|
406
409
|
*
|
|
407
410
|
* @group HomeKit Data Streams (HDS)
|
|
408
411
|
*/
|
|
409
|
-
// eslint-disable-next-line
|
|
410
|
-
class DataStreamConnection extends
|
|
412
|
+
// eslint-disable-next-line ts/no-unsafe-declaration-merging
|
|
413
|
+
export class DataStreamConnection extends EventEmitter {
|
|
411
414
|
static MAX_PAYLOAD_LENGTH = 0b11111111111111111111;
|
|
412
415
|
// time we give the remote to close its side of the socket after we called `end()`, before we destroy the socket
|
|
413
416
|
static SOCKET_END_TIMEOUT = 5000;
|
|
@@ -451,12 +454,12 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
451
454
|
},
|
|
452
455
|
});
|
|
453
456
|
this.helloTimer = setTimeout(() => {
|
|
454
|
-
debug(
|
|
457
|
+
debug('[%s] Hello message did not arrive in time. Killing the connection', this.remoteAddress);
|
|
455
458
|
this.close();
|
|
456
459
|
}, 10000);
|
|
457
|
-
this.socket.on(
|
|
458
|
-
this.socket.on(
|
|
459
|
-
this.socket.on(
|
|
460
|
+
this.socket.on('data', this.onSocketData.bind(this));
|
|
461
|
+
this.socket.on('error', this.onSocketError.bind(this));
|
|
462
|
+
this.socket.on('close', this.onSocketClose.bind(this));
|
|
460
463
|
// this is to mitigate the event emitter "memory leak warning".
|
|
461
464
|
// e.g. with HSV there might be multiple cameras subscribing to the CLOSE event. one subscription for
|
|
462
465
|
// every active recording stream on a camera. The default limit of 10 might be easily reached.
|
|
@@ -464,10 +467,9 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
464
467
|
// a warning is still be printed if running long enough.
|
|
465
468
|
this.setMaxListeners(100);
|
|
466
469
|
}
|
|
467
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
468
470
|
handleHello(id, message) {
|
|
469
471
|
// that hello is indeed the _first_ message received is verified in onSocketData(...)
|
|
470
|
-
debug(
|
|
472
|
+
debug('[%s] Received hello message from client: %o', this.remoteAddress, message);
|
|
471
473
|
clearTimeout(this.helloTimer);
|
|
472
474
|
this.helloTimer = undefined;
|
|
473
475
|
this.state = 2 /* ConnectionState.READY */;
|
|
@@ -506,9 +508,7 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
506
508
|
* @param event - name of the event (also referred to as topic. See {@link Topics} for some known ones)
|
|
507
509
|
* @param message - message dictionary which gets sent along the event
|
|
508
510
|
*/
|
|
509
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
510
511
|
sendEvent(protocol, event, message = {}) {
|
|
511
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
512
512
|
const header = {};
|
|
513
513
|
header.protocol = protocol;
|
|
514
514
|
header.event = event;
|
|
@@ -523,9 +523,8 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
523
523
|
* @param request - name of the request (also referred to as topic. See {@link Topics} for some known ones)
|
|
524
524
|
* @param message - message dictionary which gets sent along the request
|
|
525
525
|
* @param callback - handler which gets supplied with an error object if the response didn't
|
|
526
|
-
*
|
|
526
|
+
* arrive in time or the status and the message dictionary from the response
|
|
527
527
|
*/
|
|
528
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
529
528
|
sendRequest(protocol, request, message = {}, callback) {
|
|
530
529
|
let requestId;
|
|
531
530
|
do { // generate unused requestId
|
|
@@ -540,13 +539,12 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
540
539
|
delete this.responseHandlers[requestId];
|
|
541
540
|
delete this.responseTimers[requestId];
|
|
542
541
|
// handler should be able to clean up their stuff
|
|
543
|
-
handler(new Error(
|
|
542
|
+
handler(new Error('timeout'), undefined, {});
|
|
544
543
|
}, 10000); // 10s timer
|
|
545
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
546
544
|
const header = {};
|
|
547
545
|
header.protocol = protocol;
|
|
548
546
|
header.request = request;
|
|
549
|
-
header.id = new
|
|
547
|
+
header.id = new Int64(requestId);
|
|
550
548
|
this.sendHDSFrame(header, message);
|
|
551
549
|
}
|
|
552
550
|
/**
|
|
@@ -558,15 +556,12 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
558
556
|
* @param status - status indication if the request was successful. A status of zero indicates success.
|
|
559
557
|
* @param message - message dictionary which gets sent along the response
|
|
560
558
|
*/
|
|
561
|
-
sendResponse(protocol, response, id, status = HDSStatus.SUCCESS,
|
|
562
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
563
|
-
message = {}) {
|
|
564
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
559
|
+
sendResponse(protocol, response, id, status = HDSStatus.SUCCESS, message = {}) {
|
|
565
560
|
const header = {};
|
|
566
561
|
header.protocol = protocol;
|
|
567
562
|
header.response = response;
|
|
568
|
-
header.id = new
|
|
569
|
-
header.status = new
|
|
563
|
+
header.id = new Int64(id);
|
|
564
|
+
header.status = new Int64(status);
|
|
570
565
|
this.sendHDSFrame(header, message);
|
|
571
566
|
}
|
|
572
567
|
onSocketData(data) {
|
|
@@ -591,7 +586,7 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
591
586
|
// below listener is removed in .close()
|
|
592
587
|
this.connection.setMaxListeners(this.connection.getMaxListeners() + 1);
|
|
593
588
|
this.connection.on("closed" /* HAPConnectionEvent.CLOSED */, this.hapConnectionClosedListener); // register close listener
|
|
594
|
-
debug(
|
|
589
|
+
debug('[%s] Registering CLOSED handler to HAP connection. Connection currently has %d close handlers!', this.remoteAddress, this.connection.listeners("closed" /* HAPConnectionEvent.CLOSED */).length);
|
|
595
590
|
}
|
|
596
591
|
});
|
|
597
592
|
// the cast is needed as typescript doesn't know that the callback above changes the state
|
|
@@ -602,7 +597,7 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
602
597
|
}
|
|
603
598
|
for (; frameIndex < frames.length; frameIndex++) { // decrypt all remaining frames
|
|
604
599
|
if (!this.decryptHDSFrame(frames[frameIndex])) {
|
|
605
|
-
debug(
|
|
600
|
+
debug('[%s] HDS frame decryption or authentication failed. Connection will be terminated!', this.remoteAddress);
|
|
606
601
|
this.close();
|
|
607
602
|
return;
|
|
608
603
|
}
|
|
@@ -612,12 +607,12 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
612
607
|
const firstMessage = messages[0];
|
|
613
608
|
if (!firstMessage || firstMessage.protocol !== "control" /* Protocols.CONTROL */ || firstMessage.type !== 2 /* MessageType.REQUEST */ || firstMessage.topic !== "hello" /* Topics.HELLO */) {
|
|
614
609
|
// first message is not the expected hello request
|
|
615
|
-
debug(
|
|
610
|
+
debug('[%s] First message received was not the expected hello message. Instead got: %o', this.remoteAddress, firstMessage);
|
|
616
611
|
this.close();
|
|
617
612
|
return;
|
|
618
613
|
}
|
|
619
614
|
}
|
|
620
|
-
messages.forEach(message => {
|
|
615
|
+
messages.forEach((message) => {
|
|
621
616
|
if (message.type === 3 /* MessageType.RESPONSE */) {
|
|
622
617
|
// protocol and topic are currently not tested here; just assumed they are correct;
|
|
623
618
|
// probably they are as the requestId is unique per connection no matter what protocol is used
|
|
@@ -629,14 +624,14 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
629
624
|
}
|
|
630
625
|
if (!responseHandler) {
|
|
631
626
|
// we got a response to a request we did not send; we ignore it for now, since nobody will be hurt
|
|
632
|
-
debug(
|
|
627
|
+
debug('WARNING we received a response to a request we have not sent: %o', message);
|
|
633
628
|
return;
|
|
634
629
|
}
|
|
635
630
|
try {
|
|
636
631
|
responseHandler(undefined, message.status, message.message);
|
|
637
632
|
}
|
|
638
633
|
catch (error) {
|
|
639
|
-
debug(
|
|
634
|
+
debug('[%s] Error occurred while dispatching response handler for HDS message: %o', this.remoteAddress, message);
|
|
640
635
|
debug(error.stack);
|
|
641
636
|
}
|
|
642
637
|
delete this.responseHandlers[message.id];
|
|
@@ -650,22 +645,24 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
650
645
|
}
|
|
651
646
|
if (message.type === 1 /* MessageType.EVENT */) {
|
|
652
647
|
let eventHandler;
|
|
648
|
+
// eslint-disable-next-line no-cond-assign
|
|
653
649
|
if (!handler.eventHandler || !(eventHandler = handler.eventHandler[message.topic])) {
|
|
654
|
-
debug(
|
|
650
|
+
debug('[%s] WARNING no event handler was found for message: %o', this.remoteAddress, message);
|
|
655
651
|
return;
|
|
656
652
|
}
|
|
657
653
|
try {
|
|
658
654
|
eventHandler(message.message);
|
|
659
655
|
}
|
|
660
656
|
catch (error) {
|
|
661
|
-
debug(
|
|
657
|
+
debug('[%s] Error occurred while dispatching event handler for HDS message: %o', this.remoteAddress, message);
|
|
662
658
|
debug(error.stack);
|
|
663
659
|
}
|
|
664
660
|
}
|
|
665
661
|
else if (message.type === 2 /* MessageType.REQUEST */) {
|
|
666
662
|
let requestHandler;
|
|
663
|
+
// eslint-disable-next-line no-cond-assign
|
|
667
664
|
if (!handler.requestHandler || !(requestHandler = handler.requestHandler[message.topic])) {
|
|
668
|
-
debug(
|
|
665
|
+
debug('[%s] WARNING no request handler was found for message: %o', this.remoteAddress, message);
|
|
669
666
|
this.respondToUnhandledRequest(message);
|
|
670
667
|
return;
|
|
671
668
|
}
|
|
@@ -673,12 +670,12 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
673
670
|
requestHandler(message.id, message.message);
|
|
674
671
|
}
|
|
675
672
|
catch (error) {
|
|
676
|
-
debug(
|
|
673
|
+
debug('[%s] Error occurred while dispatching request handler for HDS message: %o', this.remoteAddress, message);
|
|
677
674
|
debug(error.stack);
|
|
678
675
|
}
|
|
679
676
|
}
|
|
680
677
|
else {
|
|
681
|
-
debug(
|
|
678
|
+
debug('[%s] Encountered unknown message type with id %d', this.remoteAddress, message.type);
|
|
682
679
|
}
|
|
683
680
|
}
|
|
684
681
|
});
|
|
@@ -699,7 +696,7 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
699
696
|
const payloadType = data.readUInt8(frameBegin); // type defining structure of payload; 8-bit; currently expected to be 1
|
|
700
697
|
const payloadLength = data.readUIntBE(frameBegin + 1, 3); // read 24-bit big-endian uint length field
|
|
701
698
|
if (payloadLength > DataStreamConnection.MAX_PAYLOAD_LENGTH) {
|
|
702
|
-
debug(
|
|
699
|
+
debug('[%s] Connection send payload with size bigger than the maximum allow for data stream', this.remoteAddress);
|
|
703
700
|
this.close();
|
|
704
701
|
return [];
|
|
705
702
|
}
|
|
@@ -718,14 +715,14 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
718
715
|
frameBegin = authTagBegin + 16; // move to next frame
|
|
719
716
|
if (payloadType === 1) {
|
|
720
717
|
const hdsFrame = {
|
|
721
|
-
header
|
|
722
|
-
cipheredPayload
|
|
723
|
-
authTag
|
|
718
|
+
header,
|
|
719
|
+
cipheredPayload,
|
|
720
|
+
authTag,
|
|
724
721
|
};
|
|
725
722
|
frames.push(hdsFrame);
|
|
726
723
|
}
|
|
727
724
|
else {
|
|
728
|
-
debug(
|
|
725
|
+
debug('[%s] Encountered unknown payload type %d with a payload of %d bytes', this.remoteAddress, payloadType, payloadLength);
|
|
729
726
|
}
|
|
730
727
|
}
|
|
731
728
|
return frames;
|
|
@@ -736,18 +733,18 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
736
733
|
* @private
|
|
737
734
|
*/
|
|
738
735
|
respondToUnhandledRequest(message) {
|
|
739
|
-
if (message.type !== 2 /* MessageType.REQUEST */ || typeof message.id !==
|
|
736
|
+
if (message.type !== 2 /* MessageType.REQUEST */ || typeof message.id !== 'number' || this.isConsideredClosed()) {
|
|
740
737
|
return;
|
|
741
738
|
}
|
|
742
739
|
try {
|
|
743
740
|
this.sendResponse(message.protocol, message.topic, message.id, HDSStatus.MISSING_PROTOCOL);
|
|
744
741
|
}
|
|
745
742
|
catch (error) {
|
|
746
|
-
debug(
|
|
743
|
+
debug('[%s] Failed to answer the unhandled request: %s', this.remoteAddress, error.message);
|
|
747
744
|
}
|
|
748
745
|
}
|
|
749
746
|
/**
|
|
750
|
-
* @private
|
|
747
|
+
* @private
|
|
751
748
|
*/
|
|
752
749
|
decryptHDSFrame(frame, keyOverwrite) {
|
|
753
750
|
hapCrypto.writeUInt64LE(this.controllerToAccessoryNonce, this.controllerToAccessoryNonceBuffer, 0); // update nonce buffer
|
|
@@ -764,50 +761,48 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
764
761
|
}
|
|
765
762
|
decodePayloads(frames) {
|
|
766
763
|
const messages = [];
|
|
767
|
-
frames.forEach(frame => {
|
|
764
|
+
frames.forEach((frame) => {
|
|
768
765
|
const payload = frame.plaintextPayload;
|
|
769
766
|
if (!payload) {
|
|
770
|
-
throw new HDSConnectionError(
|
|
767
|
+
throw new HDSConnectionError('Reached illegal state. Encountered HDSFrame with wasn\'t decrypted yet!', 1 /* HDSConnectionErrorType.ILLEGAL_STATE */);
|
|
771
768
|
}
|
|
772
769
|
if (payload.length < 1 || payload.readUInt8(0) > payload.length - 1) {
|
|
773
|
-
debug(
|
|
770
|
+
debug('[%s] Encountered HDS payload with a missing or too long header (payload: %s)', this.remoteAddress, payload.toString('hex'));
|
|
774
771
|
return;
|
|
775
772
|
}
|
|
776
773
|
const headerLength = payload.readUInt8(0);
|
|
777
774
|
const messageLength = payload.length - headerLength - 1;
|
|
778
775
|
const headerBegin = 1;
|
|
779
776
|
const messageBegin = headerBegin + headerLength;
|
|
780
|
-
const headerPayload = new
|
|
781
|
-
const messagePayload = new
|
|
782
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
777
|
+
const headerPayload = new DataStreamReader(payload.subarray(headerBegin, headerBegin + headerLength));
|
|
778
|
+
const messagePayload = new DataStreamReader(payload.subarray(messageBegin, messageBegin + messageLength));
|
|
783
779
|
let headerDictionary;
|
|
784
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
785
780
|
let messageDictionary;
|
|
786
781
|
try {
|
|
787
|
-
headerDictionary =
|
|
782
|
+
headerDictionary = DataStreamParser.decode(headerPayload);
|
|
788
783
|
headerPayload.finished();
|
|
789
784
|
}
|
|
790
785
|
catch (error) {
|
|
791
|
-
debug(
|
|
786
|
+
debug('[%s] Failed to decode header payload: %s', this.remoteAddress, error.message);
|
|
792
787
|
return;
|
|
793
788
|
}
|
|
794
789
|
try {
|
|
795
|
-
messageDictionary =
|
|
790
|
+
messageDictionary = DataStreamParser.decode(messagePayload);
|
|
796
791
|
messagePayload.finished();
|
|
797
792
|
}
|
|
798
793
|
catch (error) {
|
|
799
|
-
debug(
|
|
794
|
+
debug('[%s] Failed to decode message payload: %s (header: %o)', this.remoteAddress, error.message, headerDictionary);
|
|
800
795
|
return;
|
|
801
796
|
}
|
|
802
|
-
if (headerDictionary === null || typeof headerDictionary !==
|
|
803
|
-
debug(
|
|
797
|
+
if (headerDictionary === null || typeof headerDictionary !== 'object') {
|
|
798
|
+
debug('[%s] Encountered payload header which is not a dictionary: %o', this.remoteAddress, headerDictionary);
|
|
804
799
|
return;
|
|
805
800
|
}
|
|
806
801
|
let type;
|
|
807
802
|
const protocol = headerDictionary.protocol;
|
|
808
803
|
let topic;
|
|
809
|
-
let id
|
|
810
|
-
let status
|
|
804
|
+
let id;
|
|
805
|
+
let status;
|
|
811
806
|
if (headerDictionary.event !== undefined) {
|
|
812
807
|
type = 1 /* MessageType.EVENT */;
|
|
813
808
|
topic = headerDictionary.event;
|
|
@@ -824,35 +819,38 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
824
819
|
status = headerDictionary.status;
|
|
825
820
|
}
|
|
826
821
|
else {
|
|
827
|
-
debug(
|
|
822
|
+
debug('[%s] Encountered unknown payload header format: %o (message: %o)', this.remoteAddress, headerDictionary, messageDictionary);
|
|
828
823
|
return;
|
|
829
824
|
}
|
|
830
825
|
const message = {
|
|
831
|
-
type
|
|
832
|
-
protocol
|
|
833
|
-
topic
|
|
834
|
-
id
|
|
835
|
-
status
|
|
826
|
+
type,
|
|
827
|
+
protocol,
|
|
828
|
+
topic,
|
|
829
|
+
id,
|
|
830
|
+
status,
|
|
836
831
|
message: messageDictionary,
|
|
837
832
|
};
|
|
838
833
|
messages.push(message);
|
|
839
834
|
});
|
|
840
835
|
return messages;
|
|
841
836
|
}
|
|
842
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
843
837
|
sendHDSFrame(header, message) {
|
|
844
838
|
if (this.state >= 3 /* ConnectionState.CLOSING */) {
|
|
845
|
-
throw new HDSConnectionError(
|
|
839
|
+
throw new HDSConnectionError('Cannot send message on closing/closed socket!', 2 /* HDSConnectionErrorType.CLOSED_SOCKET */);
|
|
846
840
|
}
|
|
847
|
-
const headerWriter = new
|
|
848
|
-
const messageWriter = new
|
|
849
|
-
|
|
850
|
-
|
|
841
|
+
const headerWriter = new DataStreamWriter();
|
|
842
|
+
const messageWriter = new DataStreamWriter();
|
|
843
|
+
DataStreamParser.encode(header, headerWriter);
|
|
844
|
+
DataStreamParser.encode(message, messageWriter);
|
|
851
845
|
const payloadHeaderBuffer = Buffer.alloc(1);
|
|
852
846
|
payloadHeaderBuffer.writeUInt8(headerWriter.length(), 0);
|
|
853
|
-
const payloadBuffer = Buffer.concat([
|
|
847
|
+
const payloadBuffer = Buffer.concat([
|
|
848
|
+
payloadHeaderBuffer,
|
|
849
|
+
headerWriter.getData(),
|
|
850
|
+
messageWriter.getData(),
|
|
851
|
+
]);
|
|
854
852
|
if (payloadBuffer.length > DataStreamConnection.MAX_PAYLOAD_LENGTH) {
|
|
855
|
-
throw new HDSConnectionError(
|
|
853
|
+
throw new HDSConnectionError('Tried sending payload with length larger than the maximum allowed for data stream', 3 /* HDSConnectionErrorType.MAX_PAYLOAD_LENGTH */);
|
|
856
854
|
}
|
|
857
855
|
const frameTypeBuffer = Buffer.alloc(1);
|
|
858
856
|
frameTypeBuffer.writeUInt8(1, 0);
|
|
@@ -873,7 +871,7 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
873
871
|
};
|
|
874
872
|
const sentMessage = this.decodePayloads([frame])[0];
|
|
875
873
|
console.log("Sent message: " + JSON.stringify(sentMessage, null, 4));
|
|
876
|
-
|
|
874
|
+
// */
|
|
877
875
|
}
|
|
878
876
|
close() {
|
|
879
877
|
if (this.state >= 3 /* ConnectionState.CLOSING */) {
|
|
@@ -883,7 +881,7 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
883
881
|
this.socket.end();
|
|
884
882
|
// a remote which never closes its side would otherwise keep the half-closed socket (and this connection) around forever
|
|
885
883
|
this.socketEndTimer = setTimeout(() => {
|
|
886
|
-
debug(
|
|
884
|
+
debug('[%s] Remote did not close the socket in time. Destroying it now.', this.remoteAddress);
|
|
887
885
|
this.socket.destroy();
|
|
888
886
|
}, DataStreamConnection.SOCKET_END_TIMEOUT);
|
|
889
887
|
this.socketEndTimer.unref();
|
|
@@ -893,11 +891,11 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
893
891
|
}
|
|
894
892
|
onHAPSessionClosed() {
|
|
895
893
|
// If the hap connection is closed it is probably also a good idea to close the data stream connection
|
|
896
|
-
debug(
|
|
894
|
+
debug('[%s] HAP connection disconnected. Also closing DataStream connection now.', this.remoteAddress);
|
|
897
895
|
this.close();
|
|
898
896
|
}
|
|
899
897
|
onSocketError(error) {
|
|
900
|
-
debug(
|
|
898
|
+
debug('[%s] Encountered socket error: %s', this.remoteAddress, error.message);
|
|
901
899
|
// onSocketClose will be called next
|
|
902
900
|
}
|
|
903
901
|
onSocketClose() {
|
|
@@ -913,5 +911,4 @@ class DataStreamConnection extends events_1.EventEmitter {
|
|
|
913
911
|
this.removeAllListeners();
|
|
914
912
|
}
|
|
915
913
|
}
|
|
916
|
-
exports.DataStreamConnection = DataStreamConnection;
|
|
917
914
|
//# sourceMappingURL=DataStreamServer.js.map
|