From 1c706e71091715bd52f8053890e9ce7ac8523daf Mon Sep 17 00:00:00 2001 From: tituspijean Date: Tue, 1 Jun 2021 21:56:36 +0200 Subject: [PATCH] Improve discovery ports opening --- scripts/_common.sh | 3 +++ scripts/install | 36 ++++++++++++++++--------- scripts/restore | 66 +++++++++++++++++++++++++++------------------- scripts/upgrade | 48 ++++++++++++++++++++------------- 4 files changed, 96 insertions(+), 57 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 429ccc7..f9614fd 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -12,6 +12,9 @@ ffmpeg_pkg_version="4.3.2-1" architecture=$(dpkg --print-architecture) +discovery_service_port=1900 +discovery_client_port=7359 + #================================================= # PERSONAL HELPERS #================================================= diff --git a/scripts/install b/scripts/install index aa228d8..8d98100 100644 --- a/scripts/install +++ b/scripts/install @@ -73,25 +73,27 @@ port=$(ynh_find_port --port=8095) ynh_app_setting_set --app=$app --key=port --value=$port if [ $discovery -eq 1 ]; then - ynh_script_progression --message="Configuring firewall..." --time --weight=1 + ynh_script_progression --message="Configuring firewall..." --weight=1 # Open port 1900 for service auto-discovery - if ynh_port_available --port=1900; then - ynh_exec_warn_less yunohost firewall allow UDP 1900 - ynh_app_setting_set --app=$app --key=discovery_service --value=1 + if ynh_port_available --port=$discovery_service_port; then + discovery_service=1 + ynh_exec_warn_less yunohost firewall allow UDP $discovery_service_port else - ynh_print_warn --message="Port 1900 (for service auto-discovery) is not available. Continuing nonetheless." - ynh_app_setting_set --app=$app --key=discovery_service --value=0 + discovery_service=0 + ynh_print_warn --message="Port $discovery_service_port (for service auto-discovery) is not available. Continuing nonetheless." fi + ynh_app_setting_set --app=$app --key=discovery_service --value=$discovery_service # Open port 7359 for client auto-discovery - if ynh_port_available --port=7359; then - ynh_exec_warn_less yunohost firewall allow UDP 7359 - ynh_app_setting_set --app=$app --key=discovery_client --value=1 + if ynh_port_available --port=$discovery_client_port; then + discovery_client=1 + ynh_exec_warn_less yunohost firewall allow UDP $discovery_client_port else - ynh_print_warn --message="Port 7359 (for client auto-discovery) is not available. Continuing nonetheless." - ynh_app_setting_set --app=$app --key=discovery_client --value=0 + discovery_client=0 + ynh_print_warn --message="Port $discovery_client_port (for client auto-discovery) is not available. Continuing nonetheless." fi + ynh_app_setting_set --app=$app --key=discovery_service --value=$discovery_client fi #================================================= @@ -198,7 +200,17 @@ ynh_use_logrotate #================================================= ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 -yunohost service add $app --description="Jellyfin media center" --log_type="systemd" +# Build the ports list +needed_ports=() +(( $discovery_server == 1 )) && needed_ports+=( "$discovery_server_port" ) +(( $discovery_client == 1 )) && needed_ports+=( "$discovery_client_port" ) + +# Integrate service and require to expose the ports if needed +if [ -z ${needed_ports[@]} ]; then + yunohost service add $app --description="Jellyfin media center" --log_type="systemd" +else + yunohost service add $app --description="Jellyfin media center" --log_type="systemd" --needs_exposed_ports=${needed_ports[@]} +fi #================================================= # START SYSTEMD SERVICE diff --git a/scripts/restore b/scripts/restore index f8b2b3e..094de42 100644 --- a/scripts/restore +++ b/scripts/restore @@ -56,25 +56,27 @@ fi #================================================= if [ $discovery -eq 1 ]; then - ynh_script_progression --message="Configuring firewall..." --time --weight=1 + ynh_script_progression --message="Configuring firewall..." --weight=1 - # Open port 1900 for service auto-discovery - if ynh_port_available --port=1900; then - ynh_exec_warn_less yunohost firewall allow UDP 1900 - ynh_app_setting_set --app=$app --key=discovery_service --value=1 - else - ynh_print_warn --message="Port 1900 (for service auto-discovery) is not available. Continuing nonetheless." - ynh_app_setting_set --app=$app --key=discovery_service --value=0 - fi + # Open port 1900 for service auto-discovery + if ynh_port_available --port=$discovery_service_port; then + discovery_service=1 + ynh_exec_warn_less yunohost firewall allow UDP $discovery_service_port + else + discovery_service=0 + ynh_print_warn --message="Port $discovery_service_port (for service auto-discovery) is not available. Continuing nonetheless." + fi + ynh_app_setting_set --app=$app --key=discovery_service --value=$discovery_service - # Open port 7359 for client auto-discovery - if ynh_port_available --port=7359; then - ynh_exec_warn_less yunohost firewall allow UDP 7359 - ynh_app_setting_set --app=$app --key=discovery_client --value=1 - else - ynh_print_warn --message="Port 7359 (for client auto-discovery) is not available. Continuing nonetheless." - ynh_app_setting_set --app=$app --key=discovery_client --value=0 - fi + # Open port 7359 for client auto-discovery + if ynh_port_available --port=$discovery_client_port; then + discovery_client=1 + ynh_exec_warn_less yunohost firewall allow UDP $discovery_client_port + else + discovery_client=0 + ynh_print_warn --message="Port $discovery_client_port (for client auto-discovery) is not available. Continuing nonetheless." + fi + ynh_app_setting_set --app=$app --key=discovery_service --value=$discovery_client fi #================================================= @@ -102,15 +104,6 @@ ynh_script_progression --message="Recreating the dedicated system user..." --wei # Create the dedicated user (if not existing) ynh_system_user_create --username=$app -#================================================= -# RESTORE USER RIGHTS -#================================================= -ynh_script_progression --message="Restoring user rights..." - -# Restore permissions on app files -chown -R $app: $final_path -chown -R $app: $config_path - #================================================= # SPECIFIC RESTORATION #================================================= @@ -145,7 +138,26 @@ systemctl enable jellyfin.service --quiet #================================================= ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 -yunohost service add $app --description="Jellyfin media center" --log_type="systemd" +# Build the ports list +needed_ports=() +(( $discovery_server == 1 )) && needed_ports+=( "$discovery_server_port" ) +(( $discovery_client == 1 )) && needed_ports+=( "$discovery_client_port" ) + +# Integrate service and require to expose the ports if needed +if [ -z ${needed_ports[@]} ]; then + yunohost service add $app --description="Jellyfin media center" --log_type="systemd" +else + yunohost service add $app --description="Jellyfin media center" --log_type="systemd" --needs_exposed_ports=${needed_ports[@]} +fi + +#================================================= +# RESTORE USER RIGHTS +#================================================= +ynh_script_progression --message="Restoring user rights..." + +# Restore permissions on app files +chown -R $app: $final_path +chown -R $app: $config_path #================================================= # START SYSTEMD SERVICE diff --git a/scripts/upgrade b/scripts/upgrade index b324d30..d1f8335 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -92,25 +92,27 @@ ynh_abort_if_errors #================================================= if [ $discovery -eq 1 ]; then - ynh_script_progression --message="Configuring firewall..." --time --weight=1 + ynh_script_progression --message="Configuring firewall..." --weight=1 - # Open port 1900 for service auto-discovery - if ynh_port_available --port=1900; then - ynh_exec_warn_less yunohost firewall allow UDP 1900 - ynh_app_setting_set --app=$app --key=discovery_service --value=1 - else - ynh_print_warn --message="Port 1900 (for service auto-discovery) is not available. Continuing nonetheless." - ynh_app_setting_set --app=$app --key=discovery_service --value=0 - fi + # Open port 1900 for service auto-discovery + if ynh_port_available --port=$discovery_service_port; then + discovery_service=1 + ynh_exec_warn_less yunohost firewall allow UDP $discovery_service_port + else + discovery_service=0 + ynh_print_warn --message="Port $discovery_service_port (for service auto-discovery) is not available. Continuing nonetheless." + fi + ynh_app_setting_set --app=$app --key=discovery_service --value=$discovery_service - # Open port 7359 for client auto-discovery - if ynh_port_available --port=7359; then - ynh_exec_warn_less yunohost firewall allow UDP 7359 - ynh_app_setting_set --app=$app --key=discovery_client --value=1 - else - ynh_print_warn --message="Port 7359 (for client auto-discovery) is not available. Continuing nonetheless." - ynh_app_setting_set --app=$app --key=discovery_client --value=0 - fi + # Open port 7359 for client auto-discovery + if ynh_port_available --port=$discovery_client_port; then + discovery_client=1 + ynh_exec_warn_less yunohost firewall allow UDP $discovery_client_port + else + discovery_client=0 + ynh_print_warn --message="Port $discovery_client_port (for client auto-discovery) is not available. Continuing nonetheless." + fi + ynh_app_setting_set --app=$app --key=discovery_service --value=$discovery_client fi #================================================= @@ -229,7 +231,17 @@ ynh_use_logrotate --non-append #================================================= ynh_script_progression --message="Integrating service in YunoHost..." --weight=1 -yunohost service add $app --description="Jellyfin media center" --log_type="systemd" +# Build the ports list +needed_ports=() +(( $discovery_server == 1 )) && needed_ports+=( "$discovery_server_port" ) +(( $discovery_client == 1 )) && needed_ports+=( "$discovery_client_port" ) + +# Integrate service and require to expose the ports if needed +if [ -z ${needed_ports[@]} ]; then + yunohost service add $app --description="Jellyfin media center" --log_type="systemd" +else + yunohost service add $app --description="Jellyfin media center" --log_type="systemd" --needs_exposed_ports=${needed_ports[@]} +fi #================================================= # START SYSTEMD SERVICE