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