Skip to content

Conversation

@balki
Copy link
Contributor

@balki balki commented Mar 28, 2025

Brief summary

Support listening on unix socket

Which issue is fixed?

Fixes #3819

In-depth Description

When the HOST options/environment variable starts with unix/, rest of it is assumed to be unix socket path.

How have you tested this?

Tested in archlinux. see: https://gitea.balki.me/balki-aur/audiobookshelf

Screenshots

N/A

@advplyr
Copy link
Owner

advplyr commented Mar 28, 2025

Does this work with the mobile apps?

@balki
Copy link
Contributor Author

balki commented Mar 28, 2025

Does this work with the mobile apps?

Yes. does not affect mobile apps. I use both PWA and Android mobile app.

@JBlond
Copy link
Contributor

JBlond commented Apr 26, 2025

@balki Does the PWA now connect via the socket? Or how you want to use the socket since AFAIK it works only locally.

@balki
Copy link
Contributor Author

balki commented Apr 26, 2025

@JBlond @advplyr

AFAIK it works only locally.

Yes. right sockets work locally only. You need a web server like Caddy / Nginx / Apache that connects to audiobookshelf over unix socket. E.g. I have below in my caddy config.

audiobookshelf.mydomain.com {
    log {
        output file /var/log/caddy/audiobookshelf.log
    }
    header {
        Content-Security-Policy "default-src 'self' 'unsafe-inline' data: blob:;"
        Referrer-Policy same-origin
    }
    reverse_proxy unix//run/audiobookshelf/web.sock
}

The reverse_proxy line above is the path to the unix socket.

I use audiobookshelf installed on archlinux: package which uses environment variables for configuration.

I have set the environment variable HOST=unix//run/audiobookshelf/web.sock. With this PR, audiobookshelf listens on the unix socket at that path.

Both PWA and android app, connects to https://audiobookshelf.mydomain.com which is served by caddy web server. They don't know/care that the web server internally connects to audiobookshelf via unix socket.

@balki
Copy link
Contributor Author

balki commented Apr 29, 2025

@advplyr It would be very helpful to have this feature merged soon. I have been patching and manually compiling audiobookshelf for past few releases now to get this support.

I run all my selfhosted applications listening via unix sockets so that they can be isolated into dedicated network namespaces.

This is a common feature and supported by most self-hosted applications. Few examples:

App Doc Example
Miniflux https://miniflux.app/docs/configuration.html#listen-addr LISTEN_ADDR=/run/miniflux.sock
Gitea https://docs.gitea.com/administration/config-cheat-sheet#server-server PROTOCOL=http+unix HTTP_ADDR=/run/gitea.sock
Navidrome https://www.navidrome.org/docs/usage/configuration-options/#basic-configuration Address = unix:/path/to/web.sock
Jellyfin jellyfin/jellyfin.org#766 (comment) "JELLYFIN_kestrel__socketPermissions=0777" "JELLYFIN_kestrel__socketPath=/run/jelly.sock" "JELLYFIN_kestrel__socket=true"

Also the change is small, straightforward and does not add any new configuration.
Absolutely no side-effect for users who don't use the feature.

server/Server.js Outdated
if(this.Host.startsWith(unixSocketPrefix)) {
const sockPath = this.Host.slice(unixSocketPrefix.length)
this.server.listen(sockPath, () => {
fs.chmodSync(sockPath, 0o666)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for this? If that is staying in I think it should be async

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default permissions will depend on user's umask setting. Setting permissions so that the webserver will be able to communicate. This code runs at startup and chmod is fast. Also all other fs functions in this file are not async.

@advplyr advplyr merged commit 241b56a into advplyr:master May 16, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement]: Add support for launch instance under a unix domain socket.

3 participants