librespot-org / librespot

Open Source Spotify client library
MIT License
4.86k stars 616 forks source link

Spotify does not detect Librespot as a device #876

Closed itwasonlyabug closed 2 years ago

itwasonlyabug commented 3 years ago

Hi, I have been using Librespot for a few months now without any issues. Starting this month (October) though, none of my Librespot instances show up in Spotify.

I am running Librespot in Docker containers on a Raspberry Pi 4, but I also tried a direct cargo install librespot on my laptop with no success.

I have made no changes to my home network and I have tried with various combinations of Wi-Fi 2,ghz, Wi-Fi 5ghz and just everything wired together via Ethernet cable. I see no errors in Librespot's logs (besides ones about the Avahi demon when running inside a container).

Versions tried: 0.1.6 (this one was previously working) 0.2.0 (ditto) 0.3.1

Any tips on how to debug this?

JasonLG1979 commented 2 years ago

Well something has changed and it wasn't librespot.

dubo-dubon-duponey commented 2 years ago

hm. I have been running librespot on this machine for 3 years, without authentication

@sdetweil so, I assume you have been playing music on that librespot instance, right?

Then that means you have been authenticating: whenever you do play music on that librespot instance, you effectively authenticate with your user account. Then, this librespot instance will show-up in the list of devices attached to your user account, which will be returned by your node module...

Maybe the question is actually: what do you want to achieve here? (beside technical details)

If what you want to do is: "list available speakers on my local network", then you are clearly using the wrong nodejs library, since that library can only list devices that have been playing music already, or have been manually authenticated.

JasonLG1979 commented 2 years ago

If you enable caching librespot will use the cached auth token. You ofc have to connect at least once.

dubo-dubon-duponey commented 2 years ago

If you enable caching librespot will use the cached auth token. You ofc have to connect at least once.

Yeah, that's my guess about what has happened for @sdetweil and why they think things have changed / they did not have to authenticate before.

JasonLG1979 commented 2 years ago

Yeah, that's my guess about what has happened for @sdetweil and why they think things have changed / they did not have to authenticate before.

Ofc if you disable caching you can't expect to use the cached auth token. And I honestly have no idea how long the tokens are good for?

sdetweil commented 2 years ago

this smart-mirror is voice centered
so the command (after getting the oauth done) is

smart-mirror, ask/tell spotify to play ???? on [ this device | device name ] and I can transfer play , etc..

and it would play. the code had a retry problem i fixed last year, not playing for a day, didn't find it on the first try, but did on retry, thats when I found the spotify connect app but only used it once or twice. (why use a UI when voice works?!)

anyhow, it worked.. and then this year, spring ish I saw the it was looping in the background.. got the newer librespot code and all was well.. and then last week saw my cpu heat was up and found it looping again ..

then found this issue..

my command from before today

librespot  -n 'sams unix' -b 320 -c2 ./cache --enable-volume-normalisation --initial-volume 75 --device-type avr  --device "hdmi:CARD=NVidia,DEV=0"

the voice user has always been oauth authenticated for spotify

JasonLG1979 commented 2 years ago

-c2 ./cache is not valid.

sdetweil commented 2 years ago

its running, no error messages..

found that on some google search years ago.. it worked..

JasonLG1979 commented 2 years ago

its running, no error messages..

found that on some google search years ago.. it worked..

Then by all means listen to Google and not the guy that wrote the code that parses the command line args in librespot...

sdetweil commented 2 years ago

my point was, ok, so I am not a librespot commandline guy (and don't want to be).. I got enough on my plate.. the doc didn't help me.. terms I don't understand, so, I searched around til I found something that worked. and stuck with it.

but , it didn't complain, or I would have found something else that didn't..

I don't play music often, I have a small hearing problem that i find is minimized with less surround sound content. but my users don't have that problem, and also just want it to work.

I will gladly take your advice on how to change it.

JasonLG1979 commented 2 years ago

-c2 ./cache creates a cache folder called 2 (the ./cache part is ignored) inside the home of the user that starts librespot. In the case of Raspotify it doesn't work because it uses a dynamic user that has no home. And even if that folder existed somewhere you wouldn't have permission to write to it. You should use /var/cache/raspotify as documented since that's the ONLY place the Raspotify service has permission to write to.

sdetweil commented 2 years ago

ok, note that I am running on ubuntu now, using librespot, not raspotify

JasonLG1979 commented 2 years ago

How do you run the service?

sdetweil commented 2 years ago

I use pm2 to launch the binary under my user

JasonLG1979 commented 2 years ago

Is that user privileged?

JasonLG1979 commented 2 years ago

I have no experience with pm2 I personally would just use systemd since it's more native and allows you to sandbox services.

sdetweil commented 2 years ago

i do not think so.. i am in sudoers I had to make /var/cache/librespot 777 for my command to not get access denied on start (666 didn't work as I would have expected)

JasonLG1979 commented 2 years ago

Oh, wow. running a network facing service as a sudoer with 777 cache... fully loaded foot guns.

sdetweil commented 2 years ago

Oh, wow. running a network facing service as a sudoer with 777 cache... fully loaded foot guns.

what the heck do you want me to do?

I just want the damn thing to RUN,.. u wanted cache, so I tried to set that up.. I TOLD you what i did, not hiding anything

JasonLG1979 commented 2 years ago

No, what I mean is that above you were complaining about having your password and username in plain text and you're basically running naked though a corn field with that thing. Your Spotify creds are the least of your worries if that's your approach to security. It's actually kinda funny.

sdetweil commented 2 years ago

ok, thanks.. I'm gonna go get some dinner.. running with /c2

JasonLG1979 commented 2 years ago

I'm sorry. All kidding aside:

  1. Never run system services as a privileged user if you can help it. And you can help it for sure in this case. Nothing about librespot requires privileges.

  2. 700 should be fine for the cache. That means that only the user who owns the folder can read, write and execute.

  3. Don't use 777 if you have to use 777 more than likely you're doing something wrong.

sdetweil commented 2 years ago

the folder /var/cache/librespot did not exist I had to use sudo to create it, so that set the user to root

so 600 won't work as I am not root 666 did not work but should have

JasonLG1979 commented 2 years ago

the folder /var/cache/librespot did not exist

I'm sorry the folder has to be 700 you want the files 600.

It would if you configured librespot to start with systemd and configured the cache folder to be /var/cache/librespot in the unit file with:

# makes everything written by the `librespot` service 600
UMask=077
# read, write, & execute only for owner
CacheDirectoryMode=0700
# sets cache to /var/cache/librespot
CacheDirectory=librespot

Systemd will create the folder for you if it does not exist. You still need to point librespot at /var/cache/librespot. The above just takes care of creating the folder and setting the permissions.

You can see a full example of a reasonably secure librespot systemd service unit file here.

You're mileage may vary depending on what version of Ubuntu you're running. The sandbox settings target the version of systemd that comes with Raspberry Pi OS (based on Debian Bullseye) which is 247.

With that configuration librespot will more than likely be the most secure service on you system as reported by systemd-analyze security and still be fully functional.

JasonLG1979 commented 2 years ago

You can also clear the cache by stopping the service with sudo systemctl stop librespot then sudo systemctl clean librespot and finally sudo systemctl restart librespot if you want to restart it. That's assuming you name the service librespot ofc.

It would probably be easier for you to just use the Raspotify package if you have a recent enough version of systemd.

JasonLG1979 commented 2 years ago

I feel like this has gone way off topic. I will stop now and say that yours is a problem of configuration and a lack of understanding @sdetweil if you'd like to continue we can move to a discussion. I'm done here as it's just noise for anyone who is actually having this legitimate issue.

roderickvd commented 2 years ago

And I honestly have no idea how long the tokens are good for?

The new-api branch has a new caching token provider that automatically refreshes tokens 10 seconds before they expire. It also reveals when the expiry time is. This is something that Spotify sets and isn't guaranteed to be any set value in the future.