Advertisement
rs232

p2partisan 5.02

Jun 2nd, 2015
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 65.49 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # p2partisan v5.02 (02/06/2015)
  4. #
  5. # Official page - http://www.linksysinfo.org/index.php?posts/235301/
  6. #
  7. # <CONFIGURATION> ###########################################
  8. # Adjust location where the files are kept
  9. P2Partisandir=/cifs1/p2partisan
  10. #
  11. # Enable logging? Use only for troubleshooting. 0=off 1=on
  12. syslogs=1
  13. # Maximum number of logs to be recorded in a given 60 min
  14. # Consider set this very low (like 3 or 6) once your are
  15. # happy with the installation. To troubleshoot blocked
  16. # connection close all the secondary traffic e.g. p2p
  17. # and try a connection to the blocked site/port you should
  18. # find a reference in the logs.
  19. maxloghour=1
  20. #
  21. # Ports to be whitelisted. Whitelisted ports will never be
  22. # blocked no matter what the source/destination IP is.
  23. # This is very important if you're running a service like
  24. # e.g. SMTP/HTTP/IMAP/else. Separate value in the list below
  25. # with commas - NOTE: It is suggested to leave the following ports
  26. # always on as a minimum:
  27. # tcp:43,80,443
  28. # udp:53,67,68,123,1194:1196
  29. # you might want to append remote admin and VPN ports, and
  30. # anything else you think it's relevant.
  31. # Standard iptables syntax, individual ports divided by "," and ":" to
  32. # define a range e.g. 80,443,2100:2130. Do not whitelist you P2P client!
  33. whiteports_tcp=2544,80,443,3658,8080
  34. whiteports_udp=53,123,1194:1197,1723,3658
  35. #
  36. # Greyports are port/s you absolutely want to filter against lists.
  37. # Think of an Internet host that has its P2P client set on port 53 UDP.
  38. # If you have the DNS port is in the whiteports_udp then P2Partisan would
  39. # be completely bypassed. Internet-client:53 -> your-client:"P2Pport""
  40. # greyport is in a nutshell a list of port/s used by your LAN P2Pclient/s.
  41. # It's suggested you disable random port on your P2Pclient and add the
  42. # client port/s here. NOTE:
  43. # Accepted syntax: single port, multiple ports and ranges e.g.
  44. # greyports=22008,6789
  45. # the above would grey list 22008 and 6789. Don't know your client port?
  46. # try ./p2partisan.sh detective
  47. greyports_tcp=
  48. greyports_udp=
  49. #
  50. # Greyline is the limit of connections per given "IP:port" above which
  51. # Detective becomes suspicious. NOTE: This counts 1/2 of the sessions the
  52. # router actually reports on because of the NAT implication. So this number
  53. # represents the session as seen on the LAN client. Affects detective only.
  54. greyline=100
  55. #
  56. # Fastrouting will process the IP classes very quickly but use
  57. # Lot of resources. If you disable the effect is transparent
  58. # but the full process will take minutes rather than seconds
  59. # 0=disabled 1=enabled
  60. fastroutine=1
  61. #
  62. # Enable check on script availability to help autorun.
  63. # If the ./partisan.sh is remote wait for the file to be available
  64. # instead of quit with a file missing error
  65. autorun_availability_check=1
  66. #
  67. # Schedule updates? (once a week is plenty). Custom syntax:
  68. # m = random minute picked up in the range[0-59]
  69. # h = random hour picked up in the range [1-5]am
  70. # d = random day of the week picked up in the range Sun to Sat [0-6]
  71. # if unwanted set your own specific time e.g.
  72. # "30 4 * * 1" 4:30 on a Monday
  73. # or use a combination e.g. random minute at 1am on a Tuesday:
  74. # "m 1 * * 3"
  75. # Specify this always in between "" please
  76. schedule="m h * * d"
  77. #
  78. # IP for testing Internet connectivity
  79. testip=8.8.8.8
  80. # </CONFIGURATION> ###########################################
  81.  
  82. paranoiastuck=`iptables -L | grep PARANOIA | wc -l`
  83.     if [[ $paranoiastuck -lt 1 ]]; then
  84.  
  85.         # Wait until Internet is available
  86.         while :
  87.         do
  88.                 ping -c 3 $testip >/dev/null 2>&1
  89.                 if [ $? = 0 ]; then
  90.                         break
  91.                 fi
  92.                 sleep 5
  93.         done
  94.         fi
  95.  
  96. pidfile="/var/run/p2partisan.pid"
  97. logfile=`nvram get log_file_path`
  98. cd $P2Partisandir
  99. version=`head -3 ./p2partisan.sh | tail -1 | cut -f 3- -d " "`
  100. ipsetversion=`ipset -V | grep ipset | awk '{print $2}' | cut -c2` #4=old 6=new
  101.  
  102. alias ipset='/bin/nice -n19 /usr/sbin/ipset'
  103. alias sed='/bin/nice -n19 /bin/sed'
  104. alias iptables='/usr/sbin/iptables'
  105. alias service='/sbin/service'
  106. alias plog='logger -t "| P2PARTISAN" -s'
  107. now=`date +%s`
  108. wanif=`nvram get wan_ifname`
  109. lanif=`nvram get lan_ifname`
  110.  
  111.  
  112. # DHCP hardcoded patch
  113. p1=`echo $whiteports_udp | grep -Eo '[,|:]67[,|:| ]' | wc -l`
  114. p2=`echo $whiteports_udp | grep -Eo '[,|:]68[,|:| ]' | wc -l`
  115. if [ $p1 -eq "0" ]; then
  116.     whiteports_udp=${whiteports_udp},67
  117. fi     
  118. if [ $p2 -eq "0" ]; then
  119.     whiteports_udp=${whiteports_udp},68
  120. fi
  121.  
  122. psoftstop() {
  123.         [ -f /tmp/p2partisan.loading ] && echo "P2Partisan is still loading. Can't stop right now Exiting..." && exit
  124.     echo -e "
  125. +------------------------- P2Partisan --------------------------+
  126. |                   _______ __              
  127. |                  |     __|  |_.-----.-----.
  128. |                  |__     |   _|  _  |  _  |
  129. |            Soft  |_______|____|_____|   __|
  130. |                                     |__|  
  131. |                                            
  132. +---------------------------------------------------------------+"
  133.         echo -e "| Stopping P2Partisan..."
  134.         ./iptables-del 2> /dev/null
  135.         plog "Stopping P2Partisan..."
  136.         [ -f $pidfile ] && rm -f "$pidfile" 2> /dev/null
  137.         [ -f iptables-add ] && rm -f "iptables-add" 2> /dev/null
  138.         [ -f iptables-del ] && rm -f "iptables-del" 2> /dev/null
  139. echo "+---------------------------------------------------------------+"
  140. }
  141.  
  142.        
  143. pblock() {
  144.         plog "P2PArtisan: Applying paranoia block"
  145.         iptables -N PARANOIA-DROP 2> /dev/null
  146.     echo $whiteports_tcp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  147.         echo "| loading whitelisted ports $w exemption for paranoia-update"    
  148.         iptables -A PARANOIA-DROP -p tcp --match multiport --sports $w -j ACCEPT 2> /dev/null
  149.         iptables -A PARANOIA-DROP -p tcp --match multiport --dports $w -j ACCEPT 2> /dev/null
  150.         done               
  151.     echo $whiteports_udp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  152.         echo "| loading whitelisted UDP ports $w exemption"
  153.         iptables -A PARANOIA-DROP -p udp --match multiport --sports $w -j ACCEPT 2> /dev/null
  154.         iptables -A PARANOIA-DROP -p udp --match multiport --dports $w -j ACCEPT 2> /dev/null
  155.         done
  156.        
  157.         iptables -A PARANOIA-DROP -m set --set whitelist dst -j ACCEPT 2> /dev/null
  158.         iptables -A PARANOIA-DROP -m limit --limit $maxloghour/hour --limit-burst 5 -j LOG --log-prefix "P2Partisan DROP (PARANOIA) >> " --log-level 1 2> /dev/null
  159.         iptables -A PARANOIA-DROP -j DROP
  160.         iptables -I wanin 1 -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  161.         iptables -I wanout 1 -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  162.         iptables -I INPUT 1 -i $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  163.         iptables -I OUTPUT 1 -o $wanif -m state --state NEW -j PARANOIA-DROP 2> /dev/null
  164. }
  165.  
  166. punblock() {
  167.         while iptables -L wanin 2> /dev/null | grep "PARANOIA-DROP"  > /dev/null 2>&1
  168.         do
  169.                 iptables -D wanin -i $wanif -m state --state NEW -j PARANOIA-DROP > /dev/null 2>&1
  170.         done
  171.         while iptables -L wanout 2> /dev/null | grep "PARANOIA-DROP" > /dev/null 2>&1
  172.         do
  173.                 iptables -D wanout -o $wanif -m state --state NEW -j PARANOIA-DROP > /dev/null 2>&1
  174.         done
  175.         while iptables -L OUTPUT 2> /dev/null | grep "PARANOIA-DROP" > /dev/null 2>&1
  176.         do
  177.                 iptables -D OUTPUT -o $wanif -m state --state NEW -j PARANOIA-DROP > /dev/null 2>&1
  178.         done
  179.         while iptables -L INPUT 2> /dev/null | grep "PARANOIA-DROP" > /dev/null 2>&1
  180.         do
  181.                 iptables -D INPUT -i $wanif -m state --state NEW -j PARANOIA-DROP > /dev/null 2>&1
  182.         done
  183.         iptables -F PARANOIA-DROP 2> /dev/null && plog "P2PArtisan: Removing paranoia block"
  184.         iptables -X PARANOIA-DROP 2> /dev/null
  185. }
  186.  
  187. pforcestop() {
  188.     echo -e "
  189. +------------------------- P2Partisan --------------------------+
  190. |                   _______ __              
  191. |                  |     __|  |_.-----.-----.
  192. |                  |__     |   _|  _  |  _  |
  193. |            Hard  |_______|____|_____|   __|
  194. |                                     |__|  
  195. |                                            
  196. +---------------------------------------------------------------+"
  197. counter=0
  198.         while iptables -L wanin 2> /dev/null | grep P2PARTISAN-IN > /dev/null 2>&1
  199.         do
  200.                 iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN > /dev/null 2>&1
  201.         done
  202.         while iptables -L wanout 2> /dev/null | grep P2PARTISAN-OUT > /dev/null 2>&1
  203.         do
  204.                 iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT > /dev/null 2>&1
  205.         done
  206.         while iptables -L INPUT | grep P2PARTISAN-IN > /dev/null 2>&1
  207.         do
  208.                 iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN > /dev/null 2>&1
  209.         done
  210.         while iptables -L OUTPUT | grep P2PARTISAN-OUT > /dev/null 2>&1
  211.         do
  212.                 iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT > /dev/null 2>&1
  213.         done
  214.         iptables -F P2PARTISAN-DROP-IN > /dev/null 2>&1
  215.         iptables -F P2PARTISAN-DROP-OUT > /dev/null 2>&1
  216.         iptables -F P2PARTISAN-LISTS-IN > /dev/null 2>&1
  217.         iptables -F P2PARTISAN-LISTS-OUT > /dev/null 2>&1      
  218.         iptables -F P2PARTISAN-IN > /dev/null 2>&1
  219.         iptables -F P2PARTISAN-OUT > /dev/null 2>&1
  220.         iptables -X P2PARTISAN-DROP-IN > /dev/null 2>&1    
  221.         iptables -X P2PARTISAN-DROP-OUT > /dev/null 2>&1
  222.         iptables -X P2PARTISAN-LISTS-IN > /dev/null 2>&1    
  223.         iptables -X P2PARTISAN-LISTS-OUT > /dev/null 2>&1
  224.         iptables -X P2PARTISAN-IN > /dev/null 2>&1
  225.         iptables -X P2PARTISAN-OUT > /dev/null 2>&1
  226.         ipset -F  > /dev/null 2>&1
  227.         for i in `ipset --list | grep Name | cut -f2 -d ":" `; do
  228.                 ipset -X $i > /dev/null 2>&1
  229.         done
  230.         chmod 777 ./*.gz > /dev/null 2>&1
  231.     [ -f iptables-add ] && rm iptables-add
  232.     [ -f iptables-del ] && rm iptables-del
  233.     [ -f ipset-del ] && rm ipset-del
  234.     [ -f $pidfile ] && rm -f "$pidfile" > /dev/null 2>&1
  235.     [ -f runtime ] && rm -f "runtime" > /dev/null 2>&1     
  236.     [ -f /tmp/p2partisan.loading ] && rm -r "status.loading" >/dev/null 2>&1
  237. plog " Unloading ipset modules"
  238.         lsmod | grep "ipt_set" > /dev/null 2>&1 && sleep 2 ; rmmod -f ipt_set > /dev/null 2>&1
  239.         lsmod | grep "ip_set_iptreemap" > /dev/null 2>&1 && sleep 2 ; rmmod -f ip_set_iptreemap > /dev/null 2>&1
  240.         lsmod | grep "ip_set" > /dev/null 2>&1 && sleep 2 ; rmmod -f ip_set > /dev/null 2>&1
  241. plog " Removing the list files"
  242.         cat blacklists |  grep -v "^#" | grep -v "^$" | tr -d "\r" |
  243.    (
  244.     while read line
  245.     do
  246.             counter=`expr $counter + 1`
  247.             name=`echo $line | awk '{print $1}'`
  248.             echo "| Removing blacklist #$counter --> ***$name***"
  249.                   [ -f ./$name.gz ] && rm -f ./$name.gz > /dev/null 2>&1
  250.                         done
  251.     )
  252. plog " P2Partisan stopped."
  253. echo -e "+---------------------------------------------------------------+"
  254. }
  255.  
  256. pstatus() {
  257. counter=0
  258.         running3=`iptables -L INPUT  2> /dev/null | grep P2PARTISAN-IN | wc -l`
  259.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  260.         running5=`nvram get script_fire | grep "p2partisan.sh ]" >/dev/null && echo "\033[1;32mYes\033[0;39m" || echo "\033[1;31mNo\033[0;39m"`
  261.         running6=`cru l | grep P2Partisan-update >/dev/null && echo "\033[1;32mYes\033[0;39m" || echo "\033[1;31mNo\033[0;39m"`
  262.         running7=`tail -200 $logfile | grep Dropped | tail -1 | awk '{printf "| %s %s %s ",$1,$2,$3;for (i=4;i<=NF;i++) if ($i~/(IN|OUT|SRC|DST|PROTO|SPT|DPT)=/) printf "%s ",$i;print ""}'`
  263.        running7a=`tail -200 $logfile | grep Rejected | tail -1 | awk '{printf "| %s %s %s ",$1,$2,$3;for (i=4;i<=NF;i++) if ($i~/(IN|OUT|SRC|DST|PROTO|SPT|DPT)=/) printf "%s ",$i;print ""}'`
  264.         running9=`nvram get script_fire | grep "P2Partisan-tutor" >/dev/null && echo "\033[1;32mYes\033[0;39m" || echo "\033[1;31mNo\033[0;39m"`
  265.         runningA=`cat $logfile | grep "Applying paranoia" | wc -l`
  266.         runningB=`cat $logfile | grep "Stuck on Loading" | wc -l`
  267.         runningC=`cat blacklists | grep -v "^#" | grep -v "^$" | tr -d "\r" | wc -l`
  268.         runningD=`[ -f ./runtime ] && cat ./runtime`
  269.         runningF=`iptables -L P2PARTISAN-DROP-IN 2> /dev/null | grep DEBUG | wc -l`
  270.                 if [ -e ./iptables-add ]; then
  271.             from=`head -1 ./iptables-add 2> /dev/null | awk '{print $2}'`
  272.          runtime=`echo $(( $now - $from ))`
  273.                d=`echo $(( $runtime / 86400 ))`
  274.                h=`echo $((( $runtime / 3600 ) %24 ))`
  275.                m=`echo $((( $runtime / 60 ) %60 ))`
  276.                s=`echo $(( $runtime %60 ))`
  277.         runtime=`printf "$d - %02d:%02d:%02d\n" $h $m $s`
  278.                 fi
  279.         drop_packet_count_in=`iptables -vL P2PARTISAN-DROP-IN 2> /dev/null | grep " DROP " | awk '{print $1}'`
  280.        drop_packet_count_out=`iptables -vL P2PARTISAN-DROP-OUT 2> /dev/null | grep " REJECT " | awk '{print $1}'`
  281.             if [ -e ./iptables-debug-del ]; then
  282.           dfrom=`[ -f ./iptables-debug ] && head -1 ./iptables-debug 2> /dev/null | awk '{print $2}'`
  283.        druntime=`echo $(( $now - $dfrom ))`
  284.               h=`echo $((( $druntime / 3600 ) %24 ))`
  285.               m=`echo $((( $druntime / 60 ) %60 ))`
  286.               s=`echo $(( $druntime %60 ))`
  287.        druntime=`printf "%02d:%02d:%02d\n" $h $m $s`
  288.        dendtime=`[ -f ./iptables-debug-del ] && head -2 ./iptables-debug-del | tail -n 1 | awk '{print $2}'`
  289.           ttime=`echo $(( $dendtime / 60 ))`
  290.           ttime=`echo $(( $dfrom + $dendtime ))`
  291.         leftime=`echo $(( $ttime - $now ))`
  292.               m=`echo $((( $leftime / 60 ) %60 ))`
  293.               s=`echo $(( $leftime %60 ))`
  294.         leftime=`printf "%02d:%02d:%02d\n" $h $m $s`
  295.         zzztime=`echo $(( $dendtime / 60 ))`
  296.             fi
  297.        
  298.         if [[ $running3 -eq "0" ]] && [[ $running4 -eq "0" ]]; then
  299.                 running8="\033[1;31mNo\033[0;39m"
  300.         elif [[ $running3 -eq "0" ]] && [[ $running4 -eq "1" ]]; then
  301.                 running8="\033[1;35mLoading...\033[0;39m"
  302.         elif [[ $running3 -gt "0" ]] && [[ $running4 -eq "0" ]]; then
  303.                 running8="\033[1;31mNot quite... try to run \"p2partisan.sh update\"\033[0;39m"
  304.         else
  305.                 running8="\033[1;32mYes\033[0;39m"
  306.         fi
  307.        
  308.         if [[ $runningF -eq 1 ]]; then
  309.                 runningF="\033[1;31mOn\033[0;39m IP \033[1;33m`iptables -L P2PARTISAN-DROP-IN  2> /dev/null | grep DEBUG |  awk '{print $5}'` \033[1;33m$f\033[0;39mrunning for \033[1;33m$druntime\033[0;39m /\033[1;33m$zzztime\033[0;39m min (\033[1;33m$leftime\033[0;39m left)"
  310.         else
  311.                 runningF="Off"     
  312.         fi
  313.  
  314. whiteip=`ipset -L whitelist 2> /dev/null | 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])" | wc -l`
  315. 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`
  316. if [[ $whiteextra == "0" ]]; then
  317. whiteextra=" "
  318. else
  319. whiteextra=`echo "/ $whiteextra" LAN IP ref defined`
  320. fi
  321. blackip=`ipset -L blacklist-custom 2> /dev/null | 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])" | wc -l`
  322.      
  323. echo -e "
  324. +------------------------- P2Partisan --------------------------+
  325. |            _______ __          __              
  326. |           |     __|  |_.---.-.|  |_.--.--.-----.
  327. |           |__     |   _|  _  ||   _|  |  |__ --|
  328. |           |_______|____|___._||____|_____|_____|
  329. |  
  330. |             Release version: $version
  331. +---------------------------------------------------------------+
  332. |         Running:  $running8
  333. |         Autorun:  $running5
  334. |       Scheduled:  $running6 / $runningA since device boot
  335. |           Tutor:  $running9 / $runningB since device boot
  336. |        Debugger:  $runningF
  337. +---------------------------------------------------------------+
  338. |          Uptime:  $runtime
  339. |    Startup time:  $runningD seconds
  340. |      Dropped in:  $drop_packet_count_in
  341. |    Rejected out:  $drop_packet_count_out
  342. +---------------------------------------------------------------+
  343. |       Black IPs:  $blackip
  344. |       White IPs:  $whiteip $whiteextra"
  345. transmissionenable=`nvram get bt_enable`
  346. if [ -z $transmissionenable ]; then
  347.     echo "|  TransmissionBT:  Not available"
  348.  elif [ "$transmissionenable" = "0" ]; then
  349.     echo "|  TransmissionBT:  Off"
  350.  elif [ "$transmissionenable" = "1" ]; then
  351.     echo "|  TransmissionBT:  On"
  352.     transmissionport=`nvram get bt_port 2> /dev/null`
  353.     greyports_tcp=$greyports_tcp,$transmissionport
  354.     greyports_udp=$greyports_udp,$transmissionport
  355. fi
  356. echo $greyports_tcp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  357.             echo "|  Grey ports TCP:  $w"
  358.         done
  359. echo $greyports_udp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  360.             echo "|  Grey ports UDP:  $w"
  361.         done
  362. echo $whiteports_tcp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  363.             echo "| White ports TCP:  $w"
  364.         done
  365. echo $whiteports_udp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  366.             echo "| White ports UDP:  $w"
  367.         done
  368.             echo "|     Black lists:  $runningC"       
  369.        
  370. echo "+------------------------- Logs --------------------------------+      
  371. | Remember your max logs per hour is set to: $maxloghour
  372. $running7
  373. $running7a
  374. +---------------------------------------------------------------+"
  375. }
  376.  
  377.  
  378. if [ $autorun_availability_check = 1 ]; then
  379. av="while true; do [ -f $P2Partisandir/p2partisan.sh ] && break || sleep 5; done ;"
  380. fi
  381.  
  382. pautorunset() {
  383. echo -e "
  384. +------------------------- P2Partisan --------------------------+
  385. |            ______               __               __
  386. |           |      |.-----.-----.|  |_.----.-----.|  |
  387. |           |   ---||  _  |     ||   _|   _|  _  ||  |
  388. |           |______||_____|__|__||____|__| |_____||__|
  389. |                                                  
  390. +--------------------------- Autorun ---------------------------+"
  391.         p=`nvram get script_fire | grep "p2partisan.sh restart" | grep -v cru | wc -l`
  392.         if [ $p -eq "0" ] ; then
  393.                 t=`nvram get script_fire`; t=`printf "$t\n$av$P2Partisandir/p2partisan.sh restart\n"` ; nvram set "script_fire=$t"
  394.         fi
  395.         plog "P2Partisan AUTO RUN is ON"
  396.         echo "+---------------------------------------------------------------+"
  397.         nvram commit
  398. }
  399.  
  400. pautorununset() {
  401. echo -e "
  402. +------------------------- P2Partisan --------------------------+
  403. |            ______               __               __
  404. |           |      |.-----.-----.|  |_.----.-----.|  |
  405. |           |   ---||  _  |     ||   _|   _|  _  ||  |
  406. |           |______||_____|__|__||____|__| |_____||__|
  407. |                                                  
  408. +--------------------------- Autorun ---------------------------+"
  409.         p=`nvram get script_fire | grep "p2partisan.sh restart" | grep -v cru | wc -l`
  410.         if [ $p -eq "1" ]; then
  411.         t=`nvram get script_fire`; t=`printf "$t" | grep -v "p2partisan.sh restart"` ; nvram set "script_fire=$t"
  412.         fi
  413.         plog "P2Partisan AUTO RUN is OFF"
  414.         echo "+---------------------------------------------------------------+"
  415.         nvram commit
  416. }
  417.  
  418. pscheduleset() {
  419. echo -e "
  420. +------------------------- P2Partisan --------------------------+
  421. |            ______               __               __
  422. |           |      |.-----.-----.|  |_.----.-----.|  |
  423. |           |   ---||  _  |     ||   _|   _|  _  ||  |
  424. |           |______||_____|__|__||____|__| |_____||__|
  425. |                                                  
  426. +-------------------------- Scheduler --------------------------+"
  427.         cru d P2Partisan-update
  428.         e=`tr -cd 0-5 </dev/urandom | head -c 1`
  429.         f=`tr -cd 0-9 </dev/urandom | head -c 1`
  430.         a=`echo $e$f`
  431.  
  432.         b=`tr -cd 1-5 </dev/urandom | head -c 1`
  433.         c=`tr -cd 0-6 </dev/urandom | head -c 1`
  434.         d=`echo $c | sed -e 's/0/Sundays/' -e 's/1/Mondays/' -e 's/3/Tuesdays/' -e 's/4/Wednesys/' -e 's/5/Thursdays/' -e 's/6/Fridays/' -e 's/7/Saturdays/'`
  435.         scheduleme=`echo "$schedule" | sed s/m/$a/`
  436.         scheduleme=`echo "$scheduleme" | sed s/h/$b/`
  437.         scheduleme=`echo "$scheduleme" | sed s/d/$c/`
  438.         cru a P2Partisan-update "$scheduleme $P2Partisandir/p2partisan.sh paranoia-update"
  439.         pp=`nvram get script_fire | grep "p2partisan.sh paranoia-update" | grep -v cru | wc -l`
  440.         p=`nvram get script_fire | grep "cru a P2Partisan-update" | wc -l`
  441.         if [ $p -eq "0" ] ; then
  442.                 if [ $pp -eq "0" ]; then
  443.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-update \"$scheduleme $P2Partisandir/p2partisan.sh paranoia-update\"\n"` ; nvram set "script_fire=$t"
  444.                 else
  445.                 pautorununset
  446.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-update \"$scheduleme $P2Partisandir/p2partisan.sh paranoia-update\"\n"` ; nvram set "script_fire=$t"
  447.                 pautorunset
  448.                 fi
  449.         fi
  450.         plog "P2Partisan AUTO UPDATE is ON"
  451.         echo "| at $b:$a on $d
  452. +---------------------------------------------------------------+"
  453.         nvram commit
  454. }
  455.  
  456. pscheduleunset() {
  457. echo -e "
  458. +------------------------- P2Partisan --------------------------+
  459. |            ______               __               __
  460. |           |      |.-----.-----.|  |_.----.-----.|  |
  461. |           |   ---||  _  |     ||   _|   _|  _  ||  |
  462. |           |______||_____|__|__||____|__| |_____||__|
  463. |                                                  
  464. +-------------------------- Schedule ---------------------------+"
  465.         cru d P2Partisan-update
  466.         p=`nvram get script_fire | grep "cru a P2Partisan-update" | wc -l`
  467.         if [ $p -eq "1" ] ; then
  468.         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"
  469.         fi
  470.         plog "P2Partisan AUTO UPDATE is OFF"
  471.         echo "+---------------------------------------------------------------+"
  472.         nvram commit
  473. }
  474.  
  475. pdetective() {
  476. echo -e "
  477. +------------------------- P2Partisan --------------------------+
  478. |         __         __               __   __              
  479. |     .--|  |.-----.|  |_.-----.----.|  |_|__|.--.--.-----.
  480. |     |  _  ||  -__||   _|  -__|  __||   _|  ||  |  |  -__|
  481. |     |_____||_____||____|_____|____||____|__| \___/|_____|
  482. |
  483. +---------------------------------------------------------------+"
  484.  
  485. echo "| After an investigation it appears that the following socket/s
  486. | should be considered a greyports candidates. Consider adding the
  487. | ports under greyports_tcp & greyports_udp.
  488. +---------------------------------------------------------------+"
  489. cat /proc/net/ip_conntrack | awk '{for (i=1;i<=NF;i++) if ($i~/(src|dst|sport|dport)=/) printf "%s ",$i;print "\n"}' | grep -vE '^$' | sed s/\ src=/'\n'/ | awk '{print $1" "$3" "$2" "$4}' | sed s/\ dst=/'\n'/ | sed s/sport=//  | sed s/dport=// | grep -E '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)' | grep -v "`nvram get lan_ipaddr`$" | grep -v "`nvram get lan1_ipaddr`$" | awk '/[0-9]/ {cnt[$1" "$2]++}END{for(k in cnt) print cnt[k],k}' | sort -nr | while read socket; do echo $socket | if [ `cut -f1 -d" "` -gt $greyline ];then echo $socket | awk '{print "| "$2" "$3" - "$1" Sessions"}'; fi ; done
  490. echo "+---------------------------------------------------------------+"
  491. }
  492.  
  493. pupgrade() {
  494.         [ -f p2partisan_new.sh ] && rm -f "p2partisan_new.sh" 2> /dev/null
  495.         wget -q -O - http://pastebin.com/raw.php?i=eDgM0S5i | grep "p2partisan v" | grep -v grep> ./latest
  496.         latest=`cat ./latest | cut -c3-31`
  497.         current=`cat ./p2partisan.sh | grep "p2partisan v" | head -1 | cut -c3-32 `
  498.         if [[ "$latest" == "$current" ]]; then
  499.         echo "
  500. +------------------------- P2Partisan --------------------------+
  501. |          _______                            __        
  502. |         |   |   |.-----.-----.----.---.-.--|  |.-----.
  503. |         |   |   ||  _  |  _  |   _|  _  |  _  ||  -__|
  504. |         |_______||   __|___  |__| |___._|_____||_____|
  505. |                  |__|  |_____|                        
  506. |
  507. +---------------------------------------------------------------+
  508. You're already running the latest version of P2Partisan
  509. "
  510.         else
  511.         echo "
  512. +------------------------- P2Partisan --------------------------+
  513. |          _______                            __        
  514. |         |   |   |.-----.-----.----.---.-.--|  |.-----.
  515. |         |   |   ||  _  |  _  |   _|  _  |  _  ||  -__|
  516. |         |_______||   __|___  |__| |___._|_____||_____|
  517. |                  |__|  |_____|                        
  518. |
  519. +---------------------------------------------------------------+
  520. | There's a new P2Partisan update available. Do you want to upgrade?
  521. |  
  522. |                  current = $current
  523. |  
  524. |                          to
  525. |                      
  526. |                   latest = $latest
  527. |
  528. | y/n"
  529.         read answer
  530.                 if [[ $answer == "y" ]]; then
  531. wget -q -O ./p2partisan_new.sh http://pastebin.com/raw.php?i=eDgM0S5i
  532. pupgraderoutine
  533.                 else
  534.                 echo "| Upgrade skipped. Quitting...
  535. +---------------------------------------------------------------+"
  536.                 exit
  537.                 fi
  538.        
  539.         fi
  540.  }
  541.  
  542. pupgradebeta() {
  543.         [ -f p2partisan_new.sh ] && rm -f "p2partisan_new.sh" 2> /dev/null
  544.         wget -q -O - http://pastebin.com/raw.php?i=qe79uDJi | grep "p2partisan v" | grep -v grep > ./latest
  545.         echo "| Do you want to install the latest testing beta (not suggested)?
  546. |
  547. | y/n"
  548.         read answer
  549.                 if [[ $answer == "y" ]]; then
  550. wget -q -O ./p2partisan_new.sh http://pastebin.com/raw.php?i=qe79uDJi
  551. pupgraderoutine
  552.                 else
  553.                 echo "| Beta upgrade skipped. Quitting...
  554. +---------------------------------------------------------------+"
  555.                
  556.                 exit
  557.                 fi
  558.  }
  559.  
  560. pupgraderoutine() {
  561.                 echo "| Upgrading, please wait:"
  562.                 echo "| 1/6) Stopping the script"
  563.                 pforcestop
  564.                 [ -f p2partisan_new.sh ] || plog "There's a problem with the p2partisan upgrade. Please try again"
  565.                 echo "| 2/6) Migrating the configuration"
  566.                 sed '1,/P2Partisandir/{s@P2Partisandir=.*@'"P2Partisandir=$P2Partisandir"'@'} -i ./p2partisan_new.sh
  567.                 sed '1,/syslogs/{s@syslogs=.*@'"syslogs=$syslogs"'@'} -i ./p2partisan_new.sh
  568.                 sed '1,/maxloghour/{s@maxloghour=.*@'"maxloghour=$maxloghour"'@'} -i ./p2partisan_new.sh
  569.                 sed '1,/whiteports_tcp/{s@whiteports_tcp=.*@'"whiteports_tcp=$whiteports_tcp"'@'} -i ./p2partisan_new.sh
  570.                 sed '1,/whiteports_udp/{s@whiteports_udp=.*@'"whiteports_udp=$whiteports_udp"'@'} -i ./p2partisan_new.sh
  571.                 sed '1,/greyports_tcp/{s@greyports_tcp=.*@'"greyports_tcp=$greyports_tcp"'@'} -i ./p2partisan_new.sh
  572.                 sed '1,/greyports_udp/{s@greyports_udp=.*@'"greyports_udp=$greyports_udp"'@'} -i ./p2partisan_new.sh
  573.                 sed '1,/greyline/{s@greyline=.*@'"greyline=$greyline"'@'} -i ./p2partisan_new.sh
  574.                 sed '1,/fastroutine/{s@fastroutine=.*@'"fastroutine=$fastroutine"'@'} -i ./p2partisan_new.sh
  575.                 sed '1,/autorun_availability_check/{s@autorun_availability_check=.*@'"autorun_availability_check=$autorun_availability_check"'@'} -i ./p2partisan_new.sh
  576.                 sed '1,/schedule/{s@schedule=.*@'"schedule=\"$schedule\""'@'} -i ./p2partisan_new.sh
  577.                 sed '1,/testip/{s@testip=.*@'"testip=$testip"'@'} -i ./p2partisan_new.sh              
  578.                 tr -d "\r"< ./p2partisan_new.sh > ./.temp ; mv ./.temp ./p2partisan_new.sh
  579.                 echo "| 3/6) Copying p2partisan.sh into p2partisan.sh.old"
  580.                 cp ./p2partisan.sh ./p2partisan_old
  581.                 echo "| 4/6) Installing new script into p2partisan.sh"
  582.                 mv ./p2partisan_new.sh ./p2partisan.sh
  583.                 echo "| 5/6) Setting up permissions"
  584.                 chmod -R 777 ./p2partisan.sh
  585.                 echo -e "| 6/6) all done, \033[1;32mPlease run the script manually!\033[0;39m
  586. | NOTE: autorun, autoupdate and tutor settings are left as they were found
  587. +---------------------------------------------------------------+
  588. "
  589. exit
  590. }
  591.  
  592. ptutor() {
  593.         pwhitelist
  594.         pblacklistcustom
  595.         running3=`iptables -L INPUT | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  596.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  597.         runningE=`iptables -L wanin | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  598.         if [[ $runningE -gt "1" ]]; then
  599.                         pforcestop
  600.                         plog "P2Partisan tutor had to restart due to: iptables redundant rules found"
  601.                         pstart
  602.         elif [[ $running3 -eq "1" ]] && [[ $running4 -eq "0" ]]; then
  603.                         plog "P2Partisan tutor had to restart due to: pid file missing"
  604.                         pforcestop
  605.                         pstart
  606.         elif [[ $running3 -eq "0" ]] && [[ $running4 -eq "1" ]]; then
  607.                         plog "P2Partisan tutor had to restart due to: iptables instructions missing"
  608.                         pforcestop
  609.                         pstart     
  610.         elif [[ $running3 -ne "1" ]] && [[ $running4 -eq "1" ]]; then
  611.                         plog "P2Partisan appears to be loading, I'll wait 5 minutes..."    
  612.                         sleep 300
  613.                 if [[ $running3 -ne "1" ]] && [[ $running4 -eq "1" ]]; then
  614.                         plog "P2Partisan tutor had to restart due to Stuck on Loading"
  615.                         pforcestop
  616.                         pstart
  617.                 fi
  618.         else
  619.         echo -e "
  620. +------------------------- P2Partisan --------------------------+
  621. |                _______         __              
  622. |               |_     _|.--.--.|  |_.-----.----.
  623. |                 |   |  |  |  ||   _|  _  |   _|
  624. |                 |___|  |_____||____|_____|__|                                                  
  625. |
  626. +---------------------------------------------------------------+      
  627. | P2Partisan up and running. The tutor is happy
  628. +---------------------------------------------------------------+       "
  629.         fi
  630.  }
  631.  
  632. ptutorset() {
  633.         echo -e "
  634. +------------------------- P2Partisan --------------------------+
  635. |                _______         __              
  636. |               |_     _|.--.--.|  |_.-----.----.
  637. |                 |   |  |  |  ||   _|  _  |   _|
  638. |                 |___|  |_____||____|_____|__|                                                  
  639. |
  640. +-------------------------- Scheduler --------------------------+"
  641.         cru d P2Partisan-tutor
  642.         ab=`tr -cd 0-5 </dev/urandom | head -c 1`
  643.         a=`tr -cd 0-9 </dev/urandom | head -c 1`
  644.         a=`echo $ab$a`
  645.         scheduleme=`echo "$a * * * *"`
  646.         cru a P2Partisan-tutor "$scheduleme $P2Partisandir/p2partisan.sh tutor"
  647.         pp=`nvram get script_fire | grep "p2partisan.sh tutor" | grep -v cru | wc -l`
  648.         p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  649.         if [ $p -eq "0" ] ; then
  650.                 if [ $pp -eq "0" ]; then
  651.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-tutor \"$scheduleme $P2Partisandir/p2partisan.sh tutor\"\n"` ; nvram set "script_fire=$t"
  652.                 else
  653.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-tutor \"$scheduleme $P2Partisandir/p2partisan.sh tutor\"\n"` ; nvram set "script_fire=$t"
  654.                 fi
  655.         fi
  656.         plog "P2Partisan tutor is ON"
  657.         echo "+---------------------------------------------------------------+"
  658.         nvram commit
  659. }
  660.  
  661. ptutorunset() {
  662.         echo -e "
  663. +------------------------- P2Partisan --------------------------+
  664. |                _______         __              
  665. |               |_     _|.--.--.|  |_.-----.----.
  666. |                 |   |  |  |  ||   _|  _  |   _|
  667. |                 |___|  |_____||____|_____|__|                                                  
  668. |
  669. +-------------------------- Scheduler --------------------------+"
  670.         cru d P2Partisan-tutor
  671.         p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  672.         if [ $p -eq "1" ] ; then
  673.         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"
  674.         fi
  675.         plog "P2Partisan tutor is OFF"
  676.         echo "+---------------------------------------------------------------+"
  677.         nvram commit
  678.  }
  679.  
  680.  ptest() {
  681. checklist="blacklist-custom whitelist `cat blacklists | grep -v "^#" | grep -v "^$" | tr -d "\r" | awk '{print $1}'`"
  682. echo -e "
  683. +------------------------- P2Partisan --------------------------+
  684. |                  _______               __  
  685. |                 |_     _|.-----.-----.|  |_
  686. |                   |   |  |  -__|__ --||   _|
  687. |                   |___|  |_____|_____||____|
  688. |                                              
  689. +----------- Lists are sorted in order of precedence -----------+"
  690.     if [[ -z $1 ]]; then
  691. echo "+---------------------------------------------------------------+
  692. | Invalid input. Please specify a valid IP address.
  693. +---------------------------------------------------------------+" 
  694.     else
  695.     test=$1
  696.     echo $test | grep -E "(^[2][5][0-5].|^[2][0-4][0-9].|^[1][0-9][0-9].|^[0-9][0-9].|^[0-9].)([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5]|[2][0-4][0-9]|[1][0-9][0-9]|[0-9][0-9]|[0-9])$" >/dev/null 2>&1 && test=1 || test=0
  697.     if [[ $test -eq 1 ]]; then
  698.     echo $checklist | tr " " "\n" |
  699.     (
  700.                 while read LIST
  701.                 do
  702.                 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"
  703.         done                                                                                                                                    
  704.     )
  705.     echo "+---------------------------------------------------------------+
  706. |        in case of multiple match the first prevails
  707. +---------------------------------------------------------------+"
  708.     elif [[ $test -eq 0 ]]; then
  709.     echo "| Invalid input. Please specify a valid IP address.
  710. +---------------------------------------------------------------+" 
  711.     fi
  712. fi
  713. }
  714.  
  715. pdebug() {
  716. echo -e "
  717. +------------------------- P2Partisan --------------------------+
  718. |                _____         __                
  719. |               |     \.-----.|  |--.--.--.-----.
  720. |               |  --  |  -__||  _  |  |  |  _  |
  721. |               |_____/|_____||_____|_____|___  |
  722. |                                         |_____|
  723. |                                              
  724. +--------------------------- Guide -----------------------------+
  725. | Debug allows to fully log the P2Partisan interventions given a LAN IP
  726. | Maximum 1 debug at the time / Debug automatically times out or can be forced off manually
  727. +---------------------------------------------------------------+
  728. | p2partisan.sh debug <LAN IP> <minutes>    Syntax
  729. | p2partisan.sh debug                       Displays debug status and this help text
  730. | p2partisan.sh debug 192.168.0.3 <1-60>    Enables debug for the given LAN IP for N min (15 def)
  731. | p2partisan.sh debug 192.168.0.3 9         Enables debug for the given LAN IP for 9 min
  732. | p2partisan.sh debug off                   Disable debug without waiting for the timer
  733. | p2partisan.sh debug-display <in|out>      Syntax
  734. | p2partisan.sh debug-display               Displays in&out debug logs + guide
  735. | p2partisan.sh debug-display out           Same as above but displays outbound records only
  736. +-------------------------- Activity ---------------------------+"
  737. echo "$1" | grep -Eo "([2][5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5]|[2][0-4][0-9]|[1][0-9][0-9]|[0-9][0-9]|[0-9])" >/dev/null 2>&1 && q=0 || q=1
  738. echo "$1" | grep "off" >/dev/null 2>&1 && off=1 || off=0
  739.  
  740.             if [ -e ./iptables-debug-del ]; then
  741.           dfrom=`head -1 ./iptables-debug 2> /dev/null | awk '{print $2}'`
  742.        druntime=`echo $(( $now - $dfrom ))`
  743.               h=`echo $((( $druntime / 3600 ) %24 ))`
  744.               m=`echo $((( $druntime / 60 ) %60 ))`
  745.               s=`echo $(( $druntime %60 ))`
  746.        druntime=`printf "%02d:%02d:%02d\n" $h $m $s`
  747.        dendtime=`head -2 ./iptables-debug-del | tail -n 1 | awk '{print $2}'`
  748.           ttime=`echo $(( $dendtime / 60 ))`
  749.           ttime=`echo $(( $dfrom + $dendtime ))`
  750.         leftime=`echo $(( $ttime - $now ))`
  751.               m=`echo $((( $leftime / 60 ) %60 ))`
  752.               s=`echo $(( $leftime %60 ))`
  753.         leftime=`printf "%02d:%02d:%02d\n" $h $m $s`
  754.         zzztime=`echo $(( $dendtime / 60 ))`
  755.             fi
  756.            
  757.  
  758.     if [[ $off -eq 1 ]]; then
  759.     f=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG `
  760.     fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  761.         if [[ $fc -ge 1 ]]; then
  762.         kill `ps | grep -E "sleep $dendtime$" | awk '{print $1}'`
  763.         plog "| All DEBUG activities have stopped"
  764.         while iptables -L P2PARTISAN-DROP-IN | grep DEBUG > /dev/null 2>&1
  765.         do
  766.                 iptables -D P2PARTISAN-DROP-IN 1  > /dev/null 2>&1
  767.         done
  768.         while iptables -L P2PARTISAN-DROP-OUT | grep DEBUG > /dev/null 2>&1
  769.         do
  770.                 iptables -D P2PARTISAN-DROP-OUT 1  > /dev/null 2>&1
  771.         done
  772.         echo -e "| Use \033[1;33m./p2partisan.sh debug-display\033[0;39m to show debug information, if any.
  773. +---------------------------------------------------------------+" ; exit
  774.         else
  775.             echo -e "| Debug is currently off and not collecting any information.
  776. | Use \033[1;33m./p2partisan.sh debug-display\033[0;39m to show existing debug information, if any.
  777. +---------------------------------------------------------------+" ; exit  
  778.         fi
  779.     fi
  780.  
  781.     if [[ -z $1 ]]; then   
  782.         f=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | awk '{print $5}'`
  783.         fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  784.         if [[ $fc -ge 1 ]]; then
  785. echo -e "| P2partisan is currently debugging IP \033[1;33m$f\033[0;39m for \033[1;33m$druntime\033[0;39m /\033[1;33m$zzztime\033[0;39m min (\033[1;33m$leftime\033[0;39m left)
  786. | Use \033[1;33m./p2partisan.sh debug-display\033[0;39m to show debug information
  787. +---------------------------------------------------------------+" ; exit
  788.         elif [[ $fc -eq 0 ]]; then
  789.             echo -e "| Debug is currently off and not collecting any information.
  790. | Use \033[1;33m./p2partisan.sh debug-display\033[0;39m to show existing debug information, if any.
  791. +---------------------------------------------------------------+" ; exit
  792.         fi
  793.     elif [[ $q -ge 1 ]]; then
  794.             echo -e "| The input \033[1;31m$1\033[0;39m doesn't appear to be a valid IP
  795. +---------------------------------------------------------------+" ; exit
  796.     fi
  797.  
  798.     f=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | awk '{print $5}'`
  799.     fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  800.     if [[ $fc -ge 1 ]]; then
  801. echo -e "| P2partisan is currently debugging IP \033[1;33m$f\033[0;39m for \033[1;33m$druntime\033[0;39m /\033[1;33m$zzztime\033[0;39m min (\033[1;33m$leftime\033[0;39m left)
  802. | NOTE: Only one debug at the time is possible! Command ignored.
  803. | Use \033[1;33m./p2partisan.sh debug-display\033[0;39m to show the debug information
  804. +---------------------------------------------------------------+" ; exit
  805.     fi
  806.  
  807. if [ -z $2 ]; then
  808.         minutes=15
  809.         time=900
  810. elif [[ $2 -gt 60 ]] || [[ $2 -eq 0 ]]; then
  811.         echo "| Please specify an acceptable time: 1 to 60 (min). If omitted 15 will be used
  812. | Debug NOT enabled. Exiting...
  813. +---------------------------------------------------------------+" ; exit
  814. else
  815.         minutes=$2
  816.         time=` echo $(( $2 * 60 ))`
  817. fi
  818. echo "# $now
  819. iptables -I P2PARTISAN-DROP-IN 1 -d $1 -j LOG --log-prefix \"P2Partisan-DEBUG-IN->> \" --log-level 1 > /dev/null 2>&1
  820. iptables -I P2PARTISAN-DROP-OUT 1 -s $1 -j LOG --log-prefix \"P2Partisan-DEBUG-OUT->> \" --log-level 1 > /dev/null 2>&1" > ./iptables-debug
  821. chmod 777 ./iptables-debug  > /dev/null 2>&1
  822. plog "Debug started for IP $1 for $minutes minute"
  823. ./iptables-debug 1>/dev/null &
  824.         echo -e "| Enabled full debug logging for LAN IP \033[1;32m$1\033[0;39m for \033[1;32m$minutes\033[0;39m minutes
  825. | Use \033[1;33m./p2partisan.sh debug-display\033[0;39m to show the debug information
  826. +---------------------------------------------------------------+"
  827.  
  828. echo "# $now
  829. sleep $time
  830. iptables -D P2PARTISAN-DROP-IN -d $1 -j LOG --log-prefix \"P2Partisan-DEBUG-IN->> \" --log-level 1  > /dev/null 2>&1
  831. iptables -D P2PARTISAN-DROP-OUT -s $1 -j LOG --log-prefix \"P2Partisan-DEBUG-OUT->> \" --log-level 1 > /dev/null 2>&1" > ./iptables-debug-del
  832. chmod 777 ./iptables-debug-del 2> /dev/null
  833. ./iptables-debug-del 1>/dev/null &
  834. }
  835.  
  836. pdebugdisplay() {
  837. echo -e "
  838. +------------------------- P2Partisan --------------------------+
  839. _____         __                          __ __               __              
  840. |     \.-----.|  |--.--.--.-----.______.--|  |__|.-----.-----.|  |.---.-.--.--.
  841. |  --  |  -__||  _  |  |  |  _  |______|  _  |  ||__ --|  _  ||  ||  _  |  |  |
  842. |_____/|_____||_____|_____|___  |      |_____|__||_____|   __||__||___._|___  |
  843.                          |_____|                      |__|             |_____|
  844.  
  845. +---------------------------------------------------------------+
  846. | p2partisan.sh debug-display               Displays in & outbound debug logs
  847. | p2partisan.sh debug-display in            Displays inbound debug logs only
  848. | p2partisan.sh debug-display out           Displays outbound debug logs only
  849. +-------------------------- Drop Logs --------------------------+"
  850.  
  851.    dfrom=`head -1 ./iptables-debug 2> /dev/null | awk '{print $2}'`
  852.      now=`date +%s`
  853. druntime=`echo $(( $now - $dfrom ))`
  854.       h=`echo $((( $druntime / 3600 ) %24 ))`
  855.       m=`echo $((( $druntime / 60 ) %60 ))`
  856.       s=`echo $(( $druntime %60 ))`
  857. druntime=`printf "%02d:%02d:%02d\n" $h $m $s`
  858. dendtime=`head -2 ./iptables-debug-del | tail -n 1 | awk '{print $2}'`
  859.   ttime=`echo $(( $dendtime / 60 ))`
  860.   ttime=`echo $(( $dfrom + $dendtime ))`
  861. leftime=`echo $(( $ttime - $now ))`
  862.       m=`echo $((( $leftime / 60 ) %60 ))`
  863.       s=`echo $(( $leftime %60 ))`
  864. leftime=`printf "%02d:%02d:%02d\n" $h $m $s`
  865. zzztime=`echo $(( $dendtime / 60 ))`
  866.  
  867. c=0
  868. rm ./debug.rev  > /dev/null 2>&1
  869. tail -800 $logfile | grep -i "P2Partisan" > ./debug.log
  870. cat ./debug.log | sed '1!G;h;$!d' |
  871.  (  
  872.  while read line
  873.  do
  874. testo=`echo $line | grep "Debug started for IP" | wc -l`
  875. if [[ $testo -ge 1 ]]; then
  876.     echo $line >> ./debug.rev
  877.     cat ./debug.rev | sed '1!G;h;$!d' > ./debug.log
  878.     rm ./debug.rev  > /dev/null 2>&1
  879.     exit
  880. else
  881.     echo $line >> ./debug.rev
  882. fi
  883. done
  884. )
  885.  
  886. if [ -z $1 ]; then
  887.     echo -e "\033[48;5;60m+----------------------- INPUT & OUTPUT ------------------------+\033[0m"
  888.     head -1 ./debug.log
  889.     cat ./debug.log | grep "DEBUG" | awk '{printf "%s %s %s ",$1,$2,$3;for (i=4;i<=NF;i++) if ($i~/(IN|OUT|SRC|DST|PROTO|SPT|DPT)=/) printf "%s ",$i;print ""}' | while read line; do
  890.       [ $(($c%2)) -eq 1 ] && printf "\033[48;5;60m"
  891.       printf "%s\033[0m\n" "$line"
  892.       c=$(($c+1))
  893.         done
  894.     fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  895.         if [[ $fc -ge 1 ]]; then
  896.         echo -e "\e[93mNOTE: debugging is active for $druntime /$zzztime min ($leftime left). Run this command again to update the report\033[0m"
  897.         fi
  898.     echo -e "\033[48;5;60m+----------------------- INPUT & OUTPUT ------------------------+\033[0m"
  899. elif [[ $1 = "in" ]]; then
  900.     echo -e "\033[48;5;60m+--------------------------- INPUT -----------------------------+\033[0m"
  901.     head -1 ./debug.log
  902.     cat ./debug.log | grep "DEBUG-IN" | awk '{printf "%s %s %s ",$1,$2,$3;for (i=4;i<=NF;i++) if ($i~/(IN|OUT|SRC|DST|PROTO|SPT|DPT)=/) printf "%s ",$i;print ""}' | while read line; do
  903.       [ $(($c%2)) -eq 1 ] && printf "\033[48;5;60m"
  904.       printf "%s\033[0m\n" "$line"
  905.       c=$(($c+1))
  906.         done
  907.     fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  908.         if [[ $fc -ge 1 ]]; then
  909.         echo -e "\e[93mNOTE: debugging is active for $druntime /$zzztime min ($leftime left). Run this command again to update the report\033[0m"
  910.         fi
  911.     echo -e "\033[48;5;60m+--------------------------- INPUT -----------------------------+\033[0m"
  912. elif [[ $1 = "out" ]]; then
  913.         echo -e "\033[48;5;60m+--------------------------- OUTPUT ----------------------------+\033[0m"
  914.         head -1 ./debug.log
  915.     cat ./debug.log | grep "DEBUG-OUT" | awk '{printf "%s %s %s ",$1,$2,$3;for (i=4;i<=NF;i++) if ($i~/(IN|OUT|SRC|DST|PROTO|SPT|DPT)=/) printf "%s ",$i;print ""}' | while read line; do
  916.       [ $(($c%2)) -eq 1 ] && printf "\033[48;5;60m"
  917.       printf "%s\033[0m\n" "$line"
  918.       c=$(($c+1))
  919.         done
  920.     fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  921.         if [[ $fc -ge 1 ]]; then
  922.         echo -e "\e[93mNOTE: debugging is active for $druntime /$zzztime min ($leftime left). Run this command again to update the report\033[0m"
  923.         fi
  924.         echo -e "\033[48;5;60m+--------------------------- OUTPUT ----------------------------+\033[0m"
  925. fi
  926.     echo -e "+---------------------------------------------------------------+"
  927. }
  928.  
  929.  
  930. pwhitelist() {
  931.     ipset -F whitelist
  932.     [ -f ./whitelist ] && cat ./whitelist | grep -v "^#" | grep -v "^$" | tr -d "\r" |
  933.     (
  934.     while read IP
  935.     do
  936.         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
  937.         echo "$IP" | grep -Eo "([2][5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5]|[2][0-4][0-9]|[1][0-9][0-9]|[0-9][0-9]|[0-9])" >/dev/null 2>&1 && q=0
  938.  
  939.     if [[ $q -eq 0 ]]; then
  940.                 ipset -A whitelist $IP
  941.     elif [[ $q -eq 1 ]]; then
  942.         nslookup $IP | grep "Address [0-9]*:" | grep -v 127.0.0.1 | grep -v "\:\:" | grep -Eo "([0-9\.]{7,15})" |
  943.                 while read IPO
  944.                 do
  945.                 ipset -A whitelist $IPO
  946.                 done
  947.     fi
  948.     done
  949.     )
  950. }
  951.  
  952. pblacklistcustom() {
  953.     ipset -F blacklist-custom
  954.     [ -f ./blacklist-custom ] && cat ./blacklist-custom | grep -v "^#" | grep -v "^$" | tr -d "\r" |
  955.     (
  956.     while read IP
  957.     do
  958.         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
  959.         echo "$IP" | grep -Eo "([2][5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5].|[2][0-4][0-9].|[1][0-9][0-9].|[0-9][0-9].|[0-9].)([2][0-5][0-5]|[2][0-4][0-9]|[1][0-9][0-9]|[0-9][0-9]|[0-9])" >/dev/null 2>&1 && q=0
  960.  
  961.     if [[ $q -eq 0 ]]; then
  962.                 ipset -A blacklist-custom $IP
  963.     elif [[ $q -eq 1 ]]; then
  964.         nslookup $IP | grep "Address [0-9]*:" | grep -v 127.0.0.1 | grep -v "\:\:" | grep -Eo "([0-9\.]{7,15})" |
  965.                 while read IPO
  966.                 do
  967.                 ipset -A blacklist-custom $IPO
  968.                 done
  969.     fi
  970.     done
  971.     )  
  972. }
  973.    
  974. pstart() {
  975.         if [[ $paranoiastuck -gt 1 ]]; then
  976.         punblock
  977.         fi
  978.        
  979.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  980.         if [ $running4 -eq "0" ] ; then
  981.  
  982.         [ -f /tmp/p2partisan.loading ] && echo "P2Partisan is still loading. Exiting..." && exit
  983.         touch /tmp/p2partisan.loading
  984.  
  985.         /bin/ntpsync > /dev/null 2>&1
  986.         pre=`date +%s`
  987.         sleep 1
  988.  
  989.         echo $$ > $pidfile
  990.        
  991.     [ -e iptables-add ] && rm iptables-add
  992.     [ -e iptables-del ] && rm iptables-del
  993.     [ -e ipset-del ] && rm ipset-del
  994.  
  995.     echo -e "
  996. +------------------------- P2Partisan --------------------------+
  997. |                 _______ __               __  
  998. |                |     __|  |_.---.-.----.|  |_
  999. |                |__     |   _|  _  |   _||   _|
  1000. |                |_______|____|___._|__|  |____|
  1001. |                                            
  1002. +---------------------------------------------------------------+
  1003. +--------- PREPARATION --------"
  1004.         echo "| Loading the ipset modules"
  1005.         lsmod | awk '{print $1}' | grep "ip_set" > /dev/null 2>&1 || insmod ip_set
  1006.         lsmod | awk '{print $1}' | grep "ip_set_iptreemap" > /dev/null 2>&1 || insmod ip_set_iptreemap
  1007.         lsmod | awk '{print $1}' | grep "ipt_set" > /dev/null 2>&1 || insmod ipt_set
  1008.  
  1009. counter=0
  1010. pos=1
  1011.  
  1012.                 echo "+---- CUSTOM IP BLACKLIST -----
  1013. | preparing blacklist-custom ..."
  1014.                 echo "| Loading blacklist #$counter --> ***Custom IP blacklist***"
  1015.         if [ "$(ipset --swap blacklist-custom blacklist-custom 2>&1 | grep 'Unknown set')" != "" ]
  1016.             then
  1017.             ipset --create blacklist-custom iptreemap > /dev/null 2>&1
  1018.             fi             
  1019.     pblacklistcustom               
  1020.  
  1021. [ -e /tmp/iptables-add.tmp ] && rm /tmp/iptables-add.tmp > /dev/null 2>&1
  1022. echo "+--------- GREYPORTs ----------"
  1023. echo $greyports_tcp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  1024.             echo "| Loading grey TCP ports  $w"
  1025.             echo "iptables -A P2PARTISAN-IN -i $wanif -p tcp --match multiport --dports $w -g P2PARTISAN-LISTS-IN 2> /dev/null
  1026. iptables -A P2PARTISAN-OUT -o $wanif -p tcp --match multiport --sports $w -g P2PARTISAN-LISTS-OUT 2> /dev/null" >> /tmp/iptables-add.tmp
  1027. done
  1028. echo $greyports_udp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  1029.             echo "| Loading grey UDP ports  $w"
  1030.             echo "iptables -A P2PARTISAN-IN -i $wanif -p udp --match multiport --dports $w -g P2PARTISAN-LISTS-IN 2> /dev/null
  1031. iptables -A P2PARTISAN-OUT -o $wanif -p udp --match multiport --sports $w -g P2PARTISAN-LISTS-OUT 2> /dev/null" >> /tmp/iptables-add.tmp
  1032. done
  1033. # Get transmission port for greylisting if enabled
  1034. transmissionenable=`nvram get bt_enable`
  1035. if [ -z $transmissionenable ]; then
  1036.     echo "|  TransmissionBT:  Not available"
  1037.   elif [ "$transmissionenable" = "0" ]; then
  1038.     echo "|  TransmissionBT:  Off"
  1039.   elif [ "$transmissionenable" = "1" ]; then
  1040.     echo "|  TransmissionBT:  On"
  1041.     transmissionport=`nvram get bt_port 2> /dev/null`
  1042.     wanip=`nvram get wan_ipaddr`
  1043.     p3=`echo $greyports_tcp | grep -Eo '$transmissionport' | wc -l`
  1044.     p4=`echo $greyports_udp | grep -Eo '$transmissionport' | wc -l`
  1045.     if [ $p3 -eq "0" ]; then
  1046.             echo "iptables -A P2PARTISAN-IN -i $wanif -p tcp -d $wanip --dport $transmissionport -g P2PARTISAN-LISTS-IN 2> /dev/null
  1047. iptables -A P2PARTISAN-OUT -o $wanif -p tcp -s $wanip --sport $transmissionport -g P2PARTISAN-LISTS-OUT 2> /dev/null" >> /tmp/iptables-add.tmp
  1048.     fi     
  1049.     if [ $p4 -eq "0" ]; then
  1050.             echo "iptables -A P2PARTISAN-IN -i $wanif -p udp -d $wanip --dport $transmissionport -g P2PARTISAN-LISTS-IN 2> /dev/null
  1051. iptables -A P2PARTISAN-OUT -o $wanif -p udp -s $wanip --sport $transmissionport -g P2PARTISAN-LISTS-OUT 2> /dev/null" >> /tmp/iptables-add.tmp
  1052.     fi
  1053. fi
  1054.        
  1055. echo "+--------- WHITEPORTs ---------"
  1056. echo $whiteports_tcp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  1057.         echo "| Loading white TCP ports $w"
  1058.         echo "iptables -A P2PARTISAN-IN -i $wanif -p tcp --match multiport --sports $w -j RETURN 2> /dev/null
  1059. iptables -A P2PARTISAN-IN -i $wanif -p tcp --match multiport --dports $w -j RETURN 2> /dev/null
  1060. iptables -A P2PARTISAN-OUT -o $wanif -p tcp --match multiport --sports $w -j RETURN 2> /dev/null
  1061. iptables -A P2PARTISAN-OUT -o $wanif -p tcp --match multiport --dports $w -j RETURN 2> /dev/null" >> /tmp/iptables-add.tmp
  1062.         done
  1063. echo $whiteports_udp | awk -v RS=',' -F : '{ gsub(/\n$/, "") } NF > 1 { r=(r ? r "," : "") $0; if (r ~ /([^,]*,){6}/) { print r; r=""; } next } { s=(s ? s "," : "") $0; if (s ~ /([^,]*,){14}/) { print s; s=""; } }  END { if (r && s) { p = r "," s; if (p !~ /([^,:]*[:,]){15}/) { print p; r=s="" } } if (r) print r ; if (s) print s }' | while read w; do
  1064.         echo "| Loading white UDP ports $w"
  1065.         echo "iptables -A P2PARTISAN-IN -i $wanif -p udp --match multiport --sports $w -j RETURN 2> /dev/null
  1066. iptables -A P2PARTISAN-IN -i $wanif -p udp --match multiport --dports $w -j RETURN 2> /dev/null
  1067. iptables -A P2PARTISAN-OUT -o $wanif -p udp --match multiport --sports $w -j RETURN 2> /dev/null
  1068. iptables -A P2PARTISAN-OUT -o $wanif -p udp --match multiport --dports $w -j RETURN 2> /dev/null" >> /tmp/iptables-add.tmp
  1069.         done
  1070. echo "iptables -A P2PARTISAN-IN -j P2PARTISAN-LISTS-IN 2> /dev/null
  1071. iptables -A P2PARTISAN-OUT -j P2PARTISAN-LISTS-OUT 2> /dev/null" >> /tmp/iptables-add.tmp
  1072.        
  1073.                 echo "# $now
  1074. iptables -N P2PARTISAN-IN 2> /dev/null
  1075. iptables -N P2PARTISAN-OUT 2> /dev/null
  1076. iptables -N P2PARTISAN-LISTS-IN 2> /dev/null
  1077. iptables -N P2PARTISAN-LISTS-OUT 2> /dev/null
  1078. iptables -N P2PARTISAN-DROP-IN 2> /dev/null
  1079. iptables -N P2PARTISAN-DROP-OUT 2> /dev/null
  1080. iptables -F P2PARTISAN-IN 2> /dev/null
  1081. iptables -F P2PARTISAN-OUT 2> /dev/null
  1082. iptables -F P2PARTISAN-LISTS-IN 2> /dev/null
  1083. iptables -F P2PARTISAN-LISTS-OUT 2> /dev/null
  1084. iptables -F P2PARTISAN-DROP-IN 2> /dev/null
  1085. iptables -F P2PARTISAN-DROP-OUT 2> /dev/null
  1086. iptables -A P2PARTISAN-IN -m set --set blacklist-custom src -j P2PARTISAN-DROP-IN 2> /dev/null
  1087. iptables -A P2PARTISAN-OUT -m set --set blacklist-custom dst -j P2PARTISAN-DROP-OUT 2> /dev/null" > iptables-add
  1088.  
  1089.  
  1090.                 echo "# $now
  1091. iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN >/dev/null 2>&1
  1092. iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT >/dev/null 2>&1
  1093. iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN >/dev/null 2>&1
  1094. iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT >/dev/null 2>&1
  1095. iptables -F P2PARTISAN-DROP-IN >/dev/null 2>&1
  1096. iptables -F P2PARTISAN-DROP-OUT >/dev/null 2>&1
  1097. iptables -F P2PARTISAN-LISTS-IN >/dev/null 2>&1
  1098. iptables -F P2PARTISAN-LISTS-OUT >/dev/null 2>&1
  1099. iptables -F P2PARTISAN-IN >/dev/null 2>&1
  1100. iptables -F P2PARTISAN-OUT >/dev/null 2>&1
  1101. iptables -X P2PARTISAN-IN  >/dev/null 2>&1
  1102. iptables -X P2PARTISAN-OUT >/dev/null 2>&1
  1103. iptables -X P2PARTISAN-LISTS-IN >/dev/null 2>&1
  1104. iptables -X P2PARTISAN-LISTS-OUT >/dev/null 2>&1
  1105. iptables -X P2PARTISAN-DROP-IN >/dev/null 2>&1
  1106. iptables -X P2PARTISAN-DROP-OUT >/dev/null 2>&1" >> iptables-del
  1107.  
  1108. echo "+--------- WHITE IPs ---------"
  1109. echo "| preparing IP whitelist ..."
  1110. #Load the whitelist
  1111.     if [ "$(ipset --swap whitelist whitelist 2>&1 | grep 'Unknown set')" != "" ]
  1112.         then
  1113.         ipset --create whitelist iptreemap > /dev/null 2>&1
  1114.     fi
  1115.     pwhitelist
  1116.  
  1117.     echo "# $now
  1118. ipset -F
  1119. ipset -X blacklist-custom
  1120. ipset -X whitelist" > ipset-del
  1121.  
  1122.                         echo "| Loading IP whitelist"
  1123.                         echo "iptables -A P2PARTISAN-IN -m set --set whitelist src -j RETURN > /dev/null 2>&1
  1124. iptables -A P2PARTISAN-IN -m set --set whitelist dst -j RETURN > /dev/null 2>&1
  1125. iptables -A P2PARTISAN-OUT -m set --set whitelist src -j RETURN > /dev/null 2>&1
  1126. iptables -A P2PARTISAN-OUT -m set --set whitelist dst -j RETURN > /dev/null 2>&1" >> iptables-add
  1127.  
  1128. cat /tmp/iptables-add.tmp >> iptables-add
  1129. rm /tmp/iptables-add.tmp > /dev/null 2>&1
  1130.  
  1131.                 if [ $syslogs -eq "1" ]; then        
  1132. 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
  1133. 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
  1134.  
  1135.                 fi
  1136.                 echo "iptables -A P2PARTISAN-DROP-IN -j DROP
  1137. iptables -A P2PARTISAN-DROP-OUT -j REJECT --reject-with icmp-admin-prohibited"  >> iptables-add
  1138.  
  1139.  
  1140. echo "+------- IP BLACKLISTs -------"
  1141.        
  1142.         cat blacklists | grep -v "^#" | grep -v "^$" | tr -d "\r" |
  1143.    (
  1144.     while read line
  1145.     do
  1146.             counter=`expr $counter + 1`
  1147.             name=`echo $line | awk '{print $1}'`
  1148.             url=`echo $line | awk '{print $2}'`
  1149.             echo "| loading blacklist #$counter --> ***$name***"
  1150.      
  1151.     if [ $fastroutine -eq "1" ]; then
  1152.      
  1153.      if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  1154.       then
  1155.                   [ -e ./runtime ] && rm -f ./runtime 2> /dev/null
  1156.                   [ -e $name.gz ] || wget -q -O $name.gz "$url"
  1157.                   { echo "-N $name iptreemap"
  1158.                         gunzip -c  $name.gz | \
  1159.                         sed -e "/^[\t ]*#.*\|^[\t ]*$/d;s/^.*:/-A $name /" | \
  1160.                         grep -Ev '(^10\.|(^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.)|^192\.168\.)'
  1161.                         echo COMMIT
  1162.                   } | ipset -R
  1163.      fi
  1164.     else
  1165.      
  1166.                 if [ "$(ipset --swap $name $name 2>&1 | grep 'Unknown set')" != "" ]
  1167.             then
  1168.                         [ -e ./runtime ] && rm -f ./runtime 2> /dev/null
  1169.             ipset --create $name iptreemap
  1170.             [ -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
  1171.             for IP in $(cat $name.lst)
  1172.                     do
  1173.                     ipset -A $name $IP
  1174.                     done
  1175.                         fi
  1176.                          
  1177.         fi
  1178.  
  1179.                                 echo "ipset -X $name " >> ipset-del
  1180.                                 echo "iptables -A P2PARTISAN-LISTS-IN -m set --set $name src -j P2PARTISAN-DROP-IN 2> /dev/null
  1181. iptables -A P2PARTISAN-LISTS-OUT -m set --set $name dst -j P2PARTISAN-DROP-OUT 2> /dev/null" >> iptables-add
  1182.                         done
  1183. echo "iptables -A P2PARTISAN-LISTS-IN -i $wanif -j RETURN 2> /dev/null
  1184. iptables -A P2PARTISAN-LISTS-OUT -o $wanif -j RETURN 2> /dev/null" >> iptables-add
  1185.     )
  1186.  
  1187.  
  1188. echo "iptables -I INPUT $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  1189. iptables -I OUTPUT $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null
  1190. iptables -I wanin $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN 2> /dev/null
  1191. iptables -I wanout $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT 2> /dev/null" >> iptables-add
  1192.  
  1193. chmod 777 ./iptables-*
  1194. chmod 777 ./ipset-*
  1195. ./iptables-add  #protecting
  1196.  
  1197. plog "... P2Partisan started"
  1198. echo "+------------------------- Controls ----------------------------+"
  1199.  
  1200. p=`nvram get dnsmasq_custom | grep log-async | wc -l`
  1201. if [ $p -eq "1" ]; then
  1202.         plog "log-async found under dnsmasq -> OK"
  1203. echo "+---------------------------------------------------------------+"
  1204. else
  1205.         plog "
  1206. | It appears like you don't have a log-async parameter in your dnsmasq
  1207. | config. This is strongly suggested due to the amount of logs involved,
  1208. | especially while debugging to consider adding the following command
  1209. | under Advanced/DHCP/DNS/Dnsmasq Custom configuration:
  1210. |  
  1211. | log-async=20
  1212. |  
  1213. +---------------------------------------------------------------+"
  1214. fi
  1215.  
  1216. punblock  #remove paranoia DROPs if any
  1217.  
  1218.         post=`date +%s`
  1219.         runtime=`echo $(( $post - $pre ))`
  1220.         [ -f ./runtime ] || echo $runtime > ./runtime
  1221.         [ -f /tmp/p2partisan.loading ] && rm -r "/tmp/p2partisan.loading" >/dev/null 2>&1
  1222.         else
  1223.     echo -e "
  1224. +------------------------- P2Partisan --------------------------+
  1225. |                 _______ __               __  
  1226. |                |     __|  |_.---.-.----.|  |_
  1227. |                |__     |   _|  _  |   _||   _|
  1228. |        already |_______|____|___._|__|  |____| ed
  1229. |                                            
  1230. +---------------------------------------------------------------+
  1231. | It appears like P2Partisan is already running. Skipping...
  1232. |                      
  1233. | Is this is not what you expected? Try:
  1234. | \033[1;33m./p2partisan.sh update\033[0;39m
  1235. +---------------------------------------------------------------+
  1236.         "
  1237.         fi
  1238. }
  1239.  
  1240.  
  1241. for p in $1
  1242. do
  1243. case "$p" in
  1244.         "start")
  1245.                 pstart
  1246.                 exit
  1247.                 ;;            
  1248.         "stop")
  1249.                 pforcestop
  1250.                 exit
  1251.                 ;;
  1252.         "restart")
  1253.                 psoftstop
  1254.                 ;;
  1255.         "status")
  1256.                 pstatus
  1257.                 exit                          
  1258.                 ;;
  1259.         "pause")
  1260.                 psoftstop
  1261.                 exit
  1262.                 ;;
  1263.         "detective")       
  1264.                 pdetective
  1265.                 exit
  1266.                 ;;
  1267.         "test")
  1268.                 ptest $2
  1269.                 exit
  1270.                 ;;    
  1271.         "debug")
  1272.                 pdebug $2 $3
  1273.                 exit
  1274.                 ;;  
  1275.         "debug-display")
  1276.                 pdebugdisplay $2
  1277.                 exit
  1278.                 ;;                 
  1279.         "update")
  1280.                 pforcestop
  1281.                 echo "| Now updating..."
  1282.                 ;;
  1283.         "paranoia-update")
  1284.         echo -e "
  1285. +------------------------- P2Partisan --------------------------+
  1286. |     ______                                __        
  1287. |    |   __ \.---.-.----.---.-.-----.-----.|__|.---.-.
  1288. |    |    __/|  _  |   _|  _  |     |  _  ||  ||  _  |
  1289. |    |___|   |___._|__| |___._|__|__|_____||__||___._| update
  1290. |
  1291. +---------------------------------------------------------------+"
  1292.                 pblock
  1293.                 pforcestop
  1294.                 ;;
  1295.         "autorun-on")
  1296.                 pautorunset
  1297.                 exit
  1298.                 ;;
  1299.         "autorun-off")
  1300.                 pautorununset
  1301.                 exit
  1302.                 ;;
  1303.         "autoupdate-on")
  1304.                 pscheduleset
  1305.                 exit
  1306.                 ;;
  1307.         "autoupdate-off")
  1308.                 pscheduleunset
  1309.                 exit
  1310.                 ;;
  1311.         "tutor-on")
  1312.                 ptutorset
  1313.                 exit
  1314.                 ;;
  1315.         "tutor-off")
  1316.                 ptutorunset
  1317.                 exit
  1318.                 ;;
  1319.         "tutor")
  1320.                 ptutor
  1321.                 exit
  1322.                 ;;
  1323.         "upgrade")
  1324.                 pupgrade
  1325.                 ;;
  1326.         "upgrade-silent")
  1327.                 pupgradesilent
  1328.                 ;;
  1329.         "upgrade-beta")
  1330.                 pupgradebeta
  1331.                 ;;                            
  1332.         "help")
  1333.        
  1334. version=`head -3 ./p2partisan.sh | tail -1 | cut -f 3- -d " "`
  1335.        
  1336.                 echo -e "\033[48;5;60m
  1337.       ______ ______ ______              __   __                                        
  1338.      |   __ \__    |   __ \.---.-.----.|  |_|__|.-----.---.-.-----.                    
  1339.      |    __/    __|    __/|  _  |   _||   _|  ||__ --|  _  |     |                    
  1340.      |___|  |______|___|   |___._|__|  |____|__||_____|___._|__|__| $version
  1341.                         \e[39m\e[49m
  1342.              
  1343.        help                    Display this text              
  1344.        \e[97mstart                   Starts the process (this runs also if no option is provided)
  1345.        stop                    Stops P2Partisan
  1346.        restart                 Soft restart, updates whiteports & whitelist only
  1347.        pause                   Soft stop P2Partisan allowing for quick start
  1348.        update                  Hard restart, slow removes p2partisan, updates
  1349.                                the lists and does a fresh start
  1350.        paranoia-update         Like update but blocks any new connection until
  1351.                                P2Partisan is running again\e[39m
  1352.        status                  Display P2Partisan running status + extra info
  1353.        \e[93mtest <IP>               Verify existence of the given IP against lists
  1354.        debug                   Shows a guide on how to operate debug
  1355.        debug-display <in|out>  Shows all the logs relevant to the last debug only
  1356.        detective               Determines highest impact IPs:ports (number of sessions)
  1357.        \e[36mautorun-on              Sets P2Partisan to boot with the router
  1358.        autorun-off             Sets P2Partisan not to boot with the router
  1359.        autoupdate-on           Sets automatic weekly updates to on
  1360.        autoupdate-off          Sets automatic weekly updates to off
  1361.        tutor-on                Sets hourly running-status checks to on
  1362.        tutor-off               Sets hourly running-status checks to off
  1363.        upgrade                 Download and install the latest P2Partisan\e[39m
  1364. "
  1365.                                 exit
  1366.                 ;;
  1367.         *)
  1368.                 echo "parameter not valid. please run:
  1369.                              
  1370.        p2partisan.sh help
  1371.        "
  1372.                                 exit
  1373.                 ;;
  1374.  
  1375. esac
  1376. done
  1377.  
  1378. pstart
  1379.  
  1380. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement