Advertisement
rs232

p2partisan 4.60

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