From e5479e84fec29beb0fca9c91bb68fe1fa188baaa Mon Sep 17 00:00:00 2001 From: Stefan Adelbert Date: Fri, 7 Jun 2024 15:04:48 +1000 Subject: [PATCH] nfpm: Modified zrok-share to cater for private temporary and reserved shares, controlled by ZROK_FRONTEND_MODE; Modified env file with additional variable ZROK_FRONTEND_MODE; --- nfpm/zrok-share.bash | 23 ++++++++++++++++------- nfpm/zrok-share.env | 4 ++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/nfpm/zrok-share.bash b/nfpm/zrok-share.bash index a0480d3d..ec7ed03e 100644 --- a/nfpm/zrok-share.bash +++ b/nfpm/zrok-share.bash @@ -54,9 +54,12 @@ fi exit 1 } -# default mode is reserved (public), override mode is temp-public, i.e., "share public" without a reserved subdomain +# default mode is 'temp-public' (unreserved), override modes are temp-private, reserver-public, reserved-private. +: "${ZROK_FRONTEND_MODE:-temp-public}" if [[ "${ZROK_FRONTEND_MODE:-}" == temp-public ]]; then ZROK_CMD="share public --headless ${ZROK_VERBOSE:-}" +elif [[ "${ZROK_FRONTEND_MODE:-}" == temp-private ]]; then + ZROK_CMD="share private --headless ${ZROK_VERBOSE:-}" elif [[ -s ~/.zrok/reserved.json ]]; then ZROK_RESERVED_TOKEN="$(jq -r '.token' ~/.zrok/reserved.json 2>/dev/null)" if [[ -z "${ZROK_RESERVED_TOKEN}" || "${ZROK_RESERVED_TOKEN}" == null ]]; then @@ -73,8 +76,13 @@ elif [[ -s ~/.zrok/reserved.json ]]; then exit 0 fi fi -else +elif [[ "${ZROK_FRONTEND_MODE:-}" == reserved-public ]]; then ZROK_CMD="reserve public --json-output ${ZROK_VERBOSE:-}" +elif [[ "${ZROK_FRONTEND_MODE:-}" == reserved-private ]]; then + ZROK_CMD="reserve private --json-output ${ZROK_VERBOSE:-}" +else + echo "ERROR: invalid value for ZROK_FRONTEND_MODE '${ZROK_FRONTEND_MODE}'" >&2 + exit 1 fi [[ -n "${ZROK_BACKEND_MODE:-}" ]] || { @@ -88,12 +96,12 @@ case "${ZROK_BACKEND_MODE}" in echo "ERROR: ZROK_TARGET='${ZROK_TARGET}' is not an HTTP URL" >&2 exit 1 else - echo "INFO: validated backend mode ${ZROK_BACKEND_MODE} and target ${ZROK_TARGET}" + echo "INFO: validated backend mode '${ZROK_BACKEND_MODE}' and target '${ZROK_TARGET}'" fi ;; caddy) if ! [[ "${ZROK_TARGET}" =~ ^/ ]]; then - echo "ERROR: ZROK_TARGET='${ZROK_TARGET}' is not an absolute filesystem path." >&2 + echo "ERROR: ZROK_TARGET='${ZROK_TARGET}' is not an absolute filesystem path" >&2 exit 1 elif ! [[ -f "${ZROK_TARGET}" && -r "${ZROK_TARGET}" ]]; then echo "ERROR: ZROK_TARGET='${ZROK_TARGET}' is not a readable regular file" >&2 @@ -104,7 +112,7 @@ case "${ZROK_BACKEND_MODE}" in ;; web|drive) if ! [[ "${ZROK_TARGET}" =~ ^/ ]]; then - echo "ERROR: ZROK_TARGET='${ZROK_TARGET}' is not an absolute filesystem path." >&2 + echo "ERROR: ZROK_TARGET='${ZROK_TARGET}' is not an absolute filesystem path" >&2 exit 1 elif ! [[ -d "${ZROK_TARGET}" && -r "${ZROK_TARGET}" ]]; then echo "ERROR: ZROK_TARGET='${ZROK_TARGET}' is not a readable directory" >&2 @@ -142,8 +150,9 @@ fi echo "INFO: running: zrok ${ZROK_CMD}" -if [[ "${ZROK_FRONTEND_MODE:-}" == temp-public ]]; then - # share until exit +if [[ "${ZROK_FRONTEND_MODE:-}" =~ ^temp- ]]; then + # frontend mode starts with 'temp-', so is temporary. + # share without reserving until exit. exec zrok ${ZROK_CMD} else # reserve and continue diff --git a/nfpm/zrok-share.env b/nfpm/zrok-share.env index 323f5b8c..9bb31514 100644 --- a/nfpm/zrok-share.env +++ b/nfpm/zrok-share.env @@ -86,3 +86,7 @@ ZROK_SHARE_OPTS="" # set if self-hosting zrok and not using only the default frontend name 'public'; must be a space-separated list # WARNING: changes take effect the next time the frontend URL is reserved #ZROK_FRONTENDS="public" + +# you MAY set to change the frontend mode: temp-public (default), temp-private, reserved-public, reserved-private +# WARNING: changes take effect the next time the frontend URL is reserved +#ZROK_FRONTEND_MODE="temp-public"