2020-06-01 09:17:44 +00:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
|
2021-10-25 14:51:27 +00:00
|
|
|
# This script adds a skylink to the sia blocklist and removes the skylink from
|
|
|
|
# nginx cache. The script should be run locally on each skynet webportal
|
|
|
|
# server. The automatic script that is used to continuously sync an Airtable
|
|
|
|
# sheet list with the blocklist on the web portals is
|
|
|
|
# /setup-scripts/blocklist-airtable.py
|
2021-07-26 20:03:49 +00:00
|
|
|
|
2020-06-01 09:17:44 +00:00
|
|
|
set -e # exit on first error
|
|
|
|
|
2021-12-13 16:35:00 +00:00
|
|
|
# Number of skylinks to block within one batch
|
|
|
|
BATCH_SIZE=1000
|
|
|
|
|
2020-06-01 09:17:44 +00:00
|
|
|
if [ -z "$1" ]; then
|
2020-11-24 13:51:20 +00:00
|
|
|
echo "Please provide either a skylink or file with skylinks separated by new lines" && exit 1
|
2020-06-01 09:17:44 +00:00
|
|
|
fi
|
|
|
|
|
2020-11-24 11:39:44 +00:00
|
|
|
#########################################################
|
2021-04-27 09:13:41 +00:00
|
|
|
# read either a file containing skylinks separated by new
|
2020-11-24 11:39:44 +00:00
|
|
|
# lines or a single skylink and put them in an array
|
|
|
|
#########################################################
|
|
|
|
skylinks=()
|
|
|
|
if test -f "$1"; then
|
2020-11-24 14:14:22 +00:00
|
|
|
line_number=1
|
2021-10-25 14:51:27 +00:00
|
|
|
|
|
|
|
# Read file including the last line even when it doesn't end with newline
|
|
|
|
while IFS="" read -r line || [ -n "$line" ];
|
2020-11-24 11:39:44 +00:00
|
|
|
do
|
2021-10-25 14:51:27 +00:00
|
|
|
if [[ $line =~ (^[a-zA-Z0-9_-]{46}$) ]]; then
|
|
|
|
skylinks+=("$line")
|
2020-11-24 11:39:44 +00:00
|
|
|
else
|
2020-11-24 14:14:22 +00:00
|
|
|
echo "Incorrect skylink at line ${line_number}: $line" && exit 1
|
2020-11-24 11:39:44 +00:00
|
|
|
fi
|
2020-11-24 14:14:22 +00:00
|
|
|
let line_number+=1
|
2020-11-24 11:39:44 +00:00
|
|
|
done < $1;
|
|
|
|
else
|
|
|
|
skylinks=("$1") # just single skylink passed as input argument
|
|
|
|
fi
|
|
|
|
|
2021-12-13 16:35:00 +00:00
|
|
|
# Block skylinks in batches
|
|
|
|
skylinks_len=${#skylinks[@]}
|
|
|
|
for (( i = 0; i < $skylinks_len; i++ )); do
|
|
|
|
# Add skylink to batch
|
|
|
|
skylink="${skylinks[$i]}"
|
|
|
|
echo ".. ⌁ Adding skylink ${skylink} to batch..."
|
|
|
|
batch_skylinks+=("$skylink")
|
|
|
|
|
|
|
|
# For performance reasons on each iteration we do not block a single
|
|
|
|
# skylink, but we block skylinks in batches with BATCH_SIZE size mainly
|
|
|
|
# because of nginx cache search.
|
|
|
|
# If (batch len == batch size) or (we have last batch):
|
|
|
|
if (( ${#batch_skylinks[@]} == $BATCH_SIZE || $i == $skylinks_len - 1 )); then
|
|
|
|
echo "--------------------------------------------"
|
|
|
|
|
|
|
|
# Add to Sia blocklist
|
|
|
|
echo "Blocking batch skylinks in skyd..."
|
|
|
|
skylinks_space_separated="$(IFS=' '; echo "${batch_skylinks[*]}")"
|
|
|
|
docker exec sia siac skynet blocklist add $skylinks_space_separated
|
|
|
|
|
|
|
|
# Remove from NGINX cache
|
|
|
|
# NOTE:
|
|
|
|
# If there are changes to how the NGINX cache is being cleared, the same
|
|
|
|
# changes need to be applied to the /setup-scripts/blocklist-airtable.py
|
|
|
|
# script.
|
|
|
|
echo "Removing batch skylinks from Nginx cache..."
|
|
|
|
skylinks_pipe_separated="$(IFS='|'; echo "${batch_skylinks[*]}")"
|
|
|
|
cached_files_command="find /data/nginx/cache/ -type f | xargs -r grep -Els '^Skynet-Skylink: ($skylinks_pipe_separated)'"
|
|
|
|
docker exec -it nginx bash -c "${cached_files_command} | xargs -r rm"
|
|
|
|
|
|
|
|
# Clear batch
|
|
|
|
batch_skylinks=()
|
|
|
|
|
|
|
|
echo "--------------------------------------------"
|
|
|
|
fi
|
2020-06-01 09:17:44 +00:00
|
|
|
done
|
|
|
|
|
2021-12-13 16:35:00 +00:00
|
|
|
# Hot reload Nginx to get rid of deleted open files
|
|
|
|
echo "Hot reloading nginx..."
|
|
|
|
docker exec nginx nginx -s reload
|
|
|
|
|
2020-11-24 11:39:44 +00:00
|
|
|
echo "✓ All done !"
|