sermayoral / ha-samsungtv-encrypted

Samsung TV Encrypted Models (H & J Series) custom component for Home Assistant
Apache License 2.0
39 stars 24 forks source link

UE32J5200 - Handshake status 400 Bad Request when trying to get token #62

Closed EPMatt closed 3 years ago

EPMatt commented 3 years ago

Hi @sermayoral, I'm trying to connect a UE32J5200 TV with this integration. Unfortunately I'm stuck at the token retrieval.

Running get_token.py --ip <myip> --port 8080 results in a connection refusederror. I found that the procedure defined in connect() method of the PySmartCrypto library is using the hardcoded port 8000, so that the port supplied via cli argument is not considered in the pairing section.

This issue has been addressed in #61, but I still can't connect to my device. Running get_token.py --ip <myip> --port 8080 on the PR #61 results in a different error:

Traceback (most recent call last):
  File "get_token.py", line 40, in <module>
    main(sys.argv[1:])
  File "get_token.py", line 36, in main
    PySmartCrypto(ip, port)
  File "[omitted]/ha-samsungtv-encrypted/custom_components/samsungtv_encrypted/PySmartCrypto/pysmartcrypto.py", line 123, in __init__
    self._connection = self.connect()
  File "[omitted]/ha-samsungtv-encrypted/custom_components/samsungtv_encrypted/PySmartCrypto/pysmartcrypto.py", line 99, in connect
    connection = websocket.create_connection(websocket_url)
  File "[omitted]/lib/python3.6/site-packages/websocket/_core.py", line 515, in create_connection
    websock.connect(url, **options)
  File "[omitted]/lib/python3.6/site-packages/websocket/_core.py", line 226, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "[omitted]/lib/python3.6/site-packages/websocket/_handshake.py", line 80, in handshake
    status, resp = _get_resp_headers(sock)
  File "[omitted]/lib/python3.6/site-packages/websocket/_handshake.py", line 165, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers)
websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Bad Request

I tried to debug the issue by myself without success, but I discovered that the value of websocket_response after its initialization is <Response [403]>, while the value of websocket_urlafter its initialization is

ws://<myip>:8080/socket.io/1/websocket/<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http

which I think it's not what it should be.

So the problem seems to be with the first request receiving a Forbidden response at line 95. I'd be glad to help you solving this issue. 👍🏻

estevez-dev commented 3 years ago

I have the same issue with UE55H6203AK model. I'm also getting <Response [403]>

I've tried everything from #61 with reverting latest fixes, changing the port to 8000, but still no luck.

EPMatt commented 3 years ago

Hi @estevez-dev, could you please post the command you used for retrieving the token, and the output you received?

estevez-dev commented 3 years ago

Hi @EPMatt Sure. I'm using:

python3 ./get_token.py --ip 192.168.2.23 --port 8080

and getting:

<Response [403]>
Traceback (most recent call last):
  File "./get_token.py", line 40, in <module>
    main(sys.argv[1:])
  File "./get_token.py", line 36, in main
    PySmartCrypto(ip, port)
  File "/home/estevez/src/ha-samsungtv-encrypted/custom_components/samsungtv_encrypted/PySmartCrypto/pysmartcrypto.py", line 124, in __init__
    self._connection = self.connect()
  File "/home/estevez/src/ha-samsungtv-encrypted/custom_components/samsungtv_encrypted/PySmartCrypto/pysmartcrypto.py", line 100, in connect
    connection = websocket.create_connection(websocket_url)
  File "/home/estevez/.local/lib/python3.7/site-packages/websocket/_core.py", line 515, in create_connection
    websock.connect(url, **options)
  File "/home/estevez/.local/lib/python3.7/site-packages/websocket/_core.py", line 226, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "/home/estevez/.local/lib/python3.7/site-packages/websocket/_handshake.py", line 80, in handshake
    status, resp = _get_resp_headers(sock)
  File "/home/estevez/.local/lib/python3.7/site-packages/websocket/_handshake.py", line 165, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers)
websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Bad Request

Should I add this info to #21 as well?

EPMatt commented 3 years ago

Hi @estevez-dev, could you please try to execute the following command? Switching to port 8000.

python3 ./get_token.py --ip 192.168.2.23 --port 8000

estevez-dev commented 3 years ago

Hi @EPMatt I'm getting Connection refused when trying to use 8000 in any case. I tried like this:

python3 ./get_token.py --ip 192.168.2.23 --port 8000

I also tried to hardcode 8000 in connect function for step4_url, for websocket_url and for both. The answer is always the same:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.2.23', port=8000): Max retries exceeded with url: /socket.io/1/?t=1606845620690 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ae57b664748>: Failed to establish a new connection: [Errno 111] Connection refused'))

So looks like 8000 is not an option for my TV as well.

EPMatt commented 3 years ago

Hi @estevez-dev, sorry for my delayed response. Could you please try to run the following command and post its entire output? So we can check which ports are opened on your TV.

nmap <your_ip>

Moreover, If you'd like we can move this issue to #21 and keep track of the progress there. :)

EPMatt commented 3 years ago

Moved to #21.

Ramias1 commented 5 months ago

getting the same thing with my J6200