Advertisement
rs232

p2partisan 5.00

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