Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 43.04 KB | None | 0 0
  1. #!/bin/sh
  2. ##FreshJR_QOS_v7 released 08/03/2018
  3. ##Script Tested on ASUS AC-68U, FW384.4, using Adaptive QOS with Manual Bandwidth Settings
  4. ##Script Changes Unidentified Packet QOS destination from "Default" Traffic Container (Category7) into user definable (in WebUI) "Others" Traffic Container
  5. ##Script Changes Minimum Guaranteed Bandwidth per QOS category from 128Kbit into user defined percentages of upload and download.
  6. ##Script moves "Defaults" (unidentified traffic) Traffic Container priority into position 6 (originally position 7)
  7. ##  This moves the users "bottom traffic container" into position 7 from position position 6
  8. ##  In simplest terms, the user defined "bottom traffic container" will have worse priority than unidentified traffic
  9. ##Script supports custom QOS rules. To create rules, copy applicable rules from templates below, change filter parameters as desired, and then paste into applicable location.
  10. ##  Included custom rule moves any UDP traffic on ports 500 & 4500 into VOIP traffic Container.             (Wifi Calling)
  11. ##  Included custom rule moves any UDP traffic on ports 16384 - 16415 into VOIP traffic Container.          (Facetime)
  12. ##  Included custom rule moves any Snapchat traffic into Others traffic Container.                          (Snapchat)
  13. ##  Included custom rule moves any Speedtest.net traffic into Downloads traffic Container.                  (Speedtest.net)
  14. ##  Included custom rule moves any Google Play Store traffic into Downloads traffic Container.              (Google Play)
  15. ##  Included custom rule moves any Apple AppStore traffic into Downloads traffic Container.                 (Apple AppStore)
  16. ##  Included custom rule moves any Gaming traffic from ports 80 & 443 into Defaults traffic Container.      (Gaming)
  17. ##     port 80 & 443 is primarily game downloads & patches BUT game login protocols are also caught within
  18. ##  Included, but disabled by default (commented out) is an additional gaming device oriented rule          (Gaming)
  19. ##     The use of this gaming rule REQUIRES gaming devices to have a static dhcp ip assignment (in webUI)  <<AND>>
  20. ##     this static IP needs to once AGAIN be specified within this scripts up/down gaming rules, using CIDR notation
  21. ##     The gaming rule takes unidentified traffic for user specified devices and routes it into the Gaming traffic Container.
  22. #
  23. #---------------------------------------------------------------------------------------------------------------
  24. #   VALID FLOW ID'S FOR TC APP REDIRECTION
  25. #    ${VOIP}, ${Gaming}, ${Others}, ${Web}, ${Streaming}, ${Downloads}, ${Net}, ${Defaults}
  26. #
  27. #   VALID MARKS FOR **DOWNLOAD** IPTABLES RULES
  28. #    ${VOIP_mark_down}, ${Gaming_mark_down}, ${Others_mark_down}, ${Web_mark_down}, ${Streaming_mark_down}, ${Downloads_mark_down}, ${Net_mark_down}, ${Default_mark_down}
  29. #
  30. #   VALID MARKS FOR **UPLOAD** IPTABLES RULES
  31. #    ${VOIP_mark_up}, ${Gaming_mark_up}, ${Others_mark_up}, ${Web_mark_up}, ${Streaming_mark_up}, ${Downloads_mark_up}, ${Net_mark_up}, ${Default_mark_up}
  32. #
  33. #   DOWNLOAD/INCOMMING TRAFFIC rule templates.  See comments next to rule for details.  Make sure to correctly define TCP or UDP designation.
  34. #    See comments next to rule for details.  
  35. #    Make sure to correctly define TCP or UDP designation for port rules.
  36. #
  37. #    iptables -D POSTROUTING -t mangle -o br0 -p tcp --dport 1234:1236 -j MARK --set-mark ${Downloads_mark_down} &> /dev/null       #Template Rule 1    (Download traffic towards LAN (local) device ports 1234-1236 via TCP    goes to "Downloads" Traffic Container)
  38. #    iptables -A POSTROUTING -t mangle -o br0 -p tcp --dport 1234:1236 -j MARK --set-mark ${Downloads_mark_down}                                          (XXXX:YYYY defines port range, XXXX defines for single port)
  39. #                                                                                                                                  
  40. #    iptables -D POSTROUTING -t mangle -o br0 -d 192.168.2.123/32 -j MARK --set-mark ${Downloads_mark_down} &> /dev/null            #Template Rule 2    (Download traffic towards LAN (local) device 192.168.2.123              goes to "Downloads" Traffic Container)
  41. #    iptables -A POSTROUTING -t mangle -o br0 -d 192.168.2.123/32 -j MARK --set-mark ${Downloads_mark_down}                                               ( /32 CIDR mask defines only one ip, see CIDR calculator ip ranges)
  42. #                                                                                                                                  
  43. #    iptables -D POSTROUTING -t mangle -o br0 -p udp --sport 5555 -j MARK --set-mark ${VOIP_mark_down} &> /dev/null                 #Template Rule 3    (Download traffic from WAN (remote) server port 5555 via UDP)           goes to "VOIP" Traffic Container)
  44. #    iptables -A POSTROUTING -t mangle -o br0 -p udp --sport 5555 -j MARK --set-mark ${VOIP_mark_down}                                                    (XXXX:YYYY defines port range, XXXX defines for single port)
  45. #                                                                                                                                                  
  46. #    iptables -D POSTROUTING -t mangle -o br0 -s 75.75.75.75/32 -j MARK --set-mark ${VOIP_mark_down} &> /dev/null                   #Template Rule 4    (Download traffic from WAN (remote) server 75.75.75.75                  goes to "VOIP" Traffic Container)
  47. #    iptables -A POSTROUTING -t mangle -o br0 -s 75.75.75.75/32 -j MARK --set-mark ${VOIP_mark_down}                                                      ( /32 CIDR mask defines only one ip, see CIDR calculator ip ranges)
  48. #
  49. #
  50. #   UPLOAD/OUTOING TRAFFIC rule templates.     
  51. #    See comments next to rule for details.  
  52. #    Make sure to correctly define TCP or UDP designation for port rules.
  53. #
  54. #     iptables -D POSTROUTING -t mangle -o $wan -p tcp --sport 1234:1236 -j MARK --set-mark ${Downloads_mark_up} &> /dev/null       #Template Rule 1      (Upload traffic from LAN (local) device ports 1234-1236 via TCP)      goes to "Downloads" Traffic Container)  
  55. #     iptables -A POSTROUTING -t mangle -o $wan -p tcp --sport 1234:1236 -j MARK --set-mark ${Downloads_mark_up}                                            (XXXX:YYYY defines port range, XXXX defines for single port)
  56. #                                                                                                                                  
  57. #     iptables -D POSTROUTING -t mangle -o $wan -s 192.168.2.123/32 -j MARK --set-mark ${Downloads_mark_up} &> /dev/null            #Template Rule 2      (Upload traffic from LAN (local) device ip 192.168.2.123              goes to "Downloads" Traffic Container)
  58. #     iptables -A POSTROUTING -t mangle -o $wan -s 192.168.2.123/32 -j MARK --set-mark ${Downloads_mark_up}                                                 ( /32 CIDR mask defines only one ip, see CIDR calculator ip ranges)
  59. #                                                                                                                                  
  60. #     iptables -D POSTROUTING -t mangle -o $wan -p udp --dport 5555 -j MARK --set-mark ${VOIP_mark_up} &> /dev/null                 #Template Rule 3      (Upload traffic towards WAN (remote) server port 5555 via UDP         goes to "VOIP" Traffic Container)
  61. #     iptables -A POSTROUTING -t mangle -o $wan -p udp --dport 5555 -j MARK --set-mark ${VOIP_mark_up}                                                      (XXXX:YYYY defines port range, XXXX defines for single port)
  62. #                                                                                                                                  
  63. #     iptables -D POSTROUTING -t mangle -o $wan -d 75.75.75.75/32 -j MARK --set-mark ${VOIP_mark_up} &> /dev/null                   #Template Rule 4      (Upload traffic towards WAN (remote) server 75.75.75.75               goes to "VOIP" Traffic Container)
  64. #     iptables -A POSTROUTING -t mangle -o $wan -d 75.75.75.75/32 -j MARK --set-mark ${VOIP_mark_up}                                                        ( /32 CIDR mask defines only one ip, see CIDR calculator ip ranges)
  65. #                                                                                                                                  
  66. #---------------------------------------------------------------------------------------------------------------                       
  67.  
  68.  
  69. ####################  Bandwidth Setup #####################
  70.  
  71.     user_variables() {
  72.         #Percent of download speed guaranteed per QOS category, change below as desired     (minimum value per section 5, sum should not be greater than 100)
  73.         NetControl_DownBandPercent=5                    #This value can be adjust as desired        **  no spaces before or after the "=" sign **
  74.         VoIP_DownBandPercent=20                         #This value can be adjust as desired        **                no decimals              **
  75.         Gaming_DownBandPercent=15                       #This value can be adjust as desired
  76.         Others_DownBandPercent=10                       #This value can be adjust as desired        #Note: New destination for unidentified traffic
  77.         WebSurfing_DownBandPercent=10                   #This value can be adjust as desired
  78.         Video_DownBandPercent=30                        #This value can be adjust as desired
  79.         FileTransfer_DownBandPercent=5                  #This value can be adjust as desired
  80.         Default_DownBandPercent=5                       #This value can be adjust as desired        #Note: Original destination for unidentified traffic, repurposed for "Gaming Downloads on ports 80/443"
  81.    
  82.         #Percent of upload speed guaranteed per QOS category, change below as desired       (minimum value per section 5, sum should not be greater than 100)
  83.         NetControl_UpBandPercent=5                      #This value can be adjust as desired
  84.         VoIP_UpBandPercent=20                           #This value can be adjust as desired
  85.         Gaming_UpBandPercent=15                         #This value can be adjust as desired
  86.         Others_UpBandPercent=30                         #This value can be adjust as desired        #Note: New destination for unidentified traffic
  87.         WebSurfing_UpBandPercent=10                     #This value can be adjust as desired
  88.         Video_UpBandPercent=10                          #This value can be adjust as desired
  89.         FileTransfer_UpBandPercent=5                    #This value can be adjust as desired
  90.         Default_UpBandPercent=5                         #This value can be adjust as desired        #Note: Original destination for unidentified traffic, repurposed for "Gaming Downloads on ports 80/443"
  91.     }
  92.  
  93.  
  94. ####################  Custom Rules Setup #####################
  95.    
  96.     iptable_down_rules() {
  97.         echo "Applying - Iptable Down Rules"
  98.         ##DOWNLOAD (INCOMMING TRAFFIC) CUSTOM RULES START HERE 
  99.            
  100.             iptables -D POSTROUTING -t mangle -o br0 -p udp --sport 500 -j MARK --set-mark ${VOIP_mark_down} &> /dev/null                                               #Wifi Calling (1/2) - (All incoming traffic w/ WAN source port 500  goes to "VOIP" Traffic Container)                              
  101.             iptables -A POSTROUTING -t mangle -o br0 -p udp --sport 500 -j MARK --set-mark ${VOIP_mark_down}
  102.            
  103.             iptables -D POSTROUTING -t mangle -o br0 -p udp --sport 4500 -j MARK --set-mark ${VOIP_mark_down} &> /dev/null                                              #Wifi Calling (2/2) - (All incoming traffic w/ WAN source port 4500 goes to "VOIP" Traffic Container)
  104.             iptables -A POSTROUTING -t mangle -o br0 -p udp --sport 4500 -j MARK --set-mark ${VOIP_mark_down}
  105.            
  106.             iptables -D POSTROUTING -t mangle -o br0 -p udp --dport 16384:16415 -j MARK --set-mark ${VOIP_mark_down} &> /dev/null                                       #Facetime
  107.             iptables -A POSTROUTING -t mangle -o br0 -p udp --dport 16384:16415 -j MARK --set-mark ${VOIP_mark_down}
  108.            
  109.             iptables -D POSTROUTING -t mangle -o br0 -m mark --mark 0x80080000/0xc03f0000 -p tcp --sport 80 -j MARK --set-mark ${Default_mark_down} &> /dev/null        #Gaming (1/3) - Routes "Gaming" traffic coming from port 443 into "Defaults"
  110.             iptables -A POSTROUTING -t mangle -o br0 -m mark --mark 0x80080000/0xc03f0000 -p tcp --sport 80 -j MARK --set-mark ${Default_mark_down}
  111.            
  112.             iptables -D POSTROUTING -t mangle -o br0 -m mark --mark 0x80080000/0xc03f0000 -p tcp --sport 443 -j MARK --set-mark ${Default_mark_down} &> /dev/null       #Gaming (2/3) - Routes "Gaming" traffic coming from port 80 into "Defaults"
  113.             iptables -A POSTROUTING -t mangle -o br0 -m mark --mark 0x80080000/0xc03f0000 -p tcp --sport 443 -j MARK --set-mark ${Default_mark_down}
  114.            
  115.             #iptables -D POSTROUTING -t mangle -o br0 -d 192.168.2.100/30 -m mark --mark 0x80000000/0x8000ffff -p tcp -m multiport ! --sports 443,80  -j MARK --set-mark ${Gaming_mark_down} &> /dev/null       #Gaming (3/3) - Routes Unidentified Traffic into "Gaming", instead of "Others", for LAN clients specified
  116.             #iptables -A POSTROUTING -t mangle -o br0 -d 192.168.2.100/30 -m mark --mark 0x80000000/0x8000ffff -p tcp -m multiport ! --sports 443,80  -j MARK --set-mark ${Gaming_mark_down}
  117.            
  118.             #iptables -D POSTROUTING -t mangle -o br0 -d 192.168.2.100/30 -m mark --mark 0x80000000/0x8000ffff -p udp -m multiport ! --sports 443,80  -j MARK --set-mark ${Gaming_mark_down} &> /dev/null       #Gaming (3/3) - Routes Unidentified Traffic into "Gaming", instead of "Others", for LAN clients specified
  119.             #iptables -A POSTROUTING -t mangle -o br0 -d 192.168.2.100/30 -m mark --mark 0x80000000/0x8000ffff -p udp -m multiport ! --sports 443,80  -j MARK --set-mark ${Gaming_mark_down}           
  120.         ##DOWNLOAD (INCOMMING TRAFFIC) CUSTOM RULES END HERE
  121.     }
  122.  
  123.     iptable_up_rules(){
  124.        
  125.         #wan="ppp0"             ## WAN interface over-ride for upload traffic -- Variable ONLY needs to be defined for users non-Rmerlin 384+ firmware
  126.                                 # RMerlin v384+ Firmware AUTOMATICALLY detects correct interface --> this variable should be left COMMENTED/DISABLED on RMerlin v384+
  127.                                 # Other firmwares are configured to assume an eth0 interface --> this variable should be manually set if the connection type differs from eth0 ( ppp0, vlanXXX, etc)
  128.                                
  129.         echo "Applying - Iptable Up   Rules ($wan)"
  130.  
  131.         ##UPLOAD (OUTGOING TRAFFIC) CUSTOM RULES START HERE
  132.        
  133.             iptables -D POSTROUTING -t mangle -o $wan -p udp --dport 500 -j MARK --set-mark ${VOIP_mark_up} &> /dev/null                                            #Wifi Calling (1/2) - (All outgoing traffic w/ WAN destination port 500  goes to "VOIP" Traffic Container)                                         
  134.             iptables -A POSTROUTING -t mangle -o $wan -p udp --dport 500 -j MARK --set-mark ${VOIP_mark_up}
  135.            
  136.             iptables -D POSTROUTING -t mangle -o $wan -p udp --dport 4500 -j MARK --set-mark ${VOIP_mark_up} &> /dev/null                                           #Wifi Calling (2/2) - (All outgoing traffic w/ WAN destination port 4500 goes to "VOIP" Traffic Container)
  137.             iptables -A POSTROUTING -t mangle -o $wan -p udp --dport 4500 -j MARK --set-mark ${VOIP_mark_up}
  138.            
  139.             iptables -D POSTROUTING -t mangle -o $wan -p udp --sport 16384:16415 -j MARK --set-mark ${VOIP_mark_up} &> /dev/null                                    #Facetime
  140.             iptables -A POSTROUTING -t mangle -o $wan -p udp --sport 16384:16415 -j MARK --set-mark ${VOIP_mark_up}
  141.            
  142.             iptables -D POSTROUTING -t mangle -o $wan -m mark --mark 0x40080000/0xc03f0000 -p tcp --sport 80 -j MARK --set-mark ${Default_mark_up} &> /dev/null     #Gaming (1/3) - Routes "Gaming" traffic going to port 443 into "Defaults"
  143.             iptables -A POSTROUTING -t mangle -o $wan -m mark --mark 0x40080000/0xc03f0000 -p tcp --sport 80 -j MARK --set-mark ${Default_mark_up}
  144.            
  145.             iptables -D POSTROUTING -t mangle -o $wan -m mark --mark 0x40080000/0xc03f0000 -p tcp --sport 443 -j MARK --set-mark ${Default_mark_up} &> /dev/null    #Gaming (2/3) - Routes "Gaming" traffic going to port 80 into "Defaults"
  146.             iptables -A POSTROUTING -t mangle -o $wan -m mark --mark 0x40080000/0xc03f0000 -p tcp --sport 443 -j MARK --set-mark ${Default_mark_up}
  147.            
  148.             #iptables -D POSTROUTING -t mangle -o $wan -s 192.168.2.100/30 -m mark --mark 0x40000000/0x4000ffff -p tcp -m multiport ! --dports 80,443 -j MARK --set-mark ${Gaming_mark_up} &> /dev/null     #Gaming (3/3) - Routes Unidentified Traffic into "Gaming", instead of "Others", from specified LAN devices in rule (line 1/2)
  149.             #iptables -A POSTROUTING -t mangle -o $wan -s 192.168.2.100/30 -m mark --mark 0x40000000/0x4000ffff -p tcp -m multiport ! --dports 80,443 -j MARK --set-mark ${Gaming_mark_up}
  150.  
  151.             #iptables -D POSTROUTING -t mangle -o $wan -s 192.168.2.100/30 -m mark --mark 0x40000000/0x4000ffff -p udp -m multiport ! --dports 80,443 -j MARK --set-mark ${Gaming_mark_up} &> /dev/null     #Gaming (3/3) - Routes Unidentified Traffic into "Gaming", instead of "Others", from specified LAN devices in rule (line 1/2)
  152.             #iptables -A POSTROUTING -t mangle -o $wan -s 192.168.2.100/30 -m mark --mark 0x40000000/0x4000ffff -p udp -m multiport ! --dports 80,443 -j MARK --set-mark ${Gaming_mark_up}
  153.            
  154.         ##UPLOAD (OUTGOING TRAFFIC) CUSTOM RULES END HERE
  155.     }
  156.    
  157.     tc_redirection_down_rules() {
  158.         echo "Applying  TC Down Rules"
  159.         ${tc} filter del dev br0 parent 1: prio $1                                                                                  #remove original unidentified traffic rule
  160.         ${tc} filter del dev br0 parent 1: prio 22 &> /dev/null                                                                     #remove original HTTPS rule
  161.         ${tc} filter del dev br0 parent 1: prio 23 &> /dev/null                                                                     #remove original HTTPS rule
  162.         ${tc} filter add dev br0 protocol all prio 22 u32 match mark 0x80130000 0xc03f0000 flowid ${Web}                            #recreate HTTPS rule with different destination
  163.         ${tc} filter add dev br0 protocol all prio 23 u32 match mark 0x80140000 0xc03f0000 flowid ${Web}                            #recreate HTTPS rule with different destination
  164.         ##DOWNLOAD APP_DB TRAFFIC REDIRECTION RULES START HERE 
  165.            
  166.             ${tc} filter add dev br0 protocol all prio $1 u32 match mark 0x8000006B 0xc03fffff flowid ${Others}                         #Snapchat
  167.             ${tc} filter add dev br0 protocol all prio 15 u32 match mark 0x800D0007 0xc03fffff flowid ${Downloads}                      #Speedtest.net
  168.             ${tc} filter add dev br0 protocol all prio 15 u32 match mark 0x800D0086 0xc03fffff flowid ${Downloads}                      #Google Play
  169.             ${tc} filter add dev br0 protocol all prio 15 u32 match mark 0x800D00A0 0xc03fffff flowid ${Downloads}                      #Apple AppStore
  170.             ${tc} filter add dev br0 protocol all prio 6 u32 match mark 0x80050004 0xc03fffff flowid ${Downloads}                       #NNTP
  171.            
  172.         ##DOWNLOAD APP_DB TRAFFIC REDIRECTION RULES END HERE   
  173.         ${tc} filter add dev br0 protocol all prio $1 u32 match mark 0x80000000 0x8000ffff flowid ${Others}                         #recreate unidentified traffic rule with different destination - Routes Unidentified Traffic into webUI adjustable "Others" traffic container instead of "Defaults"
  174.         ${tc} filter add dev br0 protocol all prio 10 u32 match mark 0x803f0001 0xc03fffff flowid ${Defaults}                       #Used to achieve iptables Default_mark_down functionality
  175.     }
  176.  
  177.     tc_redirection_up_rules() {
  178.         echo "Applying  TC Up   Rules"
  179.         ${tc} filter del dev eth0 parent 1: prio $1                                                                                 #remove original unidentified traffic rule
  180.         ${tc} filter del dev eth0 parent 1: prio 22 &> /dev/null                                                                    #remove original HTTPS rule
  181.         ${tc} filter del dev eth0 parent 1: prio 23 &> /dev/null                                                                    #remove original HTTPS rule
  182.         ${tc} filter add dev eth0 protocol all prio 22 u32 match mark 0x40130000 0xc03f0000 flowid ${Web}                           #recreate HTTPS rule with different destination
  183.         ${tc} filter add dev eth0 protocol all prio 23 u32 match mark 0x40140000 0xc03f0000 flowid ${Web}                           #recreate HTTPS rule with different destination
  184.         ##UPLOAD APP_DB TRAFFIC REDIRECTION RULES START HERE   
  185.            
  186.             ${tc} filter add dev eth0 protocol all prio $1 u32 match mark 0x4000006B 0xc03fffff flowid ${Others}                        #Snapchat
  187.             ${tc} filter add dev eth0 protocol all prio 15 u32 match mark 0x400D0007 0xc03fffff flowid ${Downloads}                     #Speedtest.net
  188.             ${tc} filter add dev eth0 protocol all prio 15 u32 match mark 0x400D0086 0xc03fffff flowid ${Downloads}                     #Google Play
  189.             ${tc} filter add dev eth0 protocol all prio 15 u32 match mark 0x400D00A0 0xc03fffff flowid ${Downloads}                     #Apple AppStore
  190.             ${tc} filter add dev eth0 protocol all prio 6 u32 match mark 0x40050004 0xc03fffff flowid ${Downloads}                      #NNTP
  191.            
  192.         ##UPLOAD APP_DB TRAFFIC REDIRECTION RULES END HERE
  193.         ${tc} filter add dev eth0 protocol all prio $1 u32 match mark 0x40000000 0x4000ffff flowid ${Others}                        #recreate unidentified traffic rule with different destination - Routes Unidentified Traffic into webUI adjustable "Others" traffic container, instead of "Default" traffic container      
  194.         ${tc} filter add dev eth0 protocol all prio 10 u32 match mark 0x403f0001 0xc03fffff flowid ${Defaults}                      #Used to achieve iptables Default_mark_up functionality
  195.     }
  196.  
  197.     custom_rates() {
  198.         echo "Modifying TC Class Rates"
  199.         ${tc} class change dev br0 parent 1:1 classid 1:10 htb ${PARMS}prio 0 rate ${DownRate0}Kbit ceil ${DownCeil}Kbit burst ${DownBurst0} cburst ${DownCburst0}
  200.         ${tc} class change dev br0 parent 1:1 classid 1:11 htb ${PARMS}prio 1 rate ${DownRate1}Kbit ceil ${DownCeil}Kbit burst ${DownBurst1} cburst ${DownCburst1}
  201.         ${tc} class change dev br0 parent 1:1 classid 1:12 htb ${PARMS}prio 2 rate ${DownRate2}Kbit ceil ${DownCeil}Kbit burst ${DownBurst2} cburst ${DownCburst2}
  202.         ${tc} class change dev br0 parent 1:1 classid 1:13 htb ${PARMS}prio 3 rate ${DownRate3}Kbit ceil ${DownCeil}Kbit burst ${DownBurst3} cburst ${DownCburst3}
  203.         ${tc} class change dev br0 parent 1:1 classid 1:14 htb ${PARMS}prio 4 rate ${DownRate4}Kbit ceil ${DownCeil}Kbit burst ${DownBurst4} cburst ${DownCburst4}
  204.         ${tc} class change dev br0 parent 1:1 classid 1:15 htb ${PARMS}prio 5 rate ${DownRate5}Kbit ceil ${DownCeil}Kbit burst ${DownBurst5} cburst ${DownCburst5}
  205.         ${tc} class change dev br0 parent 1:1 classid 1:16 htb ${PARMS}prio 7 rate ${DownRate6}Kbit ceil ${DownCeil}Kbit burst ${DownBurst6} cburst ${DownCburst6}
  206.         ${tc} class change dev br0 parent 1:1 classid 1:17 htb ${PARMS}prio 6 rate ${DownRate7}Kbit ceil ${DownCeil}Kbit burst ${DownBurst7} cburst ${DownCburst7}
  207.        
  208.         ${tc} class change dev eth0 parent 1:1 classid 1:10 htb ${PARMS}prio 0 rate ${UpRate0}Kbit ceil ${UpCeil}Kbit burst ${UpBurst0} cburst ${UpCburst0}
  209.         ${tc} class change dev eth0 parent 1:1 classid 1:11 htb ${PARMS}prio 1 rate ${UpRate1}Kbit ceil ${UpCeil}Kbit burst ${UpBurst1} cburst ${UpCburst1}
  210.         ${tc} class change dev eth0 parent 1:1 classid 1:12 htb ${PARMS}prio 2 rate ${UpRate2}Kbit ceil ${UpCeil}Kbit burst ${UpBurst2} cburst ${UpCburst2}
  211.         ${tc} class change dev eth0 parent 1:1 classid 1:13 htb ${PARMS}prio 3 rate ${UpRate3}Kbit ceil ${UpCeil}Kbit burst ${UpBurst3} cburst ${UpCburst3}
  212.         ${tc} class change dev eth0 parent 1:1 classid 1:14 htb ${PARMS}prio 4 rate ${UpRate4}Kbit ceil ${UpCeil}Kbit burst ${UpBurst4} cburst ${UpCburst4}
  213.         ${tc} class change dev eth0 parent 1:1 classid 1:15 htb ${PARMS}prio 5 rate ${UpRate5}Kbit ceil ${UpCeil}Kbit burst ${UpBurst5} cburst ${UpCburst5}
  214.         ${tc} class change dev eth0 parent 1:1 classid 1:16 htb ${PARMS}prio 7 rate ${UpRate6}Kbit ceil ${UpCeil}Kbit burst ${UpBurst6} cburst ${UpCburst6}
  215.         ${tc} class change dev eth0 parent 1:1 classid 1:17 htb ${PARMS}prio 6 rate ${UpRate7}Kbit ceil ${UpCeil}Kbit burst ${UpBurst7} cburst ${UpCburst7}
  216.     }
  217.  
  218. ####################  DO NOT MODIFY BELOW  #####################   
  219. ####################  DO NOT MODIFY BELOW  #####################   
  220. ####################  DO NOT MODIFY BELOW  #####################   
  221. ####################  DO NOT MODIFY BELOW  #####################   
  222. ####################  DO NOT MODIFY BELOW  #####################   
  223. ####################  DO NOT MODIFY BELOW  #####################   
  224. ####################  DO NOT MODIFY BELOW  #####################   
  225. ####################  DO NOT MODIFY BELOW  #####################   
  226. ####################  DO NOT MODIFY BELOW  #####################   
  227. ####################  DO NOT MODIFY BELOW  #####################   
  228. ####################  DO NOT MODIFY BELOW  #####################   
  229.  
  230. #marks for iptable rules     
  231.     Net_mark_down="0x80090001"
  232.     VOIP_mark_down="0x80060001"         # Marks for iptables variant of download rules
  233.     Gaming_mark_down="0x80080001"           # Note these marks are same as filter match/mask combo but have a 1 at the end.  That trailing 1 prevents them from being caught by unidentified mask
  234.     Others_mark_down="0x800a0001"
  235.     Web_mark_down="0x800d0001"
  236.     Streaming_mark_down="0x80040001"
  237.     Downloads_mark_down="0x80030001"
  238.     Default_mark_down="0x803f0001" 
  239.  
  240.     Net_mark_up="0x40090001"
  241.     VOIP_mark_up="0x40060001"           # Marks for iptables variant of upload rules
  242.     Gaming_mark_up="0x40080001"         # Note these marks are same as filter match/mask combo but have a 1 at the end.  That trailing 1 prevents them from being caught by unidentified mask
  243.     Others_mark_up="0x400a0001"
  244.     Web_mark_up="0x400d0001"
  245.     Streaming_mark_up="0x40040001"
  246.     Downloads_mark_up="0x40030001"
  247.     Default_mark_up="0x403f0001"
  248.  
  249. set_all_variables(){
  250.  
  251.     if [ -e "/usr/sbin/realtc" ] ; then
  252.         tc="realtc"
  253.     else
  254.         tc="tc"
  255.     fi
  256.  
  257.     #read variables at beginning of script
  258.     user_variables                 
  259.    
  260.                                        
  261.     #read order of QOS categories
  262.     Defaults="1:17"
  263.     Net="1:10"
  264.     flowid=0
  265.     while read -r line;             # reads users order of QOS categories
  266.     do     
  267.         #logger -s "${line}"
  268.         case ${line} in
  269.          '0')
  270.             VOIP="1:1${flowid}"
  271.             eval "Cat${flowid}DownBandPercent=${VoIP_DownBandPercent}"
  272.             eval "Cat${flowid}UpBandPercent=${VoIP_UpBandPercent}"
  273.             ;;
  274.          '1')
  275.             Downloads="1:1${flowid}"
  276.             eval "Cat${flowid}DownBandPercent=${FileTransfer_DownBandPercent}"
  277.             eval "Cat${flowid}UpBandPercent=${FileTransfer_UpBandPercent}"
  278.             ;;
  279.          '4')
  280.             Streaming="1:1${flowid}"
  281.             eval "Cat${flowid}DownBandPercent=${Video_DownBandPercent}"
  282.             eval "Cat${flowid}UpBandPercent=${Video_UpBandPercent}"
  283.             ;;
  284.          '7')
  285.             Others="1:1${flowid}"
  286.             eval "Cat${flowid}DownBandPercent=${Others_DownBandPercent}"
  287.             eval "Cat${flowid}UpBandPercent=${Others_UpBandPercent}"
  288.             ;;
  289.          '8')
  290.             Gaming="1:1${flowid}"
  291.             eval "Cat${flowid}DownBandPercent=${Gaming_DownBandPercent}"
  292.             eval "Cat${flowid}UpBandPercent=${Gaming_UpBandPercent}"
  293.             ;;
  294.          '9')
  295.             flowid=0
  296.             ;;
  297.          '13')
  298.             Web="1:1${flowid}"
  299.             eval "Cat${flowid}DownBandPercent=${WebSurfing_DownBandPercent}"
  300.             eval "Cat${flowid}UpBandPercent=${WebSurfing_UpBandPercent}"
  301.             ;;
  302.         esac
  303.        
  304.         firstchar="${line%%[0-9]*}"
  305.         if [ "${firstchar}" == "[" ] ; then
  306.             flowid=$((flowid + 1))
  307.             #logger -s "flowid = ${flowid} ==========="
  308.         fi
  309.        
  310.     done <<EOF
  311.         $(cat /tmp/bwdpi/qosd.conf | sed 's/rule=//g' | sed '/na/q')       
  312. EOF
  313.  
  314.     #calculate up/down rates
  315.     DownCeil="$(printf "%.0f" $(nvram get qos_ibw))"                           
  316.     UpCeil="$(printf "%.0f" $(nvram get qos_obw))"                             
  317.    
  318.    
  319.     DownRate0="$(expr ${DownCeil} \* ${NetControl_DownBandPercent} / 100)"              #Minimum guaranteed Up/Down rates per QOS category corresponding to user defined percentages
  320.     DownRate1="$(expr ${DownCeil} \* ${Cat1DownBandPercent} / 100)"
  321.     DownRate2="$(expr ${DownCeil} \* ${Cat2DownBandPercent} / 100)"
  322.     DownRate3="$(expr ${DownCeil} \* ${Cat3DownBandPercent} / 100)"
  323.     DownRate4="$(expr ${DownCeil} \* ${Cat4DownBandPercent} / 100)"
  324.     DownRate5="$(expr ${DownCeil} \* ${Cat5DownBandPercent} / 100)"
  325.     DownRate6="$(expr ${DownCeil} \* ${Cat6DownBandPercent} / 100)"
  326.     DownRate7="$(expr ${DownCeil} \* ${Default_DownBandPercent} / 100)"
  327.  
  328.    
  329.     UpRate0="$(expr ${UpCeil} \* ${NetControl_UpBandPercent} / 100)"
  330.     UpRate1="$(expr ${UpCeil} \* ${Cat1UpBandPercent} / 100)"
  331.     UpRate2="$(expr ${UpCeil} \* ${Cat2UpBandPercent} / 100)"
  332.     UpRate3="$(expr ${UpCeil} \* ${Cat3UpBandPercent} / 100)"
  333.     UpRate4="$(expr ${UpCeil} \* ${Cat4UpBandPercent} / 100)"
  334.     UpRate5="$(expr ${UpCeil} \* ${Cat5UpBandPercent} / 100)"
  335.     UpRate6="$(expr ${UpCeil} \* ${Cat6UpBandPercent} / 100)"
  336.     UpRate7="$(expr ${UpCeil} \* ${Default_UpBandPercent} / 100)"
  337.    
  338.     ClassesPresent=0
  339.     #read existing burst/cburst per download class
  340.     while read -r line;                                                                        
  341.     do
  342.         ClassesPresent=$(($ClassesPresent+1))
  343.         if [ "$( echo ${line} | sed -n -e 's/.*1:10 //p' )" != "" ] ; then                                                     
  344.             DownBurst0=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  345.             DownCburst0=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  346.         fi
  347.        
  348.         if [ "$( echo ${line} | sed -n -e 's/.*1:11 //p' )" != "" ] ; then                                                     
  349.             DownBurst1=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  350.             DownCburst1=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  351.         fi
  352.        
  353.         if [ "$( echo ${line} | sed -n -e 's/.*1:12 //p' )" != "" ] ; then                                                     
  354.             DownBurst2=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  355.             DownCburst2=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  356.         fi
  357.  
  358.         if [ "$( echo ${line} | sed -n -e 's/.*1:13 //p' )" != "" ] ; then                                                     
  359.             DownBurst3=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  360.             DownCburst3=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  361.         fi
  362.  
  363.         if [ "$( echo ${line} | sed -n -e 's/.*1:14 //p' )" != "" ] ; then                                                     
  364.             DownBurst4=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  365.             DownCburst4=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  366.         fi
  367.  
  368.         if [ "$( echo ${line} | sed -n -e 's/.*1:15 //p' )" != "" ] ; then                                                     
  369.             DownBurst5=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  370.             DownCburst5=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  371.         fi
  372.  
  373.         if [ "$( echo ${line} | sed -n -e 's/.*1:16 //p' )" != "" ] ; then                                                     
  374.             DownBurst6=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  375.             DownCburst6=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  376.         fi             
  377.  
  378.         if [ "$( echo ${line} | sed -n -e 's/.*1:17 //p' )" != "" ] ; then                                                     
  379.             DownBurst7=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  380.             DownCburst7=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  381.         fi             
  382.     done <<EOF
  383.         $( tc class show dev br0 | grep "parent 1:1 " )
  384. EOF
  385.  
  386.     #read existing burst/cburst per upload class
  387.     while read -r line;                                                                        
  388.     do
  389.         if [ "$( echo ${line} | sed -n -e 's/.*1:10 //p' )" != "" ] ; then                                                     
  390.             UpBurst0=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  391.             UpCburst0=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  392.         fi
  393.        
  394.         if [ "$( echo ${line} | sed -n -e 's/.*1:11 //p' )" != "" ] ; then                                                     
  395.             UpBurst1=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  396.             UpCburst1=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  397.         fi
  398.        
  399.         if [ "$( echo ${line} | sed -n -e 's/.*1:12 //p' )" != "" ] ; then                                                     
  400.             UpBurst2=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  401.             UpCburst2=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  402.         fi
  403.  
  404.         if [ "$( echo ${line} | sed -n -e 's/.*1:13 //p' )" != "" ] ; then                                                     
  405.             UpBurst3=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  406.             UpCburst3=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  407.         fi
  408.  
  409.         if [ "$( echo ${line} | sed -n -e 's/.*1:14 //p' )" != "" ] ; then                                                     
  410.             UpBurst4=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  411.             UpCburst4=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  412.         fi
  413.  
  414.         if [ "$( echo ${line} | sed -n -e 's/.*1:15 //p' )" != "" ] ; then                                                     
  415.             UpBurst5=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  416.             UpCburst5=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  417.         fi
  418.  
  419.         if [ "$( echo ${line} | sed -n -e 's/.*1:16 //p' )" != "" ] ; then                                                     
  420.             UpBurst6=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  421.             UpCburst6=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  422.         fi             
  423.  
  424.         if [ "$( echo ${line} | sed -n -e 's/.*1:17 //p' )" != "" ] ; then                                                     
  425.             UpBurst7=$( echo ${line} | sed -n -e 's/.* burst \([a-zA-z0-9]*\).*/\1/p' )
  426.             UpCburst7=$( echo ${line} | sed -n -e 's/.*cburst \([a-zA-z0-9]*\).*/\1/p' )
  427.         fi             
  428.     done <<EOF
  429.         $( tc class show dev eth0 | grep "parent 1:1 " )
  430. EOF
  431.  
  432.  
  433.     #read parameters for fakeTC
  434.     PARMS=""
  435.     OVERHEAD=$(nvram get qos_overhead)
  436.     if [ ! -z "$OVERHEAD" ] && [ "$OVERHEAD" -gt "0" ] ; then
  437.         ATM=$(nvram get qos_atm)
  438.         if [ "$ATM" == "1" ] ; then
  439.             PARMS="overhead $OVERHEAD linklayer atm "
  440.         else
  441.             PARMS="overhead $OVERHEAD linklayer ethernet "
  442.         fi
  443.     fi
  444.    
  445.    
  446. }  
  447.  
  448. appdb(){
  449.  
  450.     if [ "$( grep -i "${1}"  /tmp/bwdpi/bwdpi.app.db | wc -l )" -lt "5" ] ; then
  451.         grep -i "${1}"  /tmp/bwdpi/bwdpi.app.db | while read -r line ; do
  452.             echo $line | cut -f 4 -d ","
  453.  
  454.             cat_decimal=$(echo $line | cut -f 1 -d "," )
  455.             cat_hex=$( printf "80%02x" $cat_decimal )
  456.             case "$cat_decimal" in
  457.              '9'|'20')
  458.                echo " Cat:  Net Control"
  459.                ;;
  460.              '0'|'5'|'6'|'15'|'17')
  461.                echo " Cat:  VoIP"
  462.                ;;
  463.              '8')
  464.                echo " Cat:  Gaming"
  465.                ;;
  466.              '7'|'10'|'11'|'21'|'23')
  467.                echo " Cat:  Others"
  468.                ;;
  469.              '13'|'24'|'18'|'19')
  470.                echo " Cat:  Web"
  471.                ;;              
  472.              '4'|'12')
  473.                echo " Cat:  Streaming"
  474.                ;;                          
  475.              '1'|'3'|'14')
  476.                echo " Cat:  Downloads"
  477.                ;;                  
  478.             esac
  479.            
  480.            
  481.             printf " Mark: 0x${cat_hex}"
  482.             echo $line | cut -f 2 -d "," | awk '{printf("%04x 0xc03fffff\n",$1)}'
  483.             echo -e " Prio: $(expr $(tc filter show dev br0 | grep "${cat_hex}0000" -B1 | tail -2 | cut -d " " -f7 | head -1) - 1) \n"
  484.         done
  485.     else
  486.         echo "AppDB search parameter has to be more specfic"
  487.     fi
  488. }
  489.  
  490. debug(){
  491.     current_undf_rule="$(tc filter show dev br0 | grep -v "/" | grep "000ffff" -B1)"
  492.     undf_flowid=$(echo $current_undf_rule | grep -o "flowid.*" | cut -d" " -f2 | head -1)
  493.     undf_prio=$(echo $current_undf_rule | grep -o "pref.*" | cut -d" " -f2 | head -1)
  494.     set_all_variables
  495.    
  496.     logger -t "adaptive QOS" -s "Undf Prio: $undf_prio"
  497.     logger -t "adaptive QOS" -s "Undf FlowID: $undf_flowid"
  498.     logger -t "adaptive QOS" -s "Classes Present: $ClassesPresent"
  499.     logger -t "adaptive QOS" -s "Down Band: $DownCeil"
  500.     logger -t "adaptive QOS" -s "Up Band  : $UpCeil"
  501.     logger -t "adaptive QOS" -s "***********"
  502.     logger -t "adaptive QOS" -s "Net = ${Net}"
  503.     logger -t "adaptive QOS" -s "VOIP = ${VOIP}"
  504.     logger -t "adaptive QOS" -s "Gaming = ${Gaming}"
  505.     logger -t "adaptive QOS" -s "Others = ${Others}"
  506.     logger -t "adaptive QOS" -s "Web = ${Web}"
  507.     logger -t "adaptive QOS" -s "Streaming = ${Streaming}"
  508.     logger -t "adaptive QOS" -s "Downloads = ${Downloads}"
  509.     logger -t "adaptive QOS" -s "Defaults = ${Defaults}"
  510.     logger -t "adaptive QOS" -s "***********"
  511.     logger -t "adaptive QOS" -s "Downrates -- $DownRate0, $DownRate1, $DownRate2, $DownRate3, $DownRate4, $DownRate5, $DownRate6, $DownRate7"
  512.     logger -t "adaptive QOS" -s "Downbursts -- $DownBurst0, $DownBurst1, $DownBurst2, $DownBurst3, $DownBurst4, $DownBurst5, $DownBurst6, $DownBurst7"
  513.     logger -t "adaptive QOS" -s "DownCbursts -- $DownCburst0, $DownCburst1, $DownCburst2, $DownCburst3, $DownCburst4, $DownCburst5, $DownCburst6, $DownCburst7"
  514.     logger -t "adaptive QOS" -s "***********"
  515.     logger -t "adaptive QOS" -s "Uprates -- $UpRate0, $UpRate1, $UpRate2, $UpRate3, $UpRate4, $UpRate5, $UpRate6, $UpRate7"
  516.     logger -t "adaptive QOS" -s "Upbursts -- $UpBurst0, $UpBurst1, $UpBurst2, $UpBurst3, $UpBurst4, $UpBurst5, $UpBurst6, $UpBurst7"
  517.     logger -t "adaptive QOS" -s "UpCbursts -- $UpCburst0, $UpCburst1, $UpCburst2, $UpCburst3, $UpCburst4, $UpCburst5, $UpCburst6, $UpCburst7"
  518. }
  519.  
  520.  stock_install(){
  521.     if [ "$(nvram get script_usbmount)" != "/jffs/scripts/script_usbmount" ] ; then
  522.         echo ""
  523.         echo -e  "\033[1;32m Creating environment to trigger scripts post USB Mount \033[0m"
  524.         nvram set script_usbmount="/jffs/scripts/script_usbmount"
  525.         nvram commit
  526.     fi
  527.    
  528.      if [ -f /jffs/scripts/script_usbmount ] ; then                                    #check if script_usbmount exists
  529.        if grep -q "#!/bin/sh" /jffs/scripts/script_usbmount ; then                          #check if script_usbmount header is correct
  530.             :                                                                                 #if header is correct, do nothing
  531.        else                                                                                   #if header is incorrect, fix header
  532.             echo " Detected improper header in script_usbmount, fixing header"
  533.             sed -i "1i #!/bin/sh" /jffs/scripts/script_usbmount
  534.             chmod 0755 /jffs/scripts/script_usbmount
  535.        fi
  536.        
  537.         sed -i '/FreshJR_QOS/d' /jffs/scripts/script_usbmount
  538.         echo '/jffs/scripts/FreshJR_QOS mount &' >> /jffs/scripts/script_usbmount
  539.            
  540.     else                                                                               #if script_usbmount did not exist then set it up entirely
  541.        echo " Creating script_usbmount in /jffs/scripts/"
  542.        echo " Placing FreshJR_QOS into script_usbmount"
  543.        echo "#!/bin/sh" > /jffs/scripts/script_usbmount
  544.        echo '/jffs/scripts/FreshJR_QOS mount &' >> /jffs/scripts/script_usbmount
  545.        chmod 0755 /jffs/scripts/script_usbmount
  546.     fi
  547.    
  548.     echo -e  "\033[1;32m FreshJR QOS has been installed \033[0m"
  549.     echo -e  "\033[1;32m   make sure a USB storage device is plugged in and \033[0m"
  550.     echo  -e "\033[1;31;7m   [ reboot router ] to finalize installation\033[0m"
  551. }
  552.  
  553. #Main program here, will execute different things depending on arguments
  554. arg1="$(echo "$1" | tr -d "-")"
  555. case "$arg1" in
  556.  'start'|'check'|'mount')                                                                   ##RAN ON FIREWALL-START OR CRON TASK, (RAN ONLY POST USB MOUNT IF USING STOCK ASUS FIRMWARE)
  557.     cru a FreshJR_QOS "30 3 * * * /jffs/scripts/FreshJR_QOS -check"
  558.     if [ "$(nvram get qos_enable)" == "1" ] ; then
  559.         for pid in $(pidof FreshJR_QOS); do
  560.             if [ $pid != $$ ]; then
  561.                 kill $pid
  562.                 logger -t "adaptive QOS" -s "Delayed Start Canceled"
  563.             fi
  564.         done
  565.        
  566.         if [ "$arg1" == "start" ] ; then
  567.             wan="${2}"
  568.                 if [ -z "$wan" ] ; then
  569.                     wan="eth0"
  570.                 fi
  571.             iptable_down_rules 2>&1 | logger -t "adaptive QOS"
  572.             iptable_up_rules 2>&1 | logger -t "adaptive QOS"
  573.            
  574.             logger -t "adaptive QOS" -s -- "TC Modification Delayed Start (5min)"
  575.             sleep 300s
  576.         fi
  577.  
  578.         if [ "$arg1" == "mount" ] ; then           
  579.             logger -t "adaptive QOS" -s -- "--Post USB Mount-- Delayed Start (10min)"
  580.             sleep 600s
  581.         fi
  582.        
  583.         current_undf_rule="$(tc filter show dev br0 | grep -v "/" | grep "000ffff" -B1)"
  584.         undf_flowid=$(echo $current_undf_rule | grep -o "flowid.*" | cut -d" " -f2 | head -1)
  585.         undf_prio=$(echo $current_undf_rule | grep -o "pref.*" | cut -d" " -f2 | head -1)
  586.         if [ "${undf_flowid}" == "1:17" ] ; then        #if rule setting unidentified traffic to 1:17 exists then run modification script
  587.             if [ "$arg1" == "check" ] ; then
  588.                 logger -t "adaptive QOS" -s "Scheduled Persistence Check -> Reapplying Changes"
  589.             fi
  590.            
  591.             if [ "$(nvram get script_usbmount)" == "/jffs/scripts/script_usbmount" ] && [ "$arg1" != "start" ] ; then       #used only on stock ASUS firmware -mount && -check script calls
  592.                 wan="eth0"
  593.                 iptable_down_rules 2>&1 | logger -t "adaptive QOS"
  594.                 iptable_up_rules 2>&1 | logger -t "adaptive QOS"
  595.             fi
  596.            
  597.             set_all_variables
  598.             tc_redirection_down_rules "$undf_prio"  2>&1 | logger -t "adaptive QOS"     #forwards terminal output & errors to logger
  599.             tc_redirection_up_rules "$undf_prio"  2>&1 | logger -t "adaptive QOS"       #forwards terminal output & errors to logger
  600.            
  601.             if [ "$ClassesPresent" -lt "8" ] ; then
  602.                 logger -t "adaptive QOS" -s "Adaptive QOS not fully done setting up prior to modification script"
  603.                 logger -t "adaptive QOS" -s "(Skipping class modification, delay trigger time period needs increase)"
  604.             else
  605.                 if [ "$DownCeil" -gt "500" ] && [ "$UpCeil" -gt "500" ] ; then
  606.                     custom_rates 2>&1 | logger -t "adaptive QOS"        #forwards terminal output & errors to logger
  607.                 fi
  608.             fi
  609.            
  610.         else
  611.             if [ "$arg1" == "check" ] ; then
  612.                 logger -t "adaptive QOS" -s "Scheduled Persistence Check -> No modifications necessary"
  613.             else
  614.                 logger -t "adaptive QOS" -s "No modifications necessary"
  615.             fi
  616.         fi
  617.     fi
  618.     ;; 
  619.  'install'|'enable')                                                            ## INSTALLS AND TURNS ON SCRIPT
  620.     chmod 0755 /jffs/scripts/FreshJR_QOS
  621.    
  622.     sed -i '/FreshJR_QOS/d' /jffs/scripts/init-start 2>/dev/null                                   
  623.     if [ "/jffs/scripts/FreshJR_QOS_fakeTC" -ef "/bin/tc" ] || [ "/jffs/scripts/FreshJR_QOS_fakeTC" -ef "/usr/sbin/tc" ] ; then     ##uninstall previous version FreshJR_QOS_fakeTC if not already uninstalled
  624.        
  625.         echo "Old version of FreshJR_QOS_fast(fakeTC) has been Detected"
  626.        
  627.         if [ -e "/bin/tc" ] ; then 
  628.             umount /bin/tc &> /dev/null             #suppresses error if present
  629.             mount -o bind /usr/sbin/faketc /bin/tc                                     
  630.         elif [ -e "/usr/sbin/tc" ] ; then  
  631.             umount /usr/sbin/tc &> /dev/null        #suppresses error if present
  632.             mount -o bind /usr/sbin/faketc /usr/sbin/tc                                            
  633.         fi
  634.        
  635.         rm -f /jffs/scripts/FreshJR_QOS_fakeTC
  636.         nvram unset qos_downrates
  637.         nvram unset qos_uprates
  638.         nvram commit
  639.        
  640.         if [ "/usr/sbin/faketc" -ef "/usr/sbin/tc" ] || [ "/usr/sbin/faketc" -ef "/bin/tc" ] ; then
  641.             echo "Old version of FreshJR_QOS_fast(fakeTC) has been Successfully Uninstalled"
  642.         else
  643.             echo "FreshJR_QOS_fast(fakeTC) Uninstall Process has been Initiated "
  644.             echo  -e "\033[1;31;7m Please [ reboot router ] to finish the uninstall process \033[0m"
  645.             echo  -e "\033[1;31;7m Rerun this install procedure after system reboot \033[0m"
  646.             exit 1
  647.         fi 
  648.     fi 
  649.    
  650.     if ! grep -iq "merlin" /proc/version ; then                                                                                 ##GIVE USER CHOICE TO RUN STOCK INSTALL IF Non-RMerlin FIRMWARE detected
  651.         echo -e "\033[1;31m Non-RMerlin Firmware Detected \033[0m"
  652.         echo -e -n "\033[1;31m Is this installation for (Stock / Default / Unmodified) Asus firmware?  [1=Yes 2=No]? \033[0m"   # Display prompt in red
  653.         read yn
  654.         case $yn in
  655.             '1')
  656.                 sed -i '/FreshJR_QOS/d' /jffs/scripts/firewall-start 2>/dev/null
  657.                 stock_install;
  658.                 exit 1
  659.                 ;;
  660.             '2')
  661.                 sed -i '/FreshJR_QOS/d' /jffs/scripts/script_usbmount 2>/dev/null
  662.                 echo -e "\033[1;32m Installing RMerlin version of the script \033[0m"   # Display prompt in red
  663.                 break
  664.                 ;;
  665.             *)
  666.                 echo "Invalid Option"
  667.                 echo "ABORTING INSTALLATION "
  668.                 exit 1
  669.                 ;;
  670.         esac
  671.     fi
  672.    
  673.     if [ -f /jffs/scripts/firewall-start ] ; then                                      #check if firewall-start exists
  674.        if grep -q "#!/bin/sh" /jffs/scripts/firewall-start ; then                           #check if firewall-start header is correct
  675.             :                                                                                 #if header is correct, do nothing
  676.        else                                                                                   #if header is incorrect, fix header
  677.             echo "Detected improper header in firewall-start, fixing header"
  678.             sed -i "1i #!/bin/sh" /jffs/scripts/firewall-start
  679.             chmod 0755 /jffs/scripts/firewall-start
  680.        fi
  681.    
  682.        if grep -q -x '/jffs/scripts/FreshJR_QOS -start $1 & ' /jffs/scripts/firewall-start ; then     #check if FreshJR_QOS is present as item in firewall start
  683.             :                                                                                                   #if FreshJR_QOS is present do nothing
  684.         else                                                                                                    #if not, appened it to the last line (also delete any previously formated entry)
  685.             echo "Placing FreshJR_QOS entry into firewall-start"
  686.             sed -i '/FreshJR_QOS/d' /jffs/scripts/firewall-start
  687.             echo '/jffs/scripts/FreshJR_QOS -start $1 & ' >> /jffs/scripts/firewall-start
  688.        fi                                                                                                  
  689.     else                                                                               #if firewall-start does not exist then set it up entirely
  690.        echo "Firewall-start not detected, creating firewall-start"
  691.        echo "Placing FreshJR_QOS entry into firewall-start"
  692.        echo "#!/bin/sh" > /jffs/scripts/firewall-start
  693.        echo '/jffs/scripts/FreshJR_QOS -start $1 & ' >> /jffs/scripts/firewall-start
  694.        chmod 0755 /jffs/scripts/firewall-start
  695.     fi
  696.     cru a FreshJR_QOS "30 3 * * * /jffs/scripts/FreshJR_QOS -check"
  697.     echo -e  "\033[1;32m FreshJR QOS has been installed \033[0m"
  698.     echo -e  "\033[1;31;7m [ Turn ON OR Restart QOS ] for modifications to take effect \033[0m"
  699.     ;;
  700.  'uninstall')                                                                       ## UNINSTALLS SCRIPT AND DELETES FILES
  701.     sed -i '/FreshJR_QOS/d' /jffs/scripts/firewall-start 2>/dev/null                        #remove FreshJR_QOS from firewall start
  702.     sed -i '/FreshJR_QOS/d' /jffs/scripts/script_usbmount 2>/dev/null                       #remove FreshJR_QOS from script_usbmount - only used on stock ASUS firmware installs
  703.     cru d FreshJR_QOS
  704.     rm -f /jffs/scripts/FreshJR_QOS
  705.     if [ "$(nvram get script_usbmount)" == "/jffs/scripts/script_usbmount" ] ; then                                                #only used on stock ASUS firmware installs
  706.         nvram unset script_usbmount
  707.         nvram commit
  708.     fi
  709.     echo -e  "\033[1;32m FreshJR QOS has been uninstalled \033[0m"
  710.     ;;
  711.  'disable')                                                                     ## TURNS OFF SCRIPT BUT KEEP FILES
  712.     sed -i '/FreshJR_QOS/d' /jffs/scripts/firewall-start  2>/dev/null
  713.     sed -i '/FreshJR_QOS/d' /jffs/scripts/script_usbmount 2>/dev/null
  714.     cru d FreshJR_QOS
  715.     ;;
  716.  'debug')
  717.     debug
  718.     ;; 
  719.  'appdb')
  720.     appdb "$2"
  721.     ;;
  722.  *)
  723.     echo "These are available commands:"
  724.     echo ""
  725.     echo "  FreshJR_QOS -install            installs   script"
  726.     echo "  FreshJR_QOS -uninstall          uninstalls script && deletes from disk "
  727.     echo ""
  728.     echo "  FreshJR_QOS -enable             enables    script "
  729.     echo "  FreshJR_QOS -disable            disables   script but does not delete from disk"
  730.     echo ""
  731.     echo "  FreshJR_QOS -debug              checks current status of QOS configuration"
  732.     echo ""
  733.     echo "  FreshJR_QOS -check              instantaneously performs script modifications if required"
  734.     echo ""
  735.     echo '  FreshJR_QOS -appdb "App Name"   Use this to lookup mark/prio paramters required to create an App Analysis redirection rules'
  736.     echo ""
  737.     ;;
  738. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement