Advertisement
rs232

p2partisan 4.41

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