Freenetis synchronization.sh

Z Freenetis Wiki
Verze z 2. 8. 2012, 09:48, kterou vytvořil Jeffraska (diskuse | příspěvky)
(rozdíl) ← Starší verze | zobrazit aktuální verzi (rozdíl) | Novější verze → (rozdíl)
Přejít na: navigace, hledání
#!/bin/bash
##################################################################################
#                                                                                #
# This script serves for redirection ip policy of IS FreenetIS			 #
#                                                                                #
# auhtor Sevcik Roman 2011                                                       #
# email sevcik.roman@slfree.net                                                  #
#										 #
# name frnts_synchronization.sh							 #
# version 1.9									 #
#                                                                                #
##################################################################################

#Load variables from config file
CONFIG=/etc/freenetis.cfg

#Paths where temporary data will be saved.
PATH_RANGES=/tmp/ranges
PATH_WHITELIST=/tmp/whitelist
PATH_ALLOWED=/tmp/allowed
PATH_SELF_CANCEL=/tmp/self_cancel


#Load variables
if [ -f ${CONFIG} ]; then
  . $CONFIG;
else
  echo "No config file - giving up :-(";
  exit 0
fi

# Function returns 1 if is ip valid
# @param ip adresa
# return 1 if is ip valid
valid_ip ()
{
    local  ip=$1
    local  stat=1

    if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
      OIFS=$IFS
      IFS='.'
      ip=($ip)
      IFS=$OIFS
      [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
      stat=$?
    fi;
    return $stat
}

update ()
{
    echo "Updating..."

    #Erase content of all sets
    echo "Cleaning sets...";
    ipset -F whitelist
    ipset -F allowed
    ipset -F self_cancel
    ipset -F ranges

    #Send data from seen set to server

    oount=0
    for i in $(ipset -L seen);
    do
      if valid_ip $i; then
        seen[count]=$i
        ((count++))
      fi
    done

    OIFS=$IFS
    export IFS=";"

    echo "Sending seen set data...";
    wget -q -O /tmp/out.txt             $SET_URL_SEEN --no-check-certificate --post-data "seen=${seen[*]}"

    IFS=$OIFS

    unset seen

    echo "Downloading data...";
    wget -q -O $PATH_WHITELIST          $SET_URL_WHITELIST --no-check-certificate
    wget -q -O $PATH_ALLOWED            $SET_URL_ALLOWED --no-check-certificate
    wget -q -O $PATH_SELF_CANCEL        $SET_URL_SELF_CANCEL --no-check-certificate
    wget -q -O $PATH_RANGES             $SET_URL_RANGES --no-check-certificate

    echo "Downloading data...";
    wget -q -O $PATH_WHITELIST          $SET_URL_WHITELIST --no-check-certificate
    wget -q -O $PATH_ALLOWED            $SET_URL_ALLOWED --no-check-certificate
    wget -q -O $PATH_SELF_CANCEL        $SET_URL_SELF_CANCEL --no-check-certificate
    wget -q -O $PATH_RANGES             $SET_URL_RANGES --no-check-certificate


    #Filling sets

   for i in $(cat $PATH_WHITELIST);
    do
        echo "$i - added to set whitelist"
        ipset -A whitelist $i
    done

    for i in $(cat $PATH_ALLOWED);
    do
        echo "$i - added to set allowed"
        ipset -A allowed $i
    done

    for i in $(cat $PATH_SELF_CANCEL);
    do
        echo "$i - added to set self_cancel"
        ipset -A self_cancel $i
    done

    for i in $(cat $PATH_RANGES);
    do
        echo "$i - added to set ranges"
        ipset -A ranges $i
    done

    #Erase content of seen set
    echo "Cleaning seen set...";
    ipset -F seen

    #Cleaning up...
    rm $PATH_RANGES
    rm $PATH_WHITELIST
    rm $PATH_ALLOWED
    rm $PATH_SELF_CANCEL

    echo "Sleeping..."
    sleep $DELAY;
}

while (true);
do
    update
done