Agora vamos compilar e instalar o iptables que baixamos:
# cd /usr/src/
# tar -xvjpf iptables-1.4.3.2.tar.bz2
# cd iptables-1.4.3.2/
# ./configure --prefix=/
Agora se seu sistema for 32bits (i386) use os comandos abaixo:
# make BINDIR=/sbin LIBDIR=/lib KERNEL_DIR=/usr/src/linux
# make BINDIR=/sbin LIBDIR=/lib KERNEL_DIR=/usr/src/linux install
Já se o seu sistema for 64 bits (x86_64) use os seguintes comandos:
# make BINDIR=/sbin LIBDIR=/lib64 KERNEL_DIR=/usr/src/linux
# make BINDIR=/sbin LIBDIR=/lib64 KERNEL_DIR=/usr/src/linux install
Para confirmarem que estão usando o iptables que acabamos de instalar:
# iptables -v
iptables v1.4.3.2: no command specified
Try `iptables -h' or 'iptables --help' for more information.
Perfeito! Agora já temos Kernel mais recente já com o suporte ao TProxy habilitado e o iptables novo compilado e instalado. Não precisamos aplicar patches no iptables porque esta versão que instalamos já vem com suporte ao TProxy.
Agora partiremos para o Squid, mas antes este precisará da libcap2 que baixamos. IMPORTANTE!!! Sem a libcap2 ou uma versão 2.09 ou superior, o TProxy não irá funcionar porque o Squid não compilará com o devido suporte. Isto realmente é muito, muito IMPORTANTE!
# cd /usr/src
# tar -xvzpf libcap-2.19.tar.gz
# cd libcap-2.19/
# make && make install
Simples assim mesmo. Agora podemos partir para o Squid:
# cd /usr/src
# tar -xvzpf squid-3.1.1.tar.gz
# cd squid-3.1.1/
# ./configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --datadir=/usr/share --localstatedir=/var/lib --sysconfdir=/etc/squid --libexecdir=/usr/libexec/squid --localstatedir=/var --datadir=/usr/share/squid --enable-removal-policies=lru,heap --enable-icmp --disable-ident-lookups --enable-cache-digests --enable-delay-pools --enable-arp-acl --with-pthreads --with-large-files --enable-htcp --enable-carp --enable-follow-x-forwarded-for --enable-snmp --enable-ssl --enable-async-io=32 --enable-linux-netfilter --enable-epoll --disable-poll --with-maxfd=16384 --enable-err-languages=Portuguese --enable-default-err-language=Portuguese
# make
# make install
Dos parâmetros acima, no ./configure, o mais importante para que funcione é o --enable-linux-netfilter. Não entrarei em detalhes de cada parâmetro acima, pois sairia muito do escopo desse tutorial.
Vamos criar o user e group do Squid:
# groupadd -g 23 squid
# useradd -u 23 -g 23 -d /var/spool/squid -s /sbin/nologin squid
Precisaremos acertar o squid.conf, vou usar o padrão para vocês terem uma ideia, mas as otimizações vão de cada um. Vamos editar o
/etc/squid.conf e deixá-lo como este abaixo:
cache_mem 100 MB
cache_effective_user squid
cache_effective_group squid
access_log /var/log/squid/access.log squid
cache_store_log /var/log/squid/store.log
netdb_filename /var/log/squid/netdb.state
cache_log /var/log/squid/cache.log
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl localhost src ::1/128
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
acl to_localhost dst ::1/128
# Se sua rede de teste for uma dessas abaixo, então descomente a correta ou as que for usar.
#acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
#acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
#acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
# Abaixo vou colocar as redes de teste como nos meus desenhos do tuto, mas alterem para o teste de vocês
acl localnet src 186.194.x.0/24
acl localnet src 186.193.x.0/27
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localnet
http_access allow localhost
http_access deny all
http_port 3128
http_port 3129 tproxy
hierarchy_stoplist cgi-bin ?
cache_dir ufs /var/spool/squid 30000 64 256
coredump_dir /var/spool/squid
# Add any of your own refresh_pattern entries above these.
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern .
Reparem na configuração acima: a linha em destaque (http_port 3129 tproxy) é muito importante!
Agora vamos criar uns diretórios com suas permissões:
# mkdir -p /var/spool/squid
# chown squid:squid /var/spool/squid
# mkdir -p /var/log/squid
# chown squid:squid /var/log/squid
Precisaremos inicializar o Squid e nesse momento preste atenção para ver se ele mostrará alguma mensagem de erro:
# squid -z
Vamos precisar de um script para inicializar e parar o Squid quando precisarmos. Então vamos criar um em /etc/init.d/ chamado squid com o conteúdo abaixo:
#!/bin/bash
# squid This shell script takes care of starting and stopping
# Squid Internet Object Cache
#
# chkconfig: - 90 25
# description: Squid - Internet Object Cache. Internet object caching is
# a way to store requested Internet objects (i.e., data available
# via the HTTP, FTP, and gopher protocols) on a system closer to the
# requesting site than to the source. Web browsers can then use the
# local Squid cache as a proxy HTTP server, reducing access time as
# well as bandwidth consumption.
# pidfile: /var/run/squid.pid
# config: /etc/squid/squid.conf
ulimit -HSn 16384
PATH=/usr/bin:/sbin:/bin:/usr/sbin
export PATH
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# don't raise an error if the config file is incomplete
# set defaults instead:
SQUID_OPTS=${SQUID_OPTS:-"-D"}
SQUID_PIDFILE_TIMEOUT=${SQUID_PIDFILE_TIMEOUT:-20}
SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100}
# determine the name of the squid binary
[ -f /usr/sbin/squid ] && SQUID=squid
prog="$SQUID"
# determine which one is the cache_swap directory
CACHE_SWAP=`sed -e 's/#.*//g' /etc/squid/squid.conf | \
grep cache_dir | awk '{ print $3 }'`
[ -z "$CACHE_SWAP" ] && CACHE_SWAP=/var/spool/squid
RETVAL=0
start() {
#check if the squid conf file is present
if [ ! -f /etc/squid/squid.conf ]; then
echo "Configuration file /etc/squid/squid.conf missing" 1>&2
exit 6
fi
# don't raise an error if the config file is incomplete.
# set defaults instead:
SQUID_OPTS=${SQUID_OPTS:-"-D"}
SQUID_PIDFILE_TIMEOUT=${SQUID_PIDFILE_TIMEOUT:-20}
SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100}
if [ -z "$SQUID" ]; then
echo "Insufficient privilege" 1>&2
exit 4
fi
for adir in $CACHE_SWAP; do
if [ ! -d $adir/00 ]; then
echo -n "init_cache_dir $adir... "
$SQUID -z -F -D >> /var/log/squid/squid.out 2>&1
fi
done
echo -n $"Starting $prog: "
$SQUID $SQUID_OPTS >> /var/log/squid/squid.out 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
timeout=0;
while : ; do
[ ! -f /var/run/squid.pid ] || break
if [ $timeout -ge $SQUID_PIDFILE_TIMEOUT ]; then
RETVAL=1
break
fi
sleep 1 && echo -n "."
timeout=$((timeout+1))
done
fi
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SQUID
[ $RETVAL -eq 0 ] && echo_success
[ $RETVAL -ne 0 ] && echo_failure
echo
return $RETVAL
}
stop() {
# don't raise an error if the config file is incomplete.
# set defaults instead:
SQUID_SHUTDOWN_TIMEOUT=${SQUID_SHUTDOWN_TIMEOUT:-100}
echo -n $"Stopping $prog: "
$SQUID -k check >> /var/log/squid/squid.out 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
$SQUID -k shutdown &
rm -f /var/lock/subsys/$SQUID
timeout=0
while : ; do
[ -f /var/run/squid.pid ] || break
if [ $timeout -ge $SQUID_SHUTDOWN_TIMEOUT ]; then
echo
return 1
fi
sleep 2 && echo -n "."
timeout=$((timeout+2))
done
echo_success
echo
else
echo_failure
echo
fi
return $RETVAL
}
reload() {
# don't raise an error if the config file is incomplete.
# set defaults instead:
SQUID_OPTS=${SQUID_OPTS:-"-D"}
$SQUID $SQUID_OPTS -k reconfigure
}
restart() {
stop
start
}
condrestart() {
[ -e /var/lock/subsys/squid ] && restart || :
}
rhstatus() {
status $SQUID && $SQUID -k check
}
probe() {
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
condrestart)
condrestart
;;
status)
rhstatus
;;
probe)
exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|reload|restart|condrestart}"
exit 2
esac
exit $?
Vamos colocar ele para iniciar no boot:
# chmod +x /etc/init.d/squid
# chkconfig --levels 235 squid on