grover / homebridge-ranger

A HomeKit range extender for Bluetooth Low Energy (BLE) accessories.
MIT License
73 stars 15 forks source link

Bug in pairing process related to SRP module #43

Open ejgeiger opened 3 years ago

ejgeiger commented 3 years ago

Hi,

Having trouble pairing and consistently getting this error:

[9/30/2020, 00:13:32] [HB Supervisor] OS: Linux 5.4.51+ arm
[9/30/2020, 00:13:32] [HB Supervisor] Node.js v12.18.3 /usr/local/bin/node
[9/30/2020, 00:13:32] [HB Supervisor] Homebridge Path: /usr/local/lib/node_modules/homebridge/bin/homebridge
[9/30/2020, 00:13:32] [HB Supervisor] UI Path: /usr/local/lib/node_modules/homebridge-config-ui-x/dist/bin/standalone.js
[9/30/2020, 00:13:32] [HB Supervisor] Delaying Homebridge startup by 20 seconds on low powered server
[9/30/2020, 00:13:53] [HB Supervisor] Starting Homebridge with extra flags: -I -D
[9/30/2020, 00:13:53] [HB Supervisor] Started Homebridge v1.2.3 with PID: 749
[9/30/2020, 00:14:11] [Config] Homebridge Config UI X v4.27.2 is listening on :: port 8581
[9/30/2020, 00:14:04] Loaded config.json with 0 accessories and 2 platforms.
[9/30/2020, 00:14:04] ---
[9/30/2020, 00:14:15] Loaded plugin: homebridge-config-ui-x@4.27.2
[9/30/2020, 00:14:15] Registering platform 'homebridge-config-ui-x.config'
[9/30/2020, 00:14:15] ---
[9/30/2020, 00:14:15] The plugin "homebridge-ranger" defines 'homebridge' and/or 'hap-nodejs' in their 'dependencies' section, meaning they carry an additional copy of homebridge and hap-nodejs. This not only wastes disk space, but also can cause major incompatibility issues and thus is considered bad practice. Please inform the developer to update their plugin!
[9/30/2020, 00:14:24] Loaded plugin: homebridge-ranger@0.3.7
[9/30/2020, 00:14:24] Registering platform 'homebridge-ranger.Ranger'
[9/30/2020, 00:14:24] ---
[9/30/2020, 00:14:24] Loading 2 platforms...
[9/30/2020, 00:14:24] [Config] Initializing config platform...
[9/30/2020, 00:14:24] [Config] Running in Service Mode
[9/30/2020, 00:14:24] [Ranger] Initializing Ranger platform...
[9/30/2020, 00:14:24] [Ranger] Ranger Platform Plugin Loaded
[9/30/2020, 00:14:24] [Ranger] Found device in config: "PREMIS"
[9/30/2020, 00:14:24] [Ranger] Waiting for accessory: PREMIS
[9/30/2020, 00:14:24] [Ranger] Not all accessories have their devices. Not publishing yet.
[9/30/2020, 00:14:24] [Ranger] DidFinishLaunching
[9/30/2020, 00:14:27] [Ranger] Found unpaired accessory PREMIS  address=REDACTED rssi=-81dB
[9/30/2020, 00:14:27] [Ranger] Accessory 'PREMIS' found.
[9/30/2020, 00:14:29] [Config] Starting terminal session
[9/30/2020, 00:14:31] [Ranger] Discovered services.
[9/30/2020, 00:14:33] [Ranger] Discovered GATT services and characteristics of PREMIS
[9/30/2020, 00:14:33] [Ranger] Connected to PREMIS
[9/30/2020, 00:14:44] [Ranger] homebridge-ranger is taking a long time to load and preventing Homebridge from starting.
[9/30/2020, 00:14:48] [Ranger] Device is not paired yet, pairing now.
TypeError: Cannot read property '3072' of undefined
    at new PairSetup (/usr/local/lib/node_modules/homebridge-ranger/src/hap/pairing/PairSetup.js:43:30)
    at BleAccessory._pairDevice (/usr/local/lib/node_modules/homebridge-ranger/src/BleAccessory.js:169:23)
    at BleAccessory._ensureDeviceIsPaired (/usr/local/lib/node_modules/homebridge-ranger/src/BleAccessory.js:152:29)
    at BleAccessory.start (/usr/local/lib/node_modules/homebridge-ranger/src/BleAccessory.js:129:18)
    at RangerPlatform._startAccessories (/usr/local/lib/node_modules/homebridge-ranger/src/index.js:196:7)
    at RangerPlatform._tryToPublish (/usr/local/lib/node_modules/homebridge-ranger/src/index.js:169:7)
    at RangerPlatform._onHapAccessoryDiscovered (/usr/local/lib/node_modules/homebridge-ranger/src/index.js:162:7)
[9/30/2020, 00:15:04] [Ranger] homebridge-ranger is taking a long time to load and preventing Homebridge from starting.

I already fixed some unhandled promise rejection errors. I feel like I am close, but not sure why that property is not defined. Any pointers would be helpful.

gewaechshaus commented 3 years ago

@ejgeiger - I noticed the same issue, using the fork from @palasinio https://github.com/palasinio/homebridge-ranger

The related line 43 is: this._params = srp.params["3072"];

While srp is a constant defined on line 4 via const srp = require('fast-srp-hap');

Cheers Jan

palasinio commented 3 years ago

I discovered this weekend, that pairing is no longer working. While I am no dev, I don‘t know what to do. @ejgeiger : Can you tell me the fixed unhandled promise rejection errors or open a PR please? The TypeErrors appeared imho /w later Homebridge versions, which is less tolerant for example with type conversions (using string as int). As @gewaechshaus wrote, maybe you could not change a constant?

ejgeiger commented 3 years ago

Hi, I am also not a dev. It's been a few weeks so I don't remember everything I did.

Node.js Version v12.18.3 Npm Version v6.14.6 Homebridge Version 1.2.3 on Raspberry Lite 10 Buster

For the unhandled promise rejections I changed line 171 in BleAccessory.js to const result = await this.hapExecutor.run(pairSetup).catch((err) => console.log(err)); I think I also added .catch((err) => console.log(err)); to a line or two in index.js but I wasn't using source control and my notes are... lacking detail.

For the type error I found that changing line 43 in PairSetup.js to:
this._params = srp.SRP.params['3072']; fixed that error. Then I changed line 104 to this._srp = new srp.SrpClient(this._params, this._salt, identity, password, this._key);

Now I am getting stuck in the HapExecutor.js file. I can't get through all the M1-M6 steps (handshakes?). I think something is maybe not parsing correctly or maybe the accessory I am trying to connect to is somehow different? I have since moved on to a different hobby project, but willing to contribute here. Also, the new HomePod Mini will likely act as a BLE repeater. At least I hope so.

poldueta commented 3 years ago

Hi, faced with the same issue. Using @palasinio fork with latest homebridge and node-v12.19.0-linux-arm64

After fixing

For the type error I found that changing line 43 in PairSetup.js to: this._params = srp.SRP.params['3072']; fixed that error. Then I changed line 104 to this._srp = new srp.SrpClient(this._params, this._salt, identity, password, this._key);

stuck at

[10/27/2020, 6:56:26 PM] [Ranger] Returning pairing request for M1 [10/27/2020, 6:56:27 PM] [Ranger] Received pairing response for M2 [10/27/2020, 6:56:27 PM] [Ranger] Returning pairing request for M3 [10/27/2020, 6:56:29 PM] [Ranger] Pairing failed: {} Error: HAP-BLE request failed with status 6 at HapExecutor._getResponsePayload (...homebridge-ranger/src/hap/HapExecutor.js:143:13) at HapExecutor._executeCommand (...homebridge-ranger/src/hap/HapExecutor.js:72:34) at ...homebridge-ranger/src/hap/HapExecutor.js:40:24 [10/27/2020, 6:56:29 PM] [Ranger] Failed pairing attempt #1, waiting 1s and trying again.

ejgeiger commented 3 years ago

Yes we are at the same place. My guess, but I have no evidence of this, is that M2 is not being parsed correctly leading to an incorrect M3 being sent. My next step was going to be debugging those steps, but I would first have to learn SRP etc.

poldueta commented 3 years ago

Found an official HAP-Specification from Apple, I suppose it could be helpful for further development - https://github.com/NRCHKB/node-red-contrib-homekit-bridged/raw/master/docs/HAP-Specification-Non-Commercial-Version.pdf

palasinio commented 3 years ago

I wonder about that cause the code didn't change for "years".

palasinio commented 3 years ago

PS: Not able to pair my EVE Energy again.

palasinio commented 3 years ago

I'm a bit confused now. I set up homebridge 0.4.53 with node 9.11.2 and the original grover ranger. But Pairing is still not possible?

[2020-10-29 21:55:47] [Ranger] Pairing failed: {} Error: Failed to establish connection to the device at HapExecutor._establishSecureConnection (/usr/local/lib/node_modules/homebridge-ranger/src/hap/HapExecutor.js:187:13) at at process._tickCallback (internal/process/next_tick.js:182:7) [2020-10-29 21:55:47] [Ranger] Failed pairing attempt #1, waiting 1s and trying again.

gewaechshaus commented 3 years ago

@palasinio - possibly related to hardware...

palasinio commented 3 years ago

[2020-10-29 22:17:12] [Ranger] Received pair verify response for M4 [2020-10-29 22:17:12] [Ranger] <Buffer 02> [2020-10-29 22:17:13] [Ranger] Disconnected from MINITOWER [2020-10-29 22:17:13] [Ranger] Failed to establish secure session to MINITOWER. Reason: Failed to establish secure session. [2020-10-29 22:17:13] [Ranger] Pairing failed: {} Error: Failed to establish connection to the device at HapExecutor._establishSecureConnection (/usr/local/lib/node_modules/homebridge-ranger/src/hap/HapExecutor.js:187:13) at at process._tickCallback (internal/process/next_tick.js:182:7) [2020-10-29 22:17:13] [Ranger] Failed pairing attempt #1, waiting 1s and trying again.

gewaechshaus commented 3 years ago

I will take a look if I will run into the same issues like @poldueta @ejgeiger tomorrow. Anyway, many thanks for the input.

Good night Jan

horge commented 3 years ago

Just had the same problem as @ejgeiger and digged into debugging. I was able to reproduce the same error messages after the replacements in PairSetup.js from @ejgeiger

For the type error I found that changing line 43 in PairSetup.js to: this._params = srp.SRP.params['3072']; fixed that error. Then I changed line 104 to this._srp = new srp.SrpClient(this._params, this._salt, identity, password, this._key);

Thanks to @poldueta and the attached HAP spec I just found out that the used HAP hash function is of type SHA-512. This leads to following code adaption in PairSetup.js line 43

this._params = srp.SRP.params["hap"];

After this change, I was able to pair my eve thermo as shown below (pairing result was removed manually)

[3/7/2021, 8:58:43 PM] [Ranger] Device is not paired yet, pairing now.
[3/7/2021, 8:58:43 PM] [Ranger] Returning pairing request for M1
[3/7/2021, 8:58:44 PM] [Ranger] Received pairing response for M2
[3/7/2021, 8:58:44 PM] [Ranger] Returning pairing request for M3
[3/7/2021, 8:58:48 PM] [Ranger] Received pairing response for M4
[3/7/2021, 8:58:48 PM] [Ranger] Returning pairing request for M5
[3/7/2021, 8:58:49 PM] [Ranger] Received pairing response for M6
[3/7/2021, 8:58:49 PM] [Ranger] Pairing result:
{"rangerPairingID":"...","rangerLTSK":"...","rangerLTPK":"...","accessoryPairingID":"...","accessoryLTPK":"..."}
[3/7/2021, 8:58:49 PM] [Ranger] Pairing completed.

Used versions Homebridge v1.3.1 Node.js Version v14.16.0 Npm Version v6.14.11

palasinio commented 3 years ago

@horge Damn! Nice find. I managed to pair an energy, but not a thermo, yet.

horge commented 3 years ago

Nice to hear that the energy is working now :)

These are the thermos I‘m using Model Eve Thermo 20EAR1701 Firmware revision 1.2.11 Hardware revision 1.0.0

@palasinio could you provide the log of a failed pairing process of your thermo?

palasinio commented 3 years ago

@palasinio could you provide the log of a failed pairing process of your thermo?

Hi, thx, I‘ll try to investigate a little more. I was not able to install Ranger with NPM 7 on Node 14. Then I had trouble with "ed25519": "https://github.com/pokt-network/ed25519.git". It took me (simple user;)) hours to get Ranger up and running again. But I can say now, it’s running with node 12 and 14 and it can pair again.

For now (I‘m using Raspberry Pi 3 and 4) it seems to work with Homebridge 1.25 but not with 1.3x. I get „your plugin is slowing down homebridge“ messages with hb 1.3x. „Ciao“ or „Bonjour HAP“ makes no difference. Eve Energy is working now (pairing and historical data). Will try to unpair and pair Thermo next time.

palasinio commented 3 years ago

@horge @ejgeiger: here: sudo npm install -g --unsafe-perm palasinio/homebridge-ranger I added your fixes. Pairing seems to work again - in my case EVE Thermo is only working with HomeBridge =< 1.2.5. Homebridge: 1.2.5 Node: 14.16.0 NPM: 6.14.11 (had trouble /w 7)

Since HB 1.3.0: [18.3.2021, 22:57:30] [homebridge-ranger] This plugin slows down Homebridge. The read handler for the characteristic '' was slow to respond! See https://git.io/JtMGR for more info.

Big THX! This is a great step forward.