Advertisement
rs232

p2partisan 5.09

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