Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to listen on UNIX sockets #5112

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
25 changes: 23 additions & 2 deletions config/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,27 @@ https_only: false
##
#hsts: true

##
## Path of a UNIX socket to listen on for incoming connections.
##
## Note: Enabling socket will make invidious stop listening on the address
## specified by 'host_binding' and 'port'.
##
## Accepted values: Any path to a new file (that doesn't exist yet)
## Default: <none>
##
socket_binding: /tmp/invidious.sock

##
## Permissions for the UNIX socket specified by 'socket_binding'.
##
## Note: The permissions are given in octal, following UNIX convention.
##
## Accepted values: 000-777
## Default: 777
##
socket_permissions: 777


# -----------------------------
# Network (outbound)
Expand Down Expand Up @@ -177,7 +198,7 @@ https_only: false
## Configuration for using a HTTP proxy
##
## If unset, then no HTTP proxy will be used.
##
unixfox marked this conversation as resolved.
Show resolved Hide resolved
##
http_proxy:
user:
password:
Expand Down Expand Up @@ -839,7 +860,7 @@ default_user_preferences:
## Default: true
##
#vr_mode: true

##
## Save the playback position
## Allow to continue watching at the previous position when
Expand Down
18 changes: 15 additions & 3 deletions src/invidious.cr
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ add_context_storage_type(Preferences)
add_context_storage_type(Invidious::User)

Kemal.config.logger = LOGGER
Kemal.config.host_binding = Kemal.config.host_binding != "0.0.0.0" ? Kemal.config.host_binding : CONFIG.host_binding
Kemal.config.port = Kemal.config.port != 3000 ? Kemal.config.port : CONFIG.port
Kemal.config.app_name = "Invidious"

# Use in kemal's production mode.
Expand All @@ -249,4 +247,18 @@ Kemal.config.app_name = "Invidious"
Kemal.config.env = "production" if !ENV.has_key?("KEMAL_ENV")
{% end %}

Kemal.run
Kemal.run do |config|
if CONFIG.socket_binding
if File.exists?(CONFIG.socket_binding.not_nil!)
File.delete(CONFIG.socket_binding.not_nil!)
end
# Create a socket and set its desired permissions
server = UNIXServer.new(CONFIG.socket_binding.not_nil!)
perms = CONFIG.socket_permissions.to_i(base: 8)
File.chmod(CONFIG.socket_binding.not_nil!, perms)
config.server.not_nil!.bind server
else
Kemal.config.host_binding = Kemal.config.host_binding != "0.0.0.0" ? Kemal.config.host_binding : CONFIG.host_binding
Kemal.config.port = Kemal.config.port != 3000 ? Kemal.config.port : CONFIG.port
end
end
4 changes: 4 additions & 0 deletions src/invidious/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class Config
property port : Int32 = 3000
# Host to bind (overridden by command line argument)
property host_binding : String = "0.0.0.0"
# Make Invidious listen on a UNIX socket instead of a TCP port - Example: /tmp/invidious.sock
property socket_binding : String? = nil
# Permissions of the listening socket in octal
property socket_permissions : String = "777"
# Pool size for HTTP requests to youtube.com and ytimg.com (each domain has a separate pool of `pool_size`)
property pool_size : Int32 = 100
# HTTP Proxy configuration
Expand Down