system/hosts/tatos/services/wireguard/update_ru_routes.sh

56 lines
1.8 KiB
Bash

#!/bin/bash
function ProgressBar {
let _progress=(${1}*100/${2}*100)/100
let _done=(${_progress}*4)/10
let _left=40-$_done
_fill=$(printf "%${_done}s")
_empty=$(printf "%${_left}s")
printf "\rAdd routes to route table (${1}/${2}): [${_fill// /#}${_empty// /-}] ${_progress}%%"
}
# Variables
file_raw="russian_subnets_list_raw.txt"
file_user="@subnetsUserList@"
file_for_calc="russian_subnets_list_raw_for_calc.txt"
file_processed="russian_subnets_list_processed.txt"
gateway_for_internal_ip=`ip route | awk '/default/ {print $3; exit}'`
interface=`ip route | awk '/default/ {print $5; exit}'`
# Get addresses RU segment
echo "Add stat.ripe.net to $interface"
ip route add 193.0.6.150/32 via $gateway_for_internal_ip dev $interface
echo "Download RU subnets..."
curl --progress-bar "https://stat.ripe.net/data/country-resource-list/data.json?resource=ru" | jq -r ".data.resources.ipv4[]" > $file_raw
echo "Deaggregate subnets..."
cat $file_raw |grep "-" > $file_for_calc
cat $file_raw |grep -v "-" > $file_processed
for line in $(cat $file_for_calc); do
ipcalc --no-decorate -d $line >> $file_processed;
done
if [ -e $file_user ]; then
echo "Add user subnets..."
cat $file_user | grep -v "#" >> $file_processed
fi
echo "Flush route table (restart interface $interface)..."
systemctl restart "network-addresses-${interface}.service"
# Add route
routes_count_in_file=`wc -l $file_processed`
routes_count_current=0
for line in $(cat $file_processed); do
ip route add $line via $gateway_for_internal_ip dev $interface
let "routes_count_current+=1"
ProgressBar ${routes_count_current} ${routes_count_in_file}
done
echo ""
echo "Remove temp files..."
rm $file_raw $file_processed $file_json $file_for_calc
routes_count=`ip r | wc -l`
echo "Routes in routing table: $routes_count"