Clon1998 / mobileraker_companion

Companion for mobileraker, enabling push notification.
MIT License
209 stars 12 forks source link

API Key not working #5

Closed jangrewe closed 2 years ago

jangrewe commented 2 years ago

I'm one of those people that have set force_logins: True, but the API key i'm giving to mobileraker_companion doesn't seem to work. I've tried in ~/klipper_config/Mobileraker.conf and ~/mobileraker_companion/Mobileraker.conf, with the key both raw and quoted, yet still i get a 401:

Jul 12 23:54:33 voronabox python[12299]: Traceback (most recent call last):
Jul 12 23:54:33 voronabox python[12299]:   File "/home/pi/mobileraker-env/lib/python3.7/site-packages/websockets/legacy/client.py", line 598, in __aiter__
Jul 12 23:54:33 voronabox python[12299]:     async with self as protocol:
Jul 12 23:54:33 voronabox python[12299]:   File "/home/pi/mobileraker-env/lib/python3.7/site-packages/websockets/legacy/client.py", line 633, in __aenter__
Jul 12 23:54:33 voronabox python[12299]:     return await self
Jul 12 23:54:33 voronabox python[12299]:   File "/home/pi/mobileraker-env/lib/python3.7/site-packages/websockets/legacy/client.py", line 650, in __await_impl_timeout__
Jul 12 23:54:33 voronabox python[12299]:     return await asyncio.wait_for(self.__await_impl__(), self.open_timeout)
Jul 12 23:54:33 voronabox python[12299]:   File "/usr/lib/python3.7/asyncio/tasks.py", line 416, in wait_for
Jul 12 23:54:33 voronabox python[12299]:     return fut.result()
Jul 12 23:54:33 voronabox python[12299]:   File "/home/pi/mobileraker-env/lib/python3.7/site-packages/websockets/legacy/client.py", line 664, in __await_impl__
Jul 12 23:54:33 voronabox python[12299]:     extra_headers=protocol.extra_headers,
Jul 12 23:54:33 voronabox python[12299]:   File "/home/pi/mobileraker-env/lib/python3.7/site-packages/websockets/legacy/client.py", line 328, in handshake
Jul 12 23:54:33 voronabox python[12299]:     raise InvalidStatusCode(status_code, response_headers)
Jul 12 23:54:33 voronabox python[12299]: websockets.exceptions.InvalidStatusCode: server rejected WebSocket connection: HTTP 401

My ~/klipper_config/Mobileraker.conf looks like this:

[printer Voron_2.4]
moonraker_uri: ws://127.0.0.1:7125/websocket
moonraker_api_key: "<redacted>"

The API key definitely works:

$ curl -Ni -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: 127.0.0.1" -H "Origin: http://127.0.0.1" -H "Sec-WebSocket-Key: SGVsbG8sIHdvcmxkIQ==" -H "Sec-WebSocket-Version: 13" -H "X-Api-Key: <redacted>" http://127.0.0.1:7125/websocket
HTTP/1.1 101 Switching Protocols
Server: TornadoServer/6.1
Date: Tue, 12 Jul 2022 22:04:07 GMT
Upgrade: websocket
Connection: Upgrade
Sec-Websocket-Accept: qGEgH3En71di5rrssAZTmtRTyFk=

▒~l{"jsonrpc": "2.0", "method": "notify_proc_stat_update", "params": [{"moonraker_stats": {"time": 1657663448.0504255, "cpu_usage": 3.22, "memory": 31160, "mem_units": "kB"}, "cpu_temp": 46.251, "network": {"lo": {"rx_bytes": 587374429593, "tx_bytes": 587374429593, "bandwidth": 7617.39}, "eth0": {"rx_bytes": 1246871855, "tx_bytes": 727262478, "bandwidth": 4483.76}, "wlan0": {"rx_bytes": 0, "tx_bytes": 0, "bandwidth": 0.0}}, "system_cpu_usage": {"cpu": 6.11, "cpu0": 1.02, "cpu1": 6.25, "cpu2": 12.37, "cpu3": 4.9}, "system_memory": {"total": 1864328, "available": 1521040, "used": 343288}, "websocket_connections": 2}]}
[...]
Clon1998 commented 2 years ago

Hey, it should work without putting the API key in quotes.

To verify that the config/API key is used can you restart the service and post the new log here? By default the companion should log the config file location it read and the connection tries + used API key. The logs are located in tmp/mobileraker.log

jangrewe commented 2 years ago

Okay, this is strange... i restarted all services involved, and now it seems to be able to connect, but without using the API key. I also got no push notification, yet.

Edit: starting a print got me a notification, additional log lines appended

2022-07-13 10:04:55,645 [mobileraker.py:info()] - [Voron_2.4] Trying to connect to: ws://127.0.0.1:7125/websocket api key <NO API KEY>
2022-07-13 10:04:55,696 [mobileraker.py:info()] - [Voron_2.4] WebSocket connected
2022-07-13 10:04:55,697 [mobileraker.py:info()] - [Voron_2.4] Fetching printer Objects Try#0
2022-07-13 10:04:55,700 [mobileraker.py:info()] - [Voron_2.4] Received Server Info
2022-07-13 10:04:55,701 [mobileraker.py:info()] - [Voron_2.4] Querying printer Objects
2022-07-13 10:04:55,884 [mobileraker.py:info()] - [Voron_2.4] print_state transition NONE -> standby
2022-07-13 10:04:55,885 [mobileraker.py:info()] - [Voron_2.4] Subscribing to printer Objects
2022-07-13 10:04:55,896 [mobileraker.py:info()] - [Voron_2.4] Sending to firebase fcm (https://mobileraker.eliteschw31n.de): {'printState': 'standby', 'tokens': ['<redacted>'], 'printerIdentifier': 'e43cb7d3-<redacted>'
2022-07-13 10:13:53,756 [mobileraker.py:info()] - [Voron_2.4] print_state transition standby -> printing
2022-07-13 10:13:53,783 [mobileraker.py:info()] - [Voron_2.4] Sending to firebase fcm (https://mobileraker.eliteschw31n.de): {'printState': 'printing', 'tokens': ['<redacted>'], 'printerIdentifier': 'e43cb7d3-<redacted>, 'filename': 'Calibration cube.gcode', 'progress': 0.0052528645894942706, 'printingDuration': 0.0}

Not sure why it didn't work last night, because the config and its location is unchanged (besides removing the quotes around the API key again, but that's what i had initially). But it's working, that already a big win! :-)

Clon1998 commented 2 years ago

That's weired. Did you setup a trusted client alongside enforcing logins?

Regarding the notifications, you need to open the app once after a restart and you will only receive notifications if mobileraker isn't in foreground. On Android the app can also be force closed after opening it once while on iOs it is required that mobileraker is in background. Sometimes the power saving constraints of iOS still manage to block the notifications.

jangrewe commented 2 years ago

Yes, 127.0.0.1 is in the trusted clients, but that shouldn't matter because of both force_logins: True and an existing user - which, according to the moonraker docs, disables trusted clients?

Regarding the notifications: thanks, that part is working now! <3

Clon1998 commented 2 years ago

It should disable the trusted_clients. However, it seems like your moonraker instance is using the trusted_clients instead of the users/api-key. I just tested the setup on my printer and everything works as expected however I am currently using moonraker : v0.7.1-589-g4bed314

jangrewe commented 2 years ago

Yea, it does look like it, though that's quite unexpected behaviour. Any other clients that are also listed in trusted_clients are indeed required to provide the API key. Maybe it's also a hardcoded behaviour explicitly and exclusively for 127.0.0.1? My moonraker version is v0.7.1-597-gd37f91c, so a tiny bit newer.

Clon1998 commented 2 years ago

Yea, it does look like it, though that's quite unexpected behaviour. Any other clients that are also listed in trusted_clients are indeed required to provide the API key. Maybe it's also a hardcoded behaviour explicitly and exclusively for 127.0.0.1? My moonraker version is v0.7.1-597-gd37f91c, so a tiny bit newer.

I had a chat with Arksine last week and he confirmed to me that localhost does not have any privileges and therefore I need to provide the API key.

jangrewe commented 2 years ago

Well, then something is wrong. I am providing an API key, mobileraker_companion says it is not using it, yet still it works! :-D

Clon1998 commented 2 years ago

Well, then something is wrong. I am providing an API key, mobileraker_companion says it is not using it, yet still it works! :-D

Can you update the companion? The logging was setup wrongly. Now it should actually show you that it is using an API key :stuck_out_tongue_winking_eye:

jangrewe commented 2 years ago

Ah, that would explain it! Yes, now it works as expected:

2022-07-13 12:23:40,258 [mobileraker.py:info()] - [Voron_2.4] Trying to connect to: ws://127.0.0.1:7125/websocket api key <redacted>##########################