Advertisement
rs232

p2partisan beta-rmerlin

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