From b595b3b5f495223ec04d8fa159c5fcb3ded8b44d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 28 Mar 2024 15:41:45 +0100 Subject: [PATCH 1/2] Port most recent changes of PRs #5615, #5618 amd #5620 to v6 Signed-off-by: DL6ER --- gravity.sh | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/gravity.sh b/gravity.sh index d49af29d..bc0ff23d 100755 --- a/gravity.sh +++ b/gravity.sh @@ -488,6 +488,7 @@ compareLists() { gravity_DownloadBlocklistFromUrl() { local url="${1}" adlistID="${2}" saveLocation="${3}" target="${4}" compression="${5}" gravity_type="${6}" domain="${7}" local heisenbergCompensator="" listCurlBuffer str httpCode success="" ip cmd_ext + local file_path permissions ip_addr port blocked=false download=true # Create temp file to store content on disk instead of RAM # We don't use '--suffix' here because not all implementations of mktemp support it, e.g. on Alpine @@ -591,8 +592,40 @@ gravity_DownloadBlocklistFromUrl() { fi fi - # shellcheck disable=SC2086 - httpCode=$(curl --connect-timeout ${curl_connect_timeout} -s -L ${compression} ${cmd_ext} ${heisenbergCompensator} -w "%{http_code}" "${url}" -o "${listCurlBuffer}" 2>/dev/null) + # If we are going to "download" a local file, we first check if the target + # file has a+r permission. We explicitly check for all+read because we want + # to make sure that the file is readable by everyone and not just the user + # running the script. + if [[ $url == "file://"* ]]; then + # Get the file path + file_path=$(echo "$url" | cut -d'/' -f3-) + # Check if the file exists and is a regular file (or a symlink to one) + if [[ ! -e $file_path ]]; then + # Output that the file does not exist + echo -e "${OVER} ${CROSS} ${file_path} does not exist" + download=false + elif [[ ! -f $file_path ]]; then + # Output that the file is not a regular file + echo -e "${OVER} ${CROSS} ${file_path} is not a regular file" + download=false + else + # Check if the file has a+r permissions + permissions=$(stat -Lc "%a" "$file_path") + if [[ $permissions == *4 || $permissions == *5 || $permissions == *6 || $permissions == *7 ]]; then + # Output that we are using the local file + echo -e "${OVER} ${INFO} Using local file ${file_path}" + else + # Output that the file does not have the correct permissions + echo -e "${OVER} ${CROSS} Cannot read file (file needs to have o+r permission)" + download=false + fi + fi + fi + + if [[ "${download}" == true ]]; then + # shellcheck disable=SC2086 + httpCode=$(curl --connect-timeout ${curl_connect_timeout} -s -L ${compression} ${cmd_ext} ${heisenbergCompensator} -w "%{http_code}" "${url}" -o "${listCurlBuffer}" 2>/dev/null) + fi case $url in # Did we "download" a local file? @@ -601,7 +634,7 @@ gravity_DownloadBlocklistFromUrl() { echo -e "${OVER} ${TICK} ${str} Retrieval successful" success=true else - echo -e "${OVER} ${CROSS} ${str} Not found / empty list" + echo -e "${OVER} ${CROSS} ${str} Retrieval failed / empty list" fi ;; # Did we "download" a remote file? From 91e0d668e0a552ca08e14de67937bd0a0986e9d7 Mon Sep 17 00:00:00 2001 From: Dominik Date: Sat, 30 Mar 2024 20:03:15 +0100 Subject: [PATCH 2/2] Code style: Use explicit form of CLI arguments Co-authored-by: Dan Schaper Signed-off-by: Dominik --- gravity.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gravity.sh b/gravity.sh index bc0ff23d..e37c8104 100755 --- a/gravity.sh +++ b/gravity.sh @@ -598,7 +598,7 @@ gravity_DownloadBlocklistFromUrl() { # running the script. if [[ $url == "file://"* ]]; then # Get the file path - file_path=$(echo "$url" | cut -d'/' -f3-) + file_path=$(echo "$url" | cut --delimiter='/' --fields=3-) # Check if the file exists and is a regular file (or a symlink to one) if [[ ! -e $file_path ]]; then # Output that the file does not exist @@ -610,7 +610,7 @@ gravity_DownloadBlocklistFromUrl() { download=false else # Check if the file has a+r permissions - permissions=$(stat -Lc "%a" "$file_path") + permissions=$(stat --dereference --format="%a" "$file_path") if [[ $permissions == *4 || $permissions == *5 || $permissions == *6 || $permissions == *7 ]]; then # Output that we are using the local file echo -e "${OVER} ${INFO} Using local file ${file_path}" @@ -624,7 +624,7 @@ gravity_DownloadBlocklistFromUrl() { if [[ "${download}" == true ]]; then # shellcheck disable=SC2086 - httpCode=$(curl --connect-timeout ${curl_connect_timeout} -s -L ${compression} ${cmd_ext} ${heisenbergCompensator} -w "%{http_code}" "${url}" -o "${listCurlBuffer}" 2>/dev/null) + httpCode=$(curl --connect-timeout ${curl_connect_timeout} --silent --location ${compression} ${cmd_ext} ${heisenbergCompensator} --write-out "%{http_code}" "${url}" --output "${listCurlBuffer}" 2>/dev/null) fi case $url in