@homebridge/hap-nodejs 2.2.4-beta.6 → 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 +50 -43
|
@@ -1,26 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const debugCon = (0, debug_1.default)("HAP-NodeJS:EventedHTTPServer:Connection");
|
|
17
|
-
const debugEvents = (0, debug_1.default)("HAP-NodeJS:EventEmitter");
|
|
1
|
+
/* global NodeJS */
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import { Buffer } from 'node:buffer';
|
|
4
|
+
import { EventEmitter } from 'node:events';
|
|
5
|
+
import http from 'node:http';
|
|
6
|
+
import net from 'node:net';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
import { getNetAddress } from '@homebridge/ciao/lib/util/domain-formatter.js';
|
|
9
|
+
import createDebug from 'debug';
|
|
10
|
+
import * as hapCrypto from './hapCrypto.js';
|
|
11
|
+
import { getOSLoopbackAddressIfAvailable } from './net-utils.js';
|
|
12
|
+
import * as uuid from './uuid.js';
|
|
13
|
+
const debug = createDebug('HAP-NodeJS:EventedHTTPServer');
|
|
14
|
+
const debugCon = createDebug('HAP-NodeJS:EventedHTTPServer:Connection');
|
|
15
|
+
const debugEvents = createDebug('HAP-NodeJS:EventEmitter');
|
|
18
16
|
/**
|
|
19
17
|
* Simple struct to hold vars needed to support HAP encryption.
|
|
20
18
|
*
|
|
21
19
|
* @group Cryptography
|
|
22
20
|
*/
|
|
23
|
-
class HAPEncryption {
|
|
21
|
+
export class HAPEncryption {
|
|
24
22
|
clientPublicKey;
|
|
25
23
|
secretKey;
|
|
26
24
|
publicKey;
|
|
@@ -41,17 +39,17 @@ class HAPEncryption {
|
|
|
41
39
|
this.controllerToAccessoryKey = Buffer.alloc(0);
|
|
42
40
|
}
|
|
43
41
|
}
|
|
44
|
-
exports.HAPEncryption = HAPEncryption;
|
|
45
42
|
/**
|
|
46
43
|
* @group HAP Accessory Server
|
|
47
44
|
*/
|
|
48
|
-
|
|
45
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
46
|
+
export var EventedHTTPServerEvent;
|
|
49
47
|
(function (EventedHTTPServerEvent) {
|
|
50
48
|
EventedHTTPServerEvent["LISTENING"] = "listening";
|
|
51
49
|
EventedHTTPServerEvent["CONNECTION_OPENED"] = "connection-opened";
|
|
52
50
|
EventedHTTPServerEvent["REQUEST"] = "request";
|
|
53
51
|
EventedHTTPServerEvent["CONNECTION_CLOSED"] = "connection-closed";
|
|
54
|
-
})(EventedHTTPServerEvent || (
|
|
52
|
+
})(EventedHTTPServerEvent || (EventedHTTPServerEvent = {}));
|
|
55
53
|
/**
|
|
56
54
|
* EventedHTTPServer provides an HTTP-like server that supports HAP "extensions" for security and events.
|
|
57
55
|
*
|
|
@@ -71,8 +69,8 @@ var EventedHTTPServerEvent;
|
|
|
71
69
|
*
|
|
72
70
|
* @group HAP Accessory Server
|
|
73
71
|
*/
|
|
74
|
-
// eslint-disable-next-line
|
|
75
|
-
class EventedHTTPServer extends
|
|
72
|
+
// eslint-disable-next-line ts/no-unsafe-declaration-merging
|
|
73
|
+
export class EventedHTTPServer extends EventEmitter {
|
|
76
74
|
static CONNECTION_TIMEOUT_LIMIT = 16; // if we have more (or equal) # connections we start the timeout
|
|
77
75
|
static MAX_CONNECTION_IDLE_TIME = 60 * 60 * 1000; // 1h
|
|
78
76
|
tcpServer;
|
|
@@ -89,20 +87,20 @@ class EventedHTTPServer extends events_1.EventEmitter {
|
|
|
89
87
|
connectionLoggingInterval;
|
|
90
88
|
constructor() {
|
|
91
89
|
super();
|
|
92
|
-
this.tcpServer =
|
|
90
|
+
this.tcpServer = net.createServer();
|
|
93
91
|
}
|
|
94
92
|
scheduleNextConnectionIdleTimeout() {
|
|
95
93
|
this.connectionIdleTimeout = undefined;
|
|
96
94
|
if (!this.tcpServer.listening) {
|
|
97
95
|
return;
|
|
98
96
|
}
|
|
99
|
-
debug(
|
|
100
|
-
const currentTime =
|
|
97
|
+
debug('Running idle timeout timer...');
|
|
98
|
+
const currentTime = Date.now();
|
|
101
99
|
let nextTimeout = Infinity; // the connection which reaches the idle time first
|
|
102
100
|
for (const connection of this.connections) {
|
|
103
101
|
const timeDelta = currentTime - connection.lastSocketOperation;
|
|
104
102
|
if (timeDelta >= EventedHTTPServer.MAX_CONNECTION_IDLE_TIME) {
|
|
105
|
-
debug(
|
|
103
|
+
debug(`[%s] Closing connection as it was inactive for ${timeDelta}ms`, connection.remoteAddress);
|
|
106
104
|
connection.close();
|
|
107
105
|
}
|
|
108
106
|
else {
|
|
@@ -120,17 +118,19 @@ class EventedHTTPServer extends events_1.EventEmitter {
|
|
|
120
118
|
listen(targetPort, hostname) {
|
|
121
119
|
this.tcpServer.listen(targetPort, hostname, () => {
|
|
122
120
|
const address = this.tcpServer.address(); // address() is only a string when listening to unix domain sockets
|
|
123
|
-
debug(
|
|
121
|
+
debug('Server listening on %s:%s', address.family === 'IPv6'
|
|
122
|
+
? `[${address.address}]`
|
|
123
|
+
: address.address, address.port);
|
|
124
124
|
this.connectionLoggingInterval = setInterval(() => {
|
|
125
125
|
const connectionInformation = [...this.connections]
|
|
126
126
|
.map(connection => `${connection.remoteAddress}:${connection.remotePort}`)
|
|
127
|
-
.join(
|
|
128
|
-
debug(
|
|
127
|
+
.join(', ');
|
|
128
|
+
debug('Currently %d hap connections open: %s', this.connections.size, connectionInformation);
|
|
129
129
|
}, 60_000);
|
|
130
130
|
this.connectionLoggingInterval.unref();
|
|
131
131
|
this.emit("listening" /* EventedHTTPServerEvent.LISTENING */, address.port, address.address);
|
|
132
132
|
});
|
|
133
|
-
this.tcpServer.on(
|
|
133
|
+
this.tcpServer.on('connection', this.onConnection.bind(this));
|
|
134
134
|
}
|
|
135
135
|
stop() {
|
|
136
136
|
if (this.connectionLoggingInterval != null) {
|
|
@@ -159,12 +159,12 @@ class EventedHTTPServer extends events_1.EventEmitter {
|
|
|
159
159
|
* @param value - The newly set value of the characteristic.
|
|
160
160
|
* @param originator - If specified, the connection will not get an event message.
|
|
161
161
|
* @param immediateDelivery - The HAP spec requires some characteristics to be delivery immediately.
|
|
162
|
-
*
|
|
162
|
+
* Namely, for the {@link Characteristic.ButtonEvent} and the {@link Characteristic.ProgrammableSwitchEvent} characteristics.
|
|
163
163
|
*/
|
|
164
164
|
broadcastEvent(aid, iid, value, originator, immediateDelivery) {
|
|
165
165
|
for (const connection of this.connections) {
|
|
166
166
|
if (connection === originator) {
|
|
167
|
-
debug(
|
|
167
|
+
debug('[%s] Muting event \'%s\' notification for this connection since it originated here.', connection.remoteAddress, `${aid}.${iid}`);
|
|
168
168
|
continue;
|
|
169
169
|
}
|
|
170
170
|
connection.sendEvent(aid, iid, value, immediateDelivery);
|
|
@@ -184,7 +184,7 @@ class EventedHTTPServer extends events_1.EventEmitter {
|
|
|
184
184
|
return iids;
|
|
185
185
|
}
|
|
186
186
|
onConnection(socket) {
|
|
187
|
-
// eslint-disable-next-line
|
|
187
|
+
// eslint-disable-next-line ts/no-use-before-define
|
|
188
188
|
const connection = new HAPConnection(this, socket);
|
|
189
189
|
connection.on("request" /* HAPConnectionEvent.REQUEST */, (request, response) => {
|
|
190
190
|
this.emit("request" /* EventedHTTPServerEvent.REQUEST */, connection, request, response);
|
|
@@ -192,7 +192,7 @@ class EventedHTTPServer extends events_1.EventEmitter {
|
|
|
192
192
|
connection.on("authenticated" /* HAPConnectionEvent.AUTHENTICATED */, this.handleConnectionAuthenticated.bind(this, connection));
|
|
193
193
|
connection.on("closed" /* HAPConnectionEvent.CLOSED */, this.handleConnectionClose.bind(this, connection));
|
|
194
194
|
this.connections.add(connection);
|
|
195
|
-
debug(
|
|
195
|
+
debug('[%s] New connection from client on interface %s (%s)', connection.remoteAddress, connection.networkInterface, connection.localAddress);
|
|
196
196
|
this.emit("connection-opened" /* EventedHTTPServerEvent.CONNECTION_OPENED */, connection);
|
|
197
197
|
if (this.connections.size >= EventedHTTPServer.CONNECTION_TIMEOUT_LIMIT && !this.connectionIdleTimeout) {
|
|
198
198
|
this.scheduleNextConnectionIdleTimeout();
|
|
@@ -242,12 +242,12 @@ class EventedHTTPServer extends events_1.EventEmitter {
|
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
244
|
}
|
|
245
|
-
exports.EventedHTTPServer = EventedHTTPServer;
|
|
246
245
|
/**
|
|
247
246
|
* @private
|
|
248
247
|
* @group HAP Accessory Server
|
|
249
248
|
*/
|
|
250
|
-
|
|
249
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
250
|
+
export var HAPConnectionState;
|
|
251
251
|
(function (HAPConnectionState) {
|
|
252
252
|
HAPConnectionState[HAPConnectionState["CONNECTING"] = 0] = "CONNECTING";
|
|
253
253
|
HAPConnectionState[HAPConnectionState["FULLY_SET_UP"] = 1] = "FULLY_SET_UP";
|
|
@@ -257,22 +257,23 @@ var HAPConnectionState;
|
|
|
257
257
|
HAPConnectionState[HAPConnectionState["TO_BE_TEARED_DOWN"] = 3] = "TO_BE_TEARED_DOWN";
|
|
258
258
|
HAPConnectionState[HAPConnectionState["CLOSING"] = 4] = "CLOSING";
|
|
259
259
|
HAPConnectionState[HAPConnectionState["CLOSED"] = 5] = "CLOSED";
|
|
260
|
-
})(HAPConnectionState || (
|
|
260
|
+
})(HAPConnectionState || (HAPConnectionState = {}));
|
|
261
261
|
/**
|
|
262
262
|
* @group HAP Accessory Server
|
|
263
263
|
*/
|
|
264
|
-
|
|
264
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
265
|
+
export var HAPConnectionEvent;
|
|
265
266
|
(function (HAPConnectionEvent) {
|
|
266
267
|
HAPConnectionEvent["REQUEST"] = "request";
|
|
267
268
|
HAPConnectionEvent["AUTHENTICATED"] = "authenticated";
|
|
268
269
|
HAPConnectionEvent["CLOSED"] = "closed";
|
|
269
|
-
})(HAPConnectionEvent || (
|
|
270
|
+
})(HAPConnectionEvent || (HAPConnectionEvent = {}));
|
|
270
271
|
/**
|
|
271
272
|
* Manages a single iOS-initiated HTTP connection during its lifetime.
|
|
272
273
|
* @group HAP Accessory Server
|
|
273
274
|
*/
|
|
274
|
-
// eslint-disable-next-line
|
|
275
|
-
class HAPConnection extends
|
|
275
|
+
// eslint-disable-next-line ts/no-unsafe-declaration-merging
|
|
276
|
+
export class HAPConnection extends EventEmitter {
|
|
276
277
|
/**
|
|
277
278
|
* Delay in milliseconds before queued (non-immediate) characteristic event notifications are coalesced and flushed on
|
|
278
279
|
* the connection. HAP batches regular events so a burst of value changes produces a single notification rather than one
|
|
@@ -286,7 +287,7 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
286
287
|
*/
|
|
287
288
|
static unmatchedLocalAddresses = new Set();
|
|
288
289
|
/**
|
|
289
|
-
* @private
|
|
290
|
+
* @private
|
|
290
291
|
*/
|
|
291
292
|
server;
|
|
292
293
|
sessionID; // uuid unique to every HAP connection
|
|
@@ -300,7 +301,7 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
300
301
|
httpSocket; // set when in state FULLY_SET_UP
|
|
301
302
|
internalHttpServerPort;
|
|
302
303
|
internalHttpServerAddress;
|
|
303
|
-
lastSocketOperation =
|
|
304
|
+
lastSocketOperation = Date.now();
|
|
304
305
|
pendingClientSocketData = Buffer.alloc(0); // data received from client before HTTP proxy is fully setup
|
|
305
306
|
handlingRequest = false; // true while we are composing an HTTP response (so events can wait)
|
|
306
307
|
/**
|
|
@@ -325,59 +326,61 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
325
326
|
constructor(server, clientSocket) {
|
|
326
327
|
super();
|
|
327
328
|
this.server = server;
|
|
328
|
-
this.sessionID = uuid.generate(clientSocket.remoteAddress
|
|
329
|
+
this.sessionID = uuid.generate(`${clientSocket.remoteAddress}:${clientSocket.remotePort}`);
|
|
329
330
|
this.localAddress = clientSocket.localAddress;
|
|
330
331
|
this.remoteAddress = clientSocket.remoteAddress; // cache because it becomes undefined in 'onClientSocketClose'
|
|
331
332
|
this.remotePort = clientSocket.remotePort;
|
|
332
333
|
this.networkInterface = HAPConnection.getLocalNetworkInterface(clientSocket);
|
|
333
334
|
// clientSocket is the socket connected to the actual iOS device
|
|
334
335
|
this.tcpSocket = clientSocket;
|
|
335
|
-
this.tcpSocket.on(
|
|
336
|
-
this.tcpSocket.on(
|
|
336
|
+
this.tcpSocket.on('data', this.onTCPSocketData.bind(this));
|
|
337
|
+
this.tcpSocket.on('close', this.onTCPSocketClose.bind(this));
|
|
337
338
|
// we MUST register for this event, otherwise the error will bubble up to the top and crash the node process entirely.
|
|
338
|
-
this.tcpSocket.on(
|
|
339
|
+
this.tcpSocket.on('error', this.onTCPSocketError.bind(this));
|
|
339
340
|
this.tcpSocket.setNoDelay(true); // disable Nagle algorithm
|
|
340
341
|
// "HAP accessory servers must not use keepalive messages, which periodically wake up iOS devices".
|
|
341
342
|
// Thus, we don't configure any tcp keepalive
|
|
342
343
|
// create our internal HTTP server for this connection that we will proxy data to and from
|
|
343
|
-
this.internalHttpServer =
|
|
344
|
+
this.internalHttpServer = http.createServer();
|
|
344
345
|
this.internalHttpServer.timeout = 0; // clients expect to hold connections open as long as they want
|
|
345
346
|
this.internalHttpServer.keepAliveTimeout = 0; // workaround for https://github.com/nodejs/node/issues/13391
|
|
346
|
-
this.internalHttpServer.on(
|
|
347
|
-
this.internalHttpServer.on(
|
|
348
|
-
this.internalHttpServer.on(
|
|
349
|
-
this.internalHttpServer.on(
|
|
347
|
+
this.internalHttpServer.on('listening', this.onHttpServerListening.bind(this));
|
|
348
|
+
this.internalHttpServer.on('connection', this.onHttpServerConnection.bind(this));
|
|
349
|
+
this.internalHttpServer.on('request', this.handleHttpServerRequest.bind(this));
|
|
350
|
+
this.internalHttpServer.on('error', this.onHttpServerError.bind(this));
|
|
350
351
|
// close event is added later on the "connect" event as possible listen retries would throw unnecessary close events
|
|
351
352
|
this.internalHttpServer.listen(0, this.internalHttpServerAddress = HAPConnection.internalHttpServerListenAddress());
|
|
352
353
|
}
|
|
353
354
|
debugListenerRegistration(event, registration = true, beforeCount = -1) {
|
|
354
|
-
|
|
355
|
+
// eslint-disable-next-line unicorn/error-message -- only here to capture the stack
|
|
356
|
+
const stackTrace = new Error().stack.split('\n')[3];
|
|
355
357
|
const eventCount = this.listeners(event).length;
|
|
356
|
-
const tabs1 = event === "authenticated" /* HAPConnectionEvent.AUTHENTICATED */
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
358
|
+
const tabs1 = event === "authenticated" /* HAPConnectionEvent.AUTHENTICATED */
|
|
359
|
+
? '\t'
|
|
360
|
+
: '\t\t';
|
|
361
|
+
const tabs2 = !registration
|
|
362
|
+
? '\t'
|
|
363
|
+
: '\t\t';
|
|
364
|
+
debugEvents(`[${this.remoteAddress}] ${registration ? 'Registered' : 'Unregistered'} event '${String(event).toUpperCase()}' ${tabs1}(total: ${eventCount}${!registration
|
|
365
|
+
? ` Before: ${beforeCount}`
|
|
366
|
+
: ''}) ${tabs2}${stackTrace}`);
|
|
360
367
|
}
|
|
361
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
362
368
|
on(event, listener) {
|
|
363
369
|
const result = super.on(event, listener);
|
|
364
370
|
this.debugListenerRegistration(event);
|
|
365
371
|
return result;
|
|
366
372
|
}
|
|
367
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
368
373
|
addListener(event, listener) {
|
|
369
374
|
const result = super.addListener(event, listener);
|
|
370
375
|
this.debugListenerRegistration(event);
|
|
371
376
|
return result;
|
|
372
377
|
}
|
|
373
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
374
378
|
removeListener(event, listener) {
|
|
375
379
|
const beforeCount = this.listeners(event).length;
|
|
376
380
|
const result = super.removeListener(event, listener);
|
|
377
381
|
this.debugListenerRegistration(event, false, beforeCount);
|
|
378
382
|
return result;
|
|
379
383
|
}
|
|
380
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
381
384
|
off(event, listener) {
|
|
382
385
|
const beforeCount = this.listeners(event).length;
|
|
383
386
|
const result = super.off(event, listener);
|
|
@@ -418,17 +421,17 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
418
421
|
}
|
|
419
422
|
}
|
|
420
423
|
sendEvent(aid, iid, value, immediateDelivery) {
|
|
421
|
-
(
|
|
422
|
-
(
|
|
423
|
-
const eventName = aid
|
|
424
|
+
assert(aid != null, 'HAPConnection.sendEvent: aid must be defined!');
|
|
425
|
+
assert(iid != null, 'HAPConnection.sendEvent: iid must be defined!');
|
|
426
|
+
const eventName = `${aid}.${iid}`;
|
|
424
427
|
if (!this.registeredEvents.has(eventName)) {
|
|
425
428
|
// non verified connections can't register events, so this case is covered!
|
|
426
429
|
return;
|
|
427
430
|
}
|
|
428
431
|
const event = {
|
|
429
|
-
aid
|
|
430
|
-
iid
|
|
431
|
-
value
|
|
432
|
+
aid,
|
|
433
|
+
iid,
|
|
434
|
+
value,
|
|
432
435
|
};
|
|
433
436
|
if (immediateDelivery) {
|
|
434
437
|
// some characteristics are required to deliver notifications immediately
|
|
@@ -482,7 +485,7 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
482
485
|
characteristics: [],
|
|
483
486
|
};
|
|
484
487
|
for (const queuedEvent of this.queuedEvents) {
|
|
485
|
-
if (!this.registeredEvents.has(queuedEvent.aid
|
|
488
|
+
if (!this.registeredEvents.has(`${queuedEvent.aid}.${queuedEvent.iid}`)) {
|
|
486
489
|
continue; // client unregistered that event in the meantime
|
|
487
490
|
}
|
|
488
491
|
eventData.characteristics.push(queuedEvent);
|
|
@@ -502,24 +505,24 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
502
505
|
* @param notification - The event which should be sent out
|
|
503
506
|
*/
|
|
504
507
|
writeEventNotification(notification) {
|
|
505
|
-
debugCon(
|
|
506
|
-
(
|
|
508
|
+
debugCon('[%s] Sending HAP event notifications %o', this.remoteAddress, notification.characteristics);
|
|
509
|
+
assert(!this.handlingRequest, 'Can\'t write event notifications while handling a request!');
|
|
507
510
|
// Apple backend processes events in reverse order, so we need to reverse the array
|
|
508
511
|
// so that events are processed in chronological order.
|
|
509
512
|
notification.characteristics.reverse();
|
|
510
|
-
const dataBuffer = Buffer.from(JSON.stringify(notification),
|
|
511
|
-
const header = Buffer.from(
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
513
|
+
const dataBuffer = Buffer.from(JSON.stringify(notification), 'utf8');
|
|
514
|
+
const header = Buffer.from('EVENT/1.0 200 OK\r\n'
|
|
515
|
+
+ 'Content-Type: application/hap+json\r\n'
|
|
516
|
+
+ `Content-Length: ${dataBuffer.length}\r\n`
|
|
517
|
+
+ '\r\n', 'utf8');
|
|
515
518
|
const buffer = Buffer.concat([header, dataBuffer]);
|
|
516
519
|
this.tcpSocket.write(this.encrypt(buffer), this.handleTCPSocketWriteFulfilled.bind(this));
|
|
517
520
|
}
|
|
518
521
|
enableEventNotifications(aid, iid) {
|
|
519
|
-
this.registeredEvents.add(aid
|
|
522
|
+
this.registeredEvents.add(`${aid}.${iid}`);
|
|
520
523
|
}
|
|
521
524
|
disableEventNotifications(aid, iid) {
|
|
522
|
-
this.registeredEvents.delete(aid
|
|
525
|
+
this.registeredEvents.delete(`${aid}.${iid}`);
|
|
523
526
|
}
|
|
524
527
|
/**
|
|
525
528
|
* Removes all event registrations of this connection for the accessory with the given aid.
|
|
@@ -539,7 +542,7 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
539
542
|
return iids;
|
|
540
543
|
}
|
|
541
544
|
hasEventNotifications(aid, iid) {
|
|
542
|
-
return this.registeredEvents.has(aid
|
|
545
|
+
return this.registeredEvents.has(`${aid}.${iid}`);
|
|
543
546
|
}
|
|
544
547
|
getRegisteredEvents() {
|
|
545
548
|
return this.registeredEvents;
|
|
@@ -566,24 +569,26 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
566
569
|
}
|
|
567
570
|
onHttpServerListening() {
|
|
568
571
|
const addressInfo = this.internalHttpServer.address(); // address() is only a string when listening to unix domain sockets
|
|
569
|
-
const addressString = addressInfo.family ===
|
|
572
|
+
const addressString = addressInfo.family === 'IPv6'
|
|
573
|
+
? `[${addressInfo.address}]`
|
|
574
|
+
: addressInfo.address;
|
|
570
575
|
this.internalHttpServerPort = addressInfo.port;
|
|
571
|
-
debugCon(
|
|
572
|
-
this.internalHttpServer.on(
|
|
576
|
+
debugCon('[%s] Internal HTTP server listening on %s:%s', this.remoteAddress, addressString, addressInfo.port);
|
|
577
|
+
this.internalHttpServer.on('close', this.onHttpServerClose.bind(this));
|
|
573
578
|
// now we can establish a connection to this running HTTP server for proxying data
|
|
574
|
-
this.httpSocket =
|
|
579
|
+
this.httpSocket = net.createConnection(this.internalHttpServerPort, this.internalHttpServerAddress); // previously we used addressInfo.address
|
|
575
580
|
this.httpSocket.setNoDelay(true); // disable Nagle algorithm
|
|
576
|
-
this.httpSocket.on(
|
|
581
|
+
this.httpSocket.on('data', this.handleHttpServerResponse.bind(this));
|
|
577
582
|
// we MUST register for this event, otherwise the error will bubble up to the top and crash the node process entirely.
|
|
578
|
-
this.httpSocket.on(
|
|
579
|
-
this.httpSocket.on(
|
|
580
|
-
this.httpSocket.on(
|
|
583
|
+
this.httpSocket.on('error', this.onHttpSocketError.bind(this));
|
|
584
|
+
this.httpSocket.on('close', this.onHttpSocketClose.bind(this));
|
|
585
|
+
this.httpSocket.on('connect', () => {
|
|
581
586
|
// we are now fully set up:
|
|
582
587
|
// - clientSocket is connected to the iOS device
|
|
583
588
|
// - serverSocket is connected to the httpServer
|
|
584
589
|
// - ready to proxy data!
|
|
585
590
|
this.state = 1 /* HAPConnectionState.FULLY_SET_UP */;
|
|
586
|
-
debugCon(
|
|
591
|
+
debugCon('[%s] Internal HTTP socket connected. HAPConnection now fully set up!', this.remoteAddress);
|
|
587
592
|
// start by flushing any pending buffered data received from the client while we were setting up
|
|
588
593
|
if (this.pendingClientSocketData && this.pendingClientSocketData.length > 0) {
|
|
589
594
|
this.httpSocket.write(this.pendingClientSocketData);
|
|
@@ -601,12 +606,12 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
601
606
|
return;
|
|
602
607
|
}
|
|
603
608
|
this.handlingRequest = true; // reverted to false once response was sent out
|
|
604
|
-
this.lastSocketOperation =
|
|
609
|
+
this.lastSocketOperation = Date.now();
|
|
605
610
|
try {
|
|
606
611
|
data = this.decrypt(data);
|
|
607
612
|
}
|
|
608
613
|
catch (error) { // decryption and/or verification failed, disconnect the client
|
|
609
|
-
debugCon(
|
|
614
|
+
debugCon('[%s] Error occurred trying to decrypt incoming packet: %s', this.remoteAddress, error.message);
|
|
610
615
|
this.close();
|
|
611
616
|
return;
|
|
612
617
|
}
|
|
@@ -630,7 +635,7 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
630
635
|
// the server can see our proxy socket before its "connect" event told us its local port, so until then
|
|
631
636
|
// a foreign socket is left to handleHttpServerRequest, which only runs once the proxy is connected
|
|
632
637
|
if (this.httpSocket?.localPort !== undefined && !this.isProxySocket(socket)) {
|
|
633
|
-
debugCon(
|
|
638
|
+
debugCon('[%s] Rejected a connection to the internal HTTP server from %s:%s', this.remoteAddress, socket.remoteAddress, socket.remotePort);
|
|
634
639
|
socket.destroy();
|
|
635
640
|
}
|
|
636
641
|
}
|
|
@@ -642,7 +647,7 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
642
647
|
handleHttpServerRequest(request, response) {
|
|
643
648
|
if (!this.isProxySocket(request.socket)) {
|
|
644
649
|
// a socket which connected before our proxy socket did, so onHttpServerConnection couldn't tell it apart yet
|
|
645
|
-
debugCon(
|
|
650
|
+
debugCon('[%s] Rejected a request on the internal HTTP server which did not come through the proxy socket', this.remoteAddress);
|
|
646
651
|
request.socket.destroy();
|
|
647
652
|
return;
|
|
648
653
|
}
|
|
@@ -650,9 +655,9 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
650
655
|
// don't accept data of a connection which is about to be closed or already closed
|
|
651
656
|
return;
|
|
652
657
|
}
|
|
653
|
-
debugCon(
|
|
658
|
+
debugCon('[%s] HTTP request: %s', this.remoteAddress, request.url);
|
|
654
659
|
request.socket.setNoDelay(true);
|
|
655
|
-
response.once(
|
|
660
|
+
response.once('finish', () => {
|
|
656
661
|
// the response was handed to the server side of the proxy socket: it ends at the bytes written so far
|
|
657
662
|
this.httpResponseEnd = request.socket.bytesWritten;
|
|
658
663
|
this.handleHttpResponseCompleteIfReceived();
|
|
@@ -679,7 +684,7 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
679
684
|
return;
|
|
680
685
|
}
|
|
681
686
|
this.httpResponseEnd = undefined;
|
|
682
|
-
debugCon(
|
|
687
|
+
debugCon('[%s] HTTP Response is finished', this.remoteAddress);
|
|
683
688
|
this.handlingRequest = false;
|
|
684
689
|
if (this.state === 3 /* HAPConnectionState.TO_BE_TEARED_DOWN */) {
|
|
685
690
|
setTimeout(() => this.close(), 10);
|
|
@@ -693,15 +698,15 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
693
698
|
}
|
|
694
699
|
}
|
|
695
700
|
handleTCPSocketWriteFulfilled() {
|
|
696
|
-
this.lastSocketOperation =
|
|
701
|
+
this.lastSocketOperation = Date.now();
|
|
697
702
|
}
|
|
698
703
|
onTCPSocketError(err) {
|
|
699
|
-
debugCon(
|
|
704
|
+
debugCon('[%s] Client connection error: %s', this.remoteAddress, err.message);
|
|
700
705
|
// onTCPSocketClose will be called next
|
|
701
706
|
}
|
|
702
707
|
onTCPSocketClose() {
|
|
703
708
|
this.state = 5 /* HAPConnectionState.CLOSED */;
|
|
704
|
-
debugCon(
|
|
709
|
+
debugCon('[%s] Client connection closed', this.remoteAddress);
|
|
705
710
|
if (this.timedWriteTimeout) {
|
|
706
711
|
clearTimeout(this.timedWriteTimeout);
|
|
707
712
|
this.timedWriteTimeout = undefined;
|
|
@@ -715,43 +720,43 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
715
720
|
this.removeAllListeners(); // cleanup listeners, we are officially dead now
|
|
716
721
|
}
|
|
717
722
|
onHttpServerError(err) {
|
|
718
|
-
debugCon(
|
|
719
|
-
if (err.code ===
|
|
723
|
+
debugCon('[%s] HTTP server error: %s', this.remoteAddress, err.message);
|
|
724
|
+
if (err.code === 'EADDRINUSE') {
|
|
720
725
|
this.internalHttpServerPort = undefined;
|
|
721
726
|
this.internalHttpServer.close();
|
|
722
727
|
this.internalHttpServer.listen(0, this.internalHttpServerAddress = HAPConnection.internalHttpServerListenAddress());
|
|
723
728
|
}
|
|
724
729
|
}
|
|
725
730
|
onHttpServerClose() {
|
|
726
|
-
debugCon(
|
|
731
|
+
debugCon('[%s] HTTP server was closed', this.remoteAddress);
|
|
727
732
|
// make sure the iOS side is closed as well
|
|
728
733
|
this.close();
|
|
729
734
|
}
|
|
730
735
|
onHttpSocketError(err) {
|
|
731
|
-
debugCon(
|
|
736
|
+
debugCon('[%s] HTTP connection error: ', this.remoteAddress, err.message);
|
|
732
737
|
// onHttpSocketClose will be called next
|
|
733
738
|
}
|
|
734
739
|
onHttpSocketClose() {
|
|
735
|
-
debugCon(
|
|
740
|
+
debugCon('[%s] HTTP connection was closed', this.remoteAddress);
|
|
736
741
|
// we only support a single long-lived connection to our internal HTTP server. Since it's closed,
|
|
737
742
|
// we'll need to shut it down entirely.
|
|
738
743
|
this.internalHttpServer.close();
|
|
739
744
|
}
|
|
740
745
|
getLocalAddress(ipVersion) {
|
|
741
|
-
const interfaceDetails =
|
|
746
|
+
const interfaceDetails = os.networkInterfaces()[this.networkInterface];
|
|
742
747
|
if (!interfaceDetails) {
|
|
743
|
-
throw new Error(
|
|
748
|
+
throw new Error(`Could not find ${ipVersion} address for interface ${this.networkInterface}`);
|
|
744
749
|
}
|
|
745
750
|
// Find our first local IPv4 address.
|
|
746
|
-
if (ipVersion ===
|
|
747
|
-
const ipv4Info = interfaceDetails.find(info => info.family ===
|
|
751
|
+
if (ipVersion === 'ipv4') {
|
|
752
|
+
const ipv4Info = interfaceDetails.find(info => info.family === 'IPv4');
|
|
748
753
|
if (ipv4Info) {
|
|
749
754
|
return ipv4Info.address;
|
|
750
755
|
}
|
|
751
|
-
throw new Error(
|
|
756
|
+
throw new Error(`Could not find ${ipVersion} address for interface ${this.networkInterface}.`);
|
|
752
757
|
}
|
|
753
758
|
let localUniqueAddress;
|
|
754
|
-
for (const v6entry of interfaceDetails.filter(entry => entry.family ===
|
|
759
|
+
for (const v6entry of interfaceDetails.filter(entry => entry.family === 'IPv6')) {
|
|
755
760
|
if (!v6entry.scopeid) {
|
|
756
761
|
return v6entry.address;
|
|
757
762
|
}
|
|
@@ -760,33 +765,33 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
760
765
|
if (localUniqueAddress) {
|
|
761
766
|
return localUniqueAddress;
|
|
762
767
|
}
|
|
763
|
-
throw new Error(
|
|
768
|
+
throw new Error(`Could not find ${ipVersion} address for interface ${this.networkInterface}`);
|
|
764
769
|
}
|
|
765
770
|
/**
|
|
766
771
|
* The internal HTTP server must only ever be reachable locally. If no loopback interface could be detected,
|
|
767
772
|
* we still bind to the IPv4 loopback address instead of letting Node.js listen on every interface.
|
|
768
773
|
*/
|
|
769
774
|
static internalHttpServerListenAddress() {
|
|
770
|
-
return
|
|
775
|
+
return getOSLoopbackAddressIfAvailable() ?? '127.0.0.1';
|
|
771
776
|
}
|
|
772
777
|
static getLocalNetworkInterface(socket) {
|
|
773
778
|
let localAddress = socket.localAddress;
|
|
774
779
|
// Grab the list of network interfaces.
|
|
775
|
-
const interfaces =
|
|
780
|
+
const interfaces = os.networkInterfaces();
|
|
776
781
|
// Default to the first non-loopback interface we see.
|
|
777
|
-
const defaultInterface = () => Object.entries(interfaces).find(([, addresses]) => addresses?.some(address => !address.internal))?.[0] ??
|
|
782
|
+
const defaultInterface = () => Object.entries(interfaces).find(([, addresses]) => addresses?.some(address => !address.internal))?.[0] ?? 'unknown';
|
|
778
783
|
// No local address return our default.
|
|
779
784
|
if (!localAddress) {
|
|
780
785
|
return defaultInterface();
|
|
781
786
|
}
|
|
782
787
|
// Handle IPv4-mapped IPv6 addresses.
|
|
783
|
-
localAddress = localAddress.replace(/^::ffff:/i,
|
|
788
|
+
localAddress = localAddress.replace(/^::ffff:/i, '');
|
|
784
789
|
// Handle edge cases where we have an IPv4-mapped IPv6 address without the requisite prefix.
|
|
785
790
|
if (/^::(?:\d{1,3}\.){3}\d{1,3}$/.test(localAddress)) {
|
|
786
|
-
localAddress = localAddress.replace(/^::/,
|
|
791
|
+
localAddress = localAddress.replace(/^::/, '');
|
|
787
792
|
}
|
|
788
793
|
// Handle link-local IPv6 addresses.
|
|
789
|
-
localAddress = localAddress.split(
|
|
794
|
+
localAddress = localAddress.split('%')[0];
|
|
790
795
|
// Let's find an exact match using the IP.
|
|
791
796
|
for (const [name, addresses] of Object.entries(interfaces)) {
|
|
792
797
|
if (addresses?.some(({ address }) => address === localAddress)) {
|
|
@@ -794,24 +799,24 @@ class HAPConnection extends events_1.EventEmitter {
|
|
|
794
799
|
}
|
|
795
800
|
}
|
|
796
801
|
// We couldn't find an interface to match the address from above, so we attempt to match subnets (see https://github.com/homebridge/HAP-NodeJS/issues/847).
|
|
797
|
-
const family =
|
|
802
|
+
const family = net.isIPv4(localAddress)
|
|
803
|
+
? 'IPv4'
|
|
804
|
+
: 'IPv6';
|
|
798
805
|
// Let's find a match based on the subnet.
|
|
799
806
|
for (const [name, addresses] of Object.entries(interfaces)) {
|
|
800
|
-
if (addresses?.some(entry => entry.family === family &&
|
|
807
|
+
if (addresses?.some(entry => entry.family === family && getNetAddress(localAddress, entry.netmask) === getNetAddress(entry.address, entry.netmask))) {
|
|
801
808
|
return name;
|
|
802
809
|
}
|
|
803
810
|
}
|
|
804
|
-
const message =
|
|
805
|
-
socket.localAddress + ".";
|
|
811
|
+
const message = `unable to determine which interface to use for socket coming from ${socket.remoteAddress}:${socket.remotePort} to ${socket.localAddress}.`;
|
|
806
812
|
if (HAPConnection.unmatchedLocalAddresses.has(localAddress)) {
|
|
807
813
|
debugCon(message);
|
|
808
814
|
}
|
|
809
815
|
else {
|
|
810
816
|
HAPConnection.unmatchedLocalAddresses.add(localAddress);
|
|
811
|
-
console.log(
|
|
817
|
+
console.log(`WARNING: ${message} Using the default interface. This is only logged once per address.`);
|
|
812
818
|
}
|
|
813
819
|
return defaultInterface();
|
|
814
820
|
}
|
|
815
821
|
}
|
|
816
|
-
exports.HAPConnection = HAPConnection;
|
|
817
822
|
//# sourceMappingURL=eventedhttp.js.map
|