diff --git a/.includes/copyparty.sh b/.includes/copyparty.sh index cc93fd9..05d6c9e 100644 --- a/.includes/copyparty.sh +++ b/.includes/copyparty.sh @@ -50,6 +50,40 @@ detect_private_subnets() { ' } +detect_first_private_ipv4() { + ip -o -4 addr show up scope global | awk ' + { + split($4, parts, "/") + split(parts[1], octets, ".") + + if (octets[1] == 10 || + (octets[1] == 192 && octets[2] == 168) || + (octets[1] == 172 && octets[2] >= 16 && octets[2] <= 31)) { + print parts[1] + exit 0 + } + } + ' +} + +show_copyparty_address() { + local lanAddress="" + + if ! copyparty_installed; then + msgbox "Copyparty is not installed." + return 1 + fi + + lanAddress="$(detect_first_private_ipv4)" + if [[ -z "$lanAddress" ]]; then + msgbox "Copyparty is installed, but no private IPv4 address could be detected automatically." + return 1 + fi + + msgbox "Copyparty should be available at http://${lanAddress}:${copypartyPort}" + return 0 +} + choose_lan_subnet() { local detectedSubnet="" local subnetChoice="" @@ -433,22 +467,50 @@ configure_copyparty_firewall() { return 0 } -serviceUser="$(choose_copyparty_user)" || return 1 -install_copyparty || return 1 -accountsFile="$(collect_copyparty_accounts)" || return 1 -usernamesCsv="$(join_account_names "$accountsFile")" -sharesFile="$(collect_copyparty_shares "$usernamesCsv" "$serviceUser")" || { - rm -f "$accountsFile" - return 1 -} +install_copyparty_flow() { + local serviceUser="" + local unitName="" + local accountsFile="" + local usernamesCsv="" + local sharesFile="" + local lanAddress="" -write_copyparty_config "$serviceUser" "$accountsFile" "$sharesFile" || { + serviceUser="$(choose_copyparty_user)" || return 1 + unitName="copyparty@${serviceUser}.service" + install_copyparty || return 1 + accountsFile="$(collect_copyparty_accounts)" || return 1 + usernamesCsv="$(join_account_names "$accountsFile")" + sharesFile="$(collect_copyparty_shares "$usernamesCsv" "$serviceUser")" || { + rm -f "$accountsFile" + return 1 + } + + write_copyparty_config "$serviceUser" "$accountsFile" "$sharesFile" || { + rm -f "$accountsFile" "$sharesFile" + return 1 + } rm -f "$accountsFile" "$sharesFile" - return 1 + + enable_copyparty_service "$serviceUser" || return 1 + configure_copyparty_firewall || return 1 + + lanAddress="$(detect_first_private_ipv4)" + if [[ -n "$lanAddress" ]]; then + msgbox "Copyparty is configured. Enabled unit: ${unitName}. It should now be available at http://${lanAddress}:${copypartyPort}. For advanced tuning, edit ${copypartyConfigFile} as root." + else + msgbox "Copyparty is configured. Enabled unit: ${unitName}. For advanced tuning, edit ${copypartyConfigFile} as root." + fi } -rm -f "$accountsFile" "$sharesFile" -enable_copyparty_service "$serviceUser" || return 1 -configure_copyparty_firewall || return 1 - -msgbox "Copyparty is configured. For advanced tuning, edit ${copypartyConfigFile} as root." +case "${1:-install}" in + install) + install_copyparty_flow + ;; + show-address) + show_copyparty_address + ;; + *) + msgbox "Unknown Copyparty action: ${1}" + return 1 + ;; +esac diff --git a/configure-server.sh b/configure-server.sh index 156cbfa..50e6fec 100644 --- a/configure-server.sh +++ b/configure-server.sh @@ -35,8 +35,18 @@ fi source .includes/functions.sh source .includes/ui.sh +copyparty_installed() { + pacman -Q copyparty &> /dev/null +} + while true; do - choice="$(menulist "Firewall" "MiniDLNA" "Mumble Server" "Nginx" "Top Speed Server" "Top Speed Console" "Copyparty" "Exit")" || break + menuOptions=("Firewall" "MiniDLNA" "Mumble Server" "Nginx" "Top Speed Server" "Top Speed Console" "Copyparty") + if copyparty_installed; then + menuOptions+=("Show Copyparty Address") + fi + menuOptions+=("Exit") + + choice="$(menulist "${menuOptions[@]}")" || break case "$choice" in "Firewall") source .includes/firewall.sh @@ -59,6 +69,9 @@ while true; do "Copyparty") source .includes/copyparty.sh ;; + "Show Copyparty Address") + source .includes/copyparty.sh show-address + ;; "Exit") break ;;