Ape / samsungctl

Remote control Samsung televisions via a TCP/IP connection
MIT License
714 stars 191 forks source link

Some TV models are not working #22

Open Random-Stack-Random-Day opened 7 years ago

Random-Stack-Random-Day commented 7 years ago

Edited by @Ape: I hijacked this issue to collect all instances where some random TV model does not work, but we don't know what is the cause or how it could be fixed. Some TV models might not simply support TCP/IP control at all, and some models may require a whole new protocol. When you have techinical details about a specific case that could be fixed please open a new issue.


Original message: I'm wondering if anyone can confirm if this works for their JS9000 or H5203? Getting 'Connection refused' from the 9000 and no response from the 5203.

kitsen13 commented 6 years ago

@riemers It would be great that it works only locally.

Another test I've made : if i use "Smartview" app, the genarated key from "main.py" of "Eclair4151 script" is no longer valid. I have to generate an other key to make it working.

marius1968 commented 6 years ago

First of all thanks to @eclair4151 and @timelery for their efforts ; solid programming skills are required for what they have succeeded. @kitsen13 , @riemers "Smartview" name seems to be reserved for Samsung app and can not be used. Finally, if someone can help to develop the server side I can host them for free for everyone.

eclair4151 commented 6 years ago

@riemers so yea my script only requires an internet connection for pairing. once thats done everything is local so there is no latency between commands caused by sending it to an external server. I have already played around with setting up the Dlls locally, but there is one huge problem. Even if you get mono up and running (which isnt actually that easy to get it up and running and connected to the Dlls), the dlls are compiled for x86, which means they would never work on raspberry pis or similar which run ARM. so for compatibility sake this is really the best solution i can find for ease of use for the average joe.

riemers commented 6 years ago

Then best case scenario would be a public open source api people can talk too (so a server somewhere) with the sourcecode on github or similar, so people can use Mono or a dockerized version of it to keep all locally if the need arrises. Then Ape can use that public api. A suggestion would be to have multiple servers on say free tiers of amazon/google/whatever or i still have some space on a dedicated box. But then you could round robin to a running server because everybody does this as best effort too.

So the bottom line now would be, who would be willing to create/make the open source mono dll public api? Perhaps @skrew is willing to extends his or open source that part, since he could also use then the 'public version' just pinging to see if possible.

johntdyer commented 6 years ago

If it’s C# could we use azure’s equivalent of lambda , aka a serverless model? Might help with costs

Sent from mobile

On Dec 3, 2017, at 15:27, Erik-jan Riemers notifications@github.com wrote:

Then best case scenario would be a public open source api people can talk too (so a server somewhere) with the sourcecode on github or similar, so people can use Mono or a dockerized version of it to keep all locally if the need arrises. Then Ape can use that public api. A suggestion would be to have multiple servers on say free tiers of amazon/google/whatever or i still have some space on a dedicated box. But then you could round robin to a running server because everybody does this as best effort too.

So the bottom line now would be, who would be willing to create/make the open source mono dll public api? Perhaps @skrew is willing to extends his or open source that part, since he could also use then the 'public version' just pinging to see if possible.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

riemers commented 6 years ago

@johntdyer since it is open source, we aim for a cost scenario of 0,- to maintain it. So that is with free tiers or people that have some 'resources' left they can spare. Hence the round robin approach in case somebody needs the resources again/dies/takeover/whatever.

eclair4151 commented 6 years ago

Aws lambda has a c# option and their free tier is like 500,000 calls a month

eclair4151 commented 6 years ago

I just checked it’s 1 millions free calls a month

marius1968 commented 6 years ago

@eclair4151 you can tell us what DLL file is involved? on server part you used python wrapper with ctypes?

eclair4151 commented 6 years ago

@marius1968 from what i have looked into its not that easy to do it in python because the dlls are a c# wrapper to the c api, and i have only been able to get it working using c# and the wrapper, i have not been able to call the method directly in python. most likely you need a simple c# server running asp .NET that can be deployed on mono. ill try to create a simple POC. the main DLLs you need are

SmartView2.Core.dll SPCApi.dll SmartView2.Devices.dll

which can be found here: https://github.com/timelery/Samsung-RemoteControl/tree/master/Drivers

update: after sending malformed requests to one of the servers i was able to get this error back:

{ "error": "Bad Request", "exception": "org.springframework.web.bind.MethodArgumentNotValidException" }

and also

{ "timestamp": 1512577747176, "status": 500, "error": "Internal Server Error", "exception": "javax.ws.rs.ProcessingException", "message": "java.net.ConnectException: ConnectException invoking http://localhost:8093/step1: Connection refused (Connection refused)", "path": "/step1" }

so looks like he is using a spring server that calls an internal server most likely running asp.net or something

marius1968 commented 6 years ago

they run asp.net for sure ...waiting for your POC, may be relevant to the approach. thx

riemers commented 6 years ago

Don't know much about asp.net but i know .net core can also be run in a docker container which makes it easy to run everywhere too.

eclair4151 commented 6 years ago

@marius1968 got a basic asp.net server up and running with the dll stuff, but its more complicated because the encryption dll isnt stateless. like you cant just call the last step of the encryption library without first doing all the encryption steps again before. So you either need to send back and forth the Encryption strings everytime, or have a database on the server keeping track of all the states using the deviceId. i personally think having the user send back and forth the encryption strings is fine, as it wont be that hard, and it avoids having to do any database connections on the server.

eclair4151 commented 6 years ago

@marius1968 https://github.com/eclair4151/SamsungEncryptServer not done yet. right now its just the server that only has step1. but now it should be pretty easy to add in the rest of the steps

marius1968 commented 6 years ago

@eclair4151 thanks a lot, I will try to install the script and test it

Christoph1982 commented 6 years ago

Pleas help me

My tv ue48h6400 (domoticz not working)

terminal app: samsungctl -i --host 192.168.0.115 error: Connections Refused

My python version 2.7.13 (not update python 3)

thanks

thanks

eclair4151 commented 6 years ago

Samsungctl does not support H series TVs. You will need to try this project out: https://github.com/eclair4151/samsung_encrypted_POC/tree/master

(Requires python3)

marius1968 commented 6 years ago

@eclair4151 I managed to install the script and step 1 works even though it returns the error "System.BadImageFormatException: Could not load file or assembly 'SPCApi, Version=1.0.5304.23677, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.\r\nFile name: 'SPCApi, Version=1.0.5304.23677, Culture=neutral, PublicKeyToken=null"

Can you tell us what you are using for the call to steps 2 and 3? I found the SPCApi.dll commands GenerateServerAck() and GetKey() but I'm not sure at which step they are used.

Thanks.

lucianf commented 6 years ago

@eclair4151 for what it's worth, I am confirming your POC works with my H6670. Happy to help with any further testing.

eclair4151 commented 6 years ago

@marius1968 please create an issue in that repo. its confusing to try and debug issues across different threads

mjpakmehr commented 6 years ago

Greaaat! Works like a charm on UE48H6500. Thanks @eclair4151 . @Ape is it possible to integrate this with samsungctl scripts? Thanks

Ape commented 6 years ago

@eclair4151 Would you like to create a pull request for integrating the H series support?

eclair4151 commented 6 years ago

@Ape sure. ill try to get that working sometime this week

riemers commented 6 years ago

As long as you still use a 3rth party server, which is not owned by one of us, it can die any time too. Or the owner of the 3rth party can decide to stop with it too or ban certain clients. Just putting it out there.

eclair4151 commented 6 years ago

@riemers true, but i dont have the time or desire to finish the server implementation and then set it up and host it and make sure its always up. If someone else wants to finish the server and host it i'm more than happy to modify the script to use a new server. other wise i think for the time being this is prob fine.

riemers commented 6 years ago

Well in that case, we can just use your solution and if it ever goes down we can always have another talk on how to proceed (hopefully we all have samsung >H series then) 🤣

flashydave commented 6 years ago

The POC by @eclair4151 (great job btw) works fine on my 2015 UE55JU6800 TV (although the external_site was returning 500 internal server errors earlier this evening).

I am losing the thread a little. Is the ultimate goal to create "on premise" docker based server solution running mono + C# wrapper around the dll's to avoid reliance on an external server? If so I presume it would have to run on a Windows platform rather than contemplating something like linux/ARM (eg raspberry pi)??

eclair4151 commented 6 years ago

@flashydave yea at this point the thread is more of a chat haha. Yea you pretty much got it. It may be able to run on linux with mono but def not an ARM device like a rasp pi. Prob easiest to just run it on a windows ec2 instance or something.

flashydave commented 6 years ago

Pity given rpi is cheap ($10 or so for a zero) and can be lost somewhere on the network and most home automation servers you might use as a convenient place run on an underlying linux platform. I for one "Don't Do Microsoft" (and have even made a career out of it in a "Microsoft House" at work!) so unless someone finds an android alternative or another way of wrapping the dll's up I would have a hard time convincing myself to run an ec2 Windows instance :-( Has anyone looked at https://hub.docker.com/r/microsoft/dotnet/ as a possible way forward?

eclair4151 commented 6 years ago

I mean mono officially supports asp.net so this server should theoretically be able to run on linux. Also i looked at that and there is not native way to link Precompiled C# dlls into those projects without doing some weird nuget package hack that i couldnt figure out but maybe you can

flashydave commented 6 years ago

Yeah I have heard of this sort of thing using techniques like those at https://github.com/Fody/Costura but I am no expert in this area. I will ask around at work where we have quite a lot of nuget and .net expertise and see what colleagues say. Seems like we are chasing a football around somewhat though!

orzechszek commented 6 years ago

Hi,

I have TV version - M (2017). If I proper understand there is no possiblity to POWER ON tv? Turning off works fine.

mjpakmehr commented 6 years ago

Hi @eclair4151 , sorry to bother! Any updates on integrating your PoC to samsungctl master? Thanks ;)

eclair4151 commented 6 years ago

@orzechszek this library doesn’t support it yet but you can turn on M series TVs with a wake on lan packet. I have tested it myself and can confirm it works. https://github.com/remcohaszing/pywakeonlan. You just need to run this line send_magic_packet('ff.ff.ff.ff.ff.ff') with you TVs MAC address

Sent with GitHawk

eclair4151 commented 6 years ago

@mjpakmehr and no not yet. The h series tv I have been testing on isn’t actually mine so I can only test my code when I have access to the tv once ever few weeks

Sent with GitHawk

mjpakmehr commented 6 years ago

@eclair4151 , Got it, thanks ;) Anything i can help you with? since i have the H series.

eclair4151 commented 6 years ago

@marius1968 I may take you up on that. I’ll send you a message when I think I have a working fork for you to test

Sent with GitHawk

orzechszek commented 6 years ago

Thanks for info :)

eclair4151 commented 6 years ago

@marius1968 Hey if you could try this out that would be great https://github.com/eclair4151/samsungctl

Usage:

from samsungctl.remote_pin import RemotePin config = { "name": "samsungctl", "description": "PC", "id": "", "host": "192.168.0.10", "port": 8000, "method": "pin", "timeout": 0, }

RemotePin.pair(config)

after it pairs it will print the session key and id. put it back into the config like this

config = { "name": "samsungctl", "description": "PC", "id": "", "host": "192.168.0.10", "port": 8000, "method": "pin", "timeout": 0, "session_key":"XXXXXX", "session_id": "X", }

and use as normal. Prob not the best way to use it but its a start

SebastianZ84 commented 6 years ago

Hi Guys, great work so far. The pin method works for my JU but only for a couple of minutes. I have to pair my PC again to make it work again... Any ideas?

eclair4151 commented 6 years ago

Yea this seems to be a semi common issue. I haven’t had time to try and figure out what causes this. Can you test if you keep the web socket connection open if it stays working? Or does it still stop? I have a theory it has to do with consistently reconnecting every time I send a command

Sent with GitHawk

SebastianZ84 commented 6 years ago

Stays it open in interactive mode? Doesn't make any difference if I use it in interactive mode or send seperate cmds.

ttonyh commented 6 years ago

A while back I was able to decompile the APK; the output helped me understand the PIN authorization steps outlined above (#issuecomment-341980253). The only thing left was, of course, the encryption part of the whole thing. Looks like that's the common blocker. If someone can decompile the code and look through the output source, maybe they can find the key to unlock this mystery. I assume the encryption is being done using a method other than the DLL, since it's written in Java and is on Android. At this point it might be a long shot but it's worth a try.

  1. Clone the decompiler repo

    https://github.com/dirkvranckaert/AndroidDecompiler
  2. Obtain the APK:

    https://apkpure.com/samsung-smart-view/com.samsung.smartviewad
  3. Run the decompiler against the APK

    
    cd AndroidDecompiler

./decompileAPK.sh -o ./OUTPUT-DIR -f ./path/to/smart_view_v_com.samsung.smartviewad.apk



NOTE: this is based off the notes I made well over a year ago, so it might be out of date.

#### Hints:
This possibly contains something to do with the encryption method
* ./OUTPUT-DIR/output/src/com/samsung/smartview/service/emp/spi/secure

Here are the pairing / steps:
* ./OUTPUT-DIR/output/src/com/samsung/smartview/service/pairing
* ./OUTPUT-DIR/output/src/com/samsung/smartview/service/pairing/steps

Other
* ./OUTPUT-DIR/output/src/com/samsung/smartview/service/pairing/PairingService.java

Good luck!!
eclair4151 commented 6 years ago

We looked in to that it’s all done inside the jni c++ library and very obfucated https://github.com/Ape/samsungctl/issues/22#issuecomment-343790580

Sent with GitHawk

scottgrobinson commented 6 years ago

Thanks for the effort going into this.

I gave it a try and am getting semi-promising results, although not fully working. Not expecting that as I understand this is development but hopefully you can point something out and it's not a silly dependancy issue.

https://pastebin.com/0C5SKQdQ

Python 3.5.2 (default, Nov 23 2017, 16:37:01)

Pip Modules beautifulsoup4==4.4.1 certifi==2017.11.5 chardet==3.0.4 dnspython==1.12.0 gyp==0.1 html5lib==0.999 idna==2.6 lxml==3.5.0 pycrypto==2.6.1 PyYAML==3.11 requests==2.18.4 samsungctl==0.7.0 six==1.10.0 urllib3==1.22 websocket-client==0.46.0

eclair4151 commented 6 years ago

@scottgrobinson i think i found the issue. can you try this? https://pastebin.com/94vQkURJ

replace aes_lib.py with this paste and let me know if it works

scottgrobinson commented 6 years ago

@eclair4151 Thank you - Progress!

Enter TV Pin: 3764 session_key: 3F324559C3558E667AEF6622F70AA115 session_id: 1 waiting for a sec... sending command! Traceback (most recent call last): File "main.py", line 76, in r = connection.send(aesLib.generate_command('KEY_VOLDOWN')) File "/home/console/samsung_encrypted_POC/aes_lib.py", line 37, in generate_command command_bytes = self.encrypt(self.generate_json(key_press)) File "/home/console/samsung_encrypted_POC/aes_lib.py", line 32, in encrypt cipher = AES.new(bytes(self.key, encoding = "utf8"), AES.MODE_ECB) TypeError: encoding without a string argument

eclair4151 commented 6 years ago

can you replace that line 32 with this cipher = AES.new(self.key, AES.MODE_ECB)

and see if it works

scottgrobinson commented 6 years ago

Perfect - That's fully working now! :) Much appreciated.

I guess the next two points I can work out from this thread:

eclair4151 commented 6 years ago

@scottgrobinson i'm not sure. I don't really have access to the H series tv anymore so its really hard for me to debug issues. When i get access to it again i will see if i can reproduce the error, and if there is some command to send to get it to start working. other than that someone else who has that issue will have to wireshark the official app and see if they can see what command it sends to continue across reboots. from what i remember i didn't have to do anything and my keys worked across reboots.