@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
package/dist/lib/HAPServer.js
CHANGED
|
@@ -1,41 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
const hapCrypto = tslib_1.__importStar(require("./util/hapCrypto"));
|
|
14
|
-
const once_1 = require("./util/once");
|
|
15
|
-
const tlv = tslib_1.__importStar(require("./util/tlv"));
|
|
16
|
-
const debug = (0, debug_1.default)("HAP-NodeJS:HAPServer");
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import crypto from 'node:crypto';
|
|
3
|
+
import { EventEmitter } from 'node:events';
|
|
4
|
+
import { URL } from 'node:url';
|
|
5
|
+
import createDebug from 'debug';
|
|
6
|
+
import { SRP, SrpServer } from 'fast-srp-hap';
|
|
7
|
+
import { consideredTrue } from '../internal-types.js';
|
|
8
|
+
import { EventedHTTPServer, HAPEncryption } from './util/eventedhttp.js';
|
|
9
|
+
import * as hapCrypto from './util/hapCrypto.js';
|
|
10
|
+
import { once } from './util/once.js';
|
|
11
|
+
import * as tlv from './util/tlv.js';
|
|
12
|
+
const debug = createDebug('HAP-NodeJS:HAPServer');
|
|
17
13
|
/**
|
|
18
14
|
* TLV error codes for the `TLVValues.ERROR_CODE` field.
|
|
19
15
|
*
|
|
20
16
|
* @group HAP Accessory Server
|
|
21
17
|
*/
|
|
22
|
-
|
|
18
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
19
|
+
export var TLVErrorCode;
|
|
23
20
|
(function (TLVErrorCode) {
|
|
24
21
|
// noinspection JSUnusedGlobalSymbols
|
|
25
22
|
TLVErrorCode[TLVErrorCode["UNKNOWN"] = 1] = "UNKNOWN";
|
|
26
23
|
TLVErrorCode[TLVErrorCode["INVALID_REQUEST"] = 2] = "INVALID_REQUEST";
|
|
27
|
-
// eslint-disable-next-line
|
|
24
|
+
// eslint-disable-next-line ts/no-duplicate-enum-values
|
|
28
25
|
TLVErrorCode[TLVErrorCode["AUTHENTICATION"] = 2] = "AUTHENTICATION";
|
|
29
26
|
TLVErrorCode[TLVErrorCode["BACKOFF"] = 3] = "BACKOFF";
|
|
30
27
|
TLVErrorCode[TLVErrorCode["MAX_PEERS"] = 4] = "MAX_PEERS";
|
|
31
28
|
TLVErrorCode[TLVErrorCode["MAX_TRIES"] = 5] = "MAX_TRIES";
|
|
32
29
|
TLVErrorCode[TLVErrorCode["UNAVAILABLE"] = 6] = "UNAVAILABLE";
|
|
33
|
-
TLVErrorCode[TLVErrorCode["BUSY"] = 7] = "BUSY";
|
|
34
|
-
})(TLVErrorCode || (
|
|
30
|
+
TLVErrorCode[TLVErrorCode["BUSY"] = 7] = "BUSY";
|
|
31
|
+
})(TLVErrorCode || (TLVErrorCode = {}));
|
|
35
32
|
/**
|
|
36
33
|
* @group HAP Accessory Server
|
|
37
34
|
*/
|
|
38
|
-
var HAPStatus;
|
|
35
|
+
export var HAPStatus;
|
|
39
36
|
(function (HAPStatus) {
|
|
40
37
|
// noinspection JSUnusedGlobalSymbols
|
|
41
38
|
/**
|
|
@@ -91,18 +88,18 @@ var HAPStatus;
|
|
|
91
88
|
*/
|
|
92
89
|
HAPStatus[HAPStatus["NOT_ALLOWED_IN_CURRENT_STATE"] = -70412] = "NOT_ALLOWED_IN_CURRENT_STATE";
|
|
93
90
|
// when adding new status codes, remember to update bounds in IsKnownHAPStatusError below
|
|
94
|
-
})(HAPStatus || (
|
|
91
|
+
})(HAPStatus || (HAPStatus = {}));
|
|
95
92
|
/**
|
|
96
93
|
* Determines if the given status code is a known {@link HAPStatus} error code.
|
|
97
94
|
*
|
|
98
95
|
* @group HAP Accessory Server
|
|
99
96
|
*/
|
|
100
|
-
function IsKnownHAPStatusError(status) {
|
|
97
|
+
export function IsKnownHAPStatusError(status) {
|
|
101
98
|
return (
|
|
102
99
|
// Lower bound (most negative error code)
|
|
103
|
-
status >=
|
|
100
|
+
status >= HAPStatus.NOT_ALLOWED_IN_CURRENT_STATE
|
|
104
101
|
// Upper bound (negative error code closest to zero)
|
|
105
|
-
status <=
|
|
102
|
+
&& status <= HAPStatus.INSUFFICIENT_PRIVILEGES);
|
|
106
103
|
}
|
|
107
104
|
/**
|
|
108
105
|
* Those status codes are the one listed as appropriate for the HAP spec!
|
|
@@ -114,7 +111,8 @@ function IsKnownHAPStatusError(status) {
|
|
|
114
111
|
*
|
|
115
112
|
* @group HAP Accessory Server
|
|
116
113
|
*/
|
|
117
|
-
|
|
114
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
115
|
+
export var HAPHTTPCode;
|
|
118
116
|
(function (HAPHTTPCode) {
|
|
119
117
|
// noinspection JSUnusedGlobalSymbols
|
|
120
118
|
HAPHTTPCode[HAPHTTPCode["OK"] = 200] = "OK";
|
|
@@ -127,14 +125,15 @@ var HAPHTTPCode;
|
|
|
127
125
|
// server error
|
|
128
126
|
HAPHTTPCode[HAPHTTPCode["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
|
|
129
127
|
HAPHTTPCode[HAPHTTPCode["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
|
|
130
|
-
})(HAPHTTPCode || (
|
|
128
|
+
})(HAPHTTPCode || (HAPHTTPCode = {}));
|
|
131
129
|
/**
|
|
132
130
|
* When in a request is made to the pairing endpoints, and mime type is 'application/pairing+tlv8'
|
|
133
131
|
* one should use the below status codes.
|
|
134
132
|
*
|
|
135
133
|
* @group HAP Accessory Server
|
|
136
134
|
*/
|
|
137
|
-
|
|
135
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
136
|
+
export var HAPPairingHTTPCode;
|
|
138
137
|
(function (HAPPairingHTTPCode) {
|
|
139
138
|
// noinspection JSUnusedGlobalSymbols
|
|
140
139
|
HAPPairingHTTPCode[HAPPairingHTTPCode["OK"] = 200] = "OK";
|
|
@@ -143,11 +142,12 @@ var HAPPairingHTTPCode;
|
|
|
143
142
|
HAPPairingHTTPCode[HAPPairingHTTPCode["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
|
|
144
143
|
HAPPairingHTTPCode[HAPPairingHTTPCode["CONNECTION_AUTHORIZATION_REQUIRED"] = 470] = "CONNECTION_AUTHORIZATION_REQUIRED";
|
|
145
144
|
HAPPairingHTTPCode[HAPPairingHTTPCode["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
|
|
146
|
-
})(HAPPairingHTTPCode || (
|
|
145
|
+
})(HAPPairingHTTPCode || (HAPPairingHTTPCode = {}));
|
|
147
146
|
/**
|
|
148
147
|
* @group HAP Accessory Server
|
|
149
148
|
*/
|
|
150
|
-
|
|
149
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
150
|
+
export var HAPServerEventTypes;
|
|
151
151
|
(function (HAPServerEventTypes) {
|
|
152
152
|
/**
|
|
153
153
|
* Emitted when the server is fully set up and ready to receive connections.
|
|
@@ -190,7 +190,7 @@ var HAPServerEventTypes;
|
|
|
190
190
|
HAPServerEventTypes["SET_CHARACTERISTICS"] = "set-characteristics";
|
|
191
191
|
HAPServerEventTypes["REQUEST_RESOURCE"] = "request-resource";
|
|
192
192
|
HAPServerEventTypes["CONNECTION_CLOSED"] = "connection-closed";
|
|
193
|
-
})(HAPServerEventTypes || (
|
|
193
|
+
})(HAPServerEventTypes || (HAPServerEventTypes = {}));
|
|
194
194
|
/**
|
|
195
195
|
* The actual HAP server that iOS devices talk to.
|
|
196
196
|
*
|
|
@@ -212,8 +212,8 @@ var HAPServerEventTypes;
|
|
|
212
212
|
*
|
|
213
213
|
* @group HAP Accessory Server
|
|
214
214
|
*/
|
|
215
|
-
// eslint-disable-next-line
|
|
216
|
-
class HAPServer extends
|
|
215
|
+
// eslint-disable-next-line ts/no-unsafe-declaration-merging
|
|
216
|
+
export class HAPServer extends EventEmitter {
|
|
217
217
|
accessoryInfo;
|
|
218
218
|
httpServer;
|
|
219
219
|
/**
|
|
@@ -233,13 +233,13 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
233
233
|
this.accessoryInfo = accessoryInfo;
|
|
234
234
|
this.allowInsecureRequest = false;
|
|
235
235
|
// internal server that does all the actual communication
|
|
236
|
-
this.httpServer = new
|
|
236
|
+
this.httpServer = new EventedHTTPServer();
|
|
237
237
|
this.httpServer.on("listening" /* EventedHTTPServerEvent.LISTENING */, this.onListening.bind(this));
|
|
238
238
|
this.httpServer.on("request" /* EventedHTTPServerEvent.REQUEST */, this.handleRequestOnHAPConnection.bind(this));
|
|
239
239
|
this.httpServer.on("connection-closed" /* EventedHTTPServerEvent.CONNECTION_CLOSED */, this.handleConnectionClosed.bind(this));
|
|
240
240
|
}
|
|
241
241
|
listen(port = 0, host) {
|
|
242
|
-
if (host ===
|
|
242
|
+
if (host === '::') {
|
|
243
243
|
// this will work around "EAFNOSUPPORT: address family not supported" errors
|
|
244
244
|
// on systems where IPv6 is not supported/enabled, we just use the node default then by supplying undefined
|
|
245
245
|
host = undefined;
|
|
@@ -271,14 +271,14 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
271
271
|
* @param value - The newly set value of the characteristic.
|
|
272
272
|
* @param originator - If specified, the connection will not get an event message.
|
|
273
273
|
* @param immediateDelivery - The HAP spec requires some characteristics to be delivery immediately.
|
|
274
|
-
*
|
|
274
|
+
* Namely, for the {@link Characteristic.ButtonEvent} and the {@link Characteristic.ProgrammableSwitchEvent} characteristics.
|
|
275
275
|
*/
|
|
276
276
|
sendEventNotifications(aid, iid, value, originator, immediateDelivery) {
|
|
277
277
|
try {
|
|
278
278
|
this.httpServer.broadcastEvent(aid, iid, value, originator, immediateDelivery);
|
|
279
279
|
}
|
|
280
280
|
catch (error) {
|
|
281
|
-
console.warn(
|
|
281
|
+
console.warn(`[${this.accessoryInfo.username}] Error when sending event notifications: ${error.message}`);
|
|
282
282
|
}
|
|
283
283
|
}
|
|
284
284
|
onListening(port, hostname) {
|
|
@@ -286,14 +286,14 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
286
286
|
}
|
|
287
287
|
// Called when an HTTP request was detected.
|
|
288
288
|
handleRequestOnHAPConnection(connection, request, response) {
|
|
289
|
-
debug(
|
|
289
|
+
debug('[%s] HAP Request: %s %s', this.accessoryInfo.username, request.method, request.url);
|
|
290
290
|
const maxBodySize = connection.isAuthenticated() || this.allowInsecureRequest
|
|
291
291
|
? HAPServer.MAX_REQUEST_BODY_SIZE
|
|
292
292
|
: HAPServer.MAX_UNAUTHENTICATED_REQUEST_BODY_SIZE;
|
|
293
293
|
const buffers = [];
|
|
294
294
|
let bodySize = 0;
|
|
295
295
|
let bodyTooLarge = false;
|
|
296
|
-
request.on(
|
|
296
|
+
request.on('data', (data) => {
|
|
297
297
|
if (bodyTooLarge) {
|
|
298
298
|
return;
|
|
299
299
|
}
|
|
@@ -301,34 +301,34 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
301
301
|
if (bodySize > maxBodySize) {
|
|
302
302
|
bodyTooLarge = true;
|
|
303
303
|
buffers.length = 0;
|
|
304
|
-
debug(
|
|
304
|
+
debug('[%s] Rejecting request %s as its body exceeds %d bytes', this.accessoryInfo.username, request.url, maxBodySize);
|
|
305
305
|
// closing the connection stops the client from sending the rest of the body
|
|
306
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
307
|
-
response.end(JSON.stringify({ status:
|
|
306
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */, 'Connection': 'close' });
|
|
307
|
+
response.end(JSON.stringify({ status: HAPStatus.OUT_OF_RESOURCE }));
|
|
308
308
|
return;
|
|
309
309
|
}
|
|
310
310
|
buffers.push(data);
|
|
311
311
|
});
|
|
312
|
-
request.on(
|
|
312
|
+
request.on('end', () => {
|
|
313
313
|
if (bodyTooLarge) {
|
|
314
314
|
return;
|
|
315
315
|
}
|
|
316
316
|
let url;
|
|
317
317
|
try {
|
|
318
|
-
url = new
|
|
318
|
+
url = new URL(request.url, 'http://hap-nodejs.local'); // parse the url (query strings etc)
|
|
319
319
|
}
|
|
320
320
|
catch {
|
|
321
321
|
// e.g. "//[" parses as an invalid host. this runs in an event listener, so a throw here would crash the process
|
|
322
|
-
debug(
|
|
323
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
324
|
-
response.end(JSON.stringify({ status:
|
|
322
|
+
debug('[%s] Received request with an invalid url: %s', this.accessoryInfo.username, request.url);
|
|
323
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
324
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
325
325
|
return;
|
|
326
326
|
}
|
|
327
327
|
const handler = this.getHandler(url);
|
|
328
328
|
if (!handler) {
|
|
329
|
-
debug(
|
|
330
|
-
response.writeHead(404 /* HAPHTTPCode.NOT_FOUND */, {
|
|
331
|
-
response.end(JSON.stringify({ status:
|
|
329
|
+
debug('[%s] WARNING: Handler for %s not implemented', this.accessoryInfo.username, request.url);
|
|
330
|
+
response.writeHead(404 /* HAPHTTPCode.NOT_FOUND */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
331
|
+
response.end(JSON.stringify({ status: HAPStatus.RESOURCE_DOES_NOT_EXIST }));
|
|
332
332
|
}
|
|
333
333
|
else {
|
|
334
334
|
const data = Buffer.concat(buffers);
|
|
@@ -336,9 +336,9 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
336
336
|
handler(connection, url, request, data, response);
|
|
337
337
|
}
|
|
338
338
|
catch (error) {
|
|
339
|
-
debug(
|
|
340
|
-
response.writeHead(500 /* HAPHTTPCode.INTERNAL_SERVER_ERROR */, {
|
|
341
|
-
response.end(JSON.stringify({ status:
|
|
339
|
+
debug('[%s] Error executing route handler: %s', this.accessoryInfo.username, error.stack);
|
|
340
|
+
response.writeHead(500 /* HAPHTTPCode.INTERNAL_SERVER_ERROR */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
341
|
+
response.end(JSON.stringify({ status: HAPStatus.RESOURCE_BUSY })); // resource busy try again, does somehow fit?
|
|
342
342
|
}
|
|
343
343
|
}
|
|
344
344
|
});
|
|
@@ -348,21 +348,21 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
348
348
|
}
|
|
349
349
|
getHandler(url) {
|
|
350
350
|
switch (url.pathname.toLowerCase()) {
|
|
351
|
-
case
|
|
351
|
+
case '/identify':
|
|
352
352
|
return this.handleIdentifyRequest.bind(this);
|
|
353
|
-
case
|
|
353
|
+
case '/pair-setup':
|
|
354
354
|
return this.handlePairSetup.bind(this);
|
|
355
|
-
case
|
|
355
|
+
case '/pair-verify':
|
|
356
356
|
return this.handlePairVerify.bind(this);
|
|
357
|
-
case
|
|
357
|
+
case '/pairings':
|
|
358
358
|
return this.handlePairings.bind(this);
|
|
359
|
-
case
|
|
359
|
+
case '/accessories':
|
|
360
360
|
return this.handleAccessories.bind(this);
|
|
361
|
-
case
|
|
361
|
+
case '/characteristics':
|
|
362
362
|
return this.handleCharacteristics.bind(this);
|
|
363
|
-
case
|
|
363
|
+
case '/prepare':
|
|
364
364
|
return this.handlePrepareWrite.bind(this);
|
|
365
|
-
case
|
|
365
|
+
case '/resource':
|
|
366
366
|
return this.handleResource.bind(this);
|
|
367
367
|
default:
|
|
368
368
|
return undefined;
|
|
@@ -374,33 +374,33 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
374
374
|
handleIdentifyRequest(connection, url, request, data, response) {
|
|
375
375
|
// POST body is empty
|
|
376
376
|
if (this.accessoryInfo.paired() && !this.allowInsecureRequest) {
|
|
377
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
378
|
-
response.end(JSON.stringify({ status:
|
|
377
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
378
|
+
response.end(JSON.stringify({ status: HAPStatus.INSUFFICIENT_PRIVILEGES }));
|
|
379
379
|
return;
|
|
380
380
|
}
|
|
381
|
-
this.emit("identify" /* HAPServerEventTypes.IDENTIFY */,
|
|
381
|
+
this.emit("identify" /* HAPServerEventTypes.IDENTIFY */, once((err) => {
|
|
382
382
|
if (!err) {
|
|
383
|
-
debug(
|
|
383
|
+
debug('[%s] Identification success', this.accessoryInfo.username);
|
|
384
384
|
response.writeHead(204 /* HAPHTTPCode.NO_CONTENT */);
|
|
385
385
|
response.end();
|
|
386
386
|
}
|
|
387
387
|
else {
|
|
388
|
-
debug(
|
|
389
|
-
response.writeHead(500 /* HAPHTTPCode.INTERNAL_SERVER_ERROR */, {
|
|
390
|
-
response.end(JSON.stringify({ status:
|
|
388
|
+
debug('[%s] Identification error: %s', this.accessoryInfo.username, err.message);
|
|
389
|
+
response.writeHead(500 /* HAPHTTPCode.INTERNAL_SERVER_ERROR */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
390
|
+
response.end(JSON.stringify({ status: HAPStatus.RESOURCE_BUSY }));
|
|
391
391
|
}
|
|
392
392
|
}));
|
|
393
393
|
}
|
|
394
394
|
handlePairSetup(connection, url, request, data, response) {
|
|
395
395
|
// Can only be directly paired with one iOS device
|
|
396
396
|
if (!this.allowInsecureRequest && this.accessoryInfo.paired()) {
|
|
397
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
397
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
398
398
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 6 /* TLVErrorCode.UNAVAILABLE */));
|
|
399
399
|
return;
|
|
400
400
|
}
|
|
401
401
|
if (this.unsuccessfulPairAttempts > 100) {
|
|
402
|
-
debug(
|
|
403
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
402
|
+
debug('[%s] Reached maximum amount of unsuccessful pair attempts!', this.accessoryInfo.username);
|
|
403
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
404
404
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 5 /* TLVErrorCode.MAX_TRIES */));
|
|
405
405
|
return;
|
|
406
406
|
}
|
|
@@ -409,13 +409,13 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
409
409
|
tlvData = tlv.decode(data);
|
|
410
410
|
}
|
|
411
411
|
catch (error) {
|
|
412
|
-
debug(
|
|
413
|
-
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, {
|
|
412
|
+
debug('[%s] Pair-Setup: failed to decode TLV request: %s', this.accessoryInfo.username, error.message);
|
|
413
|
+
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
414
414
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
415
415
|
return;
|
|
416
416
|
}
|
|
417
417
|
if (!tlvData[6 /* TLVValues.SEQUENCE_NUM */]) {
|
|
418
|
-
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, {
|
|
418
|
+
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
419
419
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
420
420
|
return;
|
|
421
421
|
}
|
|
@@ -431,33 +431,31 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
431
431
|
}
|
|
432
432
|
else {
|
|
433
433
|
// Invalid state/sequence number
|
|
434
|
-
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, {
|
|
434
|
+
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
435
435
|
response.end(tlv.encode(6 /* TLVValues.STATE */, sequence + 1, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
436
|
-
return;
|
|
437
436
|
}
|
|
438
437
|
}
|
|
439
438
|
handlePairSetupM1(connection, request, response) {
|
|
440
|
-
debug(
|
|
441
|
-
const salt =
|
|
442
|
-
const srpParams =
|
|
443
|
-
|
|
439
|
+
debug('[%s] Pair step 1/5', this.accessoryInfo.username);
|
|
440
|
+
const salt = crypto.randomBytes(16);
|
|
441
|
+
const srpParams = SRP.params.hap;
|
|
442
|
+
SRP.genKey(32).then((key) => {
|
|
444
443
|
// create a new SRP server
|
|
445
|
-
const srpServer = new
|
|
444
|
+
const srpServer = new SrpServer(srpParams, salt, Buffer.from('Pair-Setup'), Buffer.from(this.accessoryInfo.pincode), key);
|
|
446
445
|
const srpB = srpServer.computeB();
|
|
447
446
|
// attach it to the current TCP session
|
|
448
447
|
connection.srpServer = srpServer;
|
|
449
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
448
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
450
449
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 2 /* PairingStates.M2 */, 2 /* TLVValues.SALT */, salt, 3 /* TLVValues.PUBLIC_KEY */, srpB));
|
|
451
450
|
connection._pairSetupState = 2 /* PairingStates.M2 */;
|
|
452
|
-
}).catch(error => {
|
|
453
|
-
debug(
|
|
454
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
451
|
+
}).catch((error) => {
|
|
452
|
+
debug('[%s] Error occurred when generating srp key: %s', this.accessoryInfo.username, error.message);
|
|
453
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
455
454
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
456
|
-
return;
|
|
457
455
|
});
|
|
458
456
|
}
|
|
459
457
|
handlePairSetupM3(connection, request, response, tlvData) {
|
|
460
|
-
debug(
|
|
458
|
+
debug('[%s] Pair step 2/5', this.accessoryInfo.username);
|
|
461
459
|
const A = tlvData[3 /* TLVValues.PUBLIC_KEY */]; // "A is a public key that exists only for a single login session."
|
|
462
460
|
const M1 = tlvData[4 /* TLVValues.PASSWORD_PROOF */]; // "M1 is the proof that you actually know your own password."
|
|
463
461
|
// pull the SRP server we created in stepOne out of the current session
|
|
@@ -469,26 +467,26 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
469
467
|
catch (err) {
|
|
470
468
|
// most likely the client supplied an incorrect pincode.
|
|
471
469
|
this.unsuccessfulPairAttempts++;
|
|
472
|
-
debug(
|
|
473
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
470
|
+
debug('[%s] Error while checking pincode: %s', this.accessoryInfo.username, err.message);
|
|
471
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
474
472
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
|
|
475
473
|
connection._pairSetupState = undefined;
|
|
476
474
|
return;
|
|
477
475
|
}
|
|
478
476
|
// "M2 is the proof that the server actually knows your password."
|
|
479
477
|
const M2 = srpServer.computeM2();
|
|
480
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
478
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
481
479
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 4 /* TLVValues.PASSWORD_PROOF */, M2));
|
|
482
480
|
connection._pairSetupState = 4 /* PairingStates.M4 */;
|
|
483
481
|
}
|
|
484
482
|
handlePairSetupM5(connection, request, response, tlvData) {
|
|
485
|
-
debug(
|
|
483
|
+
debug('[%s] Pair step 3/5', this.accessoryInfo.username);
|
|
486
484
|
// pull the SRP server we created in stepOne out of the current session
|
|
487
485
|
const srpServer = connection.srpServer;
|
|
488
486
|
const encryptedData = tlvData[5 /* TLVValues.ENCRYPTED_DATA */];
|
|
489
487
|
if (!encryptedData || encryptedData.length < 16) {
|
|
490
|
-
debug(
|
|
491
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
488
|
+
debug('[%s] M5: Encrypted data missing or too short', this.accessoryInfo.username);
|
|
489
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
492
490
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
|
|
493
491
|
connection._pairSetupState = undefined;
|
|
494
492
|
return;
|
|
@@ -498,16 +496,16 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
498
496
|
encryptedData.copy(messageData, 0, 0, encryptedData.length - 16);
|
|
499
497
|
encryptedData.copy(authTagData, 0, encryptedData.length - 16, encryptedData.length);
|
|
500
498
|
const S_private = srpServer.computeK();
|
|
501
|
-
const encSalt = Buffer.from(
|
|
502
|
-
const encInfo = Buffer.from(
|
|
503
|
-
const outputKey = hapCrypto.HKDF(
|
|
499
|
+
const encSalt = Buffer.from('Pair-Setup-Encrypt-Salt');
|
|
500
|
+
const encInfo = Buffer.from('Pair-Setup-Encrypt-Info');
|
|
501
|
+
const outputKey = hapCrypto.HKDF('sha512', encSalt, S_private, encInfo, 32);
|
|
504
502
|
let plaintext;
|
|
505
503
|
try {
|
|
506
|
-
plaintext = hapCrypto.chacha20_poly1305_decryptAndVerify(outputKey, Buffer.from(
|
|
504
|
+
plaintext = hapCrypto.chacha20_poly1305_decryptAndVerify(outputKey, Buffer.from('PS-Msg05'), null, messageData, authTagData);
|
|
507
505
|
}
|
|
508
506
|
catch (error) {
|
|
509
|
-
debug(
|
|
510
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
507
|
+
debug('[%s] Error while decrypting and verifying M5 subTlv: %s', this.accessoryInfo.username, error);
|
|
508
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
511
509
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
|
|
512
510
|
connection._pairSetupState = undefined;
|
|
513
511
|
return;
|
|
@@ -518,8 +516,8 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
518
516
|
M5Packet = tlv.decode(plaintext);
|
|
519
517
|
}
|
|
520
518
|
catch (error) {
|
|
521
|
-
debug(
|
|
522
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
519
|
+
debug('[%s] M5: Failed to decode decrypted TLV payload: %s', this.accessoryInfo.username, error.message);
|
|
520
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
523
521
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
524
522
|
connection._pairSetupState = undefined;
|
|
525
523
|
return;
|
|
@@ -528,8 +526,8 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
528
526
|
const clientLTPK = M5Packet[3 /* TLVValues.PUBLIC_KEY */];
|
|
529
527
|
const clientProof = M5Packet[10 /* TLVValues.PROOF */];
|
|
530
528
|
if (!clientUsername || !clientLTPK || !clientProof) {
|
|
531
|
-
debug(
|
|
532
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
529
|
+
debug('[%s] M5: Missing required TLV fields in decrypted payload', this.accessoryInfo.username);
|
|
530
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
533
531
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
534
532
|
connection._pairSetupState = undefined;
|
|
535
533
|
return;
|
|
@@ -538,15 +536,15 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
538
536
|
}
|
|
539
537
|
// M5-2
|
|
540
538
|
handlePairSetupM5_2(connection, request, response, clientUsername, clientLTPK, clientProof, hkdfEncKey) {
|
|
541
|
-
debug(
|
|
539
|
+
debug('[%s] Pair step 4/5', this.accessoryInfo.username);
|
|
542
540
|
const S_private = connection.srpServer.computeK();
|
|
543
|
-
const controllerSalt = Buffer.from(
|
|
544
|
-
const controllerInfo = Buffer.from(
|
|
545
|
-
const outputKey = hapCrypto.HKDF(
|
|
541
|
+
const controllerSalt = Buffer.from('Pair-Setup-Controller-Sign-Salt');
|
|
542
|
+
const controllerInfo = Buffer.from('Pair-Setup-Controller-Sign-Info');
|
|
543
|
+
const outputKey = hapCrypto.HKDF('sha512', controllerSalt, S_private, controllerInfo, 32);
|
|
546
544
|
const completeData = Buffer.concat([outputKey, clientUsername, clientLTPK]);
|
|
547
545
|
if (!hapCrypto.ed25519Verify(completeData, clientProof, clientLTPK)) {
|
|
548
|
-
debug(
|
|
549
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
546
|
+
debug('[%s] Invalid signature', this.accessoryInfo.username);
|
|
547
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
550
548
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 6 /* PairingStates.M6 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
|
|
551
549
|
connection._pairSetupState = undefined;
|
|
552
550
|
return;
|
|
@@ -555,29 +553,29 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
555
553
|
}
|
|
556
554
|
// M5 - F + M6
|
|
557
555
|
handlePairSetupM5_3(connection, request, response, clientUsername, clientLTPK, hkdfEncKey) {
|
|
558
|
-
debug(
|
|
556
|
+
debug('[%s] Pair step 5/5', this.accessoryInfo.username);
|
|
559
557
|
const S_private = connection.srpServer.computeK();
|
|
560
|
-
const accessorySalt = Buffer.from(
|
|
561
|
-
const accessoryInfo = Buffer.from(
|
|
562
|
-
const outputKey = hapCrypto.HKDF(
|
|
558
|
+
const accessorySalt = Buffer.from('Pair-Setup-Accessory-Sign-Salt');
|
|
559
|
+
const accessoryInfo = Buffer.from('Pair-Setup-Accessory-Sign-Info');
|
|
560
|
+
const outputKey = hapCrypto.HKDF('sha512', accessorySalt, S_private, accessoryInfo, 32);
|
|
563
561
|
const serverLTPK = this.accessoryInfo.signPk;
|
|
564
562
|
const usernameData = Buffer.from(this.accessoryInfo.username);
|
|
565
563
|
const material = Buffer.concat([outputKey, usernameData, serverLTPK]);
|
|
566
564
|
const privateKey = Buffer.from(this.accessoryInfo.signSk);
|
|
567
565
|
const serverProof = hapCrypto.ed25519Sign(material, privateKey);
|
|
568
566
|
const message = tlv.encode(1 /* TLVValues.USERNAME */, usernameData, 3 /* TLVValues.PUBLIC_KEY */, serverLTPK, 10 /* TLVValues.PROOF */, serverProof);
|
|
569
|
-
const encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(hkdfEncKey, Buffer.from(
|
|
567
|
+
const encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(hkdfEncKey, Buffer.from('PS-Msg06'), null, message);
|
|
570
568
|
// finally, notify listeners that we have been paired with a client
|
|
571
|
-
this.emit("pair" /* HAPServerEventTypes.PAIR */, clientUsername.toString(), clientLTPK,
|
|
569
|
+
this.emit("pair" /* HAPServerEventTypes.PAIR */, clientUsername.toString(), clientLTPK, once((err) => {
|
|
572
570
|
if (err) {
|
|
573
|
-
debug(
|
|
574
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
571
|
+
debug('[%s] Error adding pairing info: %s', this.accessoryInfo.username, err.message);
|
|
572
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
575
573
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 6 /* PairingStates.M6 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
576
574
|
connection._pairSetupState = undefined;
|
|
577
575
|
return;
|
|
578
576
|
}
|
|
579
577
|
// send final pairing response to client
|
|
580
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
578
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
581
579
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 6 /* PairingStates.M6 */, 5 /* TLVValues.ENCRYPTED_DATA */, Buffer.concat([encrypted.ciphertext, encrypted.authTag])));
|
|
582
580
|
connection._pairSetupState = undefined;
|
|
583
581
|
}));
|
|
@@ -588,13 +586,13 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
588
586
|
tlvData = tlv.decode(data);
|
|
589
587
|
}
|
|
590
588
|
catch (error) {
|
|
591
|
-
debug(
|
|
592
|
-
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, {
|
|
589
|
+
debug('[%s] Pair-Verify: failed to decode TLV request: %s', this.accessoryInfo.username, error.message);
|
|
590
|
+
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
593
591
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
594
592
|
return;
|
|
595
593
|
}
|
|
596
594
|
if (!tlvData[6 /* TLVValues.SEQUENCE_NUM */]) {
|
|
597
|
-
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, {
|
|
595
|
+
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
598
596
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
599
597
|
return;
|
|
600
598
|
}
|
|
@@ -602,8 +600,8 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
602
600
|
if (connection.isAuthenticated()) {
|
|
603
601
|
// M1 would replace the session keys of this connection with empty ones: it would keep counting as
|
|
604
602
|
// authenticated while accepting and sending plaintext
|
|
605
|
-
debug(
|
|
606
|
-
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, {
|
|
603
|
+
debug('[%s] Pair-Verify: rejecting pair-verify on an already verified connection', this.accessoryInfo.username);
|
|
604
|
+
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
607
605
|
response.end(tlv.encode(6 /* TLVValues.STATE */, sequence + 1, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
608
606
|
return;
|
|
609
607
|
}
|
|
@@ -615,13 +613,12 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
615
613
|
}
|
|
616
614
|
else {
|
|
617
615
|
// Invalid state/sequence number
|
|
618
|
-
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, {
|
|
616
|
+
response.writeHead(400 /* HAPPairingHTTPCode.BAD_REQUEST */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
619
617
|
response.end(tlv.encode(6 /* TLVValues.STATE */, sequence + 1, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
620
|
-
return;
|
|
621
618
|
}
|
|
622
619
|
}
|
|
623
620
|
handlePairVerifyM1(connection, request, response, tlvData) {
|
|
624
|
-
debug(
|
|
621
|
+
debug('[%s] Pair verify step 1/2', this.accessoryInfo.username);
|
|
625
622
|
const clientPublicKey = tlvData[3 /* TLVValues.PUBLIC_KEY */]; // Buffer
|
|
626
623
|
// generate new encryption keys for this session
|
|
627
624
|
const keyPair = hapCrypto.generateCurve25519KeyPair();
|
|
@@ -632,23 +629,23 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
632
629
|
const material = Buffer.concat([publicKey, usernameData, clientPublicKey]);
|
|
633
630
|
const privateKey = Buffer.from(this.accessoryInfo.signSk);
|
|
634
631
|
const serverProof = hapCrypto.ed25519Sign(material, privateKey);
|
|
635
|
-
const encSalt = Buffer.from(
|
|
636
|
-
const encInfo = Buffer.from(
|
|
637
|
-
const outputKey = hapCrypto.HKDF(
|
|
638
|
-
connection.encryption = new
|
|
632
|
+
const encSalt = Buffer.from('Pair-Verify-Encrypt-Salt');
|
|
633
|
+
const encInfo = Buffer.from('Pair-Verify-Encrypt-Info');
|
|
634
|
+
const outputKey = hapCrypto.HKDF('sha512', encSalt, sharedSec, encInfo, 32).subarray(0, 32);
|
|
635
|
+
connection.encryption = new HAPEncryption(clientPublicKey, secretKey, publicKey, sharedSec, outputKey);
|
|
639
636
|
// compose the response data in TLV format
|
|
640
637
|
const message = tlv.encode(1 /* TLVValues.USERNAME */, usernameData, 10 /* TLVValues.PROOF */, serverProof);
|
|
641
|
-
const encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(outputKey, Buffer.from(
|
|
642
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
638
|
+
const encrypted = hapCrypto.chacha20_poly1305_encryptAndSeal(outputKey, Buffer.from('PV-Msg02'), null, message);
|
|
639
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
643
640
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 2 /* PairingStates.M2 */, 5 /* TLVValues.ENCRYPTED_DATA */, Buffer.concat([encrypted.ciphertext, encrypted.authTag]), 3 /* TLVValues.PUBLIC_KEY */, publicKey));
|
|
644
641
|
connection._pairVerifyState = 2 /* PairingStates.M2 */;
|
|
645
642
|
}
|
|
646
643
|
handlePairVerifyM3(connection, request, response, objects) {
|
|
647
|
-
debug(
|
|
644
|
+
debug('[%s] Pair verify step 2/2', this.accessoryInfo.username);
|
|
648
645
|
const encryptedData = objects[5 /* TLVValues.ENCRYPTED_DATA */];
|
|
649
646
|
if (!encryptedData || encryptedData.length < 16) {
|
|
650
|
-
debug(
|
|
651
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
647
|
+
debug('[%s] M3: Encrypted data missing or too short', this.accessoryInfo.username);
|
|
648
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
652
649
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
|
|
653
650
|
connection._pairVerifyState = undefined;
|
|
654
651
|
return;
|
|
@@ -661,11 +658,11 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
661
658
|
const enc = connection.encryption;
|
|
662
659
|
let plaintext;
|
|
663
660
|
try {
|
|
664
|
-
plaintext = hapCrypto.chacha20_poly1305_decryptAndVerify(enc.hkdfPairEncryptionKey, Buffer.from(
|
|
661
|
+
plaintext = hapCrypto.chacha20_poly1305_decryptAndVerify(enc.hkdfPairEncryptionKey, Buffer.from('PV-Msg03'), null, messageData, authTagData);
|
|
665
662
|
}
|
|
666
663
|
catch (error) {
|
|
667
|
-
debug(
|
|
668
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
664
|
+
debug('[%s] M3: Failed to decrypt and/or verify: %s', this.accessoryInfo.username, error);
|
|
665
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
669
666
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
|
|
670
667
|
connection._pairVerifyState = undefined;
|
|
671
668
|
return;
|
|
@@ -675,8 +672,8 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
675
672
|
decoded = tlv.decode(plaintext);
|
|
676
673
|
}
|
|
677
674
|
catch (error) {
|
|
678
|
-
debug(
|
|
679
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
675
|
+
debug('[%s] M3: Failed to decode decrypted TLV payload: %s', this.accessoryInfo.username, error.message);
|
|
676
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
680
677
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
681
678
|
connection._pairVerifyState = undefined;
|
|
682
679
|
return;
|
|
@@ -684,8 +681,8 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
684
681
|
const clientUsername = decoded[1 /* TLVValues.USERNAME */];
|
|
685
682
|
const proof = decoded[10 /* TLVValues.PROOF */];
|
|
686
683
|
if (!clientUsername || !proof) {
|
|
687
|
-
debug(
|
|
688
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
684
|
+
debug('[%s] M3: Missing required TLV fields in decrypted payload', this.accessoryInfo.username);
|
|
685
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
689
686
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
690
687
|
connection._pairVerifyState = undefined;
|
|
691
688
|
return;
|
|
@@ -696,30 +693,30 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
696
693
|
// if we're not actually paired, then there's nothing to verify - this client thinks it's paired with us, but we
|
|
697
694
|
// disagree. Respond with invalid request (seems to match HomeKit Accessory Simulator behavior)
|
|
698
695
|
if (!clientPublicKey) {
|
|
699
|
-
debug(
|
|
700
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
696
|
+
debug('[%s] Client %s attempting to verify, but we are not paired; rejecting client', this.accessoryInfo.username, clientUsername);
|
|
697
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
701
698
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
|
|
702
699
|
connection._pairVerifyState = undefined;
|
|
703
700
|
return;
|
|
704
701
|
}
|
|
705
702
|
if (!hapCrypto.ed25519Verify(material, proof, clientPublicKey)) {
|
|
706
|
-
debug(
|
|
707
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
703
|
+
debug('[%s] Client %s provided an invalid signature', this.accessoryInfo.username, clientUsername);
|
|
704
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
708
705
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 4 /* PairingStates.M4 */, 7 /* TLVValues.ERROR_CODE */, 2 /* TLVErrorCode.AUTHENTICATION */));
|
|
709
706
|
connection._pairVerifyState = undefined;
|
|
710
707
|
return;
|
|
711
708
|
}
|
|
712
|
-
debug(
|
|
713
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
709
|
+
debug('[%s] Client %s verification complete', this.accessoryInfo.username, clientUsername);
|
|
710
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
714
711
|
response.end(tlv.encode(6 /* TLVValues.SEQUENCE_NUM */, 4 /* PairingStates.M4 */));
|
|
715
712
|
// now that the client has been verified, we must "upgrade" our pseudo-HTTP connection to include
|
|
716
713
|
// TCP-level encryption. We'll do this by adding some more encryption vars to the session, and using them
|
|
717
714
|
// in future calls to onEncrypt, onDecrypt.
|
|
718
|
-
const encSalt = Buffer.from(
|
|
719
|
-
const infoRead = Buffer.from(
|
|
720
|
-
const infoWrite = Buffer.from(
|
|
721
|
-
enc.accessoryToControllerKey = hapCrypto.HKDF(
|
|
722
|
-
enc.controllerToAccessoryKey = hapCrypto.HKDF(
|
|
715
|
+
const encSalt = Buffer.from('Control-Salt');
|
|
716
|
+
const infoRead = Buffer.from('Control-Read-Encryption-Key');
|
|
717
|
+
const infoWrite = Buffer.from('Control-Write-Encryption-Key');
|
|
718
|
+
enc.accessoryToControllerKey = hapCrypto.HKDF('sha512', encSalt, enc.sharedSecret, infoRead, 32);
|
|
719
|
+
enc.controllerToAccessoryKey = hapCrypto.HKDF('sha512', encSalt, enc.sharedSecret, infoWrite, 32);
|
|
723
720
|
// Our connection is now completely setup. We now want to subscribe this connection to special
|
|
724
721
|
connection.connectionAuthenticated(clientUsername.toString());
|
|
725
722
|
connection._pairVerifyState = undefined;
|
|
@@ -727,8 +724,8 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
727
724
|
handlePairings(connection, url, request, data, response) {
|
|
728
725
|
// Only accept /pairing request if there is a secure session
|
|
729
726
|
if (!this.allowInsecureRequest && !connection.isAuthenticated()) {
|
|
730
|
-
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, {
|
|
731
|
-
response.end(JSON.stringify({ status:
|
|
727
|
+
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
728
|
+
response.end(JSON.stringify({ status: HAPStatus.INSUFFICIENT_PRIVILEGES }));
|
|
732
729
|
return;
|
|
733
730
|
}
|
|
734
731
|
let objects;
|
|
@@ -736,73 +733,72 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
736
733
|
objects = tlv.decode(data);
|
|
737
734
|
}
|
|
738
735
|
catch (error) {
|
|
739
|
-
debug(
|
|
740
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
736
|
+
debug('[%s] Pairings: failed to decode TLV request: %s', this.accessoryInfo.username, error.message);
|
|
737
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
741
738
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
742
739
|
return;
|
|
743
740
|
}
|
|
744
741
|
if (!objects[0 /* TLVValues.METHOD */] || !objects[6 /* TLVValues.STATE */]) {
|
|
745
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
742
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
746
743
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
747
744
|
return;
|
|
748
745
|
}
|
|
749
746
|
const method = objects[0 /* TLVValues.METHOD */][0]; // value is single byte with request type
|
|
750
747
|
const state = objects[6 /* TLVValues.STATE */][0];
|
|
751
748
|
if (state !== 1 /* PairingStates.M1 */) {
|
|
752
|
-
debug(
|
|
753
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
749
|
+
debug('[%s] Pairings: received unexpected state %d', this.accessoryInfo.username, state);
|
|
750
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
754
751
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
755
752
|
return;
|
|
756
753
|
}
|
|
757
754
|
if (method === 3 /* PairMethods.ADD_PAIRING */) {
|
|
758
755
|
if (!objects[1 /* TLVValues.IDENTIFIER */] || !objects[3 /* TLVValues.PUBLIC_KEY */] || !objects[11 /* TLVValues.PERMISSIONS */]) {
|
|
759
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
756
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
760
757
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
761
758
|
return;
|
|
762
759
|
}
|
|
763
760
|
const identifier = objects[1 /* TLVValues.IDENTIFIER */].toString();
|
|
764
761
|
const publicKey = objects[3 /* TLVValues.PUBLIC_KEY */];
|
|
765
762
|
const permissions = objects[11 /* TLVValues.PERMISSIONS */][0];
|
|
766
|
-
this.emit("add-pairing" /* HAPServerEventTypes.ADD_PAIRING */, connection, identifier, publicKey, permissions,
|
|
763
|
+
this.emit("add-pairing" /* HAPServerEventTypes.ADD_PAIRING */, connection, identifier, publicKey, permissions, once((error) => {
|
|
767
764
|
if (error > 0) {
|
|
768
|
-
debug(
|
|
769
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
765
|
+
debug('[%s] Pairings: failed ADD_PAIRING with code %d', this.accessoryInfo.username, error);
|
|
766
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
770
767
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, error));
|
|
771
768
|
return;
|
|
772
769
|
}
|
|
773
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
770
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
774
771
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */));
|
|
775
|
-
debug(
|
|
772
|
+
debug('[%s] Pairings: successfully executed ADD_PAIRING', this.accessoryInfo.username);
|
|
776
773
|
}));
|
|
777
774
|
}
|
|
778
775
|
else if (method === 4 /* PairMethods.REMOVE_PAIRING */) {
|
|
779
776
|
if (!objects[1 /* TLVValues.IDENTIFIER */]) {
|
|
780
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
777
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
781
778
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
782
779
|
return;
|
|
783
780
|
}
|
|
784
781
|
const identifier = objects[1 /* TLVValues.IDENTIFIER */].toString();
|
|
785
|
-
this.emit("remove-pairing" /* HAPServerEventTypes.REMOVE_PAIRING */, connection, identifier,
|
|
782
|
+
this.emit("remove-pairing" /* HAPServerEventTypes.REMOVE_PAIRING */, connection, identifier, once((error) => {
|
|
786
783
|
if (error > 0) {
|
|
787
|
-
debug(
|
|
788
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
784
|
+
debug('[%s] Pairings: failed REMOVE_PAIRING with code %d', this.accessoryInfo.username, error);
|
|
785
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
789
786
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, error));
|
|
790
787
|
return;
|
|
791
788
|
}
|
|
792
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
789
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
793
790
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */));
|
|
794
|
-
debug(
|
|
791
|
+
debug('[%s] Pairings: successfully executed REMOVE_PAIRING', this.accessoryInfo.username);
|
|
795
792
|
}));
|
|
796
793
|
}
|
|
797
794
|
else if (method === 5 /* PairMethods.LIST_PAIRINGS */) {
|
|
798
|
-
this.emit("list-pairings" /* HAPServerEventTypes.LIST_PAIRINGS */, connection,
|
|
795
|
+
this.emit("list-pairings" /* HAPServerEventTypes.LIST_PAIRINGS */, connection, once((error, data) => {
|
|
799
796
|
if (error > 0) {
|
|
800
|
-
debug(
|
|
801
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
797
|
+
debug('[%s] Pairings: failed LIST_PAIRINGS with code %d', this.accessoryInfo.username, error);
|
|
798
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
802
799
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, error));
|
|
803
800
|
return;
|
|
804
801
|
}
|
|
805
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
806
802
|
const tlvList = [];
|
|
807
803
|
data.forEach((value, index) => {
|
|
808
804
|
if (index > 0) {
|
|
@@ -811,55 +807,55 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
811
807
|
tlvList.push(1 /* TLVValues.IDENTIFIER */, value.username, 3 /* TLVValues.PUBLIC_KEY */, value.publicKey, 11 /* TLVValues.PERMISSIONS */, value.permission);
|
|
812
808
|
});
|
|
813
809
|
const list = tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, ...tlvList);
|
|
814
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
810
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': "application/pairing+tlv8" /* HAPMimeTypes.PAIRING_TLV8 */ });
|
|
815
811
|
response.end(list);
|
|
816
|
-
debug(
|
|
812
|
+
debug('[%s] Pairings: successfully executed LIST_PAIRINGS', this.accessoryInfo.username);
|
|
817
813
|
}));
|
|
818
814
|
}
|
|
819
815
|
else {
|
|
820
|
-
debug(
|
|
821
|
-
response.writeHead(200 /* HAPPairingHTTPCode.OK */, {
|
|
816
|
+
debug('[%s] Pairings: received unsupported method %d', this.accessoryInfo.username, method);
|
|
817
|
+
response.writeHead(200 /* HAPPairingHTTPCode.OK */, { 'Content-Type': 'application/pairing+tlv8' });
|
|
822
818
|
response.end(tlv.encode(6 /* TLVValues.STATE */, 2 /* PairingStates.M2 */, 7 /* TLVValues.ERROR_CODE */, 1 /* TLVErrorCode.UNKNOWN */));
|
|
823
819
|
}
|
|
824
820
|
}
|
|
825
821
|
handleAccessories(connection, url, request, data, response) {
|
|
826
822
|
if (!this.allowInsecureRequest && !connection.isAuthenticated()) {
|
|
827
|
-
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, {
|
|
828
|
-
response.end(JSON.stringify({ status:
|
|
823
|
+
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
824
|
+
response.end(JSON.stringify({ status: HAPStatus.INSUFFICIENT_PRIVILEGES }));
|
|
829
825
|
return;
|
|
830
826
|
}
|
|
831
827
|
// call out to listeners to retrieve the latest accessories JSON
|
|
832
|
-
this.emit("accessories" /* HAPServerEventTypes.ACCESSORIES */, connection,
|
|
828
|
+
this.emit("accessories" /* HAPServerEventTypes.ACCESSORIES */, connection, once((error, result) => {
|
|
833
829
|
if (error) {
|
|
834
|
-
response.writeHead(error.httpCode, {
|
|
830
|
+
response.writeHead(error.httpCode, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
835
831
|
response.end(JSON.stringify({ status: error.status }));
|
|
836
832
|
}
|
|
837
833
|
else {
|
|
838
|
-
response.writeHead(200 /* HAPHTTPCode.OK */, {
|
|
834
|
+
response.writeHead(200 /* HAPHTTPCode.OK */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
839
835
|
response.end(JSON.stringify(result));
|
|
840
836
|
}
|
|
841
837
|
}));
|
|
842
838
|
}
|
|
843
839
|
handleCharacteristics(connection, url, request, data, response) {
|
|
844
840
|
if (!this.allowInsecureRequest && !connection.isAuthenticated()) {
|
|
845
|
-
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, {
|
|
846
|
-
response.end(JSON.stringify({ status:
|
|
841
|
+
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
842
|
+
response.end(JSON.stringify({ status: HAPStatus.INSUFFICIENT_PRIVILEGES }));
|
|
847
843
|
return;
|
|
848
844
|
}
|
|
849
|
-
if (request.method ===
|
|
845
|
+
if (request.method === 'GET') {
|
|
850
846
|
const searchParams = url.searchParams;
|
|
851
|
-
const idParam = searchParams.get(
|
|
847
|
+
const idParam = searchParams.get('id');
|
|
852
848
|
if (!idParam) {
|
|
853
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
854
|
-
response.end(JSON.stringify({ status:
|
|
849
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
850
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
855
851
|
return;
|
|
856
852
|
}
|
|
857
853
|
const ids = [];
|
|
858
|
-
for (const entry of idParam.split(
|
|
859
|
-
const split = entry.split(
|
|
854
|
+
for (const entry of idParam.split(',')) { // ["1.9","2.14"]
|
|
855
|
+
const split = entry.split('.'); // ["1","9"]
|
|
860
856
|
if (split.length !== 2 || !/^\d+$/.test(split[0]) || !/^\d+$/.test(split[1])) {
|
|
861
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
862
|
-
response.end(JSON.stringify({ status:
|
|
857
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
858
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
863
859
|
return;
|
|
864
860
|
}
|
|
865
861
|
ids.push({
|
|
@@ -868,15 +864,15 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
868
864
|
});
|
|
869
865
|
}
|
|
870
866
|
const readRequest = {
|
|
871
|
-
ids
|
|
872
|
-
includeMeta:
|
|
873
|
-
includePerms:
|
|
874
|
-
includeType:
|
|
875
|
-
includeEvent:
|
|
867
|
+
ids,
|
|
868
|
+
includeMeta: consideredTrue(searchParams.get('meta')),
|
|
869
|
+
includePerms: consideredTrue(searchParams.get('perms')),
|
|
870
|
+
includeType: consideredTrue(searchParams.get('type')),
|
|
871
|
+
includeEvent: consideredTrue(searchParams.get('ev')),
|
|
876
872
|
};
|
|
877
|
-
this.emit("get-characteristics" /* HAPServerEventTypes.GET_CHARACTERISTICS */, connection, readRequest,
|
|
873
|
+
this.emit("get-characteristics" /* HAPServerEventTypes.GET_CHARACTERISTICS */, connection, readRequest, once((error, readResponse) => {
|
|
878
874
|
if (error) {
|
|
879
|
-
response.writeHead(error.httpCode, {
|
|
875
|
+
response.writeHead(error.httpCode, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
880
876
|
response.end(JSON.stringify({ status: error.status }));
|
|
881
877
|
return;
|
|
882
878
|
}
|
|
@@ -891,37 +887,39 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
891
887
|
if (errorOccurred) { // on a 207 Multi-Status EVERY characteristic MUST include a status property
|
|
892
888
|
for (const data of characteristics) {
|
|
893
889
|
if (!data.status) { // a status is undefined if the request was successful
|
|
894
|
-
data.status =
|
|
890
|
+
data.status = HAPStatus.SUCCESS; // a value of zero indicates success
|
|
895
891
|
}
|
|
896
892
|
}
|
|
897
893
|
}
|
|
898
894
|
// 207 "multi-status" is returned when an error occurs reading a characteristic. otherwise 200 is returned
|
|
899
|
-
response.writeHead(errorOccurred ? 207 /* HAPHTTPCode.MULTI_STATUS */ : 200 /* HAPHTTPCode.OK */, {
|
|
900
|
-
response.end(JSON.stringify({ characteristics
|
|
895
|
+
response.writeHead(errorOccurred ? 207 /* HAPHTTPCode.MULTI_STATUS */ : 200 /* HAPHTTPCode.OK */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
896
|
+
response.end(JSON.stringify({ characteristics }));
|
|
901
897
|
}));
|
|
902
898
|
}
|
|
903
|
-
else if (request.method ===
|
|
899
|
+
else if (request.method === 'PUT') {
|
|
904
900
|
if (!connection.isAuthenticated()) {
|
|
905
901
|
const authorization = request.headers?.authorization;
|
|
906
|
-
const authorizationBuffer = authorization
|
|
902
|
+
const authorizationBuffer = authorization
|
|
903
|
+
? Buffer.from(authorization)
|
|
904
|
+
: undefined;
|
|
907
905
|
const pincodeBuffer = Buffer.from(this.accessoryInfo.pincode);
|
|
908
906
|
if (!authorizationBuffer
|
|
909
907
|
|| authorizationBuffer.length !== pincodeBuffer.length
|
|
910
|
-
|| !
|
|
911
|
-
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, {
|
|
912
|
-
response.end(JSON.stringify({ status:
|
|
908
|
+
|| !crypto.timingSafeEqual(authorizationBuffer, pincodeBuffer)) {
|
|
909
|
+
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
910
|
+
response.end(JSON.stringify({ status: HAPStatus.INSUFFICIENT_PRIVILEGES }));
|
|
913
911
|
return;
|
|
914
912
|
}
|
|
915
913
|
}
|
|
916
914
|
if (data.length === 0) {
|
|
917
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
918
|
-
response.end(JSON.stringify({ status:
|
|
915
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
916
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
919
917
|
return;
|
|
920
918
|
}
|
|
921
|
-
const writeRequest = JSON.parse(data.toString(
|
|
922
|
-
this.emit("set-characteristics" /* HAPServerEventTypes.SET_CHARACTERISTICS */, connection, writeRequest,
|
|
919
|
+
const writeRequest = JSON.parse(data.toString('utf8'));
|
|
920
|
+
this.emit("set-characteristics" /* HAPServerEventTypes.SET_CHARACTERISTICS */, connection, writeRequest, once((error, writeResponse) => {
|
|
923
921
|
if (error) {
|
|
924
|
-
response.writeHead(error.httpCode, {
|
|
922
|
+
response.writeHead(error.httpCode, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
925
923
|
response.end(JSON.stringify({ status: error.status }));
|
|
926
924
|
return;
|
|
927
925
|
}
|
|
@@ -936,8 +934,8 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
936
934
|
}
|
|
937
935
|
if (multiStatus) {
|
|
938
936
|
// 207 is "multi-status" since HomeKit may be setting multiple things and any one can fail independently
|
|
939
|
-
response.writeHead(207 /* HAPHTTPCode.MULTI_STATUS */, {
|
|
940
|
-
response.end(JSON.stringify({ characteristics
|
|
937
|
+
response.writeHead(207 /* HAPHTTPCode.MULTI_STATUS */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
938
|
+
response.end(JSON.stringify({ characteristics }));
|
|
941
939
|
}
|
|
942
940
|
else {
|
|
943
941
|
// if everything went fine send 204 no content response
|
|
@@ -947,87 +945,87 @@ class HAPServer extends events_1.EventEmitter {
|
|
|
947
945
|
}));
|
|
948
946
|
}
|
|
949
947
|
else {
|
|
950
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
951
|
-
response.end(JSON.stringify({ status:
|
|
948
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ }); // method not allowed
|
|
949
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
952
950
|
}
|
|
953
951
|
}
|
|
954
952
|
handlePrepareWrite(connection, url, request, data, response) {
|
|
955
953
|
if (!this.allowInsecureRequest && !connection.isAuthenticated()) {
|
|
956
|
-
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, {
|
|
957
|
-
response.end(JSON.stringify({ status:
|
|
954
|
+
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
955
|
+
response.end(JSON.stringify({ status: HAPStatus.INSUFFICIENT_PRIVILEGES }));
|
|
958
956
|
return;
|
|
959
957
|
}
|
|
960
|
-
if (request.method ===
|
|
958
|
+
if (request.method === 'PUT') {
|
|
961
959
|
if (data.length === 0) {
|
|
962
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
963
|
-
response.end(JSON.stringify({ status:
|
|
960
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
961
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
964
962
|
return;
|
|
965
963
|
}
|
|
966
964
|
const prepareRequest = JSON.parse(data.toString());
|
|
967
965
|
const ttl = prepareRequest.ttl;
|
|
968
|
-
if (prepareRequest.pid && typeof ttl ===
|
|
969
|
-
debug(
|
|
966
|
+
if (prepareRequest.pid && typeof ttl === 'number' && ttl > 0 && ttl <= HAPServer.MAX_TIMED_WRITE_TTL) {
|
|
967
|
+
debug('[%s] Received prepare write request with pid %d and ttl %d', this.accessoryInfo.username, prepareRequest.pid, prepareRequest.ttl);
|
|
970
968
|
if (connection.timedWriteTimeout) { // clear any currently existing timeouts
|
|
971
969
|
clearTimeout(connection.timedWriteTimeout);
|
|
972
970
|
}
|
|
973
971
|
connection.timedWritePid = prepareRequest.pid;
|
|
974
972
|
connection.timedWriteTimeout = setTimeout(() => {
|
|
975
|
-
debug(
|
|
973
|
+
debug('[%s] Timed write request timed out for pid %d', this.accessoryInfo.username, prepareRequest.pid);
|
|
976
974
|
connection.timedWritePid = undefined;
|
|
977
975
|
connection.timedWriteTimeout = undefined;
|
|
978
976
|
}, ttl);
|
|
979
977
|
connection.timedWriteTimeout.unref(); // the HAPConnection clears it when it closes
|
|
980
|
-
response.writeHead(200 /* HAPHTTPCode.OK */, {
|
|
981
|
-
response.end(JSON.stringify({ status:
|
|
982
|
-
return;
|
|
978
|
+
response.writeHead(200 /* HAPHTTPCode.OK */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
979
|
+
response.end(JSON.stringify({ status: HAPStatus.SUCCESS }));
|
|
983
980
|
}
|
|
984
981
|
else {
|
|
985
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
986
|
-
response.end(JSON.stringify({ status:
|
|
982
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
983
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
987
984
|
}
|
|
988
985
|
}
|
|
989
986
|
else {
|
|
990
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
991
|
-
response.end(JSON.stringify({ status:
|
|
987
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
988
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
992
989
|
}
|
|
993
990
|
}
|
|
994
991
|
handleResource(connection, url, request, data, response) {
|
|
995
992
|
if (!connection.isAuthenticated()) {
|
|
996
993
|
const authorization = request.headers?.authorization;
|
|
997
|
-
const authorizationBuffer = authorization
|
|
994
|
+
const authorizationBuffer = authorization
|
|
995
|
+
? Buffer.from(authorization)
|
|
996
|
+
: undefined;
|
|
998
997
|
const pincodeBuffer = Buffer.from(this.accessoryInfo.pincode);
|
|
999
998
|
if (!(this.allowInsecureRequest && authorizationBuffer
|
|
1000
999
|
&& authorizationBuffer.length === pincodeBuffer.length
|
|
1001
|
-
&&
|
|
1002
|
-
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, {
|
|
1003
|
-
response.end(JSON.stringify({ status:
|
|
1000
|
+
&& crypto.timingSafeEqual(authorizationBuffer, pincodeBuffer))) {
|
|
1001
|
+
response.writeHead(470 /* HAPPairingHTTPCode.CONNECTION_AUTHORIZATION_REQUIRED */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
1002
|
+
response.end(JSON.stringify({ status: HAPStatus.INSUFFICIENT_PRIVILEGES }));
|
|
1004
1003
|
return;
|
|
1005
1004
|
}
|
|
1006
1005
|
}
|
|
1007
|
-
if (request.method ===
|
|
1006
|
+
if (request.method === 'POST') {
|
|
1008
1007
|
if (data.length === 0) {
|
|
1009
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
1010
|
-
response.end(JSON.stringify({ status:
|
|
1008
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
1009
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
1011
1010
|
return;
|
|
1012
1011
|
}
|
|
1013
1012
|
const resourceRequest = JSON.parse(data.toString());
|
|
1014
1013
|
// call out to listeners to retrieve the resource, snapshot only right now
|
|
1015
|
-
this.emit("request-resource" /* HAPServerEventTypes.REQUEST_RESOURCE */, resourceRequest,
|
|
1014
|
+
this.emit("request-resource" /* HAPServerEventTypes.REQUEST_RESOURCE */, resourceRequest, once((error, resource) => {
|
|
1016
1015
|
if (error) {
|
|
1017
|
-
response.writeHead(error.httpCode, {
|
|
1016
|
+
response.writeHead(error.httpCode, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ });
|
|
1018
1017
|
response.end(JSON.stringify({ status: error.status }));
|
|
1019
1018
|
}
|
|
1020
1019
|
else {
|
|
1021
|
-
response.writeHead(200 /* HAPHTTPCode.OK */, {
|
|
1020
|
+
response.writeHead(200 /* HAPHTTPCode.OK */, { 'Content-Type': "image/jpeg" /* HAPMimeTypes.IMAGE_JPEG */ });
|
|
1022
1021
|
response.end(resource);
|
|
1023
1022
|
}
|
|
1024
1023
|
}));
|
|
1025
1024
|
}
|
|
1026
1025
|
else {
|
|
1027
|
-
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, {
|
|
1028
|
-
response.end(JSON.stringify({ status:
|
|
1026
|
+
response.writeHead(400 /* HAPHTTPCode.BAD_REQUEST */, { 'Content-Type': "application/hap+json" /* HAPMimeTypes.HAP_JSON */ }); // method not allowed
|
|
1027
|
+
response.end(JSON.stringify({ status: HAPStatus.INVALID_VALUE_IN_REQUEST }));
|
|
1029
1028
|
}
|
|
1030
1029
|
}
|
|
1031
1030
|
}
|
|
1032
|
-
exports.HAPServer = HAPServer;
|
|
1033
1031
|
//# sourceMappingURL=HAPServer.js.map
|