Advertisement
rs232

p2partisan beta

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