rs232

p2partisan 6.00

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