Advertisement
rs232

p2partisan 4.45

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