#!/bin/bash

# This is the main script to build the configuration
# files in the format proposed by htb.init

# By default, if nothing is modified, the generated files
# are adequate to create classes that control minimum latency
# and maximum throughput traffic.

# But two (2) more classes are created, one specific for web traffic (port 80)
# and a 'default' class for the rest. Normally, web traffic is considered of minimum
# latency and that's the way we have configured it (it has a higher priority
# than the maximum throughput and default), but it has it's own class because
# of it's importance, so you could assign it a different class from ssh, dns, etc.

# If you want to adjust the values for your own connection, 
# it will be enough to know the download and dowload bandwidth
# to automatically adjust the RATE and CEIL values of each class.
# When you are done, don't forget to execute:
#
#    /etc/init.d/htb.init recreate
#    /etc/init.d/htb.init reload
#
# to rebuild the configuration files and restart the service.

# By default the values are calculated for a 256Kbits download
# and 128 Kbits upload bandwidth, very commom in Spain.
#
# NOTE: all values should be around 95-98% aprox. of the real ones
# to be shure the queue is managed in your Linux router.
########################################################

##############################################################
## WARNING: DON'T DELETE THE LINES WITH '/bin/cat' OR 'EOF' ##
##############################################################
# The htb.sysconfig script use the command 'cat' to generate #
# the config files necesary for htb.init                     #
##############################################################

# I have considere NET like Internet zone, LOC the local network
# and DMZ the dmz zone. Modify to your needs.
NET=eth0
LOC=eth1
DMZ=eth2

########################################################
# --- Configuration files for NET zone.
########################################################
# Allowed keywords are DEFAULT, R2Q, DCACHE.
/bin/cat > /etc/htbinit/$NET << EOF
DEFAULT=40
EOF

# Maximum for root class
# Allowed keywords are RATE, BURST, CBURST, INJECT, MTU
/bin/cat > /etc/htbinit/$NET-2.root << EOF
RATE=122Kbit
BURST=2k
EOF

# Values for minimum latency class
# Typical services: ssh, telnet, ftp, irc, dns, smtp, pop3, imap2
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
/bin/cat > /etc/htbinit/$NET-2:10.minlatency << EOF
CEIL=122Kbit
RATE=6Kbit
BURST=2K
LEAF=sfq
PRIO=3
RULE=:22
RULE=:21
RULE=:23
RULE=:6667
RULE=:53
RULE=:25
RULE=:110
RULE=:143
EOF

# Values for maximum throughput class
# Typical services: ftp-data.
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
/bin/cat > /etc/htbinit/$NET-2:20.maxthroughput << EOF
CEIL=115Kbit
RATE=55Kbit
BURST=2K
LEAF=sfq
PRIO=5
RULE=:20
EOF

# Values for web traffic
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
/bin/cat > /etc/htbinit/$NET-2:30.www << EOF
CEIL=122Kbit
RATE=31Kbit
BURST=2K
LEAF=sfq
PRIO=4
RULE=:80
RULE=:443
EOF

# Values for default class
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
/bin/cat > /etc/htbinit/$NET-2:40.default << EOF
CEIL=122Kbit
RATE=30Kbit
BURST=2k
LEAF=sfq
PRIO=6
EOF

########################################################
# --- Configuration files for local zone (eth1).
########################################################
# Allowed keywords are DEFAULT, R2Q, DCACHE.
/bin/cat > /etc/htbinit/$LOC << EOF
DEFAULT=40
EOF

# Maximum for root class
# Allowed keywords are RATE, BURST, CBURST, INJECT, MTU
/bin/cat > /etc/htbinit/$LOC-2.root << EOF
RATE=244Kbit
BURST=2k
EOF

# Values for minimum latency class
# Typical services: ssh, telnet, ftp, irc, dns, smtp, pop3, imap2
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
/bin/cat > /etc/htbinit/$LOC-2:10.minlatency << EOF
CEIL=244Kbit
RATE=12Kbit
BURST=2K
LEAF=sfq
PRIO=3
RULE=:22,
RULE=:21,
RULE=:23,
RULE=:6667,
RULE=:53,
RULE=:25,
RULE=:110,
RULE=:143,
EOF

# Values for maximum throughput class
# Typical services: ftp-data.
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
/bin/cat > /etc/htbinit/$LOC-2:20.maxthroughput << EOF
CEIL=230Kbit
RATE=110Kbit
BURST=2K
LEAF=sfq
PRIO=5
RULE=:20,
EOF

# Values for web traffic
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
/bin/cat > /etc/htbinit/$LOC-2:30.www << EOF
CEIL=244Kbit
RATE=61Kbit
BURST=2K
LEAF=sfq
PRIO=4
RULE=:80,
RULE=:443,
EOF

# Values for default class
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
/bin/cat > /etc/htbinit/$LOC-2:40.default << EOF
CEIL=244Kbit
RATE=61Kbit
BURST=2k
LEAF=sfq
PRIO=6
EOF

########################################################
# --- Configuration files for DMZ (eth2).
########################################################
# WARNING: the values in the DMZ zone are merely orientative, this is, 
# take them just as an academic example. If you have such a zone, 
# uncomment the needed parts and adjust all values. Remember, 
# download and upload traffic are shared by the local and dmz zone.
# Of course, we suppose you are offering such services in the DMZ.
# Allowed keywords are DEFAULT, R2Q, DCACHE.
#/bin/cat > /etc/htbinit/eth2 << EOF
#DEFAULT=40
#EOF

# Maximum for root class
# Allowed keywords are RATE, BURST, CBURST, INJECT, MTU
#/bin/cat > /etc/htbinit/$DMZ-2.root << EOF
#RATE=244Kbit
#BURST=2k
#EOF

# Values for minimum latency class
# Typical services: ssh, telnet, ftp, irc, dns, smtp, pop3, imap2
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
#/bin/cat > /etc/htbinit/$DMZ-2:10.minlatency << EOF
#CEIL=244Kbit
#RATE=12Kbit
#BURST=2K
#LEAF=sfq
#PRIO=3
#RULE=:22
#RULE=:21
#RULE=:23
#RULE=:6667
#RULE=:53
#RULE=:25
#RULE=:110
#RULE=:143
#EOF

# Values for maximum throughput class
# Typical services: ftp-data.
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
#/bin/cat > /etc/htbinit/$DMZ-2:20.maxthroughput << EOF
#CEIL=230Kbit
#RATE=110Kbit
#BURST=2K
#LEAF=sfq
#PRIO=5
#RULE=:20
#EOF

# Values for web traffic
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
#/bin/cat > /etc/htbinit/$DMZ-2:30.www << EOF
#CEIL=244Kbit
#RATE=61Kbit
#BURST=2K
#LEAF=sfq
#PRIO=4
#RULE=:80
#RULE=:443
#EOF

# Values for default class
# Allowed keywords are RATE, CEIL, BURST, CBURST, INJECT, PRIO,
# LEAF, MTU, QUANTUM, PERTURB, RULE, REALM, MARK, TIME
#/bin/cat > /etc/htbinit/$DMZ-2:40.default << EOF
#CEIL=244Kbit
#RATE=61Kbit
#BURST=2k
#LEAF=sfq
#PRIO=6
#EOF