Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homebridge discovering device then crashing while trying to add #47

Closed
TheHomekitWayFinder opened this issue May 21, 2019 · 61 comments
Closed

Comments

@TheHomekitWayFinder
Copy link

I own a Kogan SmarterHome Air Conditioner that can both heat and cool. I have trued adding it to HomeKit through this plugin but then when it discovers the device my local area network it proceeds to crash homebridge. Here is the link to the air conditioner: https://www.kogan.com/au/buy/kogan-smarterhome-41kw-portable-air-conditioner-14000-btu-reverse-cycle/?openModal=2

Here is the log from homebridge:

`[5/12/2019, 8:32:13 PM] [TuyaLan] Discovered AC (71645600b4e62d215921)
C:\Users\xxxxx\AppData\Roaming\npm\node_modules\homebridge-tuya-lan\node_modules\node-forge\lib\aes.js:203
var len = tmp.length();
^

TypeError: tmp.length is not a function
at forge.aes.Algorithm.initialize (C:\Users\xxxxx\AppData\Roaming\npm\node_modules\homebridge-tuya-lan\node_modules\node-forge\lib\aes.js:203:19)
at new forge.cipher.BlockCipher (C:\Users\xxxxx\AppData\Roaming\npm\node_modules\homebridge-tuya-lan\node_modules\node-forge\lib\cipher.js:118:18)
at Object.forge.cipher.createCipher (C:\Users\xxxxx\AppData\Roaming\npm\node_modules\homebridge-tuya-lan\node_modules\node-forge\lib\cipher.js:42:10)
at new TuyaAccessory (C:\Users\xxxxx\AppData\Roaming\npm\node_modules\homebridge-tuya-lan\lib\TuyaAccessory.js:28:36)
at EventEmitter.TuyaAccessory.discover.on.config (C:\Users\xxxxx\AppData\Roaming\npm\node_modules\homebridge-tuya-lan\index.js:77:32)
at EventEmitter.emit (events.js:182:13)
at Socket.server.on.msg (C:\Users\xxxxx\AppData\Roaming\npm\node_modules\homebridge-tuya-lan\lib\TuyaAccessory.js:343:24)
at Socket.emit (events.js:182:13)
at UDP.onMessage [as onmessage] (dgram.js:628:8)`

here is my config.json file:

`{
"bridge": {
"name": "Homebridge",
"username": "xx:xx:xx:xx:xx",
"port": 51826,
"pin": "xxx-xx-xxx"
},

"description": "This is an example configuration file.",

"platforms": [{
"platform": "TplinkSmarthome",
"name": "TplinkSmarthome"
},
{
"platform": "TuyaLan",
"devices": [{

		"name": "AC",
		"type": "AirConditioner",
		"manufacturer": "Kogan",
		"model": "SmarterHome Air Conditioner",
		"id": "71645600b4e62d215921",
		"key": "--------key---------"
	}
	]
}

],

"accessories": [{
"accessory": "Xbox",
"name": "Xbox",
"ipAddress": "xx.x.x.x",
"liveId": "xxxxxxxxxxxx"
}]
}`

@TheHomekitWayFinder
Copy link
Author

@AMoo-Miki can you have a look into this and try and see why it’s not working

@AMoo-Miki
Copy link
Owner

I looked at node-forge code and it looks like it is not liking your key.

  1. Update your plugin to the latest version with npm i -g AMoo-Miki/homebridge-tuya-lan as I have updated the plugin's dependencies to use a newer version of node-forge.
  2. Count the number of characters in your key; the valid lengths are 16, 24, and 32. If yours is none, please go through the process of obtaining the id and key again.

Lemme know how it goes.

@AMoo-Miki
Copy link
Owner

Please give the latest rc release a shot by doing npm i -g homebridge-tuya-lan@rc. It includes logic to understand this newer API.

A known issue is that the current state is not reported by some devices; they however work fine as soon as you interact with them. I have reached out to Tuya for a solution.

The Setup Instructions have changed in case you need to obtain a fresh id and key.

@TheHomekitWayFinder
Copy link
Author

TheHomekitWayFinder commented Sep 7, 2019

Ok, thanks. I will have a look at this know. Also, did i hear you say somewhere else that Tuya is currently working on Homekit intergration as standard.

@TheHomekitWayFinder
Copy link
Author

Just a quick update. I have just used the command that you provided and it has discovered the device however homebridge crashes not long after discovering the device and is giving an Key Error saying that my key is too long. I will try and get and updated key and id now and report back.

@AMoo-Miki
Copy link
Owner

@TheHomekitWayFinder, regarding Tuya and Homekit, that is what they told me. Let's hope it is not limited to Siri Shortcuts. Their implementation would save us from all these hoops.

@TheHomekitWayFinder
Copy link
Author

When I try to get the key for my air conditioner, tuya-lan-find starts to print out somethings and then crashes

@TheHomekitWayFinder
Copy link
Author

TheHomekitWayFinder commented Sep 7, 2019

IMG_0E042A964B80-1

this is a screenshot of the home app on ios 13 as still isn't liking my key however it does show up but I just can't control it because homebridge crashes

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 7, 2019

If it shows garbage, it is an indicator of a bad certificate. Delete any previous NodeMITMProxyCA certificates you might have on the phone before you start tuya-lan-find. I haven't had anyone else report using tuya-lan-find yet but I am sure there would be edge cases that need handling.

PS. I just noticed that you are doing this on Windows; I have not tested it on Windows. Lemme see if I can do it quickly.

Regarding Homebridge crashing, can you get your hands on some logs to see what's causing it?

@AMoo-Miki
Copy link
Owner

I ran it on Windows and it didn't give me any trouble. It shows many "socket hang up" errors but those can be ignored. I will suppress them soon.

@TheHomekitWayFinder
Copy link
Author

Unfortunately, it never seems to produce any log files and all I have is a picture
of it crashing and saying that it has an invalid key length. I will try and run tuya-lan-find again after deleting the certificates again.

Here is the photo of the error:

IMG_1626

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 7, 2019

Is it possible that you are adding the key and id inverted?

Hold on, lemme add key length verification so that it wont crash! Gimme a min.

@TheHomekitWayFinder
Copy link
Author

No, as in the app id now shows the Virtual ID and i have compared the two and they are both the same so im presuming it's just that the key is wrong.

@AMoo-Miki
Copy link
Owner

Can you please update with npm i -g homebridge-tuya-lan@rc and give it shot?

@TheHomekitWayFinder
Copy link
Author

I have just run tuya-lan-find again and it gives lots of socket hang up errors but it doesnt give the id or key of my air conditioner. I have also just today purchased a Arlec 4 outlet power strip and that uses the tuya protocol and therefore i need to get this working so that i can add that to homekit.

I will run the update and give it another shot in a sec.

@TheHomekitWayFinder
Copy link
Author

The update still isnt working. Unfortunately

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 7, 2019

It crashed? How long is your key?

Is it possible that windows firewall is preventing node from opening the sockets?

Also, can you please give me the outputs for these two 3:

tuya-lan-find -v
node -v
homebridge --version

@TheHomekitWayFinder
Copy link
Author

TheHomekitWayFinder commented Sep 7, 2019

yep it crashed again. in my config file my key is 16 characters long however it was 17 before so I believe that my key may be wrong.

Version of
tuya-lan-find is 1.5.0-rc.4
node is 10.15.0
homebridge is 0.4.50

i will check the firewall now - the firewall doesnt seem to be effecting homebridge or anything

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 7, 2019

If you are using the native firewall of Windows, to add a rule allowing:

netsh advfirewall firewall add rule name="MITM Proxy" dir=in action=allow protocol=TCP localport=8080

To remove it:

netsh advfirewall firewall delete rule name="MITM Proxy" protocol=TCP localport=8080

If you get an error from tuya-lan-find, could please paste it here or post a pic?

@TheHomekitWayFinder
Copy link
Author

im still getting the socket hang up errors and nothing else so i dont know whats happening. I used that command to change the firewall but its still giving me the same result

Image 2

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 7, 2019

While these socket timeouts are shown, can you please kill the Tuya app on your phone and open it again? Please make sure you kill it and not just minimize it. You are using the Tuya Smart app right? The one with the red icon?

PS, I just pushed v1.5.0-rc.5 to NPM to suppress the socket hang up messages.

@TheHomekitWayFinder
Copy link
Author

No, i am using the kogan smarterhome app. i am making sure that i kill the app by double clicking the home button and swiping up on the app

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 7, 2019

This will only work with the Tuya Smart app to fetch the id and key. Add it there too; you can uninstall the app later. You shouldn't need to remove it from Kogan app.

@TheHomekitWayFinder
Copy link
Author

TheHomekitWayFinder commented Sep 7, 2019

ok i will try that now. It wants me to reset the device which would involve removing it from the Kogan app. Would I still be able to acquire the key and id using the old method using anyproxy

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 7, 2019

Oops, and I guess if you add it to Kogan, you will have to reset it again which most probably changes the id and key.

Anyproxy doesn't work anymore. Lemme find a different way. Hold on a min.


Let's get your hands dirty with some code changes. (if you find this hard, lemme know and I will send you a file)

  1. Go to %APPDATA%\Roaming\npm\node_modules\homebridge-tuya-lan\bin and open the file named cli.js with WordPad (not notepad).
  2. Look for POST. You will find this line:
    } else if (ctx.clientToProxyRequest.method === 'POST' && /tuya/.test(ctx.clientToProxyRequest.headers.host)) {
    
    change that to
    } else if (ctx.clientToProxyRequest.method === 'POST') {
    
  3. Look for tuya.m.my.group.device.list and swap it with the id of your AC (which is 20 characters long I think).
  4. Change the line just after it to console.log(body); (please notice that there are no quotes).
  5. Execute tuya-lan-find and relaunch the Kogan app.

This should give you the full body of the request which is gigantic. Look for localKey and devId there.

@TheHomekitWayFinder
Copy link
Author

there are 2 instances of the tuya.m.my.group.device.list do i change both of them

@AMoo-Miki
Copy link
Owner

yes please.

@TheHomekitWayFinder
Copy link
Author

it still isnt pulling the key and id

@AMoo-Miki
Copy link
Owner

Where was EADDRINUSE ?

@TheHomekitWayFinder
Copy link
Author

Image 3

@AMoo-Miki
Copy link
Owner

Can you delete the cert from your phone and scan the QR code from the Kogan.js script?

@TheHomekitWayFinder
Copy link
Author

I deleted the certificate from the cli.js script before I scanned the QR code from the Kogan.js script. But I can’t do anything at the moment as I have just gone out

Sent with GitHawk

@AMoo-Miki
Copy link
Owner

I understand. I will think a bit to see what we can do about this and post back.

@TheHomekitWayFinder
Copy link
Author

Do you want me to see if I can source the signature some how?

Sent with GitHawk

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 7, 2019

The key and signature, yeah.

Also, if we could find a MITM proxy that has a GUI, we can see what the Kogan app is doing.

@TheHomekitWayFinder
Copy link
Author

What about MITMproxy that has a web interface like anyproxy did

Sent with GitHawk

@AMoo-Miki
Copy link
Owner

You mean this? I just tested it on Windows and it is great.

After you install it, change the proxy settings on your phone to your Windows IP and 8080. Then open mitm.it on your phone to install the certificate; the process is identical to this plugins.

@TheHomekitWayFinder
Copy link
Author

I have just tried to reset the device and add it to the TuyaSmart app, however the device keeps timing out before the app has finished pairing. But when I go back into the Kogan SmarterHome app it pairs very quickly

Sent with GitHawk

@TheHomekitWayFinder
Copy link
Author

I just tried using Mitmproxy after adding the air conditioner back to the Kogan SmarterHome app and it would pull up any details to do with it. It pulls up lots for websites but nothing for apps. The same happens when I try to get the id and key of my Arlec Grid Connect Power Strip.

Sent with GitHawk

@AMoo-Miki
Copy link
Owner

Do u have a Mac?

@TheHomekitWayFinder
Copy link
Author

Yes I do

Sent with GitHawk

@TheHomekitWayFinder
Copy link
Author

TheHomekitWayFinder commented Sep 8, 2019

I guessing this is out of date as you said previously that anyproxy no-longer works but, https://gist.github.com/aceslick911/0cac96ab122fa9c6cd9ca18a696ea961

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 8, 2019

Thanks. Sadly it didn't provide much help.

Can you follow these steps please: #53 (comment)

... but don't send me your PCAP as it would have way too much of information. A few comments below that, I have described how you can filter everything and send me only Tuya's data. Just one deviation: don't include and tcp in the filter.

My goal is to see what the Kogan app is telling the device and find my way from there.

@TheHomekitWayFinder
Copy link
Author

Ok will try that in a bit. Is there anyway of maybe asking if Tuya could present the local key in the app when connected to the same network as the devices as it would make it much easier for external integrations such as homebridge. The only thing would be security.

Sent with GitHawk

@TheHomekitWayFinder
Copy link
Author

Here is the signature that I acquired from command prompt when running homebridge with the old key and that isn’t working. It shows up but then when controlling through HomeKit the device doesn’t respond to the commands but still appears online. I guessing this is because it has the wrong key.
Ready to handle AC (AirConditoner:3.1) with signature {“1”:false, “2”:24, “3”:17, “4”:“HEAT”, “5”:“1”, “19”:”C”, “102”:false, “103”:0, “104”:false, “105”:0, “106”:0, “110”:0}

Sent with GitHawk

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 8, 2019

The 3.1 devices don't use encryption while fetching the signature.

One way to check if the key is really bad is to watch the logs and trigger a change on the device using the controls on it (not an app), maybe just turn it on or off, or change the temperature. If you see an error about Odd message ... with command 8 it means the key is bad. If no error is shown, it is probably good.

@TheHomekitWayFinder
Copy link
Author

I just tried the pcap method but it was giving errors I don’t know whether I was doing it wrong orwhat

Sent with GitHawk

@AMoo-Miki
Copy link
Owner

Do you know what error it gave you? Also can you check if the key is really good using my previous message?

@TheHomekitWayFinder
Copy link
Author

Also did you mean the UDID or the ECID

@TheHomekitWayFinder
Copy link
Author

@AMoo-Miki i have just sent through the 2 files that you requested from wireshark through to your email. I dont know my correct key so therefore i didnt provide it. If there is anything else you need just let me know. I hope that you are able to distinguish a key from that.

@TheHomekitWayFinder
Copy link
Author

I just ran thing that you asked me to do previously to check the key and it kept coming back with odd message from AC with command 8

Sent with GitHawk

@TheHomekitWayFinder
Copy link
Author

There wasn’t any error it was just that I was using the wrong device number. I was using the ECID rather than the UDID therefore it wasn’t working.

Sent with GitHawk

@AMoo-Miki
Copy link
Owner

AMoo-Miki commented Sep 9, 2019

Also did you mean the UDID or the ECID

Silly me. I fixed the instructions. Thanks for pointing it out.

I did get your email. Sadly without the key there isn't much I can figure out from the encrypted data.

Let's try and test your keys:

  1. Update to the latest rc with npm i -g homebridge-tuya-lan@rc.
  2. Run tuya-lan-find --decode THE-KEY, substitute any key that you have had.
  3. When asked to enter the encrypted message, enter AABVqgAAAAEAAAAKAAAARnsiZGV2SWQiOiI3MTY0NTYwMGI0ZTYyZDIxNTkyMSIsImd3SWQiOiI3MTY0NTYwMGI0ZTYyZDIxNTkyMSJ9r8RecgAAqlU=

If it succeeds in decoding, the key is good.

PS, that is exactly what I do with the file I get :)

@TheHomekitWayFinder
Copy link
Author

@AMoo-Miki sorry I have been so busy recently and when I have time I will try it and let you know

@AMoo-Miki
Copy link
Owner

Of course. Ping me whenever you get a chance.

@TheHomekitWayFinder
Copy link
Author

@AMoo-Miki I have just gotten round to trying some stuff out, and I bought a new powerboard the other week and i wanted to try and get that into home bridge so when I got it I loaded it into the tuya smart app and have just today been able to successfully retrieve the key from the device after following the instructions I realised that when I had been previously attempting to retrieve the key I hadn't been granting the certificate full trust settings, I have also been able to load the air conditioner into homebridge fully and I am now able to control it through HomeKit. However, do you know whether there is any way to separate the outlets on the powerboard as I am using the board in many different rooms however HomeKit won't allow me to separate them based on places and add each outlet to favourites instead of just the master board.

@AMoo-Miki
Copy link
Owner

@TheHomekitWayFinder that is good to know.

I too found the grouping annoying and good news is that with the latest iOS, they have an option to separate the icons. I just saw it in my 13.3.1; give it a shot.

Lemme know if I can help in any other way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants