diff --git a/start-finalSetupModpack b/start-finalSetupModpack index 0c18406b..20adddab 100644 --- a/start-finalSetupModpack +++ b/start-finalSetupModpack @@ -25,7 +25,7 @@ if [[ "$MODPACK" ]]; then if [[ "${MODPACK}" == *.zip ]]; then downloadUrl="${MODPACK}" else - downloadUrl=$(curl -Ls -o /dev/null -w %{url_effective} $MODPACK) + downloadUrl=$(curl -Ls -o /dev/null -w %{effective_url} $MODPACK) if ! [[ $downloadUrl == *.zip ]]; then log "ERROR Invalid URL given for MODPACK: $downloadUrl resolved from $MODPACK" log " Must be HTTP or HTTPS and a ZIP file" @@ -61,39 +61,31 @@ fi # If supplied with a URL for a plugin download it. if [[ "$MODS" ]]; then + if [ "$TYPE" = "SPIGOT" ]; then + out_dir=/data/plugins + else + out_dir=/data/mods + fi + mkdir -p "$out_dir" + for i in ${MODS//,/ } do if isURL $i; then - if [[ $i == *.jar ]]; then - EFFECTIVE_MOD_URL=$i - else - EFFECTIVE_MOD_URL=$(curl -Ls -o /dev/null -w %{url_effective} $i) - if ! [[ $EFFECTIVE_MOD_URL == *.jar ]]; then - log "ERROR Invalid URL given in MODS: $EFFECTIVE_MOD_URL resolved from $i" - log " Must be HTTP or HTTPS and a JAR file" - exit 1 + log "Downloading mod/plugin $i ..." + effective_url=$(resolveEffectiveUrl "$i") + if isValidFileURL jar "${effective_url}"; then + out_file=$(getFilenameFromUrl "${effective_url}") + if ! curl -fsSL -o "${out_dir}/$out_file" "${effective_url}"; then + log "ERROR: failed to download from $i into $out_dir" + exit 2 fi - fi - - log "Downloading mod/plugin via HTTP" - log " from $EFFECTIVE_MOD_URL ..." - if ! curl -sSL -o /tmp/${EFFECTIVE_MOD_URL##*/} $EFFECTIVE_MOD_URL; then - log "ERROR: failed to download from $EFFECTIVE_MOD_URL to /tmp/${EFFECTIVE_MOD_URL##*/}" + else + log "ERROR: $effective_url resolved from $i is not a valid jar URL" exit 2 fi - - if [ "$TYPE" = "SPIGOT" ]; then - mkdir -p /data/plugins - mv /tmp/${EFFECTIVE_MOD_URL##*/} /data/plugins/${EFFECTIVE_MOD_URL##*/} - else - mkdir -p /data/mods - mv /tmp/${EFFECTIVE_MOD_URL##*/} /data/mods/${EFFECTIVE_MOD_URL##*/} - fi - rm -f /tmp/${EFFECTIVE_MOD_URL##*/} - else log "ERROR Invalid URL given in MODS: $i" - exit 1 + exit 2 fi done fi @@ -103,7 +95,7 @@ if [[ "$MANIFEST" ]]; then EFFECTIVE_MANIFEST_FILE=$MANIFEST elif isURL "$MANIFEST"; then EFFECTIVE_MANIFEST_FILE=/tmp/manifest.json - EFFECTIVE_MANIFEST_URL=$(curl -Ls -o /dev/null -w %{url_effective} $MANIFEST) + EFFECTIVE_MANIFEST_URL=$(curl -Ls -o /dev/null -w %{effective_url} $MANIFEST) curl -Ls -o $EFFECTIVE_MANIFEST_FILE "$EFFECTIVE_MANIFEST_URL" else log "MANIFEST='$MANIFEST' is not a valid manifest url or location" @@ -124,7 +116,7 @@ case "X$EFFECTIVE_MANIFEST_FILE" in do if [ ! -f $MOD_DIR/${p}_${f}.jar ] then - redirect_url="$(curl -Ls -o /dev/null -w %{url_effective} ${CURSE_URL_BASE}/${p})" + redirect_url="$(curl -Ls -o /dev/null -w %{effective_url} ${CURSE_URL_BASE}/${p})" url="$redirect_url/download/${f}/file" log Downloading curseforge mod $url # Manifest usually doesn't have mod names. Using id should be fine, tho diff --git a/start-utils b/start-utils index a46d1a28..b461a836 100644 --- a/start-utils +++ b/start-utils @@ -1,8 +1,14 @@ #!/bin/bash -function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; } +function join_by() { + local d=$1 + shift + echo -n "$1" + shift + printf "%s" "${@/#/$d}" +} -function isURL { +function isURL() { local value=$1 if [[ ${value:0:8} == "https://" || ${value:0:7} == "http://" ]]; then @@ -12,90 +18,114 @@ function isURL { fi } -function isTrue { +function isValidFileURL() { + suffix=${1:?Missing required suffix arg} + url=${2:?Missing required url arg} + + [[ "$url" == http*://*.${suffix} || "$url" == http*://*.${suffix}\?* ]] +} + +function resolveEffectiveUrl() { + url="${1:?Missing required url argument}" + if ! curl -Ls -o /dev/null -w %{url_effective} "$url"; then + log "ERROR failed to resolve effective URL from $url" + exit 2 + fi +} + +function getFilenameFromUrl() { + url="${1:?Missing required url argument}" + strippedOfQuery="${url%\?*}" + basename "$strippedOfQuery" +} + +function isTrue() { local value=${1,,} result= case ${value} in - true|on) - result=0 - ;; - *) - result=1 - ;; + true | on) + result=0 + ;; + *) + result=1 + ;; esac return ${result} } -function isDebugging { - if [[ -v DEBUG ]] && [[ ${DEBUG^^} = TRUE ]]; then +function isDebugging() { + if [[ -v DEBUG ]] && [[ ${DEBUG^^} == TRUE ]]; then return 0 else return 1 fi } -function debug { +function debug() { if isDebugging; then log "DEBUG: $*" fi } -function logn { +function logn() { echo -n "[init] $*" } -function log { +function log() { echo "[init] $*" } -function logAutopause { +function logAutopause() { echo "[Autopause loop] $*" } -function logAutopauseAction { +function logAutopauseAction() { echo "[$(date -Iseconds)] [Autopause] $*" } -function normalizeMemSize { +function normalizeMemSize() { local scale=1 case ${1,,} in - *k) - scale=1024;; - *m) - scale=1048576;; - *g) - scale=1073741824;; + *k) + scale=1024 + ;; + *m) + scale=1048576 + ;; + *g) + scale=1073741824 + ;; esac - val=${1:0: -1} - echo $(( val * scale )) + val=${1:0:-1} + echo $((val * scale)) } -function versionLessThan { +function versionLessThan() { local activeParts - IFS=. read -ra activeParts <<< "${VANILLA_VERSION}" + IFS=. read -ra activeParts <<<"${VANILLA_VERSION}" local givenParts - IFS=. read -ra givenParts <<< "$1" + IFS=. read -ra givenParts <<<"$1" - if (( ${#activeParts[@]} < 2 )); then + if ((${#activeParts[@]} < 2)); then return 1 fi - if (( ${#activeParts[@]} == 2 )); then - if (( activeParts[0] < givenParts[0] )) || \ - (( activeParts[0] == givenParts[0] && activeParts[1] < givenParts[1] )); then + if ((${#activeParts[@]} == 2)); then + if ((activeParts[0] < givenParts[0])) || + ((activeParts[0] == givenParts[0] && activeParts[1] < givenParts[1])); then return 0 else return 1 fi else - if (( activeParts[0] < givenParts[0] )) || \ - (( activeParts[0] == givenParts[0] && activeParts[1] < givenParts[1] )) || \ - (( activeParts[0] == givenParts[0] && activeParts[1] == givenParts[1] && activeParts[2] < givenParts[2] )); then + if ((activeParts[0] < givenParts[0])) || + ((activeParts[0] == givenParts[0] && activeParts[1] < givenParts[1])) || + ((activeParts[0] == givenParts[0] && activeParts[1] == givenParts[1] && activeParts[2] < givenParts[2])); then return 0 else return 1 @@ -115,10 +145,10 @@ requireVar() { } function writeEula() { -if ! echo "# Generated via Docker on $(date) + if ! echo "# Generated via Docker on $(date) eula=${EULA,,} -" > /data/eula.txt; then - log "ERROR: unable to write eula to /data. Please make sure attached directory is writable by uid=${UID}" - exit 2 -fi +" >/data/eula.txt; then + log "ERROR: unable to write eula to /data. Please make sure attached directory is writable by uid=${UID}" + exit 2 + fi }