lazily check if onion or i2p sites exist in instances.json before running jq.

This commit is contained in:
Sahal Ansari 2024-07-25 20:26:15 -05:00
parent f19da6ca8d
commit dfba0f285b

View File

@ -20,7 +20,7 @@
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>. # this program. If not, see <https://www.gnu.org/licenses/>.
set -o pipefail set -o pipefail
@ -117,7 +117,7 @@ read_csv_row ()
local opt= local opt=
local OPTIND local OPTIND
local OPTARG local OPTARG
local -i i=0 local -i i=0
local -i quote=0 local -i quote=0
local -i esc=0 local -i esc=0
@ -154,7 +154,7 @@ read_csv_row ()
for (( i = 0; i < len; i++ )) for (( i = 0; i < len; i++ ))
do do
char="${row:${i}:1}" char="${row:${i}:1}"
# "Handle" escapes. Really, it just means writing the escape verbatim # "Handle" escapes. Really, it just means writing the escape verbatim
# into the string. Yes, that includes ". Because this is ultimately # into the string. Yes, that includes ". Because this is ultimately
# going into JSON, and making this a fully-featured CSV reader would # going into JSON, and making this a fully-featured CSV reader would
@ -167,7 +167,7 @@ read_csv_row ()
# Escape handled. Move on to next character. # Escape handled. Move on to next character.
continue continue
fi fi
# \ triggers escape. # \ triggers escape.
# shellcheck disable=SC1003 # shellcheck disable=SC1003
if [[ "${char}" == '\' ]] if [[ "${char}" == '\' ]]
@ -247,7 +247,7 @@ canonicalize_url ()
return 1 return 1
fi fi
url="${1}" url="${1}"
# Convert URL to lowercase. # Convert URL to lowercase.
url="${url,,}" url="${url,,}"
@ -256,7 +256,7 @@ canonicalize_url ()
then then
return 2 return 2
fi fi
# Strip leading /, but only if the path is /. # Strip leading /, but only if the path is /.
if [[ "${url#*://*/}" =~ ^/*$ ]] if [[ "${url#*://*/}" =~ ^/*$ ]]
then then
@ -292,7 +292,7 @@ get ()
local opt= local opt=
local OPTIND local OPTIND
local OPTARG local OPTARG
local no_tor=n local no_tor=n
local no_i2p=n local no_i2p=n
local url= local url=
@ -345,7 +345,7 @@ get ()
# - Increase curl max-time to 60 seconds. # - Increase curl max-time to 60 seconds.
if [[ "${zone,,}" == "onion" ]] if [[ "${zone,,}" == "onion" ]]
then then
# Don't bother if tor isn't running. But if both are available, # Don't bother if tor isn't running. But if both are available,
# make sure we warp curl with socks. # make sure we warp curl with socks.
if [[ "${no_tor}" == "y" ]] if [[ "${no_tor}" == "y" ]]
then then
@ -424,11 +424,11 @@ create_instance_entry ()
local url_type="url" local url_type="url"
local -i rc=0 local -i rc=0
local -a get_opts=() local -a get_opts=()
local opt= local opt=
local OPTIND local OPTIND
local OPTARG local OPTARG
while getopts "IT" opt while getopts "IT" opt
do do
case "${opt}" in case "${opt}" in
@ -438,7 +438,7 @@ create_instance_entry ()
esac esac
done done
shift $((OPTIND-1)) shift $((OPTIND-1))
local url="${1}" local url="${1}"
local country="${2}" local country="${2}"
local description="${4}" local description="${4}"
@ -716,7 +716,7 @@ main ()
echo >&2 "For more information, run: ${BASH_SOURCE[0]} -h" echo >&2 "For more information, run: ${BASH_SOURCE[0]} -h"
return 1 return 1
fi fi
# Set do_tor <- n so that we don't attempt to make tor connections. # Set do_tor <- n so that we don't attempt to make tor connections.
do_tor=n do_tor=n
@ -728,14 +728,18 @@ main ()
# (a mapfile would not ideal here since a pipe is required, inducing a # (a mapfile would not ideal here since a pipe is required, inducing a
# subshell, meaning nothing will actually get added to # subshell, meaning nothing will actually get added to
# imported_nonwww) # imported_nonwww)
IFS=$'\n' imported_nonwww=($(jq -Mcer '.instances[] | select(.onion or .i2p)' "${import_nonwww_from_file}")) if grep -q ".onion" "${import_nonwww_from_file}" || grep -q ".i2p" "${import_nonwww_from_file}" ; then
rc=$? IFS=$'\n' imported_nonwww=($(jq -Mcer '.instances[] | select(.onion or .i2p)' "${import_nonwww_from_file}"))
rc=$?
if [[ ${rc} -ne 0 ]] else
then rc=0
echo >&2 "Failed to read onion instances from existing JSON file."
return 1
fi fi
if [[ ${rc} -ne 0 ]]
then
echo >&2 "Failed to read onion instances from existing JSON file."
return 1
fi
fi fi
# Check to see if we have tor. If we don't, then we will have to import # Check to see if we have tor. If we don't, then we will have to import
@ -776,7 +780,7 @@ main ()
return 1 return 1
fi fi
fi fi
# Read in the CSV. # Read in the CSV.
if [[ "${input_file}" == "/dev/stdin" ]] if [[ "${input_file}" == "/dev/stdin" ]]
then then
@ -808,11 +812,11 @@ main ()
echo >&2 "Script will now terminate." echo >&2 "Script will now terminate."
return 2 return 2
fi fi
# Print friendly message to log while processing row. # Print friendly message to log while processing row.
url="${values[0]}" url="${values[0]}"
echo -n >&2 "${url}: " echo -n >&2 "${url}: "
instance_entry="$(IFS=$'\n' create_instance_entry "${get_opts[@]}" "${values[@]}")" instance_entry="$(IFS=$'\n' create_instance_entry "${get_opts[@]}" "${values[@]}")"
rc=$? rc=$?
@ -827,7 +831,7 @@ main ()
echo "SKIPPED" echo "SKIPPED"
else else
echo "FAILED" echo "FAILED"
if [[ "${failfast}" == "y" ]] if [[ "${failfast}" == "y" ]]
then then
return 1 return 1