Advertisement
rs232

p2partisan 4.15

Oct 12th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 26.91 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # p2partisan v4.15 (11/10/2014)
  4. #
  5. # <CONFIGURATION> ###########################################
  6. # Adjust location where the files are kept
  7. P2Partisandir=/cifs1/p2partisan
  8. #
  9. # Edit the file "blacklists" to customise if needed
  10. # Edit the "whitelist" to overwrite the blacklist if needed
  11. #
  12. # Enable logging? Use only for troubleshooting. 0=off 1=on
  13. syslogs=1
  14. # Maximum number of logs to be recorded in a given 60 min
  15. # Consider set this very low (like 3 or 6) once your are
  16. # happy with the installation. To troubleshoot blocked
  17. # connection close all the secondary traffic e.g. p2p
  18. # and try a connection to the blocked site/port you should
  19. # find a reference in the logs.
  20. maxloghour=1
  21. #
  22. # What do you want to block?
  23. # 1) Input (Router only, running transmission?)
  24. # 2) LAN (LAN clients only)
  25. # 3) Both *default
  26. protection=3
  27. #
  28. # ports to be whitelisted. Whitelisted ports will never be
  29. # blocked no matter what the source/destination IP is.
  30. # This is very important if you're running a service like
  31. # e.g. SMTP/HTTP/IMAP/else. Separate value in the list below
  32. # with commas - NOTE: Leave 80 and 443 untouched, add custom ports only
  33. # you might want to add remote admin and VPN ports here if any.
  34. # Standard iptables syntax, individual ports divided by "," and ":" to
  35. # define a range e.g. 80,443,2100:2130. Do not whitelist you P2P client!
  36. whiteports=53,80,123,443,1194:1197,1723
  37. #
  38. # Fastrouting will process the IP classes very quickly but use
  39. # Lot of resources. If you disable the effect is transparent
  40. # but the full process will take minutes rather than seconds
  41. # 0=disabled 1=enabled
  42. fastroutine=1
  43. #
  44. # Enable check on script availability to help autorun
  45. # E.g. wait for the file to be available in cifs before run it
  46. # instead of quit with a file missing error
  47. autorun_availability_check=1
  48. #
  49. # Schedule updates? (once a week is plenty). Custom syntax:
  50. # m = random minute picked up in the range[0-59]
  51. # h = random hour picked up in the range [1-5]am
  52. # d = random day of the week picked up in the range Sun to Sat [0-6]
  53. # if unwanted set your own specific time e.g.
  54. # "30 4 * * 1" 4:30 on a Monday
  55. # or use a combination e.g. random minute at 1am on a Tuesday:
  56. # "m 1 * * 3"
  57. # Specify this always in between "" please
  58. schedule="m h * * d"
  59. #
  60. # IP for testing Internet connectivity
  61. testip=8.8.8.8
  62. # </CONFIGURATION> ###########################################
  63.  
  64. # Wait until Internet is available
  65.     while :
  66.     do
  67.         ping -c 3 $testip >/dev/null 2>&1
  68.         if [ $? = 0 ]; then
  69.             break
  70.         fi
  71.         sleep 5
  72.     done
  73.  
  74. pidfile=/var/run/p2partisan.pid
  75. cd $P2Partisandir
  76. version=`head -3 ./p2partisan.sh | tail -1 | cut -f 3- -d " "`
  77.  
  78. alias ipset='/bin/nice -n19 /usr/sbin/ipset'
  79. alias sed='/bin/nice -n19 /bin/sed'
  80. alias iptables='/usr/sbin/iptables'
  81. alias service='/sbin/service'
  82. alias plog='logger -t P2PARTISAN -s'
  83. now=`date +%s`
  84. wanif=`nvram get wan_ifname`
  85. lanif=`nvram get lan_ifname`
  86.  
  87.  
  88. psoftstop() {
  89.     ./iptables-del 2> /dev/null
  90.     plog "Stopping P2Partisan"
  91.     [ -f $pidfile ] && rm -f "$pidfile" 2> /dev/null
  92.     [ -f iptables-add ] && rm -f "iptables-add" 2> /dev/null
  93.     [ -f iptables-del ] && rm -f "iptables-del" 2> /dev/null
  94. }
  95.  
  96. pblock() {
  97.     plog "P2PArtisan: Applying paranoia block"
  98.     iptables -N PARANOIA-DROP 2> /dev/null
  99.    
  100.     whiteports_number=`echo $whiteports | tr -d '\n' | sed 's/,/,\n/g' | sed 's/:/:\n/g' | wc -l`
  101.         a=1
  102.         b=8
  103.         rounds=`echo $(( $whiteports_number / $b ))`
  104.         if [ $rounds -eq 0 ]; then rounds="1"; fi
  105.     while [ $rounds -gt 0 ]
  106.     do
  107.         w=`echo $whiteports | cut -d"," -f $a-$b`
  108.         a=`echo $(( $a + $b ))`
  109.         b=`echo $(( $b + $b ))`
  110. whitep="${whitep}iptables -A PARANOIA-DROP -p tcp --match multiport --sports $w -j ACCEPT 2> /dev/null
  111. iptables -A PARANOIA-DROP -p udp --match multiport --sports $w -j ACCEPT 2> /dev/null
  112. iptables -A PARANOIA-DROP -p tcp --match multiport --dports $w -j ACCEPT 2> /dev/null
  113. iptables -A PARANOIA-DROP -p udp --match multiport --dports $w -j ACCEPT 2> /dev/null
  114. "
  115.     rounds=`echo $(( $rounds - 1 ))`
  116.     done
  117.  
  118.     iptables -A PARANOIA-DROP -m limit --limit $maxloghour/hour --limit-burst 5 -j LOG --log-prefix "P2Partisan Rejected (paranoia): " --log-level 1 2> /dev/null
  119.     iptables -A PARANOIA-DROP -j DROP
  120.     iptables -I wanin 1 -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  121.     iptables -I wanout 1 -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  122.     iptables -I INPUT 1 -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  123.     iptables -I OUTPUT 1 -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  124. }
  125.  
  126. punblock() {
  127.     while iptables -L wanin 2> /dev/null | grep "PARANOIA-DROP"
  128.     do
  129.         iptables -D wanin -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  130.     done
  131.     while iptables -L wanout 2> /dev/null | grep "PARANOIA-DROP"
  132.     do
  133.         iptables -D wanout -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  134.     done
  135.     while iptables -L OUTPUT 2> /dev/null | grep "PARANOIA-DROP"
  136.     do
  137.         iptables -D OUTPUT -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  138.     done
  139.     while iptables -L INPUT 2> /dev/null | grep "PARANOIA-DROP"
  140.     do
  141.         iptables -D INPUT -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  142.     done
  143.     iptables -F PARANOIA-DROP 2> /dev/null && plog "P2PArtisan: Removing paranoia block"
  144.     iptables -X PARANOIA-DROP 2> /dev/null
  145. }
  146.  
  147. pforcestop() {
  148.     while iptables -L wanin 2> /dev/null | grep P2PARTISAN-IN
  149.     do
  150.         iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  151.     done
  152.     while iptables -L wanout 2> /dev/null | grep P2PARTISAN-OUT
  153.     do
  154.         iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  155.     done
  156.     while iptables -L INPUT | grep P2PARTISAN-IN
  157.     do
  158.         iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  159.     done
  160.     while iptables -L INPUT | grep P2PARTISAN-OUT
  161.     do
  162.         iptables -D INPUT -i $lanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  163.     done
  164.     while iptables -L OUTPUT | grep P2PARTISAN-OUT
  165.     do
  166.         iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  167.     done
  168.     iptables -F P2PARTISAN-DROP-IN 2> /dev/null
  169.     iptables -F P2PARTISAN-DROP-OUT 2> /dev/null
  170.     iptables -F P2PARTISAN-IN 2> /dev/null
  171.     iptables -F P2PARTISAN-OUT 2> /dev/null
  172.     iptables -X P2PARTISAN-DROP-IN 2> /dev/null
  173.     iptables -X P2PARTISAN-DROP-OUT 2> /dev/null   
  174.     iptables -X P2PARTISAN-IN 2> /dev/null
  175.     iptables -X P2PARTISAN-OUT 2> /dev/null
  176.     ipset -F
  177.     for i in `ipset --list | grep Name | cut -f2 -d ":" `; do
  178.         ipset -X $i
  179.     done
  180.     chmod 777 ./*.gz
  181.     [ -f iptables-add ] && rm iptables-add
  182.     [ -f iptables-del ] && rm iptables-del
  183.     [ -f ipset-del ] && rm ipset-del
  184.     [ -f $pidfile ] && rm -f "$pidfile" 2> /dev/null
  185.     [ -f runtime ] && rm -f "runtime" 2> /dev/null
  186. plog "Unloading ipset modules"
  187.     lsmod | grep "ipt_set" > /dev/null 2>&1 && sleep 2 ; rmmod -f ipt_set 2> /dev/null
  188.     lsmod | grep "ip_set_iptreemap" > /dev/null 2>&1 && sleep 2 ; rmmod -f ip_set_iptreemap 2> /dev/null
  189.     lsmod | grep "ip_set" > /dev/null 2>&1 && sleep 2 ; rmmod -f ip_set 2> /dev/null
  190. plog "Stopping P2Partisan"
  191. }
  192.  
  193. pstatus() {
  194.         running3=`iptables -L INPUT | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  195.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  196.         running5=`nvram get script_fire | grep p2partisan >/dev/null && echo Yes || echo No`
  197.         running6=`cru l | grep P2Partisan-update >/dev/null && echo Yes || echo No`
  198.         running7=`tail -200 /var/log/messages | grep Dropped | tail -1`
  199.         running7a=`tail -200 /var/log/messages | grep Rejected | tail -1`
  200.         running9=`nvram get script_fire | grep "p2partisan.sh tutor" >/dev/null && echo Yes || echo No`
  201.         runningA=`cat /var/log/messages | grep "Applying paranoia" | wc -l`
  202.         runningB=`cat /var/log/messages | grep "Stuck on Loading" | wc -l`
  203.         runningC=`cat blacklists | grep -v "^#" | grep -v "^$" | wc -l`
  204.         runningD=`cat ./runtime`
  205.         from=`head -1 ./iptables-add 2> /dev/null | cut -c3-`
  206.         runtime=`echo $(( $now - $from ))`
  207.         d=`echo $(( $runtime / 86400 ))`
  208.         h=`echo $((( $runtime / 3600 ) %24 ))`
  209.         m=`echo $((( $runtime / 60 ) %60 ))`
  210.         s=`echo $(( $runtime %60 ))`
  211.         runtime=`printf "$d - %02d:%02d:%02d\n" $h $m $s`
  212.         drop_packet_count_in=`iptables -vL P2PARTISAN-DROP-IN 2> /dev/null| grep " DROP " | awk '{print $1}'`
  213.         drop_packet_count_out=`iptables -vL P2PARTISAN-DROP-OUT 2> /dev/null| grep " REJECT " | awk '{print $1}'`
  214.        
  215.        
  216.         if [[ $running3 -eq "0" ]] && [[ $running4 -eq "0" ]]; then
  217.                 running8=No
  218.         elif [[ $running3 -eq "0" ]] && [[ $running4 -eq "1" ]]; then
  219.                 running8=Loading...
  220.         elif [[ $running3 -gt "0" ]] && [[ $running4 -eq "0" ]]; then
  221.                 running8=Not quite... try to run \"p2partisan.sh update\"
  222.         else
  223.                 running8=Yes
  224.         fi
  225.  
  226.        
  227.         echo "################### P2Partisan ##########################
  228. #       Release version: $version
  229. ################# P2Partisan status #####################
  230. # Running:      $running8
  231. # Autorun:      $running5
  232. # Scheduled:    $running6 / $runningA since device boot
  233. # Tutor:        $running9 / $runningB since device boot
  234. #########################################################
  235. # Uptime:       $runtime
  236. # Dropped in:   $drop_packet_count_in
  237. # Rejected out: $drop_packet_count_out
  238. #########################################################
  239. # Blacklists:   $runningC
  240. # Startup time: $runningD seconds"
  241.         whiteports_number=`echo $whiteports | tr -d '\n' | sed 's/,/,\n/g' | sed 's/:/:\n/g' | wc -l`
  242.                 a=1
  243.                 b=8
  244.                 rounds=`echo $(( $whiteports_number / $b ))`
  245.                 if [ $rounds -eq 0 ]; then rounds="1"; fi
  246.         while [ $rounds -gt 0 ]
  247.         do
  248.                 w=`echo $whiteports | cut -d"," -f $a-$b`
  249.                 a=`echo $(( $a + $b ))`
  250.                 b=`echo $(( $b + $b ))`
  251.         echo "# White ports:  $w"
  252.                 rounds=`echo $(( $rounds - 1 ))`
  253.         done
  254. echo "################# Last log recorded #####################
  255. # Remember your max logs per hour is set to: $maxloghour
  256. $running7
  257. $running7a
  258. #########################################################"
  259. }
  260.  
  261.  
  262. if [ $autorun_availability_check = 1 ]; then
  263. av="while true; do [ -f $P2Partisandir/p2partisan.sh ] && break || sleep 5; done ;"
  264. fi
  265.  
  266. pautorunset() {
  267.     p=`nvram get script_fire | grep "p2partisan.sh ]" | grep -v cru | wc -l`
  268.     if [ $p -eq "0" ] ; then
  269.         t=`nvram get script_fire`; t=`printf "$t\n$av$P2Partisandir/p2partisan.sh\n"` ; nvram set "script_fire=$t"
  270.     fi
  271.     plog "P2Partisan AUTO RUN is ON"
  272.     nvram commit
  273. }
  274.  
  275. pautorununset() {
  276.     p=`nvram get script_fire | grep "p2partisan.sh ]" | grep -v cru | wc -l`
  277.     if [ $p -eq "1" ]; then
  278.     t=`nvram get script_fire`; t=`printf "$t" | grep -v "p2partisan.sh ]"` ; nvram set "script_fire=$t"
  279.     fi
  280.     plog "P2Partisan AUTO RUN is OFF"
  281.     nvram commit
  282. }
  283.  
  284. pscheduleset() {
  285.     cru d P2Partisan-update
  286.     e=`tr -cd 0-5 </dev/urandom | head -c 1`
  287.     f=`tr -cd 0-9 </dev/urandom | head -c 1`
  288.     a=`echo $e$f`
  289.     b=`tr -cd 1-5 </dev/urandom | head -c 1`
  290.     c=`tr -cd 0-6 </dev/urandom | head -c 1`
  291.     scheduleme=`echo "$schedule" | tr "m" "$a"`
  292.     scheduleme=`echo "$scheduleme" | tr "h" "$b"`
  293.     scheduleme=`echo "$scheduleme" | tr "d" "$c"`
  294.     cru a P2Partisan-update "$scheduleme $P2Partisandir/p2partisan.sh paranoia-update"
  295.     pp=`nvram get script_fire | grep "p2partisan.sh paranoia-update" | grep -v cru | wc -l`
  296.     p=`nvram get script_fire | grep "cru a P2Partisan-update" | wc -l`
  297.     if [ $p -eq "0" ] ; then
  298.         if [ $pp -eq "0" ]; then
  299.         t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-update \"$scheduleme $P2Partisandir/p2partisan.sh paranoia-update\"\n"` ; nvram set "script_fire=$t"
  300.         else
  301.         pautorununset
  302.         t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-update \"$scheduleme $P2Partisandir/p2partisan.sh paranoia-update\"\n"` ; nvram set "script_fire=$t"
  303.         pautorunset
  304.         fi
  305.     fi
  306.     plog "P2Partisan AUTO UPDATE is ON"
  307.     nvram commit
  308. }
  309.  
  310. pscheduleunset() {
  311.     cru d P2Partisan-update
  312.     p=`nvram get script_fire | grep "cru a P2Partisan-update" | wc -l`
  313.     if [ $p -eq "1" ] ; then
  314.     t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-update \"$schedule $P2Partisandir/p2partisan.sh paranoia-update\"\n" | grep -v "cru a P2Partisan-update"` ; nvram set "script_fire=$t"
  315.     fi
  316.     plog "P2Partisan AUTO UPDATE is OFF"
  317.     nvram commit
  318. }
  319.  
  320. pupgrade() {
  321.     wget -q -O - http://pastebin.com/raw.php?i=jqHD3hfT | grep "p2partisan v" | grep -v grep> ./latest
  322.     latest=`cat ./latest | cut -c3-31`
  323.     current=`cat ./p2partisan.sh | grep "p2partisan v" | head -1 | cut -c3-32 `
  324.     if [ "$latest" == "$current" ]; then
  325.     echo "
  326. You're already running the latest version of P2Partisan
  327. "
  328.     else
  329.     echo "
  330. There's a new P2Partisan update available. Do you want to upgrade?
  331.    
  332.             current = $current
  333.    
  334.                     to
  335.            
  336.              latest = $latest
  337.  
  338. y/n"
  339.     read answer
  340.     # echo "You entered: $input_variable"
  341.         if [ $answer == "y" ]; then
  342.         echo "Upgrading, please wait:"
  343.         echo "1/6) Downloading the script"
  344.         wget -q -O ./p2partisan_new.sh http://pastebin.com/raw.php?i=jqHD3hfT
  345.         echo "2/6) Migrating the configuration"
  346.         sed '1,/P2Partisandir/{s@P2Partisandir=.*@'"P2Partisandir=$P2Partisandir"'@'} -i ./p2partisan_new.sh
  347.         sed '1,/syslogs/{s@syslogs=.*@'"syslogs=$syslogs"'@'} -i ./p2partisan_new.sh
  348.         sed '1,/maxloghour/{s@maxloghour=.*@'"maxloghour=$maxloghour"'@'} -i ./p2partisan_new.sh
  349.         sed '1,/protection/{s@protection=.*@'"protection=$protection"'@'} -i ./p2partisan_new.sh
  350.         sed '1,/whiteports/{s@whiteports=.*@'"whiteports=$whiteports"'@'} -i ./p2partisan_new.sh
  351.         sed '1,/fastroutine/{s@fastroutine=.*@'"fastroutine=$fastroutine"'@'} -i ./p2partisan_new.sh
  352.         sed '1,/autorun_availability_check/{s@autorun_availability_check=.*@'"autorun_availability_check=$autorun_availability_check"'@'} -i ./p2partisan_new.sh
  353.         sed '1,/schedule/{s@schedule=.*@'"schedule=\"$schedule\""'@'} -i ./p2partisan_new.sh
  354.         sed '1,/testip/{s@testip=.*@'"testip=$testip"'@'} -i ./p2partisan_new.sh       
  355.         tr -d "\r"< ./p2partisan_new.sh > ./.temp ; mv ./.temp ./p2partisan_new.sh
  356.         echo "3/6) Copying p2partisan.sh into p2partisan.sh.old"
  357.         cp ./p2partisan.sh ./p2partisan_old
  358.         echo "4/6) Installing new script into p2partisan.sh"
  359.         mv ./p2partisan_new.sh ./p2partisan.sh
  360.         echo "5/6) Setting up permissions"
  361.         chmod -R 777 ./p2partisan.sh
  362.         echo "6/6) all done, I'm now running the script for you.
  363. NOTE: autorun, autoupdate and tutor settings are left as they were found
  364. "
  365.         pforcestop
  366.         else
  367.         echo "Upgrade skipped. Quitting..."
  368.         exit
  369.         fi
  370.    
  371.     fi
  372.  }
  373.  
  374. ptutor() {
  375.     running3=`iptables -L INPUT | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  376.     running4=`[ -f $pidfile ] && echo 1 || echo 0`
  377.     if [[ $running3 -ne "1" ]] && [[ $running4 -eq "1" ]]; then
  378.             plog "P2Partisan appears to be loading, I'll wait 5 minutes..."    
  379.             sleep 300
  380.         if [[ $running3 -eq "0" ]] && [[ $running4 -eq "1" ]]; then
  381.             psoftstop
  382.             plog "P2Partisan tutor had to restart due to Stuck on Loading"
  383.             pstart
  384.         fi
  385.     else
  386.     echo "P2Partisan up and running. The tutor is happy"
  387.     fi
  388.  }
  389.  
  390. ptutorset() {
  391.     cru d P2Partisan-tutor
  392.     ab=`tr -cd 0-5 </dev/urandom | head -c 1`
  393.     a=`tr -cd 0-9 </dev/urandom | head -c 1`
  394.     a=`echo $ab$a`
  395.     scheduleme=`echo "$a * * * *"`
  396.     cru a P2Partisan-tutor "$scheduleme $P2Partisandir/p2partisan.sh tutor"
  397.     pp=`nvram get script_fire | grep "p2partisan.sh tutor" | grep -v cru | wc -l`
  398.     p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  399.     if [ $p -eq "0" ] ; then
  400.         if [ $pp -eq "0" ]; then
  401.         t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-tutor \"$scheduleme $P2Partisandir/p2partisan.sh tutor\"\n"` ; nvram set "script_fire=$t"
  402.         else
  403.         t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-tutor \"$scheduleme $P2Partisandir/p2partisan.sh tutor\"\n"` ; nvram set "script_fire=$t"
  404.         fi
  405.     fi
  406.     plog "P2Partisan tutor is ON"
  407.     nvram commit
  408. }
  409.  
  410. ptutorunset() {
  411.     cru d P2Partisan-tutor
  412.     p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  413.     if [ $p -eq "1" ] ; then
  414.     t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-tutor \"$schedule $P2Partisandir/p2partisan.sh tutor\"\n" | grep -v "cru a P2Partisan-tutor"` ; nvram set "script_fire=$t"
  415.     fi
  416.     plog "P2Partisan tutor is OFF"
  417.     nvram commit
  418.  }
  419.  
  420. pstart() {
  421.     running4=`[ -f $pidfile ] && echo 1 || echo 0`
  422.     if [ $running4 -eq "0" ] ; then
  423.  
  424.     /bin/ntpsync > /dev/null 2>&1
  425.     pre=`date +%s`
  426.     sleep 1
  427.  
  428.     echo $$ > $pidfile
  429.    
  430.     [ -f iptables-add ] && rm iptables-add
  431.     [ -f iptables-del ] && rm iptables-del
  432.     [ -f ipset-del ] && rm ipset-del
  433.      
  434.     echo "### PREPARATION ###"
  435.     echo "Loading the ipset modules"
  436.     lsmod | cut -c1-20 | grep "ip_set " > /dev/null 2>&1 || insmod ip_set
  437.     lsmod | cut -c1-20 | grep "ip_set_iptreemap" > /dev/null 2>&1 || insmod ip_set_iptreemap
  438.     lsmod | cut -c1-20 | grep "ipt_set" > /dev/null 2>&1 || insmod ipt_set
  439.  
  440. counter=0
  441. pos=1
  442. couscous=`cat blacklist-custom | grep -v "^#" | grep -v "^$" | wc -l`
  443.  
  444.         echo "### CUSTOM BLACKLIST ###
  445. blacklist-custom file -> $couscous entries found"
  446.  if [ $couscous -eq "0" ]; then
  447.         echo "No custom blacklist entries found: skipping"
  448.  else
  449.         echo "loading blacklist #$counter --> ***Custom IP blacklist***"
  450.         ipset --create blacklist-custom iptreemap
  451.         if [ -e blacklist-custom ]; then
  452.         for IP in `cat blacklist-custom | grep -v "^#" | grep -v "^$" | grep -Ev '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)' | cut -d: -f2`
  453.             do
  454.                 ipset -A blacklist-custom $IP
  455.             done
  456.         fi
  457. fi
  458.    
  459. echo "### WHITELIST ###"
  460.        
  461.     whiteports_number=`echo $whiteports | tr -d '\n' | sed 's/,/,\n/g' | sed 's/:/:\n/g' | wc -l`
  462.         a=1
  463.         b=8
  464.         rounds=`echo $(( $whiteports_number / $b ))`
  465.         if [ $rounds -eq 0 ]; then rounds="1"; fi
  466.     while [ $rounds -gt 0 ]
  467.     do
  468.         w=`echo $whiteports | cut -d"," -f $a-$b`
  469.         a=`echo $(( $a + $b ))`
  470.         b=`echo $(( $b + $b ))`
  471.     echo "loading whitelisted ports $w exemption"
  472. whitep="${whitep}iptables -A P2PARTISAN-IN -p tcp --match multiport --sports $w -j ACCEPT 2> /dev/null
  473. iptables -A P2PARTISAN-IN -p udp --match multiport --sports $w -j ACCEPT 2> /dev/null
  474. iptables -A P2PARTISAN-IN -p tcp --match multiport --dports $w -j ACCEPT 2> /dev/null
  475. iptables -A P2PARTISAN-IN -p udp --match multiport --dports $w -j ACCEPT 2> /dev/null
  476. iptables -A P2PARTISAN-OUT -p tcp --match multiport --sports $w -j ACCEPT 2> /dev/null
  477. iptables -A P2PARTISAN-OUT -p udp --match multiport --sports $w -j ACCEPT 2> /dev/null
  478. iptables -A P2PARTISAN-OUT -p tcp --match multiport --dports $w -j ACCEPT 2> /dev/null
  479. iptables -A P2PARTISAN-OUT -p udp --match multiport --dports $w -j ACCEPT 2> /dev/null
  480. "
  481.     rounds=`echo $(( $rounds - 1 ))`
  482.     done
  483.  
  484.  
  485.         echo "# $now
  486. iptables -N P2PARTISAN-IN 2> /dev/null
  487. iptables -N P2PARTISAN-OUT 2> /dev/null
  488. iptables -N P2PARTISAN-DROP-IN 2> /dev/null
  489. iptables -N P2PARTISAN-DROP-OUT 2> /dev/null
  490. iptables -F P2PARTISAN-IN 2> /dev/null
  491. iptables -F P2PARTISAN-OUT 2> /dev/null
  492. iptables -F P2PARTISAN-DROP-IN 2> /dev/null
  493. iptables -F P2PARTISAN-DROP-OUT 2> /dev/null
  494. iptables -A P2PARTISAN-IN -m set --set blacklist-custom src -j P2PARTISAN-DROP-IN 2> /dev/null
  495. iptables -A P2PARTISAN-OUT -m set --set blacklist-custom src -j P2PARTISAN-DROP-OUT 2> /dev/null
  496. $whitep" > iptables-add
  497.  
  498.  
  499.         echo "# $now
  500. iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  501. iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  502. iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  503. iptables -D INPUT -i $lanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  504. iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  505. iptables -F P2PARTISAN-DROP-IN 2> /dev/null
  506. iptables -F P2PARTISAN-DROP-OUT 2> /dev/null
  507. iptables -F P2PARTISAN-IN 2> /dev/null
  508. iptables -F P2PARTISAN-OUT 2> /dev/null
  509. iptables -X P2PARTISAN-IN 2> /dev/null
  510. iptables -X P2PARTISAN-OUT 2> /dev/null
  511. iptables -X P2PARTISAN-DROP-IN 2> /dev/null
  512. iptables -X P2PARTISAN-DROP-OUT 2> /dev/null" >> iptables-del
  513.  
  514.  
  515. echo "preparing the IP whitelist for the iptables"
  516. #Load the whitelist
  517. if [ "$(ipset --swap whitelist whitelist 2>&1 | grep 'Unknown set')" != "" ]
  518.     then
  519.     ipset --create whitelist iptreemap
  520.     cat whitelist | grep -Ev '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)' |
  521.     (
  522.     while read IP
  523.     do
  524.             echo "$IP" | grep "^#" >/dev/null 2>&1 && continue
  525.             echo "$IP" | grep "^$" >/dev/null 2>&1 && continue
  526.                     ipset -A whitelist $IP
  527.             done
  528.     )
  529. fi
  530.         echo "# $now
  531. ipset -F
  532. ipset -X blacklist-custom
  533. ipset -X whitelist" > ipset-del
  534.  
  535.             echo "loading the IP whitelist"
  536.             echo "iptables -A P2PARTISAN-IN -m set --set whitelist src -j ACCEPT 2> /dev/null
  537. iptables -A P2PARTISAN-OUT -m set --set whitelist dst -j ACCEPT 2> /dev/null" >> iptables-add
  538.  
  539.         if [ $syslogs -eq "1" ]; then        
  540.             echo "iptables -A P2PARTISAN-DROP-IN -m limit --limit $maxloghour/hour --limit-burst 1 -j LOG --log-prefix \"P2Partisan Dropped: \" --log-level 1 2> /dev/null" >> iptables-add
  541.             echo "iptables -A P2PARTISAN-DROP-OUT -m limit --limit $maxloghour/hour --limit-burst 1 -j LOG --log-prefix \"P2Partisan Rejected: \" --log-level 1 2> /dev/null" >> iptables-add
  542.        
  543.         fi
  544.         echo "iptables -A P2PARTISAN-DROP-IN -j DROP"  >> iptables-add
  545.         echo "iptables -A P2PARTISAN-DROP-OUT -j REJECT --reject-with icmp-admin-prohibited"  >> iptables-add
  546.  
  547.  
  548. echo "### BLACKLISTs ###"
  549.    
  550.     cat blacklists |
  551.    (
  552.     while read line
  553.     do
  554.             echo "$line" | grep "^#" >/dev/null 2>&1 && continue
  555.             echo "$line" | grep "^$" >/dev/null 2>&1 && continue
  556.             counter=`expr $counter + 1`
  557.             name=`echo $line |cut -d ' ' -f1`
  558.             url=`echo $line |cut -d ' ' -f2`
  559.             echo "loading blacklist #$counter --> ***$name***"
  560.      
  561.     if [ $fastroutine -eq "1" ]; then
  562.      
  563.      if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  564.       then
  565.           [ -f ./runtime ] && rm -f ./runtime 2> /dev/null
  566.           [ -e $name.gz ] || wget -q -O $name.gz "$url"
  567.           { echo "-N $name iptreemap"
  568.             gunzip -c  $name.gz | \
  569.             sed -e "/^[\t ]*#.*\|^[\t ]*$/d;s/^.*:/-A $name /" | \
  570.             grep -Ev '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)'
  571.             echo COMMIT
  572.           } | ipset -R
  573.      fi
  574.     else
  575.      
  576.         if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  577.             then
  578.             [ -f ./runtime ] && rm -f ./runtime 2> /dev/null
  579.             ipset --create $name iptreemap
  580.             [ -e $name.lst ] || wget -q -O - "$url" | gunzip | cut -d: -f2 | grep -E "^[-0-9.]+$" | grep -Ev '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)' > $name.lst
  581.             for IP in $(cat $name.lst)
  582.                     do
  583.                     ipset -A $name $IP
  584.                     done
  585.             fi
  586.              
  587.     fi
  588.  
  589.                 echo "ipset -X $name " >> ipset-del
  590.                 echo "iptables -A P2PARTISAN-IN -m set --set $name src -j P2PARTISAN-DROP-IN 2> /dev/null
  591. iptables -A P2PARTISAN-OUT -m set --set $name dst -j P2PARTISAN-DROP-OUT 2> /dev/null" >> iptables-add 
  592.             done
  593.     )
  594.  
  595.  
  596.         if [ $protection -eq "1" ]; then
  597.             echo "iptables -I INPUT $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  598. iptables -I OUTPUT $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null" >> iptables-add
  599.         elif [ $protection -eq "2" ]; then
  600.             echo "iptables -I wanin $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  601. iptables -I wanout $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  602. iptables -I INPUT $pos -i $lanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null" >> iptables-add
  603.         elif [ $protection -eq "3" ]; then
  604.             echo "iptables -I INPUT $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  605. iptables -I INPUT $pos -i $lanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  606. iptables -I OUTPUT $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  607. iptables -I wanin $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  608. iptables -I wanout $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null" >> iptables-add
  609.  
  610.         fi
  611.  
  612. chmod 777 ./iptables-*
  613. chmod 777 ./ipset-*
  614. ./iptables-add  #protecting
  615.  
  616. plog "... P2Partisan started."
  617.  
  618. p=`nvram get dnsmasq_custom | grep log-async | wc -l`
  619. if [ $p -eq "1" ]; then
  620.     plog "log-async found under dnsmasq -> OK"
  621. else
  622.     plog "
  623. It appears like you don't have a log-async parameter
  624. in your dnsmasq config. This is strongly suggested
  625. due to the amount of logs involved. please consider
  626. adding the following command under Advanced/DHCP/DNS
  627. /Dnsmasq Custom configuration
  628.  
  629. log-async=10
  630. "
  631. fi
  632.  
  633. punblock  #remove paranoia DROPs if any
  634.  
  635.     post=`date +%s`
  636.     runtime=`echo $(( $post - $pre ))`
  637.     [ -f ./runtime ] || echo $runtime > ./runtime
  638.     else
  639.         echo "
  640.     It appears like P2Partisan is already running. Skipping...
  641.            
  642.     If this is not what you expected? Try:
  643.     p2partisan.sh update
  644.         "
  645.     fi
  646. }
  647.  
  648.  
  649. for p in $1
  650. do
  651. case "$p" in
  652.         "start")
  653.                 pstart
  654.                 exit
  655.                 ;;     
  656.         "stop")
  657.                 pforcestop
  658.                 exit
  659.                 ;;
  660.         "restart")
  661.                 pscheduleunset
  662.                 psoftstop
  663.                 pscheduleset
  664.                 ;;
  665.         "status")
  666.                 pstatus
  667.                 exit               
  668.                 ;;
  669.         "pause")
  670.                 psoftstop
  671.                 exit
  672.                 ;;
  673.         "update")
  674.                 pforcestop
  675.                 ;;
  676.         "paranoia-update")
  677.                 pblock
  678.                 pforcestop
  679.                 ;;
  680.         "autorun-on")
  681.                 pautorunset
  682.                 exit
  683.                 ;;
  684.         "autorun-off")
  685.                 pautorununset
  686.                 exit
  687.                 ;;
  688.         "autoupdate-on")
  689.                 pscheduleset
  690.                 exit
  691.                 ;;
  692.         "autoupdate-off")
  693.                 pscheduleunset
  694.                 exit
  695.                 ;;
  696.         "tutor-on")
  697.                 ptutorset
  698.                 exit
  699.                 ;;
  700.         "tutor-off")
  701.                 ptutorunset
  702.                 exit
  703.                 ;;
  704.         "tutor")
  705.                 ptutor
  706.                 exit
  707.                 ;;
  708.         "upgrade")
  709.                 pupgrade
  710.                 ;;
  711.         "help")
  712.                 echo
  713.     P2Partisan parameters:
  714.        
  715.     help            Display this text      
  716.     start           Starts the process (this runs also if no option
  717.                 is provided)
  718.     stop            Stops P2Partisan
  719.     restart         Soft restart, quick, updates iptables only
  720.     pause           Soft stop P2Partisan allowing for quick start
  721.     update          Hard restart, slow removes p2partisan, updates
  722.                 the lists and does a fresh start
  723.     paranoia-update     Like update but blocks any new connection until
  724.                 P2Partisan is running again
  725.     status          Display P2Partisan running status + extra info
  726.     autorun-on      Sets P2Partisan to boot with the router
  727.     autorun-off     Sets P2Partisan not to boot with the router
  728.     autoupdate-on       Sets automatic weekly updates to on
  729.     autoupdate-off      Sets automatic weekly updates to off
  730.     tutor-on        Sets hourly running-status checks to on
  731.     tutor-off       Sets hourly running-status checks to off
  732.     upgrade         Download and install the latest P2Partisan
  733. "
  734.                 exit
  735.                 ;;
  736.         *)
  737.                 echo "parameter not valid. please run:
  738.                
  739.     p2partisan.sh help
  740.     "
  741.                 exit
  742.             ;;
  743.  
  744. esac
  745. done
  746.  
  747. pstart
  748.  
  749. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement