Advertisement
rs232

p2partisan 4.52

Mar 4th, 2015
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 35.23 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # p2partisan v4.52 (05/03/2015)
  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=21,25,44,53,80,123,443,993,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.         whiteports_number=`echo $whiteports | sed 's/,/,\n/g' | sed 's/:/:\n/g' | wc -l`
  100.                 aa=1
  101.                 b=8
  102.                 bb=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 $aa-$bb`
  108.                 aa=`echo $(( $bb + 1 ))`
  109.                 bb=`echo $(( $bb + $b ))`
  110.                     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.         rounds=`echo $(( $rounds - 1 ))`
  115.         done
  116.         iptables -A PARANOIA-DROP -m set --set whitelist dst -j ACCEPT 2> /dev/null
  117.         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
  118.         iptables -A PARANOIA-DROP -j DROP
  119.         iptables -I wanin 1 -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  120.         iptables -I wanout 1 -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  121.         iptables -I INPUT 1 -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  122.         iptables -I OUTPUT 1 -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  123. }
  124.  
  125. punblock() {
  126.         while iptables -L wanin 2> /dev/null | grep "PARANOIA-DROP"
  127.         do
  128.                 iptables -D wanin -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  129.         done
  130.         while iptables -L wanout 2> /dev/null | grep "PARANOIA-DROP"
  131.         do
  132.                 iptables -D wanout -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  133.         done
  134.         while iptables -L OUTPUT 2> /dev/null | grep "PARANOIA-DROP"
  135.         do
  136.                 iptables -D OUTPUT -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  137.         done
  138.         while iptables -L INPUT 2> /dev/null | grep "PARANOIA-DROP"
  139.         do
  140.                 iptables -D INPUT -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  141.         done
  142.         iptables -F PARANOIA-DROP 2> /dev/null && plog "P2PArtisan: Removing paranoia block"
  143.         iptables -X PARANOIA-DROP 2> /dev/null
  144. }
  145.  
  146. pforcestop() {
  147. counter=0
  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 OUTPUT | grep P2PARTISAN-OUT
  161.         do
  162.                 iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  163.         done
  164.         iptables -F P2PARTISAN-DROP-IN 2> /dev/null
  165.         iptables -F P2PARTISAN-DROP-OUT 2> /dev/null
  166.         iptables -F P2PARTISAN-IN 2> /dev/null
  167.         iptables -F P2PARTISAN-OUT 2> /dev/null
  168.         iptables -X P2PARTISAN-DROP-IN 2> /dev/null    
  169.         iptables -X P2PARTISAN-DROP-OUT 2> /dev/null  
  170.         iptables -X P2PARTISAN-IN 2> /dev/null
  171.         iptables -X P2PARTISAN-OUT 2> /dev/null
  172.         ipset -F
  173.         for i in `ipset --list | grep Name | cut -f2 -d ":" `; do
  174.                 ipset -X $i
  175.         done
  176.         chmod 777 ./*.gz  2> /dev/null
  177.     [ -f iptables-add ] && rm iptables-add
  178.     [ -f iptables-del ] && rm iptables-del
  179.     [ -f ipset-del ] && rm ipset-del
  180.         [ -f $pidfile ] && rm -f "$pidfile" 2> /dev/null
  181.         [ -f runtime ] && rm -f "runtime" 2> /dev/null
  182. plog "Unloading ipset modules"
  183.         lsmod | grep "ipt_set" > /dev/null 2>&1 && sleep 2 ; rmmod -f ipt_set 2> /dev/null
  184.         lsmod | grep "ip_set_iptreemap" > /dev/null 2>&1 && sleep 2 ; rmmod -f ip_set_iptreemap 2> /dev/null
  185.         lsmod | grep "ip_set" > /dev/null 2>&1 && sleep 2 ; rmmod -f ip_set 2> /dev/null
  186. plog "Removing the list files"
  187.         cat blacklists |
  188.    (
  189.     while read line
  190.     do
  191.             echo "$line" | grep "^#" >/dev/null 2>&1 && continue
  192.             echo "$line" | grep "^$" >/dev/null 2>&1 && continue
  193.             counter=`expr $counter + 1`
  194.             name=`echo $line |cut -d ' ' -f1`
  195.             echo "Removing blacklist #$counter --> ***$name***"
  196.                   [ -f ./$name.gz ] && rm -f ./$name.gz
  197.                         done
  198.     )
  199. plog "Stopping P2Partisan"
  200. }
  201.  
  202. pstatus() {
  203.         running3=`iptables -L INPUT | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  204.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  205.         running5=`nvram get script_fire | grep "p2partisan.sh ]" >/dev/null && echo "\033[1;32mYes\033[0;39m" || echo "\033[1;31mNo\033[0;39m"`
  206.         running6=`cru l | grep P2Partisan-update >/dev/null && echo "\033[1;32mYes\033[0;39m" || echo "\033[1;31mNo\033[0;39m"`
  207.         running7=`tail -200 /var/log/messages | grep Dropped | tail -1`
  208.         running7a=`tail -200 /var/log/messages | grep Rejected | tail -1`
  209.         running9=`nvram get script_fire | grep "P2Partisan-tutor" >/dev/null && echo "\033[1;32mYes\033[0;39m" || echo "\033[1;31mNo\033[0;39m"`
  210.         runningA=`cat /var/log/messages | grep "Applying paranoia" | wc -l`
  211.         runningB=`cat /var/log/messages | grep "Stuck on Loading" | wc -l`
  212.         runningC=`cat blacklists | grep -v "^#" | grep -v "^$" | wc -l`
  213.         runningD=`cat ./runtime`
  214.         from=`head -1 ./iptables-add 2> /dev/null | cut -c3-`
  215.         runtime=`echo $(( $now - $from ))`
  216.                 d=`echo $(( $runtime / 86400 ))`
  217.         h=`echo $((( $runtime / 3600 ) %24 ))`
  218.                 m=`echo $((( $runtime / 60 ) %60 ))`
  219.                 s=`echo $(( $runtime %60 ))`
  220.         runtime=`printf "$d - %02d:%02d:%02d\n" $h $m $s`
  221.         drop_packet_count_in=`iptables -vL P2PARTISAN-DROP-IN 2> /dev/null| grep " DROP " | awk '{print $1}'`
  222.         drop_packet_count_out=`iptables -vL P2PARTISAN-DROP-OUT 2> /dev/null| grep " REJECT " | awk '{print $1}'`      
  223.        
  224.         if [[ $running3 -eq "0" ]] && [[ $running4 -eq "0" ]]; then
  225.                 running8="\033[1;31mNo\033[0;39m"
  226.         elif [[ $running3 -eq "0" ]] && [[ $running4 -eq "1" ]]; then
  227.                 running8="\033[1;35mLoading...\033[0;39m"
  228.         elif [[ $running3 -gt "0" ]] && [[ $running4 -eq "0" ]]; then
  229.                 running8="\033[1;31mNot quite... try to run \"p2partisan.sh update\"\033[0;39m"
  230.         else
  231.                 running8="\033[1;32mYes\033[0;39m"
  232.         fi
  233.  
  234. whiteip=`ipset -L whitelist | grep -e "^[0-9].*" | wc -l`
  235. whiteextra=`ipset -L whitelist | grep -E '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)' | wc -l`
  236. if [[ $whiteextra == "0" ]]; then
  237. whiteextra=" "
  238. else
  239. whiteextra=`echo "/ $whiteextra" LAN IP ref defined`
  240. fi
  241. blackip=`ipset -L blacklist-custom | grep -e "^[0-9].*" | wc -l`
  242. blackextra=`ipset -L blacklist-custom | grep -E '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)' | wc -l`
  243. if [[ $blackextra == "0" ]]; then
  244. blackextra=" "
  245. else
  246. blackextra=`echo "/ $blackextra" LAN IP ref defined`
  247. fi
  248.      
  249. echo -e "################### P2Partisan ##########################"
  250. echo -e "#       Release version: $version
  251. ################# P2Partisan status #####################
  252. # Running:      $running8
  253. # Autorun:      $running5
  254. # Scheduled:    $running6 / $runningA since device boot
  255. # Tutor:        $running9 / $runningB since device boot
  256. #########################################################
  257. # Uptime:       $runtime
  258. # Startup time: $runningD seconds
  259. # Dropped in:   $drop_packet_count_in
  260. # Rejected out: $drop_packet_count_out
  261. #########################################################
  262. # Custom black: $blackip $blackextra
  263. # Custom white: $whiteip $whiteextra"
  264.         whiteports_number=`echo $whiteports | sed 's/,/,\n/g' | sed 's/:/:\n/g' | wc -l`
  265.                 aa=1
  266.                 b=8
  267.                 bb=8
  268.                 rounds=`echo $(( $whiteports_number / $b ))`
  269.                 if [ $rounds -eq 0 ]; then rounds="1"; fi
  270.         while [ $rounds -gt 0 ]
  271.         do
  272.                 w=`echo $whiteports | cut -d"," -f $aa-$bb`
  273.                 aa=`echo $(( $bb + 1 ))`
  274.                 bb=`echo $(( $bb + $b ))`
  275.                                 echo "# White ports:  $w"
  276.                 rounds=`echo $(( $rounds - 1 ))`
  277.         done
  278.  
  279. echo "# Blacklists:   $runningC
  280. ################# Last log recorded #####################
  281. # Remember your max logs per hour is set to: $maxloghour
  282. $running7
  283. $running7a"
  284. echo "#########################################################"
  285. }
  286.  
  287.  
  288. if [ $autorun_availability_check = 1 ]; then
  289. av="while true; do [ -f $P2Partisandir/p2partisan.sh ] && break || sleep 5; done ;"
  290. fi
  291.  
  292. pautorunset() {
  293.         p=`nvram get script_fire | grep "p2partisan.sh ]" | grep -v cru | wc -l`
  294.         if [ $p -eq "0" ] ; then
  295.                 t=`nvram get script_fire`; t=`printf "$t\n$av$P2Partisandir/p2partisan.sh\n"` ; nvram set "script_fire=$t"
  296.         fi
  297.         plog "P2Partisan AUTO RUN is ON"
  298.         nvram commit
  299. }
  300.  
  301. pautorununset() {
  302.         p=`nvram get script_fire | grep "p2partisan.sh ]" | grep -v cru | wc -l`
  303.         if [ $p -eq "1" ]; then
  304.         t=`nvram get script_fire`; t=`printf "$t" | grep -v "p2partisan.sh ]"` ; nvram set "script_fire=$t"
  305.         fi
  306.         plog "P2Partisan AUTO RUN is OFF"
  307.         nvram commit
  308. }
  309.  
  310. pscheduleset() {
  311.         cru d P2Partisan-update
  312.         e=`tr -cd 0-5 </dev/urandom | head -c 1`
  313.         f=`tr -cd 0-9 </dev/urandom | head -c 1`
  314.         a=`echo $e$f`
  315.         b=`tr -cd 1-5 </dev/urandom | head -c 1`
  316.         c=`tr -cd 0-6 </dev/urandom | head -c 1`
  317.         scheduleme=`echo "$schedule" | tr "m" "$a"`
  318.         scheduleme=`echo "$scheduleme" | tr "h" "$b"`
  319.         scheduleme=`echo "$scheduleme" | tr "d" "$c"`
  320.         cru a P2Partisan-update "$scheduleme $P2Partisandir/p2partisan.sh paranoia-update"
  321.         pp=`nvram get script_fire | grep "p2partisan.sh paranoia-update" | grep -v cru | wc -l`
  322.         p=`nvram get script_fire | grep "cru a P2Partisan-update" | wc -l`
  323.         if [ $p -eq "0" ] ; then
  324.                 if [ $pp -eq "0" ]; then
  325.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-update \"$scheduleme $P2Partisandir/p2partisan.sh paranoia-update\"\n"` ; nvram set "script_fire=$t"
  326.                 else
  327.                 pautorununset
  328.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-update \"$scheduleme $P2Partisandir/p2partisan.sh paranoia-update\"\n"` ; nvram set "script_fire=$t"
  329.                 pautorunset
  330.                 fi
  331.         fi
  332.         plog "P2Partisan AUTO UPDATE is ON"
  333.         nvram commit
  334. }
  335.  
  336. pscheduleunset() {
  337.         cru d P2Partisan-update
  338.         p=`nvram get script_fire | grep "cru a P2Partisan-update" | wc -l`
  339.         if [ $p -eq "1" ] ; then
  340.         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"
  341.         fi
  342.         plog "P2Partisan AUTO UPDATE is OFF"
  343.         nvram commit
  344. }
  345.  
  346. pupgrade() {
  347.         [ -f p2partisan_new.sh ] && rm -f "p2partisan_new.sh" 2> /dev/null
  348.         wget -q -O - http://pastebin.com/raw.php?i=jqHD3hfT | grep "p2partisan v" | grep -v grep> ./latest
  349.         latest=`cat ./latest | cut -c3-31`
  350.         current=`cat ./p2partisan.sh | grep "p2partisan v" | head -1 | cut -c3-32 `
  351.         if [[ "$latest" == "$current" ]]; then
  352.         echo "
  353. You're already running the latest version of P2Partisan
  354. "
  355.         else
  356.         echo "
  357. There's a new P2Partisan update available. Do you want to upgrade?
  358.      
  359.                        current = $current
  360.      
  361.                                        to
  362.                      
  363.                         latest = $latest
  364.  
  365. y/n"
  366.         read answer
  367.         # echo "You entered: $input_variable"
  368.                 if [[ $answer == "y" ]]; then
  369. wget -q -O ./p2partisan_new.sh http://pastebin.com/raw.php?i=jqHD3hfT
  370. pupgraderoutine
  371.                 else
  372.                 echo "Upgrade skipped. Quitting..."
  373.                 exit
  374.                 fi
  375.        
  376.         fi
  377.  }
  378.  
  379. pupgradebeta() {
  380.         [ -f p2partisan_new.sh ] && rm -f "p2partisan_new.sh" 2> /dev/null
  381.         wget -q -O - http://pastebin.com/raw.php?i=Q8AnCaCy | grep "p2partisan v" | grep -v grep > ./latest
  382.         echo "
  383. Do you want to install the latest testing beta (not suggested)?
  384.  
  385. y/n"
  386.         read answer
  387.         # echo "You entered: $input_variable"
  388.                 if [[ $answer == "y" ]]; then
  389. wget -q -O ./p2partisan_new.sh http://pastebin.com/raw.php?i=Q8AnCaCy
  390. pupgraderoutine
  391.                 else
  392.                 echo "Beta upgrade skipped. Quitting..."
  393.                 exit
  394.                 fi
  395.  }
  396.  
  397.  pupgradesilent() {
  398.         [ -f p2partisan_new.sh ] && rm -f "p2partisan_new.sh" 2> /dev/null
  399.         wget -q -O - http://pastebin.com/raw.php?i=jqHD3hfT | grep "p2partisan v" | grep -v grep> ./latest
  400.         latest=`cat ./latest | cut -c3-31`
  401.         current=`cat ./p2partisan.sh | grep "p2partisan v" | head -1 | cut -c3-32 `
  402.         if [[ "$latest" == "$current" ]]; then
  403.         echo "
  404. You're already running the latest version of P2Partisan
  405. "
  406.         else
  407. wget -q -O ./p2partisan_new.sh http://pastebin.com/raw.php?i=jqHD3hfT
  408. pupgradroutine
  409.         fi
  410.  }
  411.  
  412. pupgraderoutine() {
  413.                 echo "Upgrading, please wait:"
  414.                 echo "1/6) Stopping the script"
  415.                 pforcestop
  416.                 [ -f p2partisan_new.sh ] || plog "There's a problem with the p2partisan upgrade. Please try again"
  417.                 echo "2/6) Migrating the configuration"
  418.                 sed '1,/P2Partisandir/{s@P2Partisandir=.*@'"P2Partisandir=$P2Partisandir"'@'} -i ./p2partisan_new.sh
  419.                 sed '1,/syslogs/{s@syslogs=.*@'"syslogs=$syslogs"'@'} -i ./p2partisan_new.sh
  420.                 sed '1,/maxloghour/{s@maxloghour=.*@'"maxloghour=$maxloghour"'@'} -i ./p2partisan_new.sh
  421.                 sed '1,/protection/{s@protection=.*@'"protection=$protection"'@'} -i ./p2partisan_new.sh
  422.                 sed '1,/whiteports/{s@whiteports=.*@'"whiteports=$whiteports"'@'} -i ./p2partisan_new.sh
  423.                 sed '1,/fastroutine/{s@fastroutine=.*@'"fastroutine=$fastroutine"'@'} -i ./p2partisan_new.sh
  424.                 sed '1,/autorun_availability_check/{s@autorun_availability_check=.*@'"autorun_availability_check=$autorun_availability_check"'@'} -i ./p2partisan_new.sh
  425.                 sed '1,/schedule/{s@schedule=.*@'"schedule=\"$schedule\""'@'} -i ./p2partisan_new.sh
  426.                 sed '1,/testip/{s@testip=.*@'"testip=$testip"'@'} -i ./p2partisan_new.sh              
  427.                 tr -d "\r"< ./p2partisan_new.sh > ./.temp ; mv ./.temp ./p2partisan_new.sh
  428.                 echo "3/6) Copying p2partisan.sh into p2partisan.sh.old"
  429.                 cp ./p2partisan.sh ./p2partisan_old
  430.                 echo "4/6) Installing new script into p2partisan.sh"
  431.                 mv ./p2partisan_new.sh ./p2partisan.sh
  432.                 echo "5/6) Setting up permissions"
  433.                 chmod -R 777 ./p2partisan.sh
  434.                 echo "6/6) all done, I'm now running the script for you.
  435. NOTE: autorun, autoupdate and tutor settings are left as they were found
  436. "
  437. }
  438.  
  439. ptutor() {
  440.         running3=`iptables -L INPUT | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  441.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  442.         runningE=`iptables -L wanin | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  443.         if [[ $runningE -gt "1" ]]; then
  444.                         pforcestop
  445.                         plog "P2Partisan tutor had to restart due to: iptables redundant rules found"
  446.                         pstart
  447.         elif [[ $running3 -eq "1" ]] && [[ $running4 -eq "0" ]]; then
  448.                         plog "P2Partisan tutor had to restart due to: pid file missing"
  449.                         pforcestop
  450.                         pstart
  451.         elif [[ $running3 -eq "0" ]] && [[ $running4 -eq "1" ]]; then
  452.                         plog "P2Partisan tutor had to restart due to: iptables instructions missing"
  453.                         pforcestop
  454.                         pstart     
  455.         elif [[ $running3 -ne "1" ]] && [[ $running4 -eq "1" ]]; then
  456.                         plog "P2Partisan appears to be loading, I'll wait 5 minutes..."    
  457.                         sleep 300
  458.                 if [[ $running3 -ne "1" ]] && [[ $running4 -eq "1" ]]; then
  459.                         plog "P2Partisan tutor had to restart due to Stuck on Loading"
  460.                         pforcestop
  461.                         pstart
  462.                 fi
  463.         else
  464.         echo "P2Partisan up and running. The tutor is happy"
  465.         fi
  466.  }
  467.  
  468. ptutorset() {
  469.         cru d P2Partisan-tutor
  470.         ab=`tr -cd 0-5 </dev/urandom | head -c 1`
  471.         a=`tr -cd 0-9 </dev/urandom | head -c 1`
  472.         a=`echo $ab$a`
  473.         scheduleme=`echo "$a * * * *"`
  474.         cru a P2Partisan-tutor "$scheduleme $P2Partisandir/p2partisan.sh tutor"
  475.         pp=`nvram get script_fire | grep "p2partisan.sh tutor" | grep -v cru | wc -l`
  476.         p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  477.         if [ $p -eq "0" ] ; then
  478.                 if [ $pp -eq "0" ]; then
  479.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-tutor \"$scheduleme $P2Partisandir/p2partisan.sh tutor\"\n"` ; nvram set "script_fire=$t"
  480.                 else
  481.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-tutor \"$scheduleme $P2Partisandir/p2partisan.sh tutor\"\n"` ; nvram set "script_fire=$t"
  482.                 fi
  483.         fi
  484.         plog "P2Partisan tutor is ON"
  485.         nvram commit
  486. }
  487.  
  488. ptutorunset() {
  489.         cru d P2Partisan-tutor
  490.         p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  491.         if [ $p -eq "1" ] ; then
  492.         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"
  493.         fi
  494.         plog "P2Partisan tutor is OFF"
  495.         nvram commit
  496.  }
  497.  
  498.  ptest() {
  499. checklist="blacklist-custom whitelist `cat blacklists | grep -v "^#" | grep -v "^$" | cut -d" " -f1`"
  500. echo "###############################################
  501. ### Lists are sorted in order of precedence ###
  502. ###############################################"
  503.         echo $checklist | tr " " "\n" |
  504.     (
  505.                 while read LIST
  506.                 do
  507.                 ipset -T $LIST $1 1>/dev/nul && if [ $LIST = "whitelist" ]; then echo -e "\033[1;32m$1 found in        $LIST\033[0;39m"; else echo -e "\033[1;31m$1 found in        $LIST\033[0;39m"; fi || echo -e "$1 not found in    $LIST"
  508.         done                                                                                                                                     #echo "\033[1;31mNo\033[0;39m"
  509.     )
  510.         echo "###############################################"
  511. }
  512.  
  513.  
  514. pstart() {
  515.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  516.         if [ $running4 -eq "0" ] ; then
  517.  
  518.         /bin/ntpsync > /dev/null 2>&1
  519.         pre=`date +%s`
  520.         sleep 1
  521.  
  522.         echo $$ > $pidfile
  523.        
  524.     [ -f iptables-add ] && rm iptables-add
  525.     [ -f iptables-del ] && rm iptables-del
  526.     [ -f ipset-del ] && rm ipset-del
  527.      
  528.         echo "### PREPARATION ###"
  529.         echo "Loading the ipset modules"
  530.         lsmod | cut -c1-20 | grep "ip_set " > /dev/null 2>&1 || insmod ip_set
  531.         lsmod | cut -c1-20 | grep "ip_set_iptreemap" > /dev/null 2>&1 || insmod ip_set_iptreemap
  532.         lsmod | cut -c1-20 | grep "ipt_set" > /dev/null 2>&1 || insmod ipt_set
  533.  
  534. counter=0
  535. pos=1
  536. couscous=`cat blacklist-custom | grep -v "^#" | grep -v "^$" | wc -l`
  537.  
  538.                 echo "### CUSTOM BLACKLIST ###
  539. blacklist-custom file -> $couscous entries found"
  540.  if [ $couscous -eq "0" ]; then
  541.                 echo "No custom blacklist entries found: skipping"
  542.  else
  543.                 echo "loading blacklist #$counter --> ***Custom IP blacklist***"
  544.                 ipset --create blacklist-custom iptreemap > /dev/null 2>&1
  545.         if [ -e blacklist-custom ]; then
  546.         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`
  547.             do
  548.                 ipset -A blacklist-custom $IP
  549.             done
  550.                 fi
  551. fi
  552.        
  553. echo "### WHITELIST ###"
  554.                
  555.         whiteports_number=`echo $whiteports | sed 's/,/,\n/g' | sed 's/:/:\n/g' | wc -l`
  556.                 aa=1
  557.                 b=8
  558.                 bb=8
  559.                 rounds=`echo $(( $whiteports_number / $b ))`
  560.                 if [ $rounds -eq 0 ]; then rounds="1"; fi
  561.         while [ $rounds -gt 0 ]
  562.         do
  563.                 w=`echo $whiteports | cut -d"," -f $aa-$bb`
  564.                 aa=`echo $(( $bb + 1 ))`
  565.                 bb=`echo $(( $bb + $b ))`
  566.         echo "loading whitelisted ports $w exemption"
  567. whitep="${whitep}iptables -A P2PARTISAN-IN -p tcp --match multiport --sports $w -j ACCEPT 2> /dev/null
  568. iptables -A P2PARTISAN-IN -p udp --match multiport --sports $w -j ACCEPT 2> /dev/null
  569. iptables -A P2PARTISAN-IN -p tcp --match multiport --dports $w -j ACCEPT 2> /dev/null
  570. iptables -A P2PARTISAN-IN -p udp --match multiport --dports $w -j ACCEPT 2> /dev/null
  571. iptables -A P2PARTISAN-OUT -p tcp --match multiport --sports $w -j ACCEPT 2> /dev/null
  572. iptables -A P2PARTISAN-OUT -p udp --match multiport --sports $w -j ACCEPT 2> /dev/null
  573. iptables -A P2PARTISAN-OUT -p tcp --match multiport --dports $w -j ACCEPT 2> /dev/null
  574. iptables -A P2PARTISAN-OUT -p udp --match multiport --dports $w -j ACCEPT 2> /dev/null
  575. "
  576.         rounds=`echo $(( $rounds - 1 ))`
  577.         done
  578.  
  579.  
  580.                 echo "# $now
  581. iptables -N P2PARTISAN-IN 2> /dev/null
  582. iptables -N P2PARTISAN-OUT 2> /dev/null
  583. iptables -N P2PARTISAN-DROP-IN 2> /dev/null
  584. iptables -N P2PARTISAN-DROP-OUT 2> /dev/null
  585. iptables -F P2PARTISAN-IN 2> /dev/null
  586. iptables -F P2PARTISAN-OUT 2> /dev/null
  587. iptables -F P2PARTISAN-DROP-IN 2> /dev/null
  588. iptables -F P2PARTISAN-DROP-OUT 2> /dev/null
  589. iptables -A P2PARTISAN-IN -m set --set blacklist-custom src -j P2PARTISAN-DROP-IN 2> /dev/null
  590. iptables -A P2PARTISAN-OUT -m set --set blacklist-custom src -j P2PARTISAN-DROP-OUT 2> /dev/null" > iptables-add
  591.  
  592.  
  593.                 echo "# $now
  594. iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  595. iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  596. iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  597. iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  598. iptables -F P2PARTISAN-DROP-IN 2> /dev/null
  599. iptables -F P2PARTISAN-DROP-OUT 2> /dev/null
  600. iptables -F P2PARTISAN-IN 2> /dev/null
  601. iptables -F P2PARTISAN-OUT 2> /dev/null
  602. iptables -X P2PARTISAN-IN 2> /dev/null
  603. iptables -X P2PARTISAN-OUT 2> /dev/null
  604. iptables -X P2PARTISAN-DROP-IN 2> /dev/null
  605. iptables -X P2PARTISAN-DROP-OUT 2> /dev/null" >> iptables-del
  606.  
  607.  
  608. echo "preparing IP whitelist for the iptables"
  609. #Load the whitelist
  610. if [ "$(ipset --swap whitelist whitelist 2>&1 | grep 'Unknown set')" != "" ]
  611.     then
  612.     ipset --create whitelist iptreemap > /dev/null 2>&1
  613.     cat whitelist |
  614.     (
  615.     while read IP
  616.     do
  617.             echo "$IP" | grep "^#" >/dev/null 2>&1 && continue
  618.             echo "$IP" | grep "^$" >/dev/null 2>&1 && continue
  619.                     ipset -A whitelist $IP
  620.             done
  621.     )
  622. fi
  623.                 echo "# $now
  624. ipset -F
  625. ipset -X blacklist-custom
  626. ipset -X whitelist" > ipset-del
  627.  
  628.                         echo "loading the IP whitelist"
  629.                         echo "iptables -A P2PARTISAN-IN -m set --set whitelist src -j ACCEPT 2> /dev/null
  630. iptables -A P2PARTISAN-OUT -m set --set whitelist dst -j ACCEPT 2> /dev/null
  631. $whitep" >> iptables-add
  632.  
  633.                 if [ $syslogs -eq "1" ]; then        
  634.                         echo "iptables -A P2PARTISAN-DROP-IN -m limit --limit $maxloghour/hour --limit-burst 1 -j LOG --log-prefix \"P2Partisan Dropped IN >> \" --log-level 1 2> /dev/null" >> iptables-add
  635.                         echo "iptables -A P2PARTISAN-DROP-OUT -m limit --limit $maxloghour/hour --limit-burst 1 -j LOG --log-prefix \"P2Partisan Rejected OUT >> \" --log-level 1 2> /dev/null" >> iptables-add
  636.                
  637.                 fi
  638.                 echo "iptables -A P2PARTISAN-DROP-IN -j DROP
  639. iptables -A P2PARTISAN-DROP-OUT -j REJECT --reject-with icmp-admin-prohibited"  >> iptables-add
  640.  
  641.  
  642. echo "### BLACKLISTs ###"
  643.        
  644.         cat blacklists |
  645.    (
  646.     while read line
  647.     do
  648.             echo "$line" | grep "^#" >/dev/null 2>&1 && continue
  649.             echo "$line" | grep "^$" >/dev/null 2>&1 && continue
  650.             counter=`expr $counter + 1`
  651.             name=`echo $line |cut -d ' ' -f1`
  652.             url=`echo $line |cut -d ' ' -f2`
  653.             echo "loading blacklist #$counter --> ***$name***"
  654.      
  655.     if [ $fastroutine -eq "1" ]; then
  656.      
  657.      if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  658.       then
  659.                   [ -f ./runtime ] && rm -f ./runtime 2> /dev/null
  660.                   [ -e $name.gz ] || wget -q -O $name.gz "$url"
  661.                   { echo "-N $name iptreemap"
  662.                         gunzip -c  $name.gz | \
  663.                         sed -e "/^[\t ]*#.*\|^[\t ]*$/d;s/^.*:/-A $name /" | \
  664.                         grep -Ev '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)'
  665.                         echo COMMIT
  666.                   } | ipset -R
  667.      fi
  668.     else
  669.      
  670.                 if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  671.             then
  672.                         [ -f ./runtime ] && rm -f ./runtime 2> /dev/null
  673.             ipset --create $name iptreemap
  674.             [ -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
  675.             for IP in $(cat $name.lst)
  676.                     do
  677.                     ipset -A $name $IP
  678.                     done
  679.                         fi
  680.                          
  681.         fi
  682.  
  683.                                 echo "ipset -X $name " >> ipset-del
  684.                                 echo "iptables -A P2PARTISAN-IN -m set --set $name src -j P2PARTISAN-DROP-IN 2> /dev/null
  685. iptables -A P2PARTISAN-OUT -m set --set $name dst -j P2PARTISAN-DROP-OUT 2> /dev/null" >> iptables-add
  686.                         done
  687.     )
  688.  
  689.  
  690. echo "iptables -I INPUT $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  691. iptables -I OUTPUT $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  692. iptables -I wanin $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  693. iptables -I wanout $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null" >> iptables-add
  694.  
  695. chmod 777 ./iptables-*
  696. chmod 777 ./ipset-*
  697. ./iptables-add  #protecting
  698.  
  699. plog "... P2Partisan started."
  700.  
  701. p=`nvram get dnsmasq_custom | grep log-async | wc -l`
  702. if [ $p -eq "1" ]; then
  703.         plog "log-async found under dnsmasq -> OK"
  704. else
  705.         plog "
  706. It appears like you don't have a log-async parameter
  707. in your dnsmasq config. This is strongly suggested
  708. due to the amount of logs involved. please consider
  709. adding the following command under Advanced/DHCP/DNS
  710. /Dnsmasq Custom configuration
  711.  
  712. log-async=5
  713. "
  714. fi
  715.  
  716. punblock  #remove paranoia DROPs if any
  717.  
  718.         post=`date +%s`
  719.         runtime=`echo $(( $post - $pre ))`
  720.         [ -f ./runtime ] || echo $runtime > ./runtime
  721.         else
  722.                 echo "
  723.        It appears like P2Partisan is already running. Skipping...
  724.                      
  725.        If this is not what you expected? Try:
  726.        p2partisan.sh update
  727.                "
  728.         fi
  729. }
  730.  
  731.  
  732. for p in $1
  733. do
  734. case "$p" in
  735.         "start")
  736.                                 pstart
  737.                                 exit
  738.                 ;;            
  739.         "stop")
  740.                                 pforcestop
  741.                                 exit
  742.                 ;;
  743.         "restart")
  744.                 psoftstop
  745.                 ;;
  746.                 "status")
  747.                 pstatus
  748.                                 exit                          
  749.                 ;;
  750.         "pause")
  751.                 psoftstop
  752.                                 exit
  753.                 ;;
  754.         "test")
  755.                 ptest $2
  756.                                 exit
  757.                 ;;                            
  758.         "update")
  759.                 pforcestop
  760.                 ;;
  761.         "paranoia-update")
  762.                                 pblock
  763.                 pforcestop
  764.                 ;;
  765.         "autorun-on")
  766.                                 pautorunset
  767.                                 exit
  768.                 ;;
  769.         "autorun-off")
  770.                                 pautorununset
  771.                                 exit
  772.                 ;;
  773.                 "autoupdate-on")
  774.                                 pscheduleset
  775.                                 exit
  776.                                 ;;
  777.                 "autoupdate-off")
  778.                                 pscheduleunset
  779.                                 exit
  780.                                 ;;
  781.                 "tutor-on")
  782.                                 ptutorset
  783.                                 exit
  784.                                 ;;
  785.                 "tutor-off")
  786.                                 ptutorunset
  787.                                 exit
  788.                                 ;;
  789.                 "tutor")
  790.                                 ptutor
  791.                                 exit
  792.                                 ;;
  793.         "upgrade")
  794.                                 pupgrade
  795.                 ;;
  796.         "upgrade-silent")
  797.                                 pupgradesilent
  798.                 ;;
  799.         "upgrade-beta")
  800.                                 pupgradebeta
  801.                 ;;                            
  802.                 "help")
  803.                                 echo "
  804.        P2Partisan parameters:
  805.              
  806.        help                    Display this text              
  807.        start                   Starts the process (this runs also if no option
  808.                                is provided)
  809.        stop                    Stops P2Partisan
  810.        restart                 Soft restart, quick, updates iptables only
  811.        pause                   Soft stop P2Partisan allowing for quick start
  812.        update                  Hard restart, slow removes p2partisan, updates
  813.                                the lists and does a fresh start
  814.        paranoia-update         Like update but blocks any new connection until
  815.                                P2Partisan is running again
  816.        status                  Display P2Partisan running status + extra info
  817.        test <IP>               Verify existence of the given IP against lists
  818.        autorun-on              Sets P2Partisan to boot with the router
  819.        autorun-off             Sets P2Partisan not to boot with the router
  820.        autoupdate-on           Sets automatic weekly updates to on
  821.        autoupdate-off          Sets automatic weekly updates to off
  822.        tutor-on                Sets hourly running-status checks to on
  823.        tutor-off               Sets hourly running-status checks to off
  824.        upgrade                 Download and install the latest P2Partisan
  825.        upgrade-silent          Like upgrade but no question asked. Useful for scheduler
  826. "
  827.                                 exit
  828.                 ;;
  829.         *)
  830.                                 echo "parameter not valid. please run:
  831.                              
  832.        p2partisan.sh help
  833.        "
  834.                                 exit
  835.                         ;;
  836.  
  837. esac
  838. done
  839.  
  840. pstart
  841.  
  842. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement