Advertisement
rs232

p2partisan 4.48

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