klattimer / LGWebOSRemote

Command line webOS remote for LGTVs
MIT License
490 stars 97 forks source link

SSL by default, failover to plain #140

Open klattimer opened 7 months ago

klattimer commented 7 months ago

Some devices will support SSL, some will not, of those which do, some may support plaintext. It makes most sense for us to take the most secure option and failover to the least secure option.

Tenzer commented 6 months ago

One slight complication with this for now, is that ws4py is using ssl.wrap_socket when SSL is enabled, which was marked as deprecated in Python 3.7 and removed in Python 3.12, so it makes lgtv fail like this:

~ % lgtv --ssl swInfo
Traceback (most recent call last):
  File "/Users/jeppe/.local/bin/lgtv", line 33, in <module>
    sys.exit(load_entry_point('LGTV', 'console_scripts', 'lgtv')())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jeppe/my-git/LGWebOSRemote/LGTV/__init__.py", line 191, in main
    ws.connect()
  File "/Users/jeppe/.local/pipx/venvs/lgtv/lib/python3.12/site-packages/ws4py/client/__init__.py", line 214, in connect
    self.sock = ssl.wrap_socket(self.sock, **self.ssl_options)
                ^^^^^^^^^^^^^^^
AttributeError: module 'ssl' has no attribute 'wrap_socket'

There's a fix for ws4py in https://github.com/Lawouach/WebSocket-for-Python/pull/283, but I wonder if it might otherwise be worth looking at migrating to another websocket library that is kept more up to date, as ws4py hasn't had a release since 2018?

klattimer commented 6 months ago

@Tenzer ws4py was the only one I found that I could make work - specifically, I had to exclude default headers in order to make it work back on webOS 2.x IIRC. You'll find this in the code where exclude_headers=['Origin'] IIRC, which was always being sent by ws4py (it IS part of the spec, so you can't blame them).

This is in itself a bit of a hack, and was likely due to a bug in the webOS web socket server libraries and may now have been fixed.

Aurshur commented 6 months ago

Is there any workaround with this?

I tried @cmer 's suggestion of rolling back to Python 3.6 but got the following error:

% brew install python@3.6 Warning: No available formula with the name "python@3.6". Did you mean python@3.9, python@3.8, python@3.7, python@3.10, python@3.12, python@3.11 or python-yq? ==> Searching for similarly named formulae and casks... ==> Formulae python@3.9 python@3.8 python@3.7 python@3.10 python@3.12 python@3.11 python-yq

Tenzer commented 6 months ago

The problem only exists with Python 3.12, so try version 3.11 for now.

Aurshur commented 6 months ago

The problem only exists with Python 3.12, so try version 3.11 for now.

That worked! Thank you!

egnappahz commented 2 months ago

I'm sorry, but the real fix is https://github.com/Lawouach/WebSocket-for-Python/pull/283/files/1d9ffff3beb71553c85e846a1b828ec7d890e751

Not all of us can afford to use ancient versions of python.