Advertisement
rs232

p2partisan beta 5.x+

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