rs232

p2partisan latest 6.x+

Jun 28th, 2015
1,506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 89.51 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # p2partisan v6.16 (19/02/2020)
  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,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=80,443,3658,8080
  34. whiteports_udp=53,123,655,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. # Schedule defines the allowed hours when P2Partisan tutor can update lists
  57. # Use the syntax from 0 to 23. e.g. 1,6 allows updates from 1 to 6 am
  58. scheduleupdates="1,6"
  59. #
  60. # Defines how many lists can be loaded concurrently at any given time. Default 2
  61. maxconcurrentlistload=$(ls -d /sys/devices/system/cpu/cpu* | wc -l)
  62. #
  63. # Enable check on script availability to help autorun.
  64. # If the ./partisan.sh is remote wait for the file to be available
  65. # instead of quit with a file missing error
  66. autorun_availability_check=1
  67. # Administration IP. This is used to certify that a list has been fully loaded into IPset.
  68. # Leave this alone, or set it to an Internet IP you definitely don't use.
  69. # Note: modifying this value requires a router reboot to be operational.
  70. adminip="0.0.0.1"
  71. # IP for testing Internet connectivity
  72. testip=google.com
  73. # </CONFIGURATION> ###########################################
  74.  
  75.     ipsetversion=`ipset -V | grep ipset | awk '{print $2}' | cut -c2` #4=old 6=new
  76.     if [ $ipsetversion != 6 ]; then
  77.         echo -e "\033[1;31mipset not compatible with this P2Partisan release.
  78. ipset available: $ipsetversion
  79. ipset supported: 6.x\033[0;40m"
  80.     exit
  81.     fi
  82.  
  83.     # Wait until Internet is available
  84.     while :
  85.     do
  86.             ping -c 1 $testip >/dev/null 2>&1
  87.             if [ $? = 0 ]; then
  88.                     break
  89.             fi
  90.             sleep 5
  91.     done
  92.  
  93. pidfile="/var/run/p2partisan.pid"
  94. logfile=`nvram get log_file_path` || logfile=`/var/log/messages`
  95. cd $P2Partisandir
  96. version=`head -3 ./p2partisan.sh | tail -1 | cut -f 3- -d " "`
  97. alias ipset='/bin/nice -n10 /usr/sbin/ipset'
  98. alias sed='/bin/sed'
  99. alias iptables='/usr/sbin/iptables'
  100. alias service='/sbin/service'
  101. alias killall='/usr/bin/killall'
  102. alias plog='logger -t "| P2PARTISAN" -s'
  103. alias deaggregate='/bin/nice -n10 /tmp/deaggregate.sh'
  104. service ntpc restart >/dev/null
  105. now=`date +%s`
  106. rm=1
  107. wanif=`nvram get wan_ifname` && rm=0 || wanif=`nvram get wan_ifnames`  #RMerlin work around
  108. lanif=`nvram get lan_ifname`
  109. #vpnif=`route | grep -E '^default.*.tun..$|^default.*.ppp.$' | awk '{print $8}'`
  110.  
  111. # DHCP hardcoded patch
  112. p1=`echo $whiteports_udp | grep -Eo '^67[,|:]|[,|:]67[,|:]|,67$' | wc -l`
  113. p2=`echo $whiteports_udp | grep -Eo '^68[,|:]|[,|:]68[,|:]|,68$' | wc -l`
  114. if [ $p1 -eq "0" ]; then
  115.         whiteports_udp=${whiteports_udp},67
  116. fi            
  117. if [ $p2 -eq "0" ]; then
  118.         whiteports_udp=${whiteports_udp},68
  119. fi
  120.  
  121.  
  122.  
  123. [ -f /tmp/deaggregate.sh ] ||
  124. {
  125.  
  126. opens=$(which openssl || which openssl11)
  127.  
  128. b64="$opens enc -base64 -d"
  129. [[ "$(echo WQ==|$b64)" != "Y" ]] && b64="b64"
  130.  
  131. {
  132. cat <<'ENDF'| $b64 | gunzip > /tmp/deaggregate.sh
  133. H4sIAAAAAAACA+1UwU7bQBC971cMxm1tYiexKQWRLhIVbVWpKkg9hlQx9iZZ4awX
  134. r0OiFv69M+slhFCJU6UemkvsmTczb98b7+5O70qqnpkxVohsOq3FNGtEEMIvZhNK
  135. 5gJilfQhW17DGzZZqLyRlQKpU6maQGqCAkyqGoJaNLwfKW50KSkVZZF32fXCaMWT
  136. weo9V4NVpxMCwao6KM1MThoqio7CKBuuRiE2wtdFreiP3bONcapJpaaehF+1Q6lR
  137. pgqKpgcHVC01r9u2GDsKW2IBzt4fbIG9rkedtgoiIjh4RuPDx89fvuHIK9kY3l9N
  138. 3I99+s69YTzyCEOErjIjuFPGT2i8UMU6kFJgOZOlgICQ8J5Tvj2LaYQGDv1HCCBz
  139. gkXglEoiiwpD2OF2VFspJ5tYd5qAqEYQ7CdxW4NVJzQN2qKrWmTXdNB7N7vTcS+6
  140. RrIPetspXs8L9tO2jzsktASgs83NNrlnbwB2aVg1sXtj8lpqq6TK5oL7CSulQYFL
  141. 7qesWjR60XB/n+kac2+ZWWaa+wesrLKC++/YRXqR1Y006J6suX/IhhD/BL+tgxG8
  142. fg2uR9+lqPJJImFZMZcK3faPGDOlQLET1go9hHFpIE6g18x1b6/79fz0DNIT6BXi
  143. tqcWZQl3sMwhLscQT8VDc1ZUrs8BPirBGPowXLOKxQ2kMBpAMxMKRSHNRT6rMHgC
  144. np2kU+2O1fVJFjvYs0tsBHbYwe0zTVXj9zchnv4THXptTS6LmtZ1/lLT+ydHYqJ8
  145. RjfZoGupJi9TXU6J6TnE4PnOUg8HbUo3XaifUuPDJeLxftEQ3wJ4P4bDY6OzXByP
  146. Rnu7l3dP3n3P4XPiVhyjAqmLbNxSGPm1/lxwnwuwaqAxTsFTiG9IR8sZfJdFr9Ya
  147. n0GcbSDckjzmtzps52lTH3L2GTfu8jGb3T7E/+wgpayDcHf3rM52fdn5fDavCjg8
  148. PPzr+9Hf3o/+//14aT/+AScn0l1NsUIT8Ir11j7qyjR8XJBUnVdmzOqFaiTez2Nr
  149. rx8E4BME3aM6CMMxazMOiP5vUXUJGvobY2HylVoIAAA=
  150. ENDF
  151. }
  152. chmod 777 /tmp/deaggregate.sh
  153. [ -f $P2Partisandir/blacklist-custom ] || wget -q -O - https://pastebin.com/raw/2xkwzR1A | tr -d "\r" > $P2Partisandir/blacklist-custom
  154. [ -f $P2Partisandir/blacklists ] || wget -q -O - https://pastebin.com/raw/ARx7NAYz | tr -d "\r" > $P2Partisandir/blacklists
  155. [ -f $P2Partisandir/greylist ] || wget -q -O - https://pastebin.com/raw/Q9NrpXYu | tr -d "\r" > $P2Partisandir/greylist
  156. [ -f $P2Partisandir/whitelist ] || wget -q -O - https://pastebin.com/raw/eb0V3YLp | tr -d "\r" > $P2Partisandir/whitelist
  157. }
  158.  
  159.  
  160. psoftstop() {
  161.                 [ -f /tmp/p2partisan.loading ] && echo "P2Partisan is still loading. Can't stop right now Exiting..." && exit
  162.         echo -e "\033[0;40m
  163. +------------------------- P2Partisan --------------------------+
  164. |                   _______ __              
  165. |                  |     __|  |_.-----.-----.
  166. |                  |__     |   _|  _  |  _  |
  167. |            Soft  |_______|____|_____|   __|
  168. |                                     |__|  
  169. |                                            
  170. +---------------------------------------------------------------+"
  171.         echo -e "| Stopping P2Partisan..."
  172.         ./iptables-del 2> /dev/null
  173.         plog "Stopping P2Partisan..."
  174.         [ -f $pidfile ] && rm -f "$pidfile" 2> /dev/null
  175.         [ -f iptables-add ] && rm -f "iptables-add" 2> /dev/null
  176.         [ -f iptables-del ] && rm -f "iptables-del" 2> /dev/null
  177.         ptutorunset
  178.         echo -e "+---------------------------------------------------------------+ \033[0;39m"
  179. }
  180.  
  181. pforcestop() {
  182. if [ -n "$1" ]; then
  183.     if [ $1 != fix ]; then
  184.     name=$1
  185.         echo -e "\033[0;40m
  186. +------------------------- P2Partisan --------------------------+
  187. |  _____   __         __                         __         __        
  188. | |     |_|__|.-----.|  |_ ______.--.--.-----.--|  |.---.-.|  |_.-----.
  189. | |       |  ||__ --||   _|______|  |  |  _  |  _  ||  _  ||   _|  -__|
  190. | |_______|__||_____||____|      |_____|   __|_____||___._||____|_____|
  191. |                                     |__|                
  192. |                                            
  193. +---------------------------------------------------------------+
  194. |            background updating list: \033[1;35m$1\033[0;40m
  195. +---------------------------------------------------------------+\033[0;39m"
  196.         cat blacklists | grep -Ev "^$" | tr -d "\r" | grep -E "^#( .*|)$name http*." > /dev/null 2>&1 && {
  197.         echo -e "\033[0;40m| Warning: \033[1;33mthe list reference exists but is currently disabled in the blacklists\033[0;40m
  198. +---------------------------------------------------------------+\033[0;39m"
  199.         exit
  200.         }  2> /dev/null
  201.         {
  202.         cat blacklists | grep -Ev "^#|^$" | tr -d "\r" | grep $name > /dev/null 2>&1 || {
  203.         echo -e "\033[0;40m| Error: \033[1;31mit appears like the list $name is not a valid reference.\033[0;40m Typo?
  204. +---------------------------------------------------------------+\033[0;39m"
  205.         exit
  206.         } 2> /dev/null
  207.         }
  208.  
  209. url=`cat blacklists | grep -Ev "^#|^$" | tr -d "\r" | grep $name | awk '{print $2}'`
  210.  
  211.     if [ -n "$url" ]; then
  212.          ps | grep -E ".*deaggregate.sh $name"| grep -v grep | cut -c1-6 | while read line; do kill $line 2> /dev/null; done
  213.          rm "/tmp/p2partisan.$name.LOAD" 2> /dev/null
  214.         if [ "$(ipset --swap "$name.bro" "$name.bro" 2>&1 | grep 'does not exist')" != "" ]
  215.             then
  216.                 ipset -N "$name.bro" hash:net hashsize 1024 --resize 5 maxelem 4096000
  217.             fi
  218.            
  219.                 primarypopulated=`ipset -L $name 2> /dev/null | head -8 | tail -1 | 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]).*" > /dev/null && echo "1" || echo "0"`
  220.                 secondarypopulated=`ipset -T $name.bro $adminip 2> /dev/null && echo "1" || echo "0"`
  221.                 if [ $primarypopulated -eq 0 ]; then
  222.                         if [ $secondarypopulated -eq 1 ]; then
  223.                             {
  224.                             ipset swap $name $name.bro
  225.                             ipset -F $name.bro
  226.                             ipset -X $name.bro
  227.                             ipset -N $name.bro hash:net hashsize 1024 --resize 5 maxelem 4096000
  228. #                           echo 1 [e][o][?]
  229.                             #echo "/tmp/deaggregate.sh "$name.bro" "$url" "$listtype" "-" "$name" "$maxconcurrentlistload" "$P2Partisandir" "$adminip" &"
  230.                             /tmp/deaggregate.sh "$name.bro" "$url" "$listtype" "-" "$name" "$maxconcurrentlistload" "$P2Partisandir" "$adminip" &
  231.                                                                     # 5 = Do not convert but add to ipset and create CIDR (e.g. raw and netset)
  232.                                                                     # 4 = On the fly record by record STOUT output
  233.                                                                     # 3 = add from public whitelist sIP-dIP to ipset only
  234.                                                                     # 2 = add from .cidr to ipset only
  235.                                                                     # 1 = convert + add live + create .cidr file (very slow)
  236.                                                                     # 0 = convert + add live + create ipset dump
  237.                                                                     # different = convert + add to ipset + create .cidr file
  238.                                 } 2> /dev/null
  239.                         elif [ $secondarypopulated -eq 0 ]; then
  240.                             {
  241.                             ipset -F $name
  242.                             ipset -N $name hash:net hashsize 1024 --resize 5 maxelem 4096000
  243. #                           echo 2 [e][e][?]
  244. #                           echo "/tmp/deaggregate.sh "$name" "$url" "$listtype" "-" "$name.bro" "$maxconcurrentlistload" "$P2Partisandir" "$adminip" &"
  245.                             /tmp/deaggregate.sh "$name" "$url" "$listtype" "-" "$name.bro" "$maxconcurrentlistload" "$P2Partisandir" "$adminip" &
  246.                             } 2> /dev/null
  247.                         fi
  248.                     elif [ $primarypopulated -eq 1 ]; then
  249.                         {
  250.                         ipset -F $name.bro
  251.                         ipset -X $name.bro
  252.                         ipset -N $name.bro hash:net hashsize 1024 --resize 5 maxelem 4096000
  253. #                       echo 3 [o][?][?]
  254. #                       echo "/tmp/deaggregate.sh "1: $name.bro" "2: $url" "3: $listtype" "4: " "5: $name" "6: $maxconcurrentlistload" "7: $P2Partisandir" "$adminip" &"
  255.                         /tmp/deaggregate.sh "$name.bro" "$url" "$listtype" "-" "$name" "$maxconcurrentlistload" "$P2Partisandir" "$adminip" &
  256.                         } 2> /dev/null
  257.                     fi
  258.         else
  259.             echo -e "|                    \033[1;31mError: list not found\033[0;40m
  260. +---------------------------------------------------------------+\033[0;39m"
  261.         fi
  262.     exit
  263.     elif [ $1 == "fix" ]; then
  264.         rm ./*.cidr 2> /dev/null
  265.        
  266.     fi
  267. fi
  268.         echo -e "\033[0;40m
  269. +------------------------- P2Partisan --------------------------+
  270. |                   _______ __              
  271. |                  |     __|  |_.-----.-----.
  272. |                  |__     |   _|  _  |  _  |
  273. |            Hard  |_______|____|_____|   __|
  274. |                                     |__|  
  275. |                                            
  276. +---------------------------------------------------------------+"
  277. {
  278.     counter=0
  279.         killall "deaggregate.sh"
  280.         while iptables -L wanin | grep P2PARTISAN-IN
  281.         do
  282.                 iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN
  283.         done
  284.         while iptables -L wanout | grep P2PARTISAN-OUT
  285.         do
  286.                 iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT
  287.         done
  288.         while iptables -L INPUT | grep P2PARTISAN-IN
  289.         do
  290.                 iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN
  291.         done
  292.         while iptables -L OUTPUT | grep P2PARTISAN-OUT
  293.         do
  294.                 iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT
  295.         done
  296.         # iptables -D INPUT -o $vpnif -m state --state NEW -j P2PARTISAN-IN
  297.         # iptables -D OUTPUT -i $vpnif -m state --state NEW -j P2PARTISAN-IN
  298.         # iptables -D FORWARD -o $vpnif -m state --state NEW -j P2PARTISAN-IN
  299.         iptables -F P2PARTISAN-DROP-IN
  300.         iptables -F P2PARTISAN-DROP-OUT
  301.         iptables -F P2PARTISAN-LISTS-IN
  302.         iptables -F P2PARTISAN-LISTS-OUT            
  303.         iptables -F P2PARTISAN-IN
  304.         iptables -F P2PARTISAN-OUT
  305.         iptables -X P2PARTISAN-DROP-IN  
  306.         iptables -X P2PARTISAN-DROP-OUT
  307.         iptables -X P2PARTISAN-LISTS-IN    
  308.         iptables -X P2PARTISAN-LISTS-OUT
  309.         iptables -X P2PARTISAN-IN
  310.         iptables -X P2PARTISAN-OUT
  311.         ipset -F
  312.         for i in `ipset --list | grep Name | cut -f2 -d ":" `; do
  313.                 ipset -X $i
  314.         done
  315.         chmod 777 ./*.gz
  316.         [ -f iptables-add ] && rm iptables-add
  317.         [ -f iptables-del ] && rm iptables-del
  318.         [ -f ipset-del ] && rm ipset-del
  319.         [ -f $pidfile ] && rm -f "$pidfile"
  320.         [ -f runtime ] && rm -f "runtime"
  321.         [ -f /tmp/p2partisan.loading ] && rm -r /tmp/p2partisan.loading
  322. plog " Unloading ipset modules"
  323.         lsmod | grep "xt_set" && sleep 2 ; rmmod -f xt_set
  324.         lsmod | grep "ip_set_hash_net" && sleep 2 ; rmmod -f ip_set_hash_net
  325.         lsmod | grep "ip_set" && sleep 2 ; rmmod -f ip_set
  326. plog " Removing the list files"
  327.         cat blacklists |  grep -Ev "^#|^$" | tr -d "\r" |
  328.    (
  329.     while read line
  330.     do
  331.             counter=`expr $counter + 1`
  332.             counter=$(printf "%02d" $counter)
  333.             name=`echo $line | awk '{print $1}'`
  334.             echo -e "| Removing Blacklist_$counter --> \033[1;37m***$name***\033[0;40m"
  335.                   [ -f ./$name.gz ] && rm -f ./$name.gz
  336.                         done
  337.     )
  338.         rm /tmp/*.LOAD
  339. } > /dev/null 2>&1
  340.         ptutorunset
  341. plog " P2Partisan stopped."
  342. echo -e "+---------------------------------------------------------------+\033[0;39m"
  343. }
  344.  
  345. pstatus() {
  346. if [ -n "$1" ]; then
  347. name=$1
  348.         echo -e "\033[0;40m
  349.        
  350. +------------------------- P2Partisan --------------------------+
  351. |  _____   __         __          _______ __          __              
  352. | |     |_|__|.-----.|  |_ ______|     __|  |_.---.-.|  |_.--.--.-----.
  353. | |       |  ||__ --||   _|______|__     |   _|  _  ||   _|  |  |__ --|
  354. | |_______|__||_____||____|      |_______|____|___._||____|_____|_____|                                                
  355. |                                            
  356. +---------------------------------------------------------------+
  357. |                    list name: \033[1;33m$1\033[0;40m
  358. +---------------------------------------------------------------+"
  359.  
  360. cat blacklists | grep -Ev "^$" | tr -d "\r" | grep -E "^#( .*|)$name http*." > /dev/null 2>&1 && {
  361. echo -e "| Warning: \033[1;33mthe list reference exists but is currently disabled in the blacklists\033[0;40m
  362. +---------------------------------------------------------------+"
  363. exit
  364. }  2> /dev/null
  365. {
  366. cat blacklists | grep -Ev "^#|^$" | tr -d "\r" | grep -o "$name " > /dev/null 2>&1 || {
  367. echo -e "| Error: \033[1;31mit appears like the list $name is not a valid reference.\033[0;40m Typo?
  368. +---------------------------------------------------------------+"
  369. exit
  370. } 2> /dev/null
  371. }
  372.             statusa=`cat /tmp/p2partisan.$name.LOAD 2> /dev/null || echo 5`
  373.             statusb=`cat /tmp/p2partisan.$name.bro.LOAD 2> /dev/null || echo 5`
  374.             statusap=`ps w | grep $name | grep -v grep | wc -l`
  375.             statusbp=`ps w | grep $name.bro | grep -v grep | wc -l`
  376.             statusaa=`ipset -L $name 2> /dev/null | head -8 | tail -1 | 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]).*" > /dev/null && echo "1" || echo "0"`
  377.             statusbb=`ipset -L $name.bro 2> /dev/null | head -8 | tail -1 | 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]).*" > /dev/null && echo "1" || echo "0"`
  378.             statusaaa=`ipset -T $name $adminip 2> /dev/null && echo "1" || echo "0"`
  379.             statusbbb=`ipset -T $name.bro $adminip 2> /dev/null && echo "1" || echo "0"`
  380.             sizeb=`ipset -L $name 2> /dev/null | head -5 | tail -1 | awk '{print $4}' || echo=0`
  381.             sizebb=`ipset -L $name.bro 2> /dev/null | head -5 | tail -1 | awk '{print $4}' || echo=0`
  382.             sizem=`echo $((sizeb/1024))`
  383.             sizemm=`echo $((sizebb/1024))`
  384.             age=`[ -e $name.cidr ] && echo $((\`date +%s\` - \`date -r $name.cidr +%s\`)) || echo 0`
  385.                         if [ $statusaaa -eq 0 ]; then
  386.                             if [ $statusaa -eq 1 ]; then
  387.                                 if [ $statusa -gt 2 ]; then
  388.                                     a="\033[1;33mPartially loaded\033[0;40m"
  389.                                 elif [ $statusa -le 2 ]; then
  390.                                     a="\033[1;35mLoading\033[0;40m"
  391.                                 fi
  392.                             else
  393.                                 if [ $statusap -eq 1 ]; then
  394.                                     a="\033[1;36mQueued\033[0;40m"
  395.                                 else
  396.                                     a="\033[1;31mEmpty\033[0;40m"
  397.                                 fi
  398.                             fi
  399.                         elif [ $statusaaa -eq 1 ]; then
  400.                             a="\033[1;32mFully loaded\033[0;40m"
  401.                         fi
  402.                        
  403.                         if [ $statusbbb -eq 0 ]; then
  404.                             if [ $statusbb -eq 1 ]; then
  405.                                 if [ $statusb -gt 2 ]; then
  406.                                     b="\033[1;37mPartially loaded\033[0;40m"
  407.                                 elif [ $statusb -le 2 ]; then
  408.                                     b="\033[1;35mLoading\033[0;40m"
  409.                                 fi
  410.                             else
  411.                                 if [ $statusbp -eq 1 ]; then
  412.                                     b="\033[1;36mQueued\033[0;40m"
  413.                                 else
  414.                                     b="\033[1;37mEmpty\033[0;40m"
  415.                                 fi
  416.                             fi
  417.                         elif [ $statusbbb -eq 1 ]; then
  418.                             b="\033[1;37mFully loaded\033[0;40m"
  419.                         fi
  420.                        
  421.                         if [ -f ./$name.cidr ]; then
  422.                             cat ./$name.cidr 2>/dev/null | cut -d" " -f3 | grep -F "$adminip" > /dev/null && c="\033[1;37mFully loaded\033[0;40m" || c="\033[1;37mPartially loaded\033[0;40m"
  423.                         else
  424.                             c="\033[1;37mEmpty\033[0;40m"
  425.                         fi
  426.  
  427.                           d=`echo $(( $age / 86400 ))`
  428.                           h=`echo $((( $age / 3600 ) %24 ))`
  429.                           m=`echo $((( $age / 60 ) %60 ))`
  430.                           s=`echo $(( $age %60 ))`
  431.                         age=`printf "$d - %02d:%02d:%02d\n" $h $m $s`
  432.                        ipta=`cat ./iptables-add | grep $name | wc -l`
  433.                        iptb=`iptables -L | grep $name | wc -l`
  434.                         if [ `echo $(( ipta + iptb ))` -eq 4 ]; then d="\033[1;32mFully loaded\033[0;40m";
  435.                         elif [ `echo $(( ipta + iptb ))` -eq 0 ]; then d="\033[1;37mEmpty\033[0;40m";
  436.                         else "\033[1;33mPartially loaded\033[0;40m";
  437.                         fi
  438. echo -e "| Primary lists and iptables are used for filtering, they are both
  439. | expected to be Fully Loaded while P2Partisan operates.
  440. | Secondary lists are used for updates only, so empty when unused
  441. | cidr file are created after a list update and allow quick startup
  442. +---------------------------------------------------------------+
  443. |           Name: $name
  444. |            URL: `cat blacklists | grep -Ev "^#|^$" | tr -d "\r" | grep $name | awk '{print $2}'`
  445. +---------------------------------------------------------------+
  446. |  ipset primary: $a
  447. |          items: `ipset -L $name 2> /dev/null | tail -n +8 | wc -l || echo 0`
  448. |    size in RAM: $sizem KB
  449. +---------------------------------------------------------------+
  450. | ipset seconday: $b
  451. |          items: `ipset -L $name.bro 2> /dev/null | tail -n +8 | wc -l || echo 0`
  452. |    size in RAM: $sizemm KB
  453. +---------------------------------------------------------------+
  454. |      cidr file: $c
  455. |          items: `cat $name.cidr 2> /dev/null | tail -n +2 | wc -l || echo 0`
  456. |   size on disk: `ls -lh $name.cidr 2> /dev/null | awk '{print $5}' || echo 0`
  457. |   Last updated: `date -r $name.cidr '+%H:%M:%S %d/%b/%y' 2> /dev/null` | \033[1;37m$age\033[0;40m ago
  458. +---------------------------------------------------------------+
  459. |       iptables: $d
  460. `cat ./iptables-add | grep $name`
  461. `iptables -L | grep $name`
  462. +---------------------------------------------------------------+\033[0;39m
  463. "
  464.  
  465. exit
  466. fi
  467.  
  468.         counter=0
  469.         running3=`iptables -L | grep -v Chain| grep 'P2PARTISAN-IN\|P2PARTISAN-OUT'  2> /dev/null | wc -l`
  470.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  471.         running5=`nvram get script_fire | grep "p2partisan.sh restart" >/dev/null && echo "\033[1;32mYes\033[0;40m" || echo "\033[1;31mNo\033[0;40m"`
  472.         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'`
  473.        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'`
  474.         running9=`nvram get script_fire | grep "P2Partisan-tutor" >/dev/null && echo "\033[1;32mYes\033[0;40m" || echo "\033[1;31mNo\033[0;40m"`
  475.         logwin=`echo $(( $now - 86400 ))`
  476.         tail -1500 $logfile | grep -i "P2Partisan tutor had" > /tmp/tutor.tmp
  477.          [ -f /tmp/tutor.temp ] && {
  478.          cat /tmp/tutor.tmp |
  479.          (  
  480.          while read line
  481.          do
  482.                 logtime=`echo $line | awk '{print $3}'`
  483.                 if [[ `date -d"$logtime" +%s` -gt $logwin ]]; then
  484.                     echo $line >> /tmp/tutor.temp  
  485.                 fi
  486.         done
  487.         )
  488.             }
  489.         [ -f /tmp/tutor.temp ] && runningB=`wc -l /tmp/tutor.temp 2> /dev/null | awk '{print $1}'`
  490.         [ -f /tmp/tutor.tmp ] && rm /tmp/tutor.tmp; [ -f /tmp/tutor.temp ] && rm /tmp/tutor.temp || runningB=0
  491.         runningD=`[ -f ./runtime ] && cat ./runtime`
  492.         runningF=`iptables -L P2PARTISAN-DROP-IN 2> /dev/null | grep DEBUG | wc -l`
  493.             from=`[ -f ./iptables-add ] && head -1 ./iptables-add 2> /dev/null | awk '{print $2}' || echo $now`
  494.          runtime=`echo $(( $now - $from ))`
  495.                d=`echo $(( $runtime / 86400 ))`
  496.                h=`echo $((( $runtime / 3600 ) %24 ))`
  497.                m=`echo $((( $runtime / 60 ) %60 ))`
  498.                s=`echo $(( $runtime %60 ))`
  499.         runtime=`printf "$d\0d - %02d:%02d:%02d\n" $h $m $s`
  500.         drop_packet_count_in=`iptables -vL P2PARTISAN-DROP-IN 2> /dev/null | grep " DROP " | awk '{print $1}'`
  501.        drop_packet_count_out=`iptables -vL P2PARTISAN-DROP-OUT 2> /dev/null | grep " REJECT " | awk '{print $1}'`
  502.                         if [ -e ./iptables-debug-del ]; then
  503.           dfrom=`[ -f ./iptables-debug ] && head -1 ./iptables-debug 2> /dev/null | awk '{print $2}'`
  504.            druntime=`echo $(( $now - $dfrom ))`
  505.                           h=`echo $((( $druntime / 3600 ) %24 ))`
  506.                           m=`echo $((( $druntime / 60 ) %60 ))`
  507.                           s=`echo $(( $druntime %60 ))`
  508.            druntime=`printf "%02d:%02d:%02d\n" $h $m $s`
  509.            dendtime=`[ -f ./iptables-debug-del ] && head -2 ./iptables-debug-del | tail -n 1 | awk '{print $2}'`
  510.                   ttime=`echo $(( $dendtime / 60 ))`
  511.                   ttime=`echo $(( $dfrom + $dendtime ))`
  512.                 leftime=`echo $(( $ttime - $now ))`
  513.                           m=`echo $((( $leftime / 60 ) %60 ))`
  514.                           s=`echo $(( $leftime %60 ))`
  515.                 leftime=`printf "%02d:%02d:%02d\n" $h $m $s`
  516.                 zzztime=`echo $(( $dendtime / 60 ))`
  517.                         fi
  518.            
  519.         if [[ $running3 -eq "0" ]] && [[ $running4 -eq "0" ]]; then
  520.                 running8="\033[1;31mNo\033[0;40m"
  521.         elif [[ $running3 -eq "0" ]] && [[ $running4 -eq "1" ]]; then
  522.                 running8="\033[1;35mLoading...\033[0;40m"
  523.         elif [[ $running3 -lt "4" ]] && [[ $running4 -eq "0" ]]; then
  524.                 running8="\033[1;31mNot quite... try to run \"p2partisan.sh update\"\033[0;40m"
  525.         elif [[ $running3 -eq "4" ]] && [[ $running4 -eq "1" ]]; then
  526.                 running8="\033[1;32mYes\033[0;40m"
  527.         fi
  528.                
  529.                 if [[ $runningF -eq 1 ]]; then
  530.                                 runningF="\033[1;35mOn\033[0;40m IP \033[1;33m`iptables -L P2PARTISAN-DROP-IN  2> /dev/null | grep DEBUG |  awk '{print $5}'` \033[1;33m$f\033[0;40mrunning for \033[1;33m$druntime\033[0;40m /\033[1;33m$zzztime\033[0;40m min (\033[1;33m$leftime\033[0;40m left)"
  531.                 elif [[ $runningF -gt 1 ]]; then
  532.                                 runningF="\033[1;35mOn - reverse \033[0;40m(entire LAN except port \033[1;33m`iptables -L P2PARTISAN-DROP-IN  2> /dev/null | grep DEBUG | head -1 |  awk '{print $7}' | cut -f2 -d!`) \033[1;33m$f\033[0;40mrunning for \033[1;33m$druntime\033[0;40m /\033[1;33m$zzztime\033[0;40m min (\033[1;33m$leftime\033[0;40m left)"
  533.                 else
  534.                                 runningF="Off"        
  535.                 fi
  536.  
  537. 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`
  538. 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`
  539.  
  540. if [[ $whiteextra == "0" ]]; then
  541.     whiteextra=" "
  542. else
  543.     whiteextra=`echo "/ $whiteextra" LAN IP ref defined`
  544. fi
  545.     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`
  546.     greyip=`ipset -L greylist 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`
  547.  
  548. echo -e "\e[40m
  549. +------------------------- P2Partisan --------------------------+
  550. |            _______ __          __              
  551. |           |     __|  |_.---.-.|  |_.--.--.-----.
  552. |           |__     |   _|  _  ||   _|  |  |__ --|
  553. |           |_______|____|___._||____|_____|_____|
  554. |  
  555. | Release version:  \033[1;40m$version\033[0;40m
  556. +---------------------------------------------------------------+
  557. |         Running:  $running8
  558. |         Autorun:  $running5
  559. |           Tutor:  $running9 / \033[1;37m$runningB\033[0;40m problems in the last 24h
  560. |        Debugger:  $runningF
  561. | Partisan uptime:  \033[1;37m$runtime\033[0;40m
  562. |    Startup time:  \033[1;37m$runningD\033[0;40m seconds
  563. |      Dropped in:  \033[1;37m$drop_packet_count_in\033[0;40m
  564. |    Rejected out:  \033[1;37m$drop_packet_count_out\033[0;40m
  565. +---------------------------------------------------------------+"
  566. echo -e "|       Black IPs:  \033[1;37m$blackip\033[0;40m"
  567. echo -e "|        Grey IPs:  \033[1;37m$greyip\033[0;40m"
  568. echo -e "|       White IPs:  \033[1;37m$whiteip $whiteextra\033[0;40m"
  569. transmissionenable=`nvram get bt_enable`
  570. if [ -z $transmissionenable ]; then
  571.     echo "|  TransmissionBT:  Not available"
  572.     elif [ $transmissionenable -eq 0 ]; then
  573.     echo "|  TransmissionBT:  Off"
  574.     else
  575.     echo -e "|  TransmissionBT:  \033[1;32mOn\033[0;40m"
  576.         transmissionport=`nvram get bt_port 2> /dev/null`
  577.         greyports_tcp=$greyports_tcp,$transmissionport
  578.         greyports_udp=$greyports_udp,$transmissionport
  579. fi
  580. 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
  581.             echo -e "|  Grey ports TCP:  \033[1;37m$w\033[0;40m"
  582.                 done
  583. 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
  584.             echo -e "|  Grey ports UDP:  \033[1;37m$w\033[0;40m"
  585.                 done
  586. 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
  587.             echo -e "| White ports TCP:  \033[1;37m$w\033[0;40m"
  588.                 done
  589. 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
  590.             ColorOff='\\\e[0;40m'
  591.             ColorOn='\\\e[1;37m'
  592.             BWhite='\\\e[100m'
  593.             p1=`head -70 ./p2partisan.sh | grep -E ^whiteports_udp= | grep -Eo '[,|:|=]67[,|:]|,67$' | wc -l`
  594.             p2=`head -70 ./p2partisan.sh | grep -E ^whiteports_udp= | grep -Eo '[,|:|=]68[,|:]|,68$' | wc -l`
  595.             if [ $p1 -eq "0" ]; then
  596.                     w=`echo -e $w | sed -e "s/^67,/${BWhite}67${ColorOn},/g" | sed -e "s/,67,/,${BWhite}67${ColorOff}${ColorOn},/g" | sed -e "s/,67$/,${BWhite}67/g"`
  597.             fi
  598.             if [ $p2 -eq "0" ]; then
  599.                     w=`echo -e $w | sed -e "s/^68,/${BWhite}68${ColorOn},/g" | sed -e "s/,68,/,${BWhite}68${ColorOff}${ColorOn},/g" | sed -e "s/,68$/,${BWhite}68/g"`
  600.             fi
  601.             echo -e "| White ports UDP:  \033[1;37m$w\033[0;40m"
  602.                 done
  603. cat blacklists | grep -Ev "^#|^$" | tr -d "\r" |
  604.    (
  605.     while read line
  606.     do
  607.             counter=`expr $counter + 1`
  608.             counter=$(printf "%02d" $counter)
  609.             name=`echo $line | awk '{print $1}'`
  610.             statusa=`cat /tmp/p2partisan.$name.LOAD 2> /dev/null || echo 5`
  611.             statusb=`cat /tmp/p2partisan.$name.bro.LOAD 2> /dev/null || echo 5`
  612.             statusap=`ps w | grep $name | grep -v grep | wc -l`
  613.             statusbp=`ps w | grep $name.bro | grep -v grep | wc -l`
  614.             statusaa=`ipset -L $name 2> /dev/null | head -8 | tail -1 | 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]).*" > /dev/null && echo "1" || echo "0"`
  615.             statusbb=`ipset -L $name.bro 2> /dev/null | head -8 | tail -1 | 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]).*" > /dev/null && echo "1" || echo "0"`
  616.             statusaaa=`ipset -T $name $adminip 2> /dev/null && echo "1" || echo "0"`
  617.             statusbbb=`ipset -T $name.bro $adminip 2> /dev/null && echo "1" || echo "0"`
  618.             sizeb=`ipset -L $name 2> /dev/null | head -5 | tail -1 | awk '{print $4}' || echo=0`
  619.             sizebb=`ipset -L $name.bro 2> /dev/null | head -5 | tail -1 | awk '{print $4}' || echo=0`
  620.             sizem=`echo $((sizeb/1024))`
  621.             sizem=$(printf "%04s" $sizem)
  622.             sizemm=`echo $((sizebb/1024))`
  623.             lin=`iptables -L P2PARTISAN-LISTS-IN 2> /dev/null | grep $name | wc -l`
  624.             lout=`iptables -L P2PARTISAN-LISTS-OUT 2> /dev/null | grep $name | wc -l`
  625.             ipt=`echo $((lin + lout))`
  626.             if [ $ipt -eq 2 ]; then
  627.                 i="\033[1;32mo\033[0;40m"
  628.             elif [ $ipt -eq 1 ]; then
  629.                 i="\033[1;33mp\033[0;40m"          
  630.             else
  631.                 i="\033[1;31me\033[0;40m"          
  632.             fi
  633.  
  634.             if [ $statusaaa -eq 0 ]; then
  635.                             if [ $statusaa -eq 1 ]; then
  636.                                 if [ $statusa -gt 2 ]; then
  637.                                     a="\033[1;33mp\033[0;40m"
  638.                                 elif [ $statusa -le 2 ]; then
  639.                                     a="\033[1;35ml\033[0;40m"
  640.                                 fi
  641.                             else
  642.                                 if [ $statusap -eq 1 ]; then
  643.                                     a="\033[1;36mq\033[0;40m"
  644.                                 else
  645.                                     a="\033[1;31me\033[0;40m"
  646.                                 fi
  647.                             fi
  648.                         elif [ $statusaaa -eq 1 ]; then
  649.                             a="\033[1;32mo\033[0;40m"
  650.                         fi
  651.                        
  652.                         if [ $statusbbb -eq 0 ]; then
  653.                             if [ $statusbb -eq 1 ]; then
  654.                                 if [ $statusb -gt 2 ]; then
  655.                                     b="\033[1;37mp\033[0;40m"
  656.                                 elif [ $statusb -le 2 ]; then
  657.                                     b="\033[1;35ml\033[0;40m"
  658.                                 fi
  659.                             else
  660.                                 if [ $statusbp -eq 1 ]; then
  661.                                     b="\033[1;36mq\033[0;40m"
  662.                                 else
  663.                                     b="\033[1;37me\033[0;40m"
  664.                                 fi
  665.                             fi
  666.                         elif [ $statusbbb -eq 1 ]; then
  667.                             b="\033[1;37mo\033[0;40m"
  668.                         fi
  669.                        
  670.                         if [ -f ./$name.cidr ];then
  671.                             cat ./$name.cidr | cut -d" " -f3 | grep -F "$adminip" > /dev/null &&
  672.                             {
  673.                             age=`[ -e $name.cidr ] && echo $((\`date +%s\` - \`date -r $name.cidr +%s\`)) || echo 0`
  674.                             d=`echo $(( $age / 86400 ))`
  675.                                 if [[ $d -eq 7 ]]; then
  676.                                     c="\033[1;33mo\033[0;40m"
  677.                                 elif [[ $d -ge 8 ]]; then
  678.                                     c="\033[1;31mo\033[0;40m"
  679.                                 else
  680.                                     c="\033[1;37mo\033[0;40m"
  681.                           fi
  682.                             } || c="\033[1;37mp\033[0;40m"
  683.                         else
  684.                             c="\033[1;37me\033[0;40m"
  685.                         fi
  686.                        
  687.                 echo -e "|    Blacklist_$counter:  [$a] [$b] [$c] [$i] - $sizem KB - \033[1;37m$name\033[0;40m"
  688.  
  689.                         sizeram=`echo $((sizeram+sizeb+sizebb))`                      
  690.         done
  691.                         sizeram=`echo $((sizeram/1024))`
  692.                 echo  "|                    ^   ^   ^   ^"      
  693.              echo  -e "|      maxload: \033[1;37m$maxconcurrentlistload\033[0;40m - \e[1;37;100mpri sec cid ipt\033[0;40m - [\033[1;37me\033[0;40m]mpty [\033[1;37ml\033[0;40m]oading l[\033[1;37mo\033[0;40m]aded [\033[1;37mp\033[0;40m]artial [\033[1;37mq\033[0;40m]ueued"  
  694.              echo  -e "|    Consumed RAM:  \033[1;37m$sizeram\033[0;40m KB"
  695.         )
  696.  
  697. echo -e "+----------------------- Logs max($maxloghour/hour) ----------------------+      
  698. $running7
  699. $running7a
  700. +---------------------------------------------------------------+\033[0;39m"
  701. }
  702.  
  703. if [ $autorun_availability_check = 1 ]; then
  704. av="while true; do [ -f $P2Partisandir/p2partisan.sh ] && break || sleep 5; done ;"
  705. fi
  706.  
  707. pautorunset() {
  708. echo -e "\033[0;40m
  709. +------------------------- P2Partisan --------------------------+
  710. |            ______               __               __
  711. |           |      |.-----.-----.|  |_.----.-----.|  |
  712. |           |   ---||  _  |     ||   _|   _|  _  ||  |
  713. |           |______||_____|__|__||____|__| |_____||__|
  714. |                                                  
  715. +--------------------------- Autorun ---------------------------+"
  716.         p=`nvram get script_fire | grep "p2partisan.sh restart" | grep -v cru | wc -l`
  717.         if [ $p -eq "0" ] ; then
  718.                 t=`nvram get script_fire`; t=`printf "$t\n$av$P2Partisandir/p2partisan.sh restart\n"` ; nvram set "script_fire=$t"
  719.         fi
  720.         plog "P2Partisan AUTO RUN is ON"
  721.                 echo -e "+---------------------------------------------------------------+\033[0;39m"
  722.         nvram commit
  723. }
  724.  
  725. pautorununset() {
  726. echo -e "\033[0;40m
  727. +------------------------- P2Partisan --------------------------+
  728. |            ______               __               __
  729. |           |      |.-----.-----.|  |_.----.-----.|  |
  730. |           |   ---||  _  |     ||   _|   _|  _  ||  |
  731. |           |______||_____|__|__||____|__| |_____||__|
  732. |                                                  
  733. +--------------------------- Autorun ---------------------------+"
  734.         p=`nvram get script_fire | grep "p2partisan.sh restart" | grep -v cru | wc -l`
  735.         if [ $p -eq "1" ]; then
  736.         t=`nvram get script_fire`; t=`printf "$t" | grep -v "p2partisan.sh restart"` ; nvram set "script_fire=$t"
  737.         fi
  738.         plog "P2Partisan AUTO RUN is OFF"
  739.                 echo -e "+---------------------------------------------------------------+\033[0;39m"
  740.         nvram commit
  741. }
  742.  
  743. pdetective() {
  744. echo -e "\033[0;40m
  745. +------------------------- P2Partisan --------------------------+
  746. |         __         __               __   __              
  747. |     .--|  |.-----.|  |_.-----.----.|  |_|__|.--.--.-----.
  748. |     |  _  ||  -__||   _|  -__|  __||   _|  ||  |  |  -__|
  749. |     |_____||_____||____|_____|____||____|__| \___/|_____| BETA
  750. |
  751. +---------------------------------------------------------------+
  752. | After an investigation it appears that the following socket/s
  753. | should be considered a greyports candidates. Consider re-run the
  754. | command multiple times to reduce the number of false positive. Once
  755. | identified the port/s can be added under greyports_tcp & greyports_udp.
  756. +---------------------------------------------------------------+"
  757. 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
  758. echo -e "+---------------------------------------------------------------+\033[0;39m"
  759. }
  760.  
  761. pupgrade() {
  762.         [ -f p2partisan_new.sh ] && rm -f "p2partisan_new.sh" 2> /dev/null
  763.         wget -q -O - http://pastebin.com/raw.php?i=mUeS6jP2 | grep "p2partisan v" | grep -v grep> ./latest
  764.         latest=`cat ./latest | cut -c3-31`
  765.         current=`cat ./p2partisan.sh | grep "p2partisan v" | head -1 | cut -c3-32 `
  766.         if [[ "$latest" == "$current" ]]; then
  767.         echo -e "\033[0;40m
  768. +------------------------- P2Partisan --------------------------+
  769. |          _______                            __        
  770. |         |   |   |.-----.-----.----.---.-.--|  |.-----.
  771. |         |   |   ||  _  |  _  |   _|  _  |  _  ||  -__|
  772. |         |_______||   __|___  |__| |___._|_____||_____|
  773. |                  |__|  |_____|                        
  774. |
  775. +---------------------------------------------------------------+
  776. You're already running the latest version of P2Partisan
  777. \033[0;39m"
  778.         else
  779.         echo -e "\033[0;40m
  780. +------------------------- P2Partisan --------------------------+
  781. |          _______                            __        
  782. |         |   |   |.-----.-----.----.---.-.--|  |.-----.
  783. |         |   |   ||  _  |  _  |   _|  _  |  _  ||  -__|
  784. |         |_______||   __|___  |__| |___._|_____||_____|
  785. |                  |__|  |_____|                        
  786. |
  787. +---------------------------------------------------------------+
  788. | There's a new P2Partisan update available. Do you want to upgrade?
  789. |  
  790. |                  current = $current
  791. |  
  792. |                          to
  793. |                      
  794. |                   latest = $latest
  795. |
  796. | y/n"
  797.         read answer
  798.                 if [[ $answer == "y" ]]; then
  799. wget -q -O ./p2partisan_new.sh http://pastebin.com/raw.php?i=mUeS6jP2
  800. pupgraderoutine
  801.                 else
  802.                 echo -e "| Upgrade skipped. Quitting...
  803. +---------------------------------------------------------------+\033[0;39m"
  804.                 exit
  805.                 fi
  806.        
  807.         fi
  808.  }
  809.  
  810. pupgradebeta() {
  811.         [ -f p2partisan_new.sh ] && rm -f "p2partisan_new.sh" 2> /dev/null
  812.         wget -q -O - http://pastebin.com/raw.php?i=Lt1axJ9a | grep "p2partisan v" | grep -v grep > ./latest
  813.         echo "| Do you want to install the latest testing beta (not suggested)?
  814. |
  815. | y/n"
  816.         read answer
  817.                 if [[ $answer == "y" ]]; then
  818. wget -q -O ./p2partisan_new.sh http://pastebin.com/raw.php?i=Lt1axJ9a
  819. pupgraderoutine
  820.                 else
  821.                 echo -e "| Beta upgrade skipped. Quitting...
  822. +---------------------------------------------------------------+\033[0;39m"
  823.                                
  824.                 exit
  825.                 fi
  826.  }
  827.  
  828. pupgraderoutine() {
  829.                 echo -e "\033[0;40m| Upgrading, please wait:"
  830.                 echo -e "\033[0;40m| 1/6) Stopping the script"
  831.                                 pforcestop
  832.                 [ -f p2partisan_new.sh ] || plog "There's a problem with the p2partisan upgrade. Please try again"
  833.                 echo -e "\033[0;40m| 2/6) Migrating the configuration"
  834.                 sed '1,/P2Partisandir/{s@P2Partisandir=.*@'"P2Partisandir=$P2Partisandir"'@'} -i ./p2partisan_new.sh
  835.                 sed '1,/syslogs/{s@syslogs=.*@'"syslogs=$syslogs"'@'} -i ./p2partisan_new.sh
  836.                 sed '1,/maxloghour/{s@maxloghour=.*@'"maxloghour=$maxloghour"'@'} -i ./p2partisan_new.sh
  837.                 sed '1,/whiteports_tcp/{s@whiteports_tcp=.*@'"whiteports_tcp=$whiteports_tcp"'@'} -i ./p2partisan_new.sh
  838.                 sed '1,/whiteports_udp/{s@whiteports_udp=.*@'"whiteports_udp=$whiteports_udp"'@'} -i ./p2partisan_new.sh
  839.                 sed '1,/greyports_tcp/{s@greyports_tcp=.*@'"greyports_tcp=$greyports_tcp"'@'} -i ./p2partisan_new.sh
  840.                 sed '1,/greyports_udp/{s@greyports_udp=.*@'"greyports_udp=$greyports_udp"'@'} -i ./p2partisan_new.sh
  841.                 sed '1,/greyline/{s@greyline=.*@'"greyline=$greyline"'@'} -i ./p2partisan_new.sh
  842.                 sed '1,/scheduleupdates/{s@scheduleupdates=.*@'"scheduleupdates=\"$scheduleupdates\""'@'} -i ./p2partisan_new.sh
  843.                 sed '1,/maxconcurrentlistload/{s@maxconcurrentlistload=.*@'"maxconcurrentlistload=$maxconcurrentlistload"'@'} -i ./p2partisan_new.sh
  844.                 sed '1,/autorun_availability_check/{s@autorun_availability_check=.*@'"autorun_availability_check=$autorun_availability_check"'@'} -i ./p2partisan_new.sh
  845.                 sed '1,/testip/{s@testip=.*@'"testip=$testip"'@'} -i ./p2partisan_new.sh              
  846.                 tr -d "\r"< ./p2partisan_new.sh > ./.temp ; mv ./.temp ./p2partisan_new.sh
  847.                 echo -e "\033[0;40m| 3/6) Copying p2partisan.sh into p2partisan.sh.old"
  848.                 cp ./p2partisan.sh ./p2partisan_old
  849.                 echo -e "\033[0;40m| 4/6) Installing new script into p2partisan.sh"
  850.                 mv ./p2partisan_new.sh ./p2partisan.sh
  851.                 echo -e "\033[0;40m| 5/6) Setting up permissions"
  852.                 chmod -R 777 ./p2partisan.sh
  853.                 echo -e "\033[0;40m| 6/6) all done, \033[1;32mPlease run the script manually!\033[0;40m
  854. | NOTE: autorun setting is left as it was found
  855. +---------------------------------------------------------------+
  856. \033[0;39m"
  857. exit
  858. }
  859.  
  860. ptutor() {
  861.         h=`date +%H`
  862.         pwhitelist
  863.         pgreylist
  864.         pblacklistcustom
  865.         running3=`iptables -L | grep -v Chain| grep 'P2PARTISAN-IN\|P2PARTISAN-OUT'  2> /dev/null | wc -l`
  866.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  867.         runningE=`iptables -L wanin | grep P2PARTISAN-IN  2> /dev/null | wc -l`
  868.          schfrom=`echo $scheduleupdates | cut -d, -f1`
  869.            schto=`echo $scheduleupdates | cut -d, -f2`
  870.  
  871.         cat blacklists |  grep -Ev "^#|^$" | tr -d "\r" |
  872.            (
  873.             while read line
  874.             do
  875.                     name=`echo $line | awk '{print $1}'`
  876.                     statusbbb=`ipset -T $name.bro $adminip 2> /dev/null && echo 1 || echo 0`
  877.                     iptables -L P2PARTISAN-LISTS-IN | grep $name > /dev/null || {
  878.                         plog "P2Partisan tutor had to reinstall the iptables due to: P2PARTISAN-LIST-IN $name instruction missing"
  879.                         ./iptables-del ; ./iptables-add
  880.                         exit
  881.                         }
  882.                     iptables -L P2PARTISAN-LISTS-OUT | grep $name  > /dev/null || {
  883.                         plog "P2Partisan tutor had to reinstall the iptables due to: P2PARTISAN-LIST-OUT $name instruction missing"
  884.                         ./iptables-del ; ./iptables-add
  885.                         exit
  886.                         }
  887.                 age=`echo $((\`date +%s\` - \`date -r $name.cidr +%s\`))`
  888.                 if [[ $age -gt "604800" ]] && [[ $h -ge $schfrom ]] && [[ $h -le $schto ]]; then
  889.                     plog "P2Partisan is updating list $name"
  890.                     pforcestop $name
  891.                     exit
  892.                 fi
  893.                 if [[ $age -gt "300" ]] && [[ $statusbbb -eq 1 ]]; then
  894.                     plog "P2Partisan is clearing the $name secondary list"
  895.                     ipset -F $name.bro
  896.                 fi
  897.             done
  898.             )
  899.                 if [[ $runningE -gt "1" ]]; then
  900.                         pforcestop
  901.                         plog "P2Partisan tutor had to restart due to: iptables redundant rules found"
  902.                         pstart
  903.                 elif [[ $running3 -eq "4" ]] && [[ $running4 -eq "0" ]]; then
  904.                         plog "P2Partisan tutor had to restart due to: pid file missing"
  905.                         pforcestop
  906.                         pstart
  907.                 # elif [[ $running3 -eq "0" ]] && [[ $running4 -eq "1" ]]; then
  908.                         # plog "P2Partisan tutor had to restart due to: iptables instructions missing"
  909.                         # pforcestop
  910.                         # pstart        
  911.                 elif [[ $running3 -ne "4" ]] && [[ $running4 -eq "1" ]]; then
  912.                         plog "P2Partisan might be loading, I'll wait 10 seconds..."    
  913.                         sleep 10
  914.                     if [[ $running3 -ne "4" ]] && [[ $running4 -eq "1" ]]; then
  915.                             plog "P2Partisan tutor had to restart due to iptables instruction missing"
  916.                             pforcestop
  917.                             pstart
  918.                     fi
  919.                 else
  920.                     echo -e "\033[0;40m
  921. +------------------------- P2Partisan --------------------------+
  922. |                _______         __              
  923. |               |_     _|.--.--.|  |_.-----.----.
  924. |                 |   |  |  |  ||   _|  _  |   _|
  925. |                 |___|  |_____||____|_____|__|                                                  
  926. |
  927. +---------------------------------------------------------------+              
  928. | P2Partisan up and running. The tutor is happy
  929. +---------------------------------------------------------------+\033[0;39m"
  930.         fi
  931. }
  932.  
  933. ptutorset() {
  934.         echo -e "\033[0;40m
  935. +------------------------- P2Partisan --------------------------+
  936. |                _______         __              
  937. |               |_     _|.--.--.|  |_.-----.----.
  938. |                 |   |  |  |  ||   _|  _  |   _|
  939. |                 |___|  |_____||____|_____|__|                                                  
  940. |
  941. +-------------------------- Scheduler --------------------------+"
  942.         cru d P2Partisan-tutor
  943.         ab=`tr -cd 0-5 </dev/urandom | head -c 1`
  944.         a=`tr -cd 0-9 </dev/urandom | head -c 1`
  945.         a=`echo $ab$a`
  946.         scheduleme=`echo "$a * * * *"`
  947.         cru a P2Partisan-tutor "$scheduleme $P2Partisandir/p2partisan.sh tutor"
  948.         p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  949.         if [ $p -eq "0" ] ; then
  950.                 t=`nvram get script_fire`; t=`printf "$t\ncru a P2Partisan-tutor \"$scheduleme $P2Partisandir/p2partisan.sh tutor\"\n"` ; nvram set "script_fire=$t"
  951.         fi
  952.         plog "P2Partisan tutor is ON"
  953.                 echo -e "+---------------------------------------------------------------+\033[0;39m"
  954.         nvram commit
  955. }
  956.  
  957. ptutorunset() {
  958.         echo -e "\033[0;40m
  959. +------------------------- P2Partisan --------------------------+
  960. |                _______         __              
  961. |               |_     _|.--.--.|  |_.-----.----.
  962. |                 |   |  |  |  ||   _|  _  |   _|
  963. |                 |___|  |_____||____|_____|__|                                                  
  964. |
  965. +-------------------------- Scheduler --------------------------+"
  966.         cru d P2Partisan-tutor
  967.         p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  968.         if [ $p -eq "1" ] ; then
  969.         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"
  970.         fi
  971.         plog "P2Partisan tutor is OFF"
  972.                 echo -e "+---------------------------------------------------------------+\033[0;39m"
  973.         nvram commit
  974.  }
  975.  
  976. ptest() {
  977.  
  978. checklist="blacklist-custom greylist whitelist `cat blacklists | grep -Ev "^#|^$" | tr -d "\r" | awk '{print $1}'`"
  979. echo -e "\033[0;40m
  980. +------------------------- P2Partisan --------------------------+
  981. |                  _______               __  
  982. |                 |_     _|.-----.-----.|  |_
  983. |                   |   |  |  -__|__ --||   _|
  984. |                   |___|  |_____|_____||____|
  985. |                                              
  986. +----------- Lists are sorted in order of precedence -----------+"
  987.         if [[ -z $1 ]]; then
  988. echo "+---------------------------------------------------------------+
  989. | Invalid input. Please specify a valid IP address.
  990. +---------------------------------------------------------------+"    
  991.         else
  992.         q=0
  993.         echo $1 | 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 && q=1
  994.         echo $1 | 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=2
  995.         if [[ $q -eq 1 ]]; then
  996.         echo $checklist | tr " " "\n" |
  997.                 while read LIST
  998.                 do
  999.                 ipset -T $LIST $1 > /dev/null 2>&1 && if [ $LIST = "whitelist" ]; then echo -e "| \033[1;32m$1 found in        $LIST\033[0;40m"; else echo -e "| \033[1;31m$1 found in        $LIST\033[0;40m"; fi || echo -e "| $1 not found in    $LIST"
  1000.         done                                                                                                                                    
  1001.         echo -e "+---------------------------------------------------------------+
  1002. |        in case of multiple match the first prevails
  1003. +---------------------------------------------------------------+\033[0;39m"
  1004.         elif [[ $q -eq 2 ]]; then
  1005.         echo $checklist | tr " " "\n" |
  1006.                 while read LIST
  1007.                 do
  1008.                 nslookup $1 | grep "Address [0-9]*:" | grep -v 127.0.0.1 | grep -v "\:\:" | grep -Eo "([0-9\.]{7,15})" |
  1009.                     while read IPO
  1010.                         do
  1011.                                     # echo $IPO
  1012. ipset -T $LIST $IPO > /dev/null 2>&1 && if [ $LIST = "whitelist" ]; then printf '%-19s%s' "| $IPO"; echo -e "\033[1;32mfound in $LIST\033[0;40m" ; else printf '%-19s%s' "| $IPO"; echo -e "\033[1;31mfound in $LIST\033[0;40m"; fi || printf '%-19s%s\n' "| $IPO" "not found in $LIST"
  1013.                     done                                                                                                                                                                                                                                                                                                       
  1014.                 done
  1015.         echo -e "+---------------------------------------------------------------+
  1016. |        in case of multiple match the first prevails
  1017. +---------------------------------------------------------------+\033[0;39m"
  1018.         elif [[ $q -eq 0 ]]; then
  1019.         echo -e "| Invalid input. Please specify a valid IP address or domain name.
  1020. +---------------------------------------------------------------+\033[0;39m"    
  1021.         fi
  1022. fi
  1023. }
  1024.  
  1025.  
  1026. pdebug() {
  1027. echo -e "\033[0;40m
  1028. +------------------------- P2Partisan --------------------------+
  1029. |                _____         __                
  1030. |               |     \.-----.|  |--.--.--.-----.
  1031. |               |  --  |  -__||  _  |  |  |  _  |
  1032. |               |_____/|_____||_____|_____|___  |
  1033. |                                         |_____|
  1034. |                                              
  1035. +--------------------------- Guide -----------------------------+
  1036. | Debug allows to fully log the P2Partisan interventions given a LAN IP
  1037. | Maximum 1 debug at the time / Debug automatically times out or can be forced off manually
  1038. +---------------------------------------------------------------+
  1039. | p2partisan.sh debug <LAN IP> <minutes>    Syntax
  1040. | p2partisan.sh debug                       Displays debug status and this help text
  1041. | p2partisan.sh debug 192.168.0.3 <1-120>   Enables debug for the given LAN IP for N min (15 default)
  1042. | p2partisan.sh debug 192.168.0.3 9         Enables debug for the given LAN IP for 9 min
  1043. | p2partisan.sh debug reverse <1-120>       Enables debug for all the LAN IPs excluding greyports_tcp/udp
  1044. | p2partisan.sh debug off                   Disable debug without waiting for the timer to timeout
  1045. | p2partisan.sh debug-display <in|out>      Display logs Syntax
  1046. | p2partisan.sh debug-display               Displays in&out debug logs + guide
  1047. | p2partisan.sh debug-display out           Same as above but displays outbound records only
  1048. +-------------------------- Activity ---------------------------+"
  1049. 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
  1050. echo "$1" | grep "reverse" >/dev/null 2>&1 && q=2
  1051. echo "$1" | grep "off" >/dev/null 2>&1 && off=1 || off=0
  1052.  
  1053.                         if [ -e ./iptables-debug-del ]; then
  1054.               dfrom=`head -1 ./iptables-debug 2> /dev/null | awk '{print $2}'`
  1055.            druntime=`echo $(( $now - $dfrom ))`
  1056.                   h=`echo $((( $druntime / 3600 ) %24 ))`
  1057.                   m=`echo $((( $druntime / 60 ) %60 ))`
  1058.                   s=`echo $(( $druntime %60 ))`
  1059.            druntime=`printf "%02d:%02d:%02d\n" $h $m $s`
  1060.            dendtime=`head -2 ./iptables-debug-del | tail -n 1 | awk '{print $2}'`
  1061.               ttime=`echo $(( $dendtime / 60 ))`
  1062.               ttime=`echo $(( $dfrom + $dendtime ))`
  1063.             leftime=`echo $(( $ttime - $now ))`
  1064.                   m=`echo $((( $leftime / 60 ) %60 ))`
  1065.                   s=`echo $(( $leftime %60 ))`
  1066.             leftime=`printf "%02d:%02d:%02d\n" $h $m $s`
  1067.             zzztime=`echo $(( $dendtime / 60 ))`
  1068.                         fi          
  1069.  
  1070.         if [[ $off -eq 1 ]]; then
  1071.         f=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG `
  1072.         fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  1073.                 if [[ $fc -ge 1 ]]; then
  1074.                 kill `ps | grep -E "sleep $dendtime$" | awk '{print $1}'` > /dev/null 2>&1
  1075.                 plog "| All DEBUG activities have stopped"
  1076.                 {
  1077.         while iptables -L P2PARTISAN-DROP-IN | grep DEBUG
  1078.         do
  1079.                 iptables -D P2PARTISAN-DROP-IN 1
  1080.                 done
  1081.         while iptables -L P2PARTISAN-DROP-OUT | grep DEBUG
  1082.         do
  1083.                 iptables -D P2PARTISAN-DROP-OUT 1
  1084.                 done
  1085.                 } > /dev/null 2>&1
  1086.                 echo -e "| Use \033[1;33m./p2partisan.sh debug-display\033[0;40m to show debug information, if any.
  1087. +---------------------------------------------------------------+\033[0;39m" ; exit
  1088.                 else
  1089.                         echo -e "| Debug is currently off and not collecting any information.
  1090. | Use \033[1;33m./p2partisan.sh debug-display\033[0;40m to show existing debug information, if any.
  1091. +---------------------------------------------------------------+\033[0;39m" ; exit      
  1092.                 fi
  1093.         fi
  1094.  
  1095.         if [[ -z $1 ]]; then  
  1096.                 f=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | awk '{print $5}' | head -1`
  1097.                 fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  1098.                 if [[ $fc -gt 1 ]]; then
  1099. echo -e "| P2partisan is currently debugging IP \033[1;33m$f\033[0;40m for \033[1;33m$druntime\033[0;40m /\033[1;33m$zzztime\033[0;40m min (\033[1;33m$leftime\033[0;40m left)
  1100. | Use \033[1;33m./p2partisan.sh debug-display\033[0;40m to show debug information
  1101. +---------------------------------------------------------------+\033[0;39m" ; exit
  1102.                 elif [[ $fc -eq 0 ]]; then
  1103.                         echo -e "| Debug is currently off and not collecting any information.
  1104. | Use \033[1;33m./p2partisan.sh debug-display\033[0;40m to show existing debug information, if any.
  1105. +---------------------------------------------------------------+\033[0;39m" ; exit
  1106.                 fi
  1107.         elif [[ $q -eq 1 ]]; then
  1108.                         echo -e "| The input \033[1;31m$1\033[0;40m doesn't appear to be a valid IP
  1109. +---------------------------------------------------------------+\033[0;39m" ; exit
  1110.         fi
  1111.  
  1112.         f=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | awk '{print $5}' | head -1`
  1113.         fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  1114.         if [[ $fc -gt 1 ]]; then
  1115. echo -e "| P2partisan is currently debugging IP \033[1;33m$f\033[0;40m for \033[1;33m$druntime\033[0;40m /\033[1;33m$zzztime\033[0;40m min (\033[1;33m$leftime\033[0;40m left)
  1116. | NOTE: Only one debug at the time is possible! Command ignored.
  1117. | Use \033[1;33m./p2partisan.sh debug-display\033[0;40m to show the debug information
  1118. +---------------------------------------------------------------+\033[0;39m" ; exit
  1119.         fi
  1120.  
  1121. if [ -z $2 ]; then
  1122.                 minutes=15
  1123.                 time=900
  1124. elif [[ $2 -gt 120 ]] || [[ $2 -eq 0 ]]; then
  1125.                 echo -e "| Please specify an acceptable time: 1 to 60 (min). If omitted 15 will be used
  1126. | Debug NOT enabled. Exiting...
  1127. +---------------------------------------------------------------+\033[0;39m" ; exit
  1128. else
  1129.                 minutes=$2
  1130.                 time=` echo $(( $2 * 60 ))`
  1131. fi
  1132. if [[ $q -eq 2 ]]; then
  1133. if [ -z $greyports_tcp ] || [ -z $greyports_udp ]; then
  1134. echo -e "| It appears like you have no greyport set. This function due to the potential amount
  1135. | of logging involved requires the both greyports_tcp and greyports_udp to be set
  1136. | if unsure on what ports to use, try to run \033[1;33m./p2partisan.sh detective\033[0;40m
  1137. +---------------------------------------------------------------+"
  1138. exit
  1139. fi
  1140. echo "# $now
  1141. iptables -I P2PARTISAN-DROP-IN 1 -p tcp --sport $greyports_tcp -j DROP
  1142. iptables -I P2PARTISAN-DROP-IN 1 -p udp --sport $greyports_udp -j DROP
  1143. iptables -I P2PARTISAN-DROP-IN 1 -p tcp --dport $greyports_tcp -j DROP
  1144. iptables -I P2PARTISAN-DROP-IN 1 -p udp --dport $greyports_udp -j DROP
  1145. iptables -I P2PARTISAN-DROP-OUT 1 -p tcp --sport $greyports_tcp -j DROP
  1146. iptables -I P2PARTISAN-DROP-OUT 1 -p udp --sport $greyports_udp -j DROP
  1147. iptables -I P2PARTISAN-DROP-OUT 1 -p tcp --dport $greyports_tcp -j DROP
  1148. iptables -I P2PARTISAN-DROP-OUT 1 -p udp --dport $greyports_udp -j DROP
  1149. iptables -I P2PARTISAN-DROP-IN 5 -j LOG --log-prefix 'P2Partisan-DEBUG-IN->> ' --log-level 1
  1150. iptables -I P2PARTISAN-DROP-OUT 5 -j LOG --log-prefix 'P2Partisan-DEBUG-OUT->> ' --log-level 1" > ./iptables-debug
  1151. chmod 777 ./iptables-debug  > /dev/null 2>&1
  1152. plog "Reverse Debug started for for $minutes minute"
  1153. ./iptables-debug 1>/dev/null &
  1154.                 echo -e "| Enabled full debug logging for all the LAN IPs for \033[1;32m$minutes\033[0;40m minutes
  1155. | This excludes the greyports_tcp $greyports_tcp and greyports_udp $greyports_udp              
  1156. | Use \033[1;33m./p2partisan.sh debug-display\033[0;40m to show the debug information
  1157. +---------------------------------------------------------------+"
  1158.  
  1159. echo "# $now
  1160. sleep $time
  1161. iptables -D P2PARTISAN-DROP-IN -p tcp -m tcp --sport $greyports_tcp -j DROP
  1162. iptables -D P2PARTISAN-DROP-IN -p udp -m udp --sport $greyports_udp -j DROP
  1163. iptables -D P2PARTISAN-DROP-IN -p tcp -m tcp --dport $greyports_tcp -j DROP
  1164. iptables -D P2PARTISAN-DROP-IN -p udp -m udp --dport $greyports_udp -j DROP
  1165. iptables -D P2PARTISAN-DROP-OUT -p tcp -m tcp --sport $greyports_tcp -j DROP
  1166. iptables -D P2PARTISAN-DROP-OUT -p udp -m udp --sport $greyports_udp -j DROP
  1167. iptables -D P2PARTISAN-DROP-OUT -p tcp -m tcp --dport $greyports_tcp -j DROP
  1168. iptables -D P2PARTISAN-DROP-OUT -p udp -m udp --dport $greyports_udp -j DROP
  1169. iptables -D P2PARTISAN-DROP-IN -j LOG --log-prefix 'P2Partisan-DEBUG-IN->> ' --log-level 1
  1170. iptables -D P2PARTISAN-DROP-OUT -j LOG --log-prefix 'P2Partisan-DEBUG-OUT->> ' --log-level 1" > ./iptables-debug-del
  1171. chmod 777 ./iptables-debug-del 2> /dev/null
  1172. ./iptables-debug-del 1>/dev/null &
  1173. else
  1174. echo "# $now
  1175. iptables -I P2PARTISAN-DROP-IN 1 -d $1 -j LOG --log-prefix \"P2Partisan-DEBUG-IN->> \" --log-level 1 > /dev/null 2>&1
  1176. iptables -I P2PARTISAN-DROP-OUT 1 -s $1 -j LOG --log-prefix \"P2Partisan-DEBUG-OUT->> \" --log-level 1 > /dev/null 2>&1" > ./iptables-debug
  1177. chmod 777 ./iptables-debug  > /dev/null 2>&1
  1178. plog "Debug started for IP $1 for $minutes minute"
  1179. ./iptables-debug 1>/dev/null &
  1180.                 echo -e "| Enabled full debug logging for LAN IP \033[1;32m$1\033[0;40m for \033[1;32m$minutes\033[0;40m minutes
  1181. | Use \033[1;33m./p2partisan.sh debug-display\033[0;40m to show the debug information
  1182. +---------------------------------------------------------------+"
  1183.  
  1184. echo "# $now
  1185. sleep $time
  1186. iptables -D P2PARTISAN-DROP-IN -d $1 -j LOG --log-prefix \"P2Partisan-DEBUG-IN->> \" --log-level 1  > /dev/null 2>&1
  1187. iptables -D P2PARTISAN-DROP-OUT -s $1 -j LOG --log-prefix \"P2Partisan-DEBUG-OUT->> \" --log-level 1 > /dev/null 2>&1" > ./iptables-debug-del
  1188. chmod 777 ./iptables-debug-del 2> /dev/null
  1189. ./iptables-debug-del 1>/dev/null &
  1190. fi
  1191. }
  1192.  
  1193. pdebugdisplay() {
  1194. echo -e "\033[0;40m
  1195. +------------------------- P2Partisan --------------------------+
  1196. _____         __                          __ __               __              
  1197. |     \.-----.|  |--.--.--.-----.______.--|  |__|.-----.-----.|  |.---.-.--.--.
  1198. |  --  |  -__||  _  |  |  |  _  |______|  _  |  ||__ --|  _  ||  ||  _  |  |  |
  1199. |_____/|_____||_____|_____|___  |      |_____|__||_____|   __||__||___._|___  |
  1200.                         |_____|                      |__|             |_____|
  1201.  
  1202. +---------------------------------------------------------------+
  1203. | p2partisan.sh debug-display               Displays in & outbound debug logs
  1204. | p2partisan.sh debug-display in            Displays inbound debug logs only
  1205. | p2partisan.sh debug-display out           Displays outbound debug logs only
  1206. +-------------------------- Drop Logs --------------------------+"
  1207.  
  1208.    dfrom=`head -1 ./iptables-debug 2> /dev/null | awk '{print $2}'`
  1209. druntime=`echo $(( $now - $dfrom ))`
  1210.       h=`echo $((( $druntime / 3600 ) %24 ))`
  1211.       m=`echo $((( $druntime / 60 ) %60 ))`
  1212.       s=`echo $(( $druntime %60 ))`
  1213. druntime=`printf "%02d:%02d:%02d\n" $h $m $s`
  1214. dendtime=`head -2 ./iptables-debug-del | tail -n 1 | awk '{print $2}'`
  1215.   ttime=`echo $(( $dendtime / 60 ))`
  1216.   ttime=`echo $(( $dfrom + $dendtime ))`
  1217. leftime=`echo $(( $ttime - $now ))`
  1218.       m=`echo $((( $leftime / 60 ) %60 ))`
  1219.       s=`echo $(( $leftime %60 ))`
  1220. leftime=`printf "%02d:%02d:%02d\n" $h $m $s`
  1221. zzztime=`echo $(( $dendtime / 60 ))`
  1222.  
  1223. c=0
  1224. rm ./debug.rev  > /dev/null 2>&1
  1225. tail -800 $logfile | grep -i "P2Partisan" > ./debug.log
  1226. cat ./debug.log | sed '1!G;h;$!d' |
  1227.  (  
  1228.  while read line
  1229.  do
  1230. testo=`echo $line | grep "Debug started for IP" | wc -l`
  1231. if [[ $testo -ge 1 ]]; then
  1232.         echo $line >> ./debug.rev
  1233.         cat ./debug.rev | sed '1!G;h;$!d' > ./debug.log
  1234.         rm ./debug.rev  > /dev/null 2>&1
  1235.         exit
  1236. else
  1237.         echo $line >> ./debug.rev
  1238. fi
  1239. done
  1240. )
  1241.  
  1242. if [ -z $1 ]; then
  1243.         echo -e "\033[48;5;89m+----------------------- INPUT & OUTPUT ------------------------+\033[40m"
  1244.         head -1 ./debug.log
  1245.         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
  1246.           [ $(($c%2)) -eq 1 ] && printf "\e[100m"
  1247.           printf "%s\033[0m\n" "$line"
  1248.           c=$(($c+1))
  1249.                 done
  1250.         fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  1251.                 if [[ $fc -ge 1 ]]; then
  1252.                 echo -e "\e[93mNOTE: debugging is active for $druntime /$zzztime min ($leftime left). Run this command again to update the report\033[0m"
  1253.                 fi
  1254.         echo -e "\033[48;5;89m+----------------------- INPUT & OUTPUT ------------------------+\033[40m"
  1255. elif [[ $1 = "in" ]]; then
  1256.         echo -e "\033[48;5;89m+--------------------------- INPUT -----------------------------+\033[40m"
  1257.         head -1 ./debug.log
  1258.         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
  1259.           [ $(($c%2)) -eq 1 ] && printf "\e[100m"
  1260.           printf "%s\033[0m\n" "$line"
  1261.           c=$(($c+1))
  1262.                 done
  1263.         fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  1264.                 if [[ $fc -ge 1 ]]; then
  1265.                 echo -e "\e[93mNOTE: debugging is active for $druntime /$zzztime min ($leftime left). Run this command again to update the report\033[0m"
  1266.                 fi
  1267.         echo -e "\033[48;5;89m+--------------------------- INPUT -----------------------------+\033[40m"
  1268. elif [[ $1 = "out" ]]; then
  1269.                 echo -e "\033[48;5;89m+--------------------------- OUTPUT ----------------------------+\033[40m"
  1270.                 head -1 ./debug.log
  1271.         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
  1272.           [ $(($c%2)) -eq 1 ] && printf "\e[100m"
  1273.           printf "%s\033[0m\n" "$line"
  1274.           c=$(($c+1))
  1275.                 done
  1276.         fc=`iptables -L P2PARTISAN-DROP-IN | grep DEBUG | wc -l`
  1277.                 if [[ $fc -ge 1 ]]; then
  1278.                 echo -e "\e[93mNOTE: debugging is active for $druntime /$zzztime min ($leftime left). Run this command again to update the report\033[0m"
  1279.                 fi
  1280.                 echo -e "\033[48;5;89m+--------------------------- OUTPUT ----------------------------+\033[40m"
  1281. fi
  1282.     echo -e "+---------------------------------------------------------------+\033[0;39m"
  1283. }
  1284.  
  1285. pwhitelist() {
  1286.         ipset -F whitelist
  1287.  
  1288.         # VPN - Tinc hosts are IP whitelisted
  1289.         if [ `nvram get tinc_wanup` -eq 1 ]; then
  1290.         for IP in `nvram get tinc_hosts | grep -Eo '\w*[a-z]\w*(\.\w*[a-z]\w*)+'`; do
  1291.         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 && nslookup $IP | grep "Address [0-9]*:" | grep -v 127.0.0.1 | grep -v "\:\:" | grep -Eo "([0-9\.]{7,15})" | {
  1292.                                 while read IPO
  1293.                                 do
  1294.                                 ipset -A whitelist ${IPO%*/32} 2> /dev/null
  1295.                                 done
  1296.                                 }
  1297.         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 && ipset -A whitelist $IP 2> /dev/null
  1298.         done
  1299.         fi
  1300.         #/ VPN - Tinc hosts are IP whitelisted
  1301.        
  1302.         [ -f ./whitelist ] && cat ./whitelist | grep -Ev "^#|^$" | tr -d "\r" |
  1303.     (
  1304.     while read IP
  1305.     do             
  1306.                 q=100
  1307.                 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
  1308.                 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
  1309.                 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=2
  1310.                 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
  1311.                 echo "$IP" | awk '{print $2}' | grep -E '^(http)' >/dev/null 2>&1 && q=4
  1312.         if [[ $q -eq 0 ]]; then
  1313.                                 echo $IP | pdeaggregate | {
  1314.                                 while read cidr
  1315.                                 do
  1316.                                         ipset -A whitelist $cidr 2> /dev/null
  1317.                                 done
  1318.                                 }
  1319.         elif [[ $q -eq 1 ]]; then
  1320.                 nslookup $IP | grep "Address [0-9]*:" | grep -v 127.0.0.1 | grep -v "\:\:" | grep -Eo "([0-9\.]{7,15})" |
  1321.                                 while read IPO
  1322.                                 do
  1323.                                 ipset -A whitelist ${IPO%*/32} 2> /dev/null
  1324.                                 done
  1325.         elif [[ $q -eq 2 ]]; then
  1326.                                 ipset -A whitelist ${IP%*/32} 2> /dev/null
  1327.         elif [[ $q -eq 3 ]]; then
  1328.                                 ipset -A whitelist $IP 2> /dev/null
  1329.         elif [[ $q -eq 4 ]]; then
  1330.         # SORT OUT
  1331.                                 url=`echo $IP | awk '{print $2}'`
  1332.                                 # deaggregate whitelist $url 3 &
  1333.         fi
  1334.         done
  1335.     )  
  1336. }
  1337.  
  1338. pgreylist() {
  1339.         ipset -F greylist
  1340.     [ -f ./greylist ] && cat ./greylist | grep -Ev "^#|^$" | tr -d "\r" |
  1341.     (
  1342.     while read IP
  1343.     do             
  1344.                 q=100
  1345.                 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
  1346.                 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
  1347.                 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=2
  1348.                 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
  1349.                 echo "$IP" | awk '{print $2}' | grep -E '^(http)' >/dev/null 2>&1 && q=4
  1350.         if [[ $q -eq 0 ]]; then
  1351.                                 echo $IP | pdeaggregate | {
  1352.                                 while read cidr
  1353.                                 do
  1354.                                         ipset -A greylist $cidr 2> /dev/null
  1355.                                 done
  1356.                                 }
  1357.         elif [[ $q -eq 1 ]]; then
  1358.                 nslookup $IP | grep "Address [0-9]*:" | grep -v 127.0.0.1 | grep -v "\:\:" | grep -Eo "([0-9\.]{7,15})" |
  1359.                                 while read IPO
  1360.                                 do
  1361.                                 ipset -A greylist ${IPO%*/32} 2> /dev/null
  1362.                                 done
  1363.         elif [[ $q -eq 2 ]]; then
  1364.                                 ipset -A greylist ${IP%*/32} 2> /dev/null
  1365.         elif [[ $q -eq 3 ]]; then
  1366.                                 ipset -A greylist $IP 2> /dev/null
  1367.         elif [[ $q -eq 4 ]]; then
  1368.         # SORT OUT
  1369.                                 url=`echo $IP | awk '{print $2}'`
  1370.                                 # deaggregate whitelist $url 3 &
  1371.         fi
  1372.         done
  1373.     )  
  1374. }
  1375.  
  1376. pblacklistcustom() {
  1377.         ipset -F blacklist-custom
  1378.     [ -f ./blacklist-custom ] && cat ./blacklist-custom | grep -Ev "^#|^$" | tr -d "\r" |
  1379.     (
  1380.     while read IP
  1381.     do
  1382.                 q=100
  1383.                 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
  1384.                 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
  1385.                 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=2
  1386.                 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
  1387.         if [[ $q -eq 0 ]]; then
  1388.                                 echo $IP | pdeaggregate | {
  1389.                                 while read cidr
  1390.                                 do
  1391.                                     ipset -A whitelist $cidr 2> /dev/null
  1392.                                 done
  1393.                                 }
  1394.         elif [[ $q -eq 1 ]]; then
  1395.                 nslookup $IP | grep "Address [0-9]*:" | grep -v 127.0.0.1 | grep -v "\:\:" | grep -Eo "([0-9\.]{7,15})" |
  1396.                                 while read IPO
  1397.                                 do
  1398.                                 ipset -A blacklist-custom ${IPO%*/32} 2> /dev/null
  1399.                                 done
  1400.         elif [[ $q -eq 2 ]]; then
  1401.                                 ipset -A blacklist-custom ${IP%*/32} 2> /dev/null
  1402.         elif [[ $q -eq 3 ]]; then
  1403.                                 ipset -A blacklist-custom $IP 2> /dev/null
  1404.         fi
  1405.         done
  1406.     )  
  1407. }
  1408.        
  1409. pstart() {
  1410.                
  1411.         running4=`[ -f $pidfile ] && echo 1 || echo 0`
  1412.         if [ $running4 -eq "0" ] ; then
  1413.             [ -f /tmp/p2partisan.loading ] && echo "P2Partisan is still loading. Exiting..." && exit
  1414.             touch /tmp/p2partisan.loading
  1415.             pre=`date +%s`
  1416.             echo $$ > $pidfile
  1417.        
  1418.     [ -e iptables-add ] && rm iptables-add
  1419.     [ -e iptables-del ] && rm iptables-del
  1420.     [ -e ipset-del ] && rm ipset-del
  1421.  
  1422.         echo -e "\033[0;40m
  1423. +------------------------- P2Partisan --------------------------+
  1424. |                 _______ __               __  
  1425. |                |     __|  |_.---.-.----.|  |_
  1426. |                |__     |   _|  _  |   _||   _|
  1427. |                |_______|____|___._|__|  |____|
  1428. |                                            
  1429. +---------------------------------------------------------------+
  1430. +--------- PREPARATION --------"
  1431.         echo "| Loading the ipset modules"
  1432.         {
  1433.         lsmod | awk '{print $1}' | grep -we "^ip_set" || insmod ip_set
  1434.         lsmod | awk '{print $1}' | grep -we "^xt_set" || insmod xt_set
  1435.         lsmod | awk '{print $1}' | grep -we "^ip_set_hash_net" || insmod ip_set_hash_net
  1436.         } > /dev/null 2>&1
  1437. counter=0
  1438. pos=1
  1439.                 counter=$(printf "%02d" $counter)
  1440.                 echo "+---- CUSTOM IP BLACKLIST -----
  1441. | preparing blacklist-custom ..."
  1442.                 echo -e "| Loading Blacklist_$counter data ---> \033[1;37m***Custom IP blacklist***\033[0;40m"
  1443.                 if [ "$(ipset --swap blacklist-custom blacklist-custom 2>&1 | grep 'does not exist')" != "" ]
  1444.                         then
  1445.                         ipset --create blacklist-custom hash:net hashsize 1024 --resize 5 maxelem 1024000  2> /dev/null
  1446.                         fi                            
  1447.  
  1448.         pblacklistcustom
  1449.  
  1450. [ -e /tmp/iptables-add.tmp ] && rm /tmp/iptables-add.tmp > /dev/null 2>&1
  1451.  
  1452. echo "+--------- GREYPORTs ----------"
  1453. 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
  1454.                         echo -e "| Loading grey TCP ports:  \033[1;37m$w\033[0;40m"
  1455.                         echo "iptables -A P2PARTISAN-IN -i $wanif -p tcp --match multiport --dports $w -g P2PARTISAN-LISTS-IN
  1456. iptables -A P2PARTISAN-OUT -o $wanif -p tcp --match multiport --sports $w -g P2PARTISAN-LISTS-OUT" >> /tmp/iptables-add.tmp
  1457. done
  1458. 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
  1459.                         echo -e "| Loading grey UDP ports:  \033[1;37m$w\033[0;40m"
  1460.                         echo "iptables -A P2PARTISAN-IN -i $wanif -p udp --match multiport --dports $w -g P2PARTISAN-LISTS-IN
  1461. iptables -A P2PARTISAN-OUT -o $wanif -p udp --match multiport --sports $w -g P2PARTISAN-LISTS-OUT" >> /tmp/iptables-add.tmp
  1462. done
  1463. # Get transmission port for greylisting if enabled
  1464. transmissionenable=`nvram get bt_enable`
  1465. if [ -z $transmissionenable ]; then
  1466.     echo "|  TransmissionBT:  Not available"
  1467.     elif [ $transmissionenable -eq 0 ]; then
  1468.     echo "|  TransmissionBT:  Off"
  1469.     else
  1470.     echo -e "|  TransmissionBT:  \033[1;32mOn\033[0;40m"
  1471.         transmissionport=`nvram get bt_port 2> /dev/null`
  1472.         wanip=`nvram get wan_ipaddr`
  1473.         p3=`echo $greyports_tcp | grep -Eo '$transmissionport' | wc -l`
  1474.         p4=`echo $greyports_udp | grep -Eo '$transmissionport' | wc -l`
  1475.         if [ $p3 -eq "0" ]; then
  1476.                         echo "iptables -A P2PARTISAN-IN -i $wanif -p tcp -d $wanip --dport $transmissionport -g P2PARTISAN-LISTS-IN
  1477. iptables -A P2PARTISAN-OUT -o $wanif -p tcp -s $wanip --sport $transmissionport -g P2PARTISAN-LISTS-OUT
  1478. iptables -A P2PARTISAN-OUT -o $wanif -p tcp -s $wanip --sport 49152:65535 -g P2PARTISAN-LISTS-OUT" >> /tmp/iptables-add.tmp
  1479.         fi            
  1480.         if [ $p4 -eq "0" ]; then
  1481.                         echo "iptables -A P2PARTISAN-IN -i $wanif -p udp -d $wanip --dport $transmissionport -g P2PARTISAN-LISTS-IN
  1482. iptables -A P2PARTISAN-OUT -o $wanif -p udp -s $wanip --sport $transmissionport -g P2PARTISAN-LISTS-OUT
  1483. iptables -A P2PARTISAN-OUT -o $wanif -p udp -s $wanip --sport 49152:65535 -g P2PARTISAN-LISTS-OUT" >> /tmp/iptables-add.tmp
  1484.         fi
  1485. fi
  1486.            
  1487. echo "+--------- WHITEPORTs ---------"
  1488. 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
  1489.                 echo -e "| Loading white TCP ports \033[1;37m$w\033[0;40m"
  1490.                 echo "iptables -A P2PARTISAN-IN -i $wanif -p tcp --match multiport --sports $w -j RETURN
  1491. iptables -A P2PARTISAN-IN -i $wanif -p tcp --match multiport --dports $w -j RETURN
  1492. iptables -A P2PARTISAN-OUT -o $wanif -p tcp --match multiport --sports $w -j RETURN
  1493. iptables -A P2PARTISAN-OUT -o $wanif -p tcp --match multiport --dports $w -j RETURN" >> /tmp/iptables-add.tmp
  1494.                 done
  1495. 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
  1496.                 echo -e "| Loading white UDP ports \033[1;37m$w\033[0;40m"
  1497.                 echo "iptables -A P2PARTISAN-IN -i $wanif -p udp --match multiport --sports $w -j RETURN
  1498. iptables -A P2PARTISAN-IN -i $wanif -p udp --match multiport --dports $w -j RETURN
  1499. iptables -A P2PARTISAN-OUT -o $wanif -p udp --match multiport --sports $w -j RETURN
  1500. iptables -A P2PARTISAN-OUT -o $wanif -p udp --match multiport --dports $w -j RETURN" >> /tmp/iptables-add.tmp
  1501.                 done
  1502. echo "iptables -A P2PARTISAN-IN -j P2PARTISAN-LISTS-IN
  1503. iptables -A P2PARTISAN-OUT -j P2PARTISAN-LISTS-OUT" >> /tmp/iptables-add.tmp
  1504.  
  1505.                 echo "# $now
  1506. iptables -N P2PARTISAN-IN
  1507. iptables -N P2PARTISAN-OUT
  1508. iptables -N P2PARTISAN-LISTS-IN
  1509. iptables -N P2PARTISAN-LISTS-OUT
  1510. iptables -N P2PARTISAN-DROP-IN
  1511. iptables -N P2PARTISAN-DROP-OUT
  1512. iptables -F P2PARTISAN-IN
  1513. iptables -F P2PARTISAN-OUT
  1514. iptables -F P2PARTISAN-LISTS-IN
  1515. iptables -F P2PARTISAN-LISTS-OUT
  1516. iptables -F P2PARTISAN-DROP-IN
  1517. iptables -F P2PARTISAN-DROP-OUT
  1518. iptables -A P2PARTISAN-IN -m set  --match-set blacklist-custom src -j P2PARTISAN-DROP-IN
  1519. iptables -A P2PARTISAN-OUT -m set  --match-set blacklist-custom dst -j P2PARTISAN-DROP-OUT" > iptables-add
  1520.  
  1521. #Add winin/wanout for RMerlin compatibility only
  1522. if [ $rm -eq 1 ]; then
  1523.         echo "iptables -N wanin
  1524. iptables -I FORWARD 1 -i $wanif -j wanin
  1525. iptables -N wanout
  1526. iptables -I FORWARD 2 -o $wanif -j wanout" >> ./iptables-add
  1527. fi
  1528. #
  1529.                 echo "# $now" >> iptables-del
  1530. [ -f ./custom-script-del ] && cat ./custom-script-add >> iptables-del
  1531. [ ! -z $vpnif ] && echo "iptables -D INPUT -o $vpnif -m state --state NEW -j P2PARTISAN-IN"  >> iptables-del
  1532. [ ! -z $vpnif ] && echo "iptables -D OUTPUT -i $vpnif -m state --state NEW -j P2PARTISAN-IN"  >> iptables-add
  1533. [ ! -z $vpnif ] && echo "iptables -D FORWARD -o $vpnif -m state --state NEW -j P2PARTISAN-IN"  >> iptables-del
  1534. echo "iptables -D wanin -i $wanif -m state --state NEW -j P2PARTISAN-IN
  1535. iptables -D wanout -o $wanif -m state --state NEW -j P2PARTISAN-OUT
  1536. iptables -D INPUT -i $wanif -m state --state NEW -j P2PARTISAN-IN
  1537. iptables -D OUTPUT -o $wanif -m state --state NEW -j P2PARTISAN-OUT
  1538. iptables -F P2PARTISAN-DROP-IN
  1539. iptables -F P2PARTISAN-DROP-OUT
  1540. iptables -F P2PARTISAN-LISTS-IN
  1541. iptables -F P2PARTISAN-LISTS-OUT
  1542. iptables -F P2PARTISAN-IN
  1543. iptables -F P2PARTISAN-OUT
  1544. iptables -X P2PARTISAN-IN
  1545. iptables -X P2PARTISAN-OUT
  1546. iptables -X P2PARTISAN-LISTS-IN
  1547. iptables -X P2PARTISAN-LISTS-OUT
  1548. iptables -X P2PARTISAN-DROP-IN
  1549. iptables -X P2PARTISAN-DROP-OUT" >> iptables-del
  1550.  
  1551. echo "+--------- GREY IPs ---------"
  1552. echo "| preparing IP greylist ..."
  1553. #Load the whitelist
  1554.         if [ "$(ipset --swap greylist greylist 2>&1 | grep 'does not exist')" != "" ]
  1555.                 then
  1556.                         ipset --create greylist hash:net hashsize 16 --resize 5 maxelem 255  > /dev/null 2>&1          
  1557.         fi
  1558.         pgreylist
  1559.                         echo -e "| Loading IP greylist data ---> \033[1;37m***IP greylist***\033[0;40m"
  1560.                         echo "iptables -A P2PARTISAN-IN -m set  --match-set greylist src -g P2PARTISAN-LISTS-IN
  1561. iptables -A P2PARTISAN-IN -m set  --match-set greylist dst -g P2PARTISAN-LISTS-IN
  1562. iptables -A P2PARTISAN-OUT -m set  --match-set greylist src -g P2PARTISAN-LISTS-OUT
  1563. iptables -A P2PARTISAN-OUT -m set  --match-set greylist dst -g P2PARTISAN-LISTS-OUT" >> iptables-add
  1564.  
  1565.  
  1566. echo "+--------- WHITE IPs ---------"
  1567. echo "| preparing IP whitelist ..."
  1568. #Load the whitelist
  1569.         if [ "$(ipset --swap whitelist whitelist 2>&1 | grep 'does not exist')" != "" ]
  1570.                 then
  1571.                         ipset --create whitelist hash:net hashsize 1024 --resize 5 maxelem 1024000  > /dev/null 2>&1          
  1572.         fi
  1573.         pwhitelist
  1574.  
  1575.         echo "# $now
  1576. ipset -F
  1577. ipset -X blacklist-custom
  1578. ipset -X greylist
  1579. ipset -X whitelist" > ipset-del
  1580.  
  1581.                         echo -e "| Loading IP whitelist data ---> \033[1;37m***IP Whitelist***\033[0;40m"
  1582.                         echo "iptables -A P2PARTISAN-IN -m set  --match-set whitelist src -j RETURN
  1583. iptables -A P2PARTISAN-IN -m set  --match-set whitelist dst -j RETURN
  1584. iptables -A P2PARTISAN-OUT -m set  --match-set whitelist src -j RETURN
  1585. iptables -A P2PARTISAN-OUT -m set  --match-set whitelist dst -j RETURN" >> iptables-add
  1586.  
  1587. cat /tmp/iptables-add.tmp >> ./iptables-add
  1588. rm /tmp/iptables-add.tmp > /dev/null 2>&1
  1589.  
  1590.                 if [ $syslogs -eq "1" ]; then        
  1591. echo "iptables -A P2PARTISAN-DROP-IN -m limit --limit $maxloghour/hour --limit-burst 1 -j LOG --log-prefix 'P2Partisan Dropped IN - ' --log-level 1
  1592. iptables -A P2PARTISAN-DROP-OUT -m limit --limit $maxloghour/hour  --limit-burst 1 -j LOG --log-prefix 'P2Partisan Rejected OUT - ' --log-level 1" >> iptables-add
  1593.                 fi
  1594.                 echo "iptables -A P2PARTISAN-DROP-IN -j DROP
  1595. iptables -A P2PARTISAN-DROP-OUT -j REJECT --reject-with icmp-admin-prohibited"  >> iptables-add
  1596.  
  1597.  
  1598. echo "+------- IP BLACKLISTs -------"
  1599.        
  1600.         cat blacklists | grep -Ev "^#|^$" | tr -d "\r" |
  1601.    (
  1602.     while read line
  1603.     do
  1604.             counter=`expr $counter + 1`
  1605.             counter=$(printf "%02d" $counter)          
  1606.             name=`echo $line | awk '{print $1}'`
  1607.             url=`echo $line | awk '{print $2}'`
  1608.      
  1609.         if [ "$(ipset swap "$name.bro" "$name.bro" 2>&1 | grep 'does not exist')" != "" ]
  1610.             then
  1611.             ipset --create "$name.bro" hash:net hashsize 1024 --resize 5 maxelem 4096000 > /dev/null
  1612.         fi
  1613.         if [ "$(ipset swap $name $name 2>&1 | grep 'does not exist')" != "" ]
  1614.             then
  1615.                 [ -f ./$name.cidr ] && cat ./$name.cidr | cut -d" " -f3 | grep -F "$adminip" > /dev/null && complete=1 || complete=0
  1616.                 if [ $complete -eq 1 ]; then                #.cidr exists and populated, using it
  1617.                         echo -e "| Async loading [\033[1;32m Cached \033[0;40m] Blacklist_$counter --> \033[1;37m***$name***\033[0;40m"
  1618.                         {
  1619.                         ipset -F $name
  1620.                         ipset -X $name
  1621.                         ipset --create $name hash:net hashsize 1024 --resize 5 maxelem 4096000
  1622. #                       echo 4 [e][e][o]
  1623. #                       echo "/tmp/deaggregate.sh "$name" "-" "2" "$pre" "$name.bro" "$maxconcurrentlistload" "$P2Partisandir" "$adminip" &"
  1624.                         deaggregate "$name" "-" "2" "$pre" "-" "$maxconcurrentlistload" "$P2Partisandir" "$adminip" &
  1625.                         } 2> /dev/null
  1626.                 else                                        #fresh load/first run
  1627.                                     # if exists what type is it (iblocklist (1), raw or ascii (5)?
  1628.                     wget -O "list.$name" "$url"  >/dev/null 2>&1
  1629.                         # contents_of_file() {
  1630.                     if gzip -t <"list.$name" >/dev/null 2>&1; then
  1631.                         listtype=1
  1632.                     else
  1633.                         listtype=5
  1634.                     fi
  1635.                    
  1636.                         echo -e "| Async loading [\033[1;35mComputed\033[0;40m] Blacklist_$counter --> \033[1;37m***$name***\033[0;40m"
  1637.                         {
  1638.                         ipset -F $name
  1639.                         ipset -X $name
  1640.                         ipset --create $name hash:net hashsize 1024 --resize 5 maxelem 4096000
  1641. #                       echo 5 [e][e][e]
  1642. #                       echo "/tmp/deaggregate.sh "$name" "$url" "$listtype" "$pre" "$name.bro" "$maxconcurrentlistload" "$P2Partisandir" "$admin" &"
  1643.                         deaggregate "$name" "$url" "$listtype" "$pre" "-" "$maxconcurrentlistload" "$P2Partisandir" "$adminip" &
  1644.                                              # 5 = Do not convert but add to ipset and create CIDR (e.g. raw and netset)
  1645.                                              # 4 = On the fly record by record STOUT output
  1646.                                              # 3 = add from public whitelist sIP-dIP to ipset only
  1647.                                              # 2 = add from .cidr to ipset only
  1648.                                              # 1 = convert + add live + create .cidr file (very slow)
  1649.                                              # 0 = convert + add live + create ipset dump
  1650.                                              # different = convert + add to ipset + create .cidr file
  1651.                         } 2> /dev/null                   
  1652.                 fi
  1653.         fi
  1654.  
  1655.         echo "ipset -X $name " >> ipset-del
  1656.         echo "iptables -A P2PARTISAN-LISTS-IN -m set  --match-set $name src -j P2PARTISAN-DROP-IN
  1657. iptables -A P2PARTISAN-LISTS-OUT -m set  --match-set $name dst -j P2PARTISAN-DROP-OUT" >> iptables-add
  1658.     done
  1659.     )
  1660.  
  1661. echo "iptables -I INPUT $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN
  1662. iptables -I OUTPUT $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT
  1663. iptables -I wanin $pos -i $wanif -m state --state NEW -j P2PARTISAN-IN
  1664. iptables -I wanout $pos -o $wanif -m state --state NEW -j P2PARTISAN-OUT" >> iptables-add
  1665.  
  1666. [ ! -z $vpnif ] && echo "iptables -I INPUT $pos -o $vpnif -m state --state NEW -j P2PARTISAN-IN"  >> iptables-add
  1667. [ ! -z $vpnif ] && echo "iptables -I OUTPUT $pos -i $vpnif -m state --state NEW -j P2PARTISAN-IN"  >> iptables-add
  1668. [ ! -z $vpnif ] && echo "iptables -I FORWARD $pos -o $vpnif -m state --state NEW -j P2PARTISAN-IN"  >> iptables-add
  1669.  
  1670. #Add winin/wanout for RMerlin compatibility only
  1671. if [ $rm -eq 1 ]; then
  1672. echo "iptables -F wanin
  1673. iptables -X wanin
  1674. iptables -D FORWARD -i $wanif -j wanin
  1675. iptables -F wanout
  1676. iptables -X wanout
  1677. iptables -D FORWARD -o $wanif -j wanout" >> iptables-del
  1678. fi
  1679. #
  1680.  
  1681. [ -f ./custom-script-add ] && cat ./custom-script-add >> iptables-add
  1682.  
  1683. chmod 777 ./iptables-*
  1684. chmod 777 ./ipset-*
  1685. ./iptables-del 2> /dev/null #cleaning
  1686. ./iptables-add 2> /dev/null  #protecting
  1687.  
  1688. plog "... P2Partisan started"
  1689. echo "+------------------------- Controls ----------------------------+"
  1690.  
  1691. p=`nvram get dnsmasq_custom | grep log-async | wc -l`
  1692. if [ $p -eq "1" ]; then
  1693.         plog "log-async found under dnsmasq -> OK"
  1694. echo "+---------------------------------------------------------------+"
  1695. else
  1696.         plog "
  1697. | It appears like you don't have a log-async parameter in your dnsmasq
  1698. | config. This is strongly suggested due to the amount of logs involved,
  1699. | especially while debugging to consider adding the following command
  1700. | under Advanced/DHCP/DNS/Dnsmasq Custom configuration:
  1701. |  
  1702. | log-async=20
  1703. |  
  1704. +---------------------------------------------------------------+\033[0;39m"
  1705.     fi
  1706. p=`nvram get script_fire | grep "cru a P2Partisan-tutor" | wc -l`
  1707.         if [ $p -eq "0" ] ; then
  1708.         ptutorset
  1709.         fi
  1710.  
  1711.         post=`date +%s`
  1712.         [ -f /tmp/p2partisan.loading ] && rm -r "/tmp/p2partisan.loading" >/dev/null 2>&1
  1713.         else
  1714.         echo -e "\033[0;40m
  1715. +------------------------- P2Partisan --------------------------+
  1716. |                 _______ __               __  
  1717. |                |     __|  |_.---.-.----.|  |_
  1718. |                |__     |   _|  _  |   _||   _|
  1719. |        already |_______|____|___._|__|  |____| ed
  1720. |                                            
  1721. +---------------------------------------------------------------+
  1722. | It appears like P2Partisan is already running. Skipping...
  1723. |                      
  1724. | Is this is not what you expected? Try:
  1725. | \033[1;33m./p2partisan.sh update\033[0;40m
  1726. +---------------------------------------------------------------+
  1727.                \033[0;39m"
  1728.         fi
  1729. }
  1730.  
  1731. b64(){
  1732. awk 'BEGIN{b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"}
  1733. {for(i=1;i<=length($0);i++){c=index(b64,substr($0,i,1));if(c--)
  1734. for(b=0;b<6;b++){o=o*2+int(c/32);c=(c*2)%64;if(++obc==8){if(o)
  1735. {printf"%c",o}else{system("echo -en \"\\0\"")}obc=o=0}}}}';}
  1736.  
  1737. pdeaggregate() {
  1738. awk '
  1739. function ip2int(ip) {
  1740. for (ret=0,n=split(ip,a,"\."),x=1;x<=n;x++) ret=or(lshift(ret,8),a[x])
  1741. return ret
  1742. }
  1743.  
  1744. function int2ip(ip,ret,x) {
  1745. ret=and(ip,255)
  1746. ip=rshift(ip,8)
  1747. for(;x<3;ret=and(ip,255)"."ret,ip=rshift(ip,8),x++);
  1748. return ret
  1749. }
  1750.  
  1751. BEGIN {
  1752. bits=0xffffffff
  1753. FS="[-]"
  1754. }
  1755.  
  1756. {
  1757. base=ip2int($1)
  1758. end=ip2int($2)
  1759. while (base <= end) {
  1760. step = 0
  1761. while ( or(base, lshift(1, step)) != base) {
  1762. if ( or(base, rshift((bits, (31-step)))) > end ) {
  1763. break;
  1764. }
  1765. step++
  1766. }
  1767. print int2ip(base)"/"(32-step)
  1768. base = base + lshift(1, step)
  1769. }
  1770. }
  1771.  
  1772. '  #end of awk script
  1773. }
  1774.  
  1775. for p in $1
  1776. do
  1777. case "$p" in
  1778.         "start")
  1779.                 pstart
  1780.                         exit
  1781.                 ;;            
  1782.         "stop")
  1783.                 pforcestop
  1784.                         exit
  1785.                 ;;
  1786.         "restart")
  1787.                 psoftstop
  1788.                 ;;
  1789.         "status")
  1790.                 pstatus $2
  1791.                         exit                          
  1792.                 ;;
  1793.         "pause")
  1794.                 psoftstop
  1795.                         exit
  1796.                 ;;
  1797.         "detective")          
  1798.                 pdetective
  1799.                         exit
  1800.                 ;;
  1801.         "test")
  1802.                 ptest $2
  1803.                         exit
  1804.                 ;;    
  1805.         "debug")
  1806.                 pdebug $2 $3
  1807.                         exit
  1808.                 ;;  
  1809.         "debug-display")
  1810.                 pdebugdisplay $2
  1811.                         exit
  1812.                 ;;                            
  1813.         "update")
  1814.                 pforcestop $2
  1815.                         echo "| Now updating..."
  1816.                         ;;
  1817.         "autorun-on")
  1818.                         pautorunset
  1819.                         exit
  1820.         ;;
  1821.         "autorun-off")
  1822.                         pautorununset
  1823.                         exit
  1824.         ;;
  1825.         "tutor")
  1826.                         ptutor
  1827.                         exit
  1828.                         ;;
  1829.         "upgrade")
  1830.                         pupgrade
  1831.                         ;;
  1832.         "upgrade-beta")
  1833.                         pupgradebeta
  1834.                         ;;                            
  1835.         "help")
  1836.                
  1837.                 echo -e "\033[48;5;89m
  1838.      ______ ______ ______              __   __                                        
  1839.     |   __ \__    |   __ \.---.-.----.|  |_|__|.-----.---.-.-----.                    
  1840.     |    __/    __|    __/|  _  |   _||   _|  ||__ --|  _  |     |                    
  1841.     |___|  |______|___|   |___._|__|  |____|__||_____|___._|__|__| $version
  1842. \e[39m\e[49m\033[0;40m
  1843.            
  1844.       help                    Display this text              
  1845.       \e[97mstart                   Starts the process (this runs also if no option is provided)
  1846.       stop                    Stops P2Partisan
  1847.       restart                 Soft restart, updates whiteports & whitelist only
  1848.       pause                   Soft stop P2Partisan allowing for quick start
  1849.       update                  Hard restart, slow removes p2partisan, updates
  1850.                               the lists and does a fresh start
  1851.       update <list|fix>       Updated the selected list only | remove cidr a start from scratch\e[39m
  1852.       status                  Display P2Partisan running status + extra information
  1853.       status <list>           Display P2Partisan detailed list information
  1854.       \e[93mtest <IP|FQDN>          Verify existence of the given IP against lists
  1855.       debug                   Shows a guide on how to operate debug
  1856.       debug-display <in|out>  Shows all the logs relevant to the last debug only
  1857.       detective               Determines highest impact IPs:ports (number of sessions)
  1858.       \e[36mautorun-on              Sets P2Partisan to boot with the router
  1859.       autorun-off             Sets P2Partisan not to boot with the router
  1860.       upgrade                 Download and install the latest P2Partisan
  1861. \033[0;39m"
  1862.                                 exit
  1863.                 ;;
  1864.         *)
  1865.                                 echo -e "\033[0;40mparameter not valid. please run:
  1866.                            
  1867.       p2partisan.sh help
  1868.       \033[0;39m"
  1869.                                 exit
  1870.                                 ;;
  1871.  
  1872. esac
  1873. done
  1874.  
  1875. pstart
  1876.  
  1877.  
  1878. exit
Add Comment
Please, Sign In to add comment