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

57 lines
1.8 KiB
Bash
Raw Permalink Normal View History

2023-07-28 17:08:13 +03:00
#!/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"
2023-08-01 10:26:56 +03:00
file_user="@subnetsUserList@"
2023-07-28 17:08:13 +03:00
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
2023-08-01 10:26:56 +03:00
echo "Add stat.ripe.net to $interface"
ip route add 193.0.6.150/32 via $gateway_for_internal_ip dev $interface
2024-04-18 12:54:40 +03:00
2023-07-28 17:08:13 +03:00
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
2023-08-01 10:26:56 +03:00
if [ -e $file_user ]; then
echo "Add user subnets..."
cat $file_user | grep -v "#" >> $file_processed
fi
2023-07-28 17:08:13 +03:00
2023-08-01 10:26:56 +03:00
echo "Flush route table (restart interface $interface)..."
systemctl restart "network-addresses-${interface}.service"
2023-07-28 17:08:13 +03:00
# 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"