<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gustavo Henrique.net &#187; Linux</title>
	<atom:link href="http://www.gustavohenrique.net/brogui/tag/linux/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gustavohenrique.net/brogui</link>
	<description>Só mais um blog com Wordpress</description>
	<lastBuildDate>Tue, 29 Jun 2010 00:00:51 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Servidor Linux com Proxy e Controle de Banda – Parte 3</title>
		<link>http://www.gustavohenrique.net/brogui/2010/01/servidor-linux-com-proxy-e-controle-de-banda-%e2%80%93-parte-3/</link>
		<comments>http://www.gustavohenrique.net/brogui/2010/01/servidor-linux-com-proxy-e-controle-de-banda-%e2%80%93-parte-3/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 19:00:10 +0000</pubDate>
		<dc:creator>gustavohenrique</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[qos]]></category>
		<category><![CDATA[shell script]]></category>

		<guid isPermaLink="false">http://www.gustavohenrique.net/brogui/?p=210</guid>
		<description><![CDATA[
Finalmente consegui escrever a última parte do artigo. 
Nas partes 1 e 2 mostrei como configurar um servidor Linux para firewall e controle de banda, usando scripts que fazem a leitura de um arquivo texto contendo as informações dos pontos de rede (IP, MAC, velocidade de download e upload&#8230;). Entretanto, acessar o servidor e ficar [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/firewall.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/firewall.jpg" alt="firewall" title="firewall" width="300" height="240" class="aligncenter size-full wp-image-219" /></a><br />
Finalmente consegui escrever a última parte do artigo. <img src='http://www.gustavohenrique.net/brogui/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
Nas partes 1 e 2 mostrei como configurar um servidor Linux para firewall e controle de banda, usando scripts que fazem a leitura de um arquivo texto contendo as informações dos pontos de rede (IP, MAC, velocidade de download e upload&#8230;). Entretanto, acessar o servidor e ficar editando um arquivo texto, na minha opinião é muito chato, possui risco de falha humana e apenas quem tem acesso ao servidor pode fazer isso. Para resolver esse contratempo foi desenvolvido o <a href="http://github.com/gustavohenrique/bandcontrol/" target="_blank">Bandcontrol</a>, um pequeno sistema web que armazena os dados dos pontos de rede em um banco de dados e gera o arquivo texto usado pelos shell scripts.<br />
O sistema foi desenvolvido em <a href="http://www.djangobrasil.org" target="_blank">Django</a>, personalizando o <a href="" target="_blank">Admin</a>. Vou mostrar apenas como instalar e usar. O código fonte está disponível para consulta de programadores, curiosos e simpatizantes.<br />
<span id="more-210"></span></p>
<h2>Instalação</h2>
<p>Há diversas maneiras de se instalar um sistema feito em django. Vou mostrar como fazer o deploy usando Apache2 com o módulo mod_wsgi.<br />
O ambiente completo usa Apache2, sqlite3, python-2.6 e Django-1.1.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">root@localhost# apt-get install apache2 libapache2-mod-wsgi sqlite3 python-pysqlite2 python-django</pre></div></div>

<p>O diretório web padrão do Apache no Ubuntu é o <code>/var/www</code>. Vamos manter assim. Faça o download do <a href="">Bandcontrol</a> pelo site <a href="http://github.com/gustavohenrique/bandcontrol" targer="_blank">github.com</a> (no site, clique no botão Download Source), descompacte no <code>/var/www</code>, renomeie o diretório para <code>bandcontrol</code> e altere o dono/grupo de acordo com o usuário do apache.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">root@localhost: /var/www# tar zxvf gustavohenrique-bandcontrol-XXXXXX.tar.gz
root@localhost: /var/www# mv gustavohenrique-bandcontrol-XXXXXX bandcontrol
root@localhost: /var/www# chown www-data.www-data -Rf bandcontrol</pre></div></div>

<p>Os shell scripts usam o <code>Iptables</code> para firewall e <code>TC</code> para controle de banda. É necessário permissão de super usuário para executar esses comandos. Vamos configurar o <code>sudo</code> para que o usuário do Apache possa executar tais comandos como root.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">root@localhost: /var/www# chmod +w /etc/sudoers
root@localhost: /var/www# vim /etc/sudoers</pre></div></div>

<p>Adicione essas linhas no arquivo <code>/etc/sudoers</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># No Ubuntu o iptables e o tc estao dentro do /sbin
Cmd_Alias BANDCONTROL = /sbin/iptables, /sbin/tc
www-data  ALL = (ALL) NOPASSWD: BANDCONTROL</pre></div></div>

<h3>Configuracao do Apache</h3>
<p>Configure um virtualhost no apache criando o arquivo <code>/etc/apache2/sites-enabled/bandcontrol</code> contendo:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">WSGIRestrictStdout Off
NameVirtualHost bandcontrol
&lt;VirtualHost bandcontrol&gt;
        ServerAdmin webmaster@localhost
        LimitInternalRecursion 1000
        ServerName bandcontrol
&nbsp;
        &lt;Directory /var/www/bandcontrol&gt;
                Options ExecCGI FollowSymLinks MultiViews
                AllowOverride FileInfo
                MultiviewsMatch Handlers
                Order deny,allow
                Allow from all
        &lt;/Directory&gt;
&nbsp;
        ErrorLog /var/log/apache2/bandcontrol_error.log
        CustomLog /var/log/apache2/bandcontrol_access.log combined
&nbsp;
        WSGIScriptAlias /     &quot;/var/www/bandcontrol/config.wsgi&quot;
&nbsp;
        Alias /admin_media/ &quot;/usr/lib/pymodules/python2.6/django/contrib/admin/media/&quot;
        Alias /media/           &quot;/var/www/bandcontrol/media/&quot;
&lt;/VirtualHost&gt;</pre></div></div>

<p>Edite o <code>/etc/hosts</code> e adicione:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">127.0.0.2     bandcontrol</pre></div></div>

<h3>Configuracao do Bandcontrol</h3>
<p>Altere o arquivo <code>/var/www/bandcontrol/config.wsgi</code> ajustando o diretório de instalação do Django. O padrão do Ubuntu é <code>/usr/lib/pymodules/python2.6/django</code>. Nesse arquivo também é configurado o diretório do bandcontrol:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
PROJECT_ROOT_PATH = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">insert</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, PROJECT_ROOT_PATH<span style="color: black;">&#41;</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/usr/lib/pymodules/python2.6/django/'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/var/www'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span>=<span style="color: #483d8b;">'settings'</span>
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> bandcontrol.<span style="color: black;">monitor</span>
bandcontrol.<span style="color: black;">monitor</span>.<span style="color: black;">start</span><span style="color: black;">&#40;</span>interval=<span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span></pre></div></div>

<p>No arquivo <code>/var/www/bandcontrol/settings.py</code> é possível fazer mais alterações.<br />
Para definir as interfaces de rede e classe IP utilizada na rede, edite os arquivos <code>/var/www/bandcontrol/scripts/CONFIG</code> e <code>/var/www/bandcontrol/scripts/firewall</code>.</p>
<p><!-- Cada vez que alterar algum arquivo <code>.py</code> do <code>bandcontrol</code> será necessário atualizar o arquivo <code>config.wsgi</code> para que as alterações surtam efeito. Para isso usamos o comando <code>touch config.wsgi</code>. Outras alternativas são reiniciar o Apache ou utilizar um arquivo <code>monitor.py</code> descrito em artigo anterior nesse mesmo blog. --><br />
Por padrão é utilizado o banco de dados sqlite3. Você pode escolher usar o MySQL ou PostgreSQL editando o arquivo <code>/var/www/bandcontrol/settings.py</code>.<br />
Para criar o BD, entre no diretório do bandcontrol e execute o comando <code>manage.py syncdb</code>. Digite <code>yes</code> para confirmar a criação do super usuário. Forneça um login, e-mail e senha:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">root@localhost: /var/www/bandcontrol# python manage.py syncdb
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Creating table rede_plano
Creating table rede_pontorede
&nbsp;
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'root'): gustavo
E-mail address: eu@gustavohenrique.net
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Message model
Installing index for admin.LogEntry model
Installing index for rede.PontoRede model</pre></div></div>

<p>Reinicie o Apache para que as alterações tenham efeito.</p>
<h2>Utilização</h2>
<p>Abra o browser e acesse <b>http://bandcontrol/</b>.<br />
Reparem que no campo de endereço da URL e os nomes de usuário utilizados não condizem com o que foi exemplificado nesse artigo. Bom... estou com preguiça para alterar as imagens, então vamos desconsiderar esse "bug".</p>
<p>Entre com o login e senha criados no passo anterior.<br />
<a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela0.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela0-300x217.jpg" alt="bandcontrol_tela0" title="bandcontrol_tela0" width="300" height="217" class="aligncenter size-medium wp-image-211" /></a></p>
<p>Tela inicial do sistema.<br />
<a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela1.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela1-300x217.jpg" alt="bandcontrol_tela1" title="bandcontrol_tela1" width="300" height="217" class="aligncenter size-medium wp-image-212" /></a></p>
<p>Clique em Planos e vamos criar alguns.<br />
<a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela2.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela2-300x217.jpg" alt="bandcontrol_tela2" title="bandcontrol_tela2" width="300" height="217" class="aligncenter size-medium wp-image-213" /></a></p>
<p>Volte à tela inicial, clique em Pontos de Rede. No menu superior há opção para executar o firewall, parar o controle de banda e exibir os IPs conectados ao servidor. Sempre que fizer alguma alteração no sistema deve executar o firewall para ativar as mudanças.<br />
<a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela3.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela3-300x217.jpg" alt="bandcontrol_tela3" title="bandcontrol_tela3" width="300" height="217" class="aligncenter size-medium wp-image-214" /></a></p>
<p>Em exibir IPs conectados, para adicionar um IP no sistema forneça uma descrição única, escolha o plano e clique em [+].<br />
<a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela4.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela4-300x217.jpg" alt="bandcontrol_tela4" title="bandcontrol_tela4" width="300" height="217" class="aligncenter size-medium wp-image-215" /></a></p>
<p>O bandcontrol permite criar outros usuários com acesso ao sistema. Trabalha com permissões e grupos. Cortesia do Django \o/<br />
<a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela5.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2010/01/bandcontrol_tela5-300x217.jpg" alt="bandcontrol_tela5" title="bandcontrol_tela5" width="300" height="217" class="aligncenter size-medium wp-image-216" /></a></p>
<h2>Conclusão</h2>
<p>Bandcontrol foi uma daquelas coisas "pra ontem". Foi feito em uma tarde de terça-feira. Tal velocidade se deve ao framework Django.<br />
Esse artigo foi escrito um pouco de cada vez, por isso há grandes chances de algo sair errado. Há muito material disponível na web sobre deploy de projetos em Django. Em caso de dúvidas estarei disposto a ajudar.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Compartilhe esse artigo</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;submitHeadline=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3&amp;submitSummary=" rel="nofollow" title="Adicionar ao&nbsp;Buzz"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/buzz.png" title="Adicionar ao&nbsp;Buzz" alt="Adicionar ao&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3" rel="nofollow" title="Adicionar ao&nbsp;Del.icio.us"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/delicious.png" title="Adicionar ao&nbsp;Del.icio.us" alt="Adicionar ao&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3" rel="nofollow" title="Adicionar ao&nbsp;digg"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/digg.png" title="Adicionar ao&nbsp;digg" alt="Adicionar ao&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F" rel="nofollow" title="Adicionar ao&nbsp;Facebook"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/facebook.png" title="Adicionar ao&nbsp;Facebook" alt="Adicionar ao&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3" rel="nofollow" title="Adicionar ao&nbsp;Google Bookmarks"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/google.png" title="Adicionar ao&nbsp;Google Bookmarks" alt="Adicionar ao&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;bm_description=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3" rel="nofollow" title="Adicionar ao&nbsp;Mister Wong"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Adicionar ao&nbsp;Mister Wong" alt="Adicionar ao&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;T=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3" rel="nofollow" title="Adicionar ao&nbsp;Netscape"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/netscape.png" title="Adicionar ao&nbsp;Netscape" alt="Adicionar ao&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3" rel="nofollow" title="Adicionar ao&nbsp;reddit"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/reddit.png" title="Adicionar ao&nbsp;reddit" alt="Adicionar ao&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3" rel="nofollow" title="Adicionar ao&nbsp;Stumble Upon"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Adicionar ao&nbsp;Stumble Upon" alt="Adicionar ao&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F" rel="nofollow" title="Adicionar ao&nbsp;Technorati"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/technorati.png" title="Adicionar ao&nbsp;Technorati" alt="Adicionar ao&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F" rel="nofollow" title="Adicionar ao&nbsp;Tip'd"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/tipd.png" title="Adicionar ao&nbsp;Tip'd" alt="Adicionar ao&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3+@+http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F" rel="nofollow" title="Adicionar ao&nbsp;Twitter"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/twitter.png" title="Adicionar ao&nbsp;Twitter" alt="Adicionar ao&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2010%2F01%2Fservidor-linux-com-proxy-e-controle-de-banda-%25e2%2580%2593-parte-3%2F&amp;t=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%E2%80%93+Parte+3" rel="nofollow" title="Adicionar ao&nbsp;Yahoo My Web"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Adicionar ao&nbsp;Yahoo My Web" alt="Adicionar ao&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.gustavohenrique.net/brogui/2010/01/servidor-linux-com-proxy-e-controle-de-banda-%e2%80%93-parte-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Servidor Linux com Proxy e Controle de Banda &#8211; Parte 2</title>
		<link>http://www.gustavohenrique.net/brogui/2009/12/servidor-linux-com-proxy-e-controle-de-banda-parte-2/</link>
		<comments>http://www.gustavohenrique.net/brogui/2009/12/servidor-linux-com-proxy-e-controle-de-banda-parte-2/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 09:00:45 +0000</pubDate>
		<dc:creator>gustavohenrique</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://www.gustavohenrique.net/brogui/?p=183</guid>
		<description><![CDATA[
Introdução
Nessa segunda parte do artigo vou mostrar como criar um shell script usando o iptables para filtragem de pacotes e CBQ para controle de banda.
O script está fácil de entender e sua utilização é bem simples. É necessário criar um arquivo texto contento informações sobre cada máquina cliente que possui acesso à internet. Informações como [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2009/12/linux_matrix.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2009/12/linux_matrix.jpg" alt="Linux" title="Linux" width="300" height="225" class="aligncenter size-full wp-image-187" /></a></p>
<h2>Introdução</h2>
<p>Nessa segunda parte do artigo vou mostrar como criar um shell script usando o iptables para filtragem de pacotes e CBQ para controle de banda.<br />
O script está fácil de entender e sua utilização é bem simples. É necessário criar um arquivo texto contento informações sobre cada máquina cliente que possui acesso à internet. Informações como IP, MAC, Download, Upload, se vai passar pelo Squid e se está bloqueado o acesso. Com base nestas informações, o script cria as regras no firewall bloqueando ou liberando acessos, amarrando IP ao endereço MAC, faz proxy transparente e determina qual a velocidade de download e upload para cada um. Fora isso, é criado o arquivo <i>/etc/dhcpd.con</i> usado pelo servidor DHCP associando o IP ao seu MAC, fazendo com que um cliente obtenha sempre o mesmo IP.<br />
<span id="more-183"></span></p>
<h2>Firewall e Controle de Banda</h2>
<p>É recomendado um conhecimento básico sobre Linux e Shell Script para bom entendimento do código.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">ARQUIVO</span>=<span style="color: #ff0000;">&quot;/var/www/bandcontrol/scripts/pontosderede.txt&quot;</span>     <span style="color: #666666; font-style: italic;"># Contem os pontos de rede cadastrados</span>
&nbsp;
<span style="color: #007800;">MOD</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> modprobe<span style="color: #7a0874; font-weight: bold;">&#41;</span>                        <span style="color: #666666; font-style: italic;"># Comando para carregar modulos do kernel</span>
<span style="color: #007800;">IPT</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> iptables<span style="color: #7a0874; font-weight: bold;">&#41;</span>                        <span style="color: #666666; font-style: italic;"># Caminho do iptables</span>
<span style="color: #007800;">TC</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> tc<span style="color: #7a0874; font-weight: bold;">&#41;</span>                               <span style="color: #666666; font-style: italic;"># tc (pacote iproute2)</span>
&nbsp;
<span style="color: #007800;">I_WAN</span>=<span style="color: #ff0000;">&quot;eth0&quot;</span>                                 <span style="color: #666666; font-style: italic;"># Interface internet</span>
<span style="color: #007800;">I_LAN1</span>=<span style="color: #ff0000;">&quot;eth1&quot;</span>                                <span style="color: #666666; font-style: italic;"># Interface Rede local</span>
&nbsp;
<span style="color: #007800;">GATEWAY_IP</span>=<span style="color: #ff0000;">&quot;192.168.254.254&quot;</span>
<span style="color: #007800;">NETMASK</span>=<span style="color: #ff0000;">&quot;255.255.255.0&quot;</span>
<span style="color: #007800;">CLASSE_LAN1</span>=<span style="color: #ff0000;">&quot;192.168.254&quot;</span>                    <span style="color: #666666; font-style: italic;"># Classe de IP utilizada na rede local. Ex.: 10.0</span>
<span style="color: #007800;">PROXY_SERVER</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$GATEWAY_IP</span>:3128&quot;</span>              <span style="color: #666666; font-style: italic;"># O proprio gateway tambem roda o Squid na porta 3128</span>
&nbsp;
<span style="color: #007800;">DHCPD_DAEMON</span>=<span style="color: #ff0000;">&quot;/etc/init.d/dhcp3-server&quot;</span>      <span style="color: #666666; font-style: italic;"># Executavel do dhcpd server</span>
<span style="color: #007800;">DHCPDCONF</span>=<span style="color: #ff0000;">&quot;/etc/dhcp3/dhcpd.conf&quot;</span>            <span style="color: #666666; font-style: italic;"># Arquivo de configuracao do dhcpd</span>
<span style="color: #007800;">DOMINIO</span>=<span style="color: #ff0000;">&quot;gustavohenrique.net&quot;</span>                <span style="color: #666666; font-style: italic;"># Dominio ao qual a rede faz parte</span>
<span style="color: #007800;">DNS_SERVERS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$GATEWAY_IP</span>, 8.8.8.8, 4.2.2.2&quot;</span>  <span style="color: #666666; font-style: italic;"># Servidores DNS. O primeiro é o próprio gateway</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Remove o controle de banda</span>
<span style="color: #000000; font-weight: bold;">function</span> parar_controle_de_banda <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">$TC</span> qdisc del dev <span style="color: #007800;">$I_LAN1</span> root
    <span style="color: #007800;">$TC</span> qdisc del dev <span style="color: #007800;">$I_WAN</span> root
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Cria as regras iniciais para controle de banda</span>
<span style="color: #000000; font-weight: bold;">function</span> iniciar_controle_de_banda <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># Remove as regras do controle de banda</span>
    parar_controle_de_banda
&nbsp;
    <span style="color: #666666; font-style: italic;"># Download</span>
    <span style="color: #007800;">$TC</span> qdisc add dev <span style="color: #007800;">$I_LAN1</span> root handle <span style="color: #000000;">1</span> cbq bandwidth 100Mbit avpkt <span style="color: #000000;">1000</span> cell <span style="color: #000000;">8</span>
    <span style="color: #007800;">$TC</span> class change dev <span style="color: #007800;">$I_LAN1</span> root cbq weight 10Mbit allot <span style="color: #000000;">1514</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Upload</span>
    <span style="color: #007800;">$TC</span> qdisc add dev <span style="color: #007800;">$I_WAN</span> root handle <span style="color: #000000;">1</span> cbq bandwidth 100Mbit avpkt <span style="color: #000000;">1000</span> cell <span style="color: #000000;">8</span>
    <span style="color: #007800;">$TC</span> class change dev <span style="color: #007800;">$I_WAN</span> root cbq weight 10Mbit allot <span style="color: #000000;">1514</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Cache Full. Pacotes marcados com TOS 60 vao para a classe 1:9999 cuja banda é 100 Mbit (rede ethernet)</span>
    <span style="color: #007800;">$TC</span> class add dev <span style="color: #007800;">$I_LAN1</span> parent <span style="color: #000000;">1</span>: classid <span style="color: #000000;">1</span>:<span style="color: #000000;">9999</span> cbq bandwidth 100Mbit rate 100Mbit weight 10Mbit prio <span style="color: #000000;">4</span> allot <span style="color: #000000;">1514</span> cell <span style="color: #000000;">8</span> maxburst <span style="color: #000000;">20</span> avpkt <span style="color: #000000;">1000</span> bounded
    <span style="color: #007800;">$TC</span> filter add dev <span style="color: #007800;">$I_LAN1</span> parent <span style="color: #000000;">1</span>:<span style="color: #000000;">0</span> protocol ip prio <span style="color: #000000;">3</span> u32 match ip protocol 0x6 0xff match ip tos <span style="color: #000000;">60</span> 0xff classid <span style="color: #000000;">1</span>:<span style="color: #000000;">9999</span>  
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># Limpa o firewall</span>
<span style="color: #000000; font-weight: bold;">function</span> parar_firewall<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># Configura a politca padrao para aceitar tudo</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-P</span> INPUT ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-P</span> OUTPUT ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-P</span> FORWARD ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-P</span> PREROUTING ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Limpa a tabela netfilter</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-F</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-X</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-F</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-X</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-F</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-X</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #666666; font-style: italic;"># Cria as regras de firewall</span>
<span style="color: #000000; font-weight: bold;">function</span> iniciar_firewall<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Finaliza o script se nao existir o arquivo contendo os pontos de rede</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-f</span> <span style="color: #007800;">$ARQUIVO</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Arquivo contendo os pontos de rede (<span style="color: #007800;">$ARQUIVO</span>) nao existe.&quot;</span>;
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Carrega modulos do iptables</span>
    <span style="color: #007800;">$MOD</span> ip_tables
    <span style="color: #007800;">$MOD</span> iptable_nat
    <span style="color: #007800;">$MOD</span> ipt_MASQUERADE
    <span style="color: #007800;">$MOD</span> ipt_LOG
    <span style="color: #007800;">$MOD</span> ipt_REJECT
    <span style="color: #007800;">$MOD</span> ip_conntrack_ftp
    <span style="color: #007800;">$MOD</span> ip_nat_ftp
&nbsp;
    <span style="color: #666666; font-style: italic;"># Limpa todas as regras do firewall</span>
    parar_firewall
&nbsp;
    <span style="color: #666666; font-style: italic;"># Recria as regras para controle de banda</span>
    iniciar_controle_de_banda
&nbsp;
    <span style="color: #666666; font-style: italic;"># Por padrao bloqueia tudo que for entrar no firewall</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-P</span> INPUT DROP
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-P</span> PREROUTING DROP
&nbsp;
    <span style="color: #666666; font-style: italic;"># Habilita redirecionamento de pacotes para compartilhar internet</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>net<span style="color: #000000; font-weight: bold;">/</span>ipv4<span style="color: #000000; font-weight: bold;">/</span>ip_forward
&nbsp;
    <span style="color: #666666; font-style: italic;"># Aumenta o limite do nf_conntrack_max</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">99999</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>net<span style="color: #000000; font-weight: bold;">/</span>netfilter<span style="color: #000000; font-weight: bold;">/</span>nf_conntrack_max
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera acesso para pacotes vindos da internet (CUIDADO)</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> <span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span> <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_WAN</span> <span style="color: #660033;">-p</span> all <span style="color: #660033;">-j</span> ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_WAN</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Cria uma nova chain para tratar do proxy transparente</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-N</span> proxy
        <span style="color: #666666; font-style: italic;"># O software conectividade social da Caixa Economica nao funciona se</span>
        <span style="color: #666666; font-style: italic;"># estiver passando pelo proxy. Tudo que for destinado ao IP da Caixa</span>
        <span style="color: #666666; font-style: italic;"># nao vai passar pelo proxy</span>
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-d</span> 200.201.174.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">16</span> <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-d</span> 200.201.173.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">16</span> <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-d</span> 200.201.0.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">16</span> <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-d</span> 200.201.174.207 <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
        <span style="color: #666666; font-style: italic;"># Internet Caixa nao passa pelo proxy</span>
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-d</span> 200.201.169.0<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span> <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-d</span> 200.201.169.69 <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
        <span style="color: #666666; font-style: italic;"># Esse servidor tambem roda o Apache na port 80. Nao quero que as</span>
        <span style="color: #666666; font-style: italic;"># paginas locais passem pelo proxy, pois assim posso controlar a banda</span>
        <span style="color: #666666; font-style: italic;"># de pacotes vindos do apache definindo a velocidade máxima da rede</span>
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-d</span> <span style="color: #007800;">$GATEWAY_IP</span> <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
        <span style="color: #666666; font-style: italic;"># Proxy transparente redirecionando os pacotes para a porta do Squid</span>
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-s</span> <span style="color: #000000;">0</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span> <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> DNAT <span style="color: #660033;">--to-dest</span> <span style="color: #007800;">$PROXY_SERVER</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Se o pacote nao for pra porta 80 (não é navegação), deixa passar</span>
        <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> proxy <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN1</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera o IP abaixo sem amarrar com MAC e sem passar pelo proxy</span>
    <span style="color: #666666; font-style: italic;">#$IPT -t nat -A PREROUTING -i $I_LAN1 -s 192.168.254.2 -j ACCEPT</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Configuracao inicial do dhcpd.conf. Distribuicao de IPs na faixa 192.168.254.80 a 192.168.254.90</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;# Arquivo dhcpd.conf criado pelo BandControl&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;ddns-update-style none;&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;option domain-name <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #007800;">$DOMINIO</span><span style="color: #000099; font-weight: bold;">\&quot;</span>;&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;option domain-name-servers <span style="color: #007800;">$DNS_SERVERS</span>;&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;default-lease-time 600;&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;max-lease-time 7200;&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;authoritative;&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;shared-network 0-2 {&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;  subnet <span style="color: #007800;">$CLASSE_LAN1</span>&quot;</span>.<span style="color: #ff0000;">&quot;0 netmask <span style="color: #007800;">$NETMASK</span> { range <span style="color: #007800;">$CLASSE_LAN1</span>.80 <span style="color: #007800;">$CLASSE_LAN1</span>.90; }&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;}&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Inicio do loop para liberacao e bloqueio de clientes.</span>
    <span style="color: #666666; font-style: italic;"># O formato do arquivo é: codigo_identificador-descricao-ip-mac-download-upload-liberado-proxy</span>
    <span style="color: #666666; font-style: italic;"># Ex.: 1-gustavo_henrique_notebook-192.168.254.10-512-256-S-S</span>
    <span style="color: #666666; font-style: italic;"># Acesso liberado e uso do proxy sao valores que devem ser True ou False</span>
    <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$ARQUIVO</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
        <span style="color: #007800;">CODIGO</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $1'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>                  <span style="color: #666666; font-style: italic;"># Numero usado como identificador no controle de banda</span>
        <span style="color: #007800;">IP</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $3'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>                      <span style="color: #666666; font-style: italic;"># IP</span>
        <span style="color: #007800;">MAC</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $4'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>                     <span style="color: #666666; font-style: italic;"># MAC em letras minusculas e separado por dois pontos (:)</span>
        <span style="color: #007800;">DOWNLOAD</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $5'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>                <span style="color: #666666; font-style: italic;"># Numero correspondente a velocidade de download em Kbit</span>
        <span style="color: #007800;">UPLOAD</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $6'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>                  <span style="color: #666666; font-style: italic;"># Numero correspondente a velocidade de upload em Kbit</span>
        <span style="color: #007800;">POSSUI_ACESSO_LIBERADO</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $7'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>  <span style="color: #666666; font-style: italic;"># True para sim, False para bloquear o acesso</span>
        <span style="color: #007800;">VAI_USAR_PROXY</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #660033;">-F</span> <span style="color: #ff0000;">&quot;-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $8'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>          <span style="color: #666666; font-style: italic;"># True para usar o proxy, False para não usar</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Configurar dhcpd para associar IP ao MAC</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$MAC</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
          <span style="color: #007800;">LINHA</span>=<span style="color: #ff0000;">&quot;host <span style="color: #007800;">$CODIGO</span> { hardware ethernet <span style="color: #007800;">$MAC</span>; fixed-address <span style="color: #007800;">$IP</span>; option subnet-mask <span style="color: #007800;">$NETMASK</span>; option routers <span style="color: #007800;">$GATEWAY_IP</span>; option domain-name-servers <span style="color: #007800;">$DNS_SERVERS</span>; }&quot;</span>
          <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$LINHA</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$DHCPDCONF</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Se o IP nao esta liberado, bloqueia no firewall</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$POSSUI_ACESSO_LIBERADO</span>&quot;</span> == <span style="color: #ff0000;">&quot;False&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
            <span style="color: #007800;">EXECUTAR_REGRA</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$IPT</span> -t nat -A PREROUTING -p tcp -i <span style="color: #007800;">$I_LAN1</span> -s <span style="color: #007800;">$IP</span> -j DROP&quot;</span>
        <span style="color: #000000; font-weight: bold;">else</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$VAI_USAR_PROXY</span>&quot;</span> == <span style="color: #ff0000;">&quot;True&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
                <span style="color: #007800;">EXECUTAR_REGRA</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$IPT</span> -t nat -A PREROUTING -i <span style="color: #007800;">$I_LAN1</span> -s <span style="color: #007800;">$IP</span> -m mac --mac-source <span style="color: #007800;">$MAC</span> -j proxy&quot;</span>
            <span style="color: #000000; font-weight: bold;">else</span>
                <span style="color: #007800;">EXECUTAR_REGRA</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$IPT</span> -t nat -A PREROUTING -i <span style="color: #007800;">$I_LAN1</span> -s <span style="color: #007800;">$IP</span> -j ACCEPT&quot;</span>
            <span style="color: #000000; font-weight: bold;">fi</span>
        <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Executa a regra referente ao IP dentro do loop</span>
        <span style="color: #007800;">$EXECUTAR_REGRA</span>
&nbsp;
&nbsp;
        <span style="color: #666666; font-style: italic;"># Aplica o controle de banda ao IP. Para cada IP é criada uma classe de download e upload.</span>
        <span style="color: #007800;">WEIGHT_DOWN</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$DOWNLOAD</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">10</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">ID_DOWN</span>=<span style="color: #000000;">1</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$CODIGO</span>&quot;</span>
        <span style="color: #007800;">$TC</span> class add dev <span style="color: #007800;">$I_LAN1</span> parent <span style="color: #000000;">1</span>: classid <span style="color: #000000;">1</span>:<span style="color: #007800;">$ID_DOWN</span> cbq bandwidth 10Mbit rate <span style="color: #ff0000;">&quot;<span style="color: #007800;">$DOWNLOAD</span>&quot;</span>Kbit weight <span style="color: #ff0000;">&quot;<span style="color: #007800;">$WEIGHT_DOWN</span>&quot;</span>Kbit prio <span style="color: #000000;">5</span> allot <span style="color: #000000;">1514</span> cell <span style="color: #000000;">8</span> maxburst <span style="color: #000000;">20</span> avpkt <span style="color: #000000;">1000</span> bounded
        <span style="color: #007800;">$TC</span> filter add dev <span style="color: #007800;">$I_LAN1</span> parent <span style="color: #000000;">1</span>:<span style="color: #000000;">0</span> protocol ip prio <span style="color: #000000;">100</span> u32 match ip dst <span style="color: #007800;">$IP</span> classid <span style="color: #000000;">1</span>:<span style="color: #007800;">$ID_DOWN</span>
&nbsp;
        <span style="color: #007800;">WEIGHT_UP</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">expr</span> <span style="color: #007800;">$UPLOAD</span> <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000;">10</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
        <span style="color: #007800;">ID_UP</span>=<span style="color: #000000;">2</span><span style="color: #ff0000;">&quot;<span style="color: #007800;">$CODIGO</span>&quot;</span>
        <span style="color: #007800;">$TC</span> class add dev <span style="color: #007800;">$I_WAN</span> parent <span style="color: #000000;">1</span>: classid <span style="color: #000000;">1</span>:<span style="color: #007800;">$ID_UP</span> cbq bandwidth 10Mbit rate <span style="color: #ff0000;">&quot;<span style="color: #007800;">$UPLOAD</span>&quot;</span>Kbit weight <span style="color: #ff0000;">&quot;<span style="color: #007800;">$WEIGHT_UP</span>&quot;</span>Kbit prio <span style="color: #000000;">5</span> allot <span style="color: #000000;">1514</span> cell <span style="color: #000000;">8</span> maxburst <span style="color: #000000;">20</span> avpkt <span style="color: #000000;">1000</span> bounded
        <span style="color: #007800;">$TC</span> qdisc add dev <span style="color: #007800;">$I_WAN</span> parent <span style="color: #000000;">1</span>:<span style="color: #007800;">$ID_UP</span> handle <span style="color: #007800;">$ID_UP</span> tbf rate <span style="color: #ff0000;">&quot;<span style="color: #007800;">$UPLOAD</span>&quot;</span>Kbit buffer 10Kb<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">8</span> limit 15Kb mtu <span style="color: #000000;">1500</span>
        <span style="color: #007800;">$TC</span> filter add dev <span style="color: #007800;">$I_WAN</span> parent <span style="color: #000000;">1</span>:<span style="color: #000000;">0</span> protocol ip prio <span style="color: #000000;">100</span> u32 match ip src <span style="color: #007800;">$IP</span> classid <span style="color: #000000;">1</span>:<span style="color: #007800;">$ID_UP</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Aplica o TOS 60 nas portas utilizadas por outros serviços. Assim é usado a velocidade total da rede</span>
    <span style="color: #666666; font-style: italic;"># para acesso ao SSH, Apache e Samba</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> OUTPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--sport</span> <span style="color: #000000;">22</span> <span style="color: #660033;">-j</span> TOS <span style="color: #660033;">--set-tos</span> <span style="color: #000000;">60</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> OUTPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--sport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> TOS <span style="color: #660033;">--set-tos</span> <span style="color: #000000;">60</span>
&nbsp;
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> OUTPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--sport</span> <span style="color: #000000;">137</span>:<span style="color: #000000;">139</span> <span style="color: #660033;">-j</span> TOS <span style="color: #660033;">--set-tos</span> <span style="color: #000000;">60</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> OUTPUT <span style="color: #660033;">-p</span> udp <span style="color: #660033;">--sport</span> <span style="color: #000000;">137</span>:<span style="color: #000000;">139</span> <span style="color: #660033;">-j</span> TOS <span style="color: #660033;">--set-tos</span> <span style="color: #000000;">60</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">137</span>:<span style="color: #000000;">139</span> <span style="color: #660033;">-j</span> TOS <span style="color: #660033;">--set-tos</span> <span style="color: #000000;">60</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> udp <span style="color: #660033;">--dport</span> <span style="color: #000000;">137</span>:<span style="color: #000000;">139</span> <span style="color: #660033;">-j</span> TOS <span style="color: #660033;">--set-tos</span> <span style="color: #000000;">60</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Compartilha a internet</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> nat <span style="color: #660033;">-A</span> POSTROUTING <span style="color: #660033;">-o</span> <span style="color: #007800;">$I_WAN</span> <span style="color: #660033;">-j</span> MASQUERADE
&nbsp;
    <span style="color: #666666; font-style: italic;"># Bloqueio contra Scanners Ocultos (Shealt Scan)</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--tcp-flags</span> SYN,ACK,FIN,RST RST <span style="color: #660033;">-m</span> limit <span style="color: #660033;">--limit</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">/</span>s <span style="color: #660033;">-j</span> DROP
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera ping (CUIDADO com ataques ddos)</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN1</span> <span style="color: #660033;">-p</span> icmp <span style="color: #660033;">-j</span> ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_WAN</span> <span style="color: #660033;">-p</span> icmp <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera acesso para o proprio gateway</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-i</span> lo <span style="color: #660033;">-j</span> ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> FORWARD <span style="color: #660033;">-i</span> lo <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera acesso ao Apache</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">443</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera acesso ao Squid</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">3128</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera acesso ao SSH</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">22</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera acesso ao DNS</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> udp <span style="color: #660033;">--dport</span> <span style="color: #000000;">53</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera acesso aos compartilhamentos do Samba</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">137</span>:<span style="color: #000000;">139</span> <span style="color: #660033;">-j</span> ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> udp <span style="color: #660033;">--dport</span> <span style="color: #000000;">137</span>:<span style="color: #000000;">139</span> <span style="color: #660033;">-j</span> ACCEPT
&nbsp;
    <span style="color: #666666; font-style: italic;"># Libera acesso ao SNMP</span>
    <span style="color: #666666; font-style: italic;">#$IPT -A INPUT -p udp --dport 160:162 -j ACCEPT</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Tratamento de conexoes TCP</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--syn</span> <span style="color: #660033;">-j</span> DROP
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">-m</span> state <span style="color: #660033;">--state</span> ESTABLISHED,RELATED,NEW <span style="color: #660033;">-j</span> ACCEPT
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-m</span> limit <span style="color: #660033;">--limit</span> <span style="color: #000000;">3</span><span style="color: #000000; font-weight: bold;">/</span>minute <span style="color: #660033;">--limit-burst</span> <span style="color: #000000;">3</span> <span style="color: #660033;">-j</span> DROP
&nbsp;
    <span style="color: #666666; font-style: italic;"># Bloqueando pacotes estranhos</span>
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--tcp-flags</span> ALL FIN,URG,PSH <span style="color: #660033;">-j</span> DROP
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--tcp-flags</span> ALL SYN,RST,ACK,FIN,URG <span style="color: #660033;">-j</span> DROP
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--tcp-flags</span> ALL ALL <span style="color: #660033;">-j</span> DROP
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--tcp-flags</span> ALL FIN <span style="color: #660033;">-j</span> DROP
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--tcp-flags</span> SYN,RST SYN,RST <span style="color: #660033;">-j</span> DROP
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--tcp-flags</span> SYN,FIN SYN,FIN <span style="color: #660033;">-j</span> DROP
    <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-A</span> INPUT <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--tcp-flags</span> ALL NONE <span style="color: #660033;">-j</span> DROP
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Firewall iniciado&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> $<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">in</span>
    <span style="color: #666666; font-style: italic;"># Inicia o firewall e o dhcpd</span>
    start<span style="color: #7a0874; font-weight: bold;">&#41;</span> iniciar_firewall; <span style="color: #007800;">$DHCPD_DAEMON</span> stop; <span style="color: #007800;">$DHCPD_DAEMON</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Finaliza o firewall sem parar o dhcp</span>
    stop<span style="color: #7a0874; font-weight: bold;">&#41;</span> parar_firewall; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Inicia o firewall sem reiniciar o dhcpd</span>
    only<span style="color: #7a0874; font-weight: bold;">&#41;</span> iniciar_firewall; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Finaliza o controle de banda</span>
    fast<span style="color: #7a0874; font-weight: bold;">&#41;</span> parar_controle_de_banda; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Se nenhum parametro for passado, mostra a ajuda abaixo</span>
    <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Use assim: $0 start|stop|only|fast&quot;</span>; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></div></div>

<h2>Conclusão</h2>
<p>Com o término da segunda parte do artigo é possível colocar um servidor Linux funcionando com eficiência, segurança e estabilidade para compartilhar uma conexão de internet. É uma ótima solução custo/benefício para micro e pequenas empresas.<br />
Na próxima parte vou mostrar como desenvolver uma interface web para gerenciar melhor os pontos de rede. Isso dá mais agilidade ao trabalhar com muitas máquinas na rede.</p>
<p>Algumas considerações:</p>
<ul>
<li>Tomem cuidado na configuração do firewall. Esse é um script simples que serve não deve ser tomado como solução definitiva.</li>
<li>Há outra alternativa para controle de banda chamada HTB. Pessoalmente prefiro o CBQ, o mesmo utilizado nesse script, mas vale a pena dar uma estudada.</li>
<li>Usar um arquivo texto contendo os dados dos pontos de rede para ser lido pelo shell script foi a idéia mais simples e não a de melhor desempenho. Também fiz o script fazer consultas ao MySQL mas não senti diferença na velocidade comparado com a leitura de um arquivo de texto.</li>
</ul>
<p>Bom, a parte 3 do artigo só em 2010. Nesse momento já estou em algum lugar sem internet e talvez sem celular. <img src='http://www.gustavohenrique.net/brogui/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /><br />
Feliz ano novo à todos!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Compartilhe esse artigo</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;submitHeadline=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2&amp;submitSummary=" rel="nofollow" title="Adicionar ao&nbsp;Buzz"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/buzz.png" title="Adicionar ao&nbsp;Buzz" alt="Adicionar ao&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2" rel="nofollow" title="Adicionar ao&nbsp;Del.icio.us"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/delicious.png" title="Adicionar ao&nbsp;Del.icio.us" alt="Adicionar ao&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2" rel="nofollow" title="Adicionar ao&nbsp;digg"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/digg.png" title="Adicionar ao&nbsp;digg" alt="Adicionar ao&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F" rel="nofollow" title="Adicionar ao&nbsp;Facebook"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/facebook.png" title="Adicionar ao&nbsp;Facebook" alt="Adicionar ao&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2" rel="nofollow" title="Adicionar ao&nbsp;Google Bookmarks"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/google.png" title="Adicionar ao&nbsp;Google Bookmarks" alt="Adicionar ao&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;bm_description=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2" rel="nofollow" title="Adicionar ao&nbsp;Mister Wong"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Adicionar ao&nbsp;Mister Wong" alt="Adicionar ao&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;T=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2" rel="nofollow" title="Adicionar ao&nbsp;Netscape"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/netscape.png" title="Adicionar ao&nbsp;Netscape" alt="Adicionar ao&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2" rel="nofollow" title="Adicionar ao&nbsp;reddit"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/reddit.png" title="Adicionar ao&nbsp;reddit" alt="Adicionar ao&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2" rel="nofollow" title="Adicionar ao&nbsp;Stumble Upon"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Adicionar ao&nbsp;Stumble Upon" alt="Adicionar ao&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F" rel="nofollow" title="Adicionar ao&nbsp;Technorati"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/technorati.png" title="Adicionar ao&nbsp;Technorati" alt="Adicionar ao&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F" rel="nofollow" title="Adicionar ao&nbsp;Tip'd"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/tipd.png" title="Adicionar ao&nbsp;Tip'd" alt="Adicionar ao&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2+@+http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F" rel="nofollow" title="Adicionar ao&nbsp;Twitter"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/twitter.png" title="Adicionar ao&nbsp;Twitter" alt="Adicionar ao&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-2%2F&amp;t=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+2" rel="nofollow" title="Adicionar ao&nbsp;Yahoo My Web"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Adicionar ao&nbsp;Yahoo My Web" alt="Adicionar ao&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.gustavohenrique.net/brogui/2009/12/servidor-linux-com-proxy-e-controle-de-banda-parte-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Servidor Linux com Proxy e Controle de Banda &#8211; Parte 1</title>
		<link>http://www.gustavohenrique.net/brogui/2009/12/servidor-linux-com-proxy-e-controle-de-banda-parte-1/</link>
		<comments>http://www.gustavohenrique.net/brogui/2009/12/servidor-linux-com-proxy-e-controle-de-banda-parte-1/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 12:09:38 +0000</pubDate>
		<dc:creator>gustavohenrique</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[firewall]]></category>
		<category><![CDATA[proxy]]></category>

		<guid isPermaLink="false">http://www.gustavohenrique.net/brogui/?p=173</guid>
		<description><![CDATA[
Vou mostrar como configurar um servidor Linux para compartilhar a internet de maneira mais segura com firewall bloqueando máquinas não cadastradas, proxy Squid fazendo cache full e controle de banda com CBQ.
Um servidor desse tipo é uma excelente opção custo/benefício para pequenas e médias empresas, para pequenos provedores de internet (ISP) ou mesmo para quem [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.gustavohenrique.net/brogui/wp-content/uploads/2009/12/linux-penguin.jpg"><img src="http://www.gustavohenrique.net/brogui/wp-content/uploads/2009/12/linux-penguin.jpg" alt="Tux" title="Tux" width="300" height="224" class="aligncenter size-full wp-image-179" /></a><br />
Vou mostrar como configurar um servidor Linux para compartilhar a internet de maneira mais segura com firewall bloqueando máquinas não cadastradas, proxy Squid fazendo cache full e controle de banda com CBQ.<br />
Um servidor desse tipo é uma excelente opção custo/benefício para pequenas e médias empresas, para pequenos provedores de internet (ISP) ou mesmo para quem deseja apenas compartilhar a internet entre vizinhos.<br />
Dessa vez é um artigo mais prático e menos teórico. Não vou entrar em explicações sobre o que é um proxy, como funciona o controle de banda ou tabelas do iptables.<br />
O artigo foi divido em 3 partes:</p>
<p>1. Instalação do Squid com o patch ZPH (para fazer cache full)<br />
2. Configuração do script de firewall e controle de banda<br />
3. Interface web (desenvolvida em Django) para gerenciar o firewall<br />
<span id="more-173"></span></p>
<h2>Introdução</h2>
<p>O uso do GNU/Linux em servidores vem crescendo a cada ano. Não só pelo custo zero com licenças mas também por sua superior vantagem em desempenho, segurança e estabilidade em relação à outros sistemas operacionais.<br />
Graças ao Linux e aos softwares open source descritos nesse artigo é possível criar uma solução economicamente viável para empresas que ainda não dispõem de uma estrutura adequada para compartilhar a internet.<br />
Nesse exemplo vamos instalar o proxy Squid na mesma máquina que funciona como gateway. Com o crescimento da rede, é aconselhável separar um ou mais servidores apenas para proxy e definindo as rotas manualmente no gateway.<br />
A distribuição utilizada foi Ubuntu Server 9.04 mas nada impede que seja aplicada à outras distros.</p>
<h2>Compilando o Squid com suporte a Cache Full</h2>
<p>Cache full é a ação de fazer com que os arquivos em cache utilizem uma velocidade maior independente do controle de banda definido para as máquinas clientes. Com o proxy Squid, alguns arquivos ficam em cache, ou seja, ficam no HD do seu servidor. Então quando um cliente acessa um site que contém algumas imagens em cache, o Squid não baixa novamente essas imagens, ele envia o que está armazenado no HD. Se arquivos em cache estão vindo do HD do servidor de proxy, então por que não usar a velocidade da rede (10/100 Ethernet por exemplo) para enviar esses arquivos aos clientes já que não consome o link de internet? É até melhor pois o carregamento do site fica mais rápido.<br />
Existe um patch para o squid, chamado de ZPH, que faz uma marcação nos pacotes que estão em cache, e sabendo dessa marcação podemos definir uma velocidade no controle de banda para esses pacotes.<br />
O patch ZPH <span style="color:red">NÃO</span> funciona com qualquer versão do Squid. Até o momento em que estou escrevendo a última versão do Squid compatível é a 3.0STABLE8.<br />
Vou baixar o código fonte do squid, o patch ZPH, aplicar o patch, compilar e instalar o Squid. Chega de teorias:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">root@localhost:~# wget http://zph.bratcheda.org/squid-3.0.STABLE8-zph.patch
root@localhost:~# apt-get source squid3
root@localhost:~# apt-get build-dep squid3
root@localhost:~# cd squid3-3.0.STABLE8
root@localhost:~/squid3-3.0.STABLE8# patch -p1 ../squid-3.0.STABLE8-zph.patch
root@localhost:~/squid3-3.0.STABLE8# dpkg-buildpackage -rfakeroot -uc -b</pre></div></div>

<p>Primeiro foi feito o download do patch ZPH usando o software wget. Depois foi baixado o source do squid3 diretamente dos repositórios do Ubuntu e então, usando o <code>apt-get build-dep squid3</code>, foi criado o diretório <code>squid3-3.0.STABLE8</code> e instaladas as dependências necessárias para compilar o squid. Após isso, entrei no diretório contendo o source do Squid, apliquei o patch ZPH (comando <code>patch -p1</code>) e com isso foram criados os pacotes no formato deb para instalação. Para instalar basta usar o <code>dpkg -i</code> em cada pacote no formato deb.</p>

<div class="wp_syntax"><div class="code"><pre class="diff" style="font-family:monospace;">root@localhost:~/squid3-3.0.STABLE8# cd ..
root@localhost:~# dpkg -i squid3_3.0.STABLE8-<span style="">3</span>+lenny2build0.9.04.1_i386.deb
root@localhost:~# dpkg -i squid3-common_3.0.STABLE8-<span style="">3</span>+lenny2build0.9.04.1_all.deb
root@localhost:~# dpkg -i squid3-cgi_3.0.STABLE8-<span style="">3</span>+lenny2build0.9.04.1_i386.deb
root@localhost:~# dpkg -i squidclient_3.0.STABLE8-<span style="">3</span>+lenny2build0.9.04.1_i386.deb</pre></div></div>

<h2>Configurando o squid.conf</h2>
<p>A localização do arquivo deve ser <code>/etc/squid3/squid.conf</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8
acl localnet src 192.168.254.0/24
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
&nbsp;
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 deny all
&nbsp;
icp_access allow localnet
icp_access deny all
htcp_access allow localnet
htcp_access deny all
&nbsp;
# Porta
http_port 3128 transparent
&nbsp;
hierarchy_stoplist cgi-bin ?
maximum_object_size_in_memory 64 KB
cache_dir aufs /var/cache/squid 40000 16 256
max_open_disk_fds 0
&nbsp;
# Marcacao TOS do ZPH
zph_tos_local 16
zph_tos_parent on
&nbsp;
# Arquivos de LOG
access_log /var/log/squid3/access.log squid
cache_log /var/log/squid3/cache.log
cache_store_log /var/log/squid3/store.log
&nbsp;
# PID
pid_filename /var/run/squid.pid
&nbsp;
refresh_pattern ^ftp:		1440	20%	10080
refresh_pattern ^gopher:	1440	0%	1440
refresh_pattern (cgi-bin|\?)	0	0%	0
refresh_pattern .		0	20%	4320
visible_hostname mainserver
icp_port 3130
coredump_dir /var/cache</pre></div></div>

<p>Consulte a <a href="http://www.squid-cache.org" target="_blank">documentação do squid</a> para mais informações.</p>
<h2>Inciando o Squid</h2>
<p>Criando a estrutura de diretórios e iniciando o serviço:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">root@localhost:~# squid -z
root@localhost:~# /etc/init.d/squid3 start</pre></div></div>

<p>Para testar o funcionamento basta configurar o browser para acessar via proxy usando o IP 127.0.0.1 e porta 3128. Se navegar na internet significa que funcionou, se não, leia os arquivos de log para tentar identificar o problema.</p>
<h2>Conclusão</h2>
<p>É possível que ocorra algum problema seguindo os passos descritos nesse artigo. O motivo se deve ao fato de que tive que simplificar bastante os passos para servir como um exemplo genérico.<br />
Problemas ou dúvidas ficarei feliz em poder ajudar.<br />
Feliz natal à todos!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Compartilhe esse artigo</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;submitHeadline=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1&amp;submitSummary=" rel="nofollow" title="Adicionar ao&nbsp;Buzz"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/buzz.png" title="Adicionar ao&nbsp;Buzz" alt="Adicionar ao&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1" rel="nofollow" title="Adicionar ao&nbsp;Del.icio.us"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/delicious.png" title="Adicionar ao&nbsp;Del.icio.us" alt="Adicionar ao&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1" rel="nofollow" title="Adicionar ao&nbsp;digg"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/digg.png" title="Adicionar ao&nbsp;digg" alt="Adicionar ao&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F" rel="nofollow" title="Adicionar ao&nbsp;Facebook"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/facebook.png" title="Adicionar ao&nbsp;Facebook" alt="Adicionar ao&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1" rel="nofollow" title="Adicionar ao&nbsp;Google Bookmarks"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/google.png" title="Adicionar ao&nbsp;Google Bookmarks" alt="Adicionar ao&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;bm_description=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1" rel="nofollow" title="Adicionar ao&nbsp;Mister Wong"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Adicionar ao&nbsp;Mister Wong" alt="Adicionar ao&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;T=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1" rel="nofollow" title="Adicionar ao&nbsp;Netscape"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/netscape.png" title="Adicionar ao&nbsp;Netscape" alt="Adicionar ao&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1" rel="nofollow" title="Adicionar ao&nbsp;reddit"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/reddit.png" title="Adicionar ao&nbsp;reddit" alt="Adicionar ao&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;title=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1" rel="nofollow" title="Adicionar ao&nbsp;Stumble Upon"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Adicionar ao&nbsp;Stumble Upon" alt="Adicionar ao&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F" rel="nofollow" title="Adicionar ao&nbsp;Technorati"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/technorati.png" title="Adicionar ao&nbsp;Technorati" alt="Adicionar ao&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F" rel="nofollow" title="Adicionar ao&nbsp;Tip'd"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/tipd.png" title="Adicionar ao&nbsp;Tip'd" alt="Adicionar ao&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1+@+http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F" rel="nofollow" title="Adicionar ao&nbsp;Twitter"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/twitter.png" title="Adicionar ao&nbsp;Twitter" alt="Adicionar ao&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F12%2Fservidor-linux-com-proxy-e-controle-de-banda-parte-1%2F&amp;t=Servidor+Linux+com+Proxy+e+Controle+de+Banda+%26%238211%3B+Parte+1" rel="nofollow" title="Adicionar ao&nbsp;Yahoo My Web"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Adicionar ao&nbsp;Yahoo My Web" alt="Adicionar ao&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.gustavohenrique.net/brogui/2009/12/servidor-linux-com-proxy-e-controle-de-banda-parte-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Configurando Apache para trabalhar com Django e PHP</title>
		<link>http://www.gustavohenrique.net/brogui/2009/01/servidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi/</link>
		<comments>http://www.gustavohenrique.net/brogui/2009/01/servidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 14:20:51 +0000</pubDate>
		<dc:creator>gustavohenrique</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[mod_wsgi]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[virtualhost]]></category>

		<guid isPermaLink="false">http://www.gustavohenrique.net/brogui/?p=54</guid>
		<description><![CDATA[Introdução
Como primeiro artigo do ano, vou mostrar de forma bem rápida como configurar o Apache no Linux para trabalhar com Django e com PHP ao mesmo tempo. Para tal, foi usada a instalação padrão do Apache2 com PHP5 do Ubuntu no servidor de desenvolvimento.
O mod_wsgi é um módulo para o Apache que permite executar aplicações [...]]]></description>
			<content:encoded><![CDATA[<h2>Introdução</h2>
<p>Como primeiro artigo do ano, vou mostrar de forma bem rápida como configurar o Apache no Linux para trabalhar com Django e com PHP ao mesmo tempo. Para tal, foi usada a instalação padrão do Apache2 com PHP5 do Ubuntu no servidor de desenvolvimento.<br />
O mod_wsgi é um módulo para o Apache que permite executar aplicações web escritas em Python. Trata-se de uma alternativa ao mod_python e ao FastCGI.<br />
<span id="more-54"></span></p>
<h2>Instalação</h2>
<p>O primeiro passo é instalar o mod_wsgi. No meu caso instalei a partir do repositório do Ubuntu, via apt-get:<br />
<code>root@notebook:~# apt-get install libapache2-mod-wsgi</code><br />
Nesse exemplo vamos utilizar o diretório padrão do Apache (<code>/var/www</code>) para hospedar os projetos feitos em Django e PHP. Abaixo a estrutura de diretório utilizada:<br />
<code><br />
+ /var/www/<br />
+--+ meusite/<br />
     +-- apps_wsgi/<br />
     |   +-- djangosite/<br />
     +-- www/<br />
</code></p>
<p>var/www &#8211; Diretório padrão do Apache<br />
meusite &#8211; Diretório do projeto<br />
apps_wsgi &#8211; Diretório de projetos feitos em Django<br />
djangosite &#8211; Projeto em Django, criado com o django-admin<br />
www &#8211; Diretório de arquivos PHP</p>
<p>Antes de criarmos o VirtualHost, é preciso configurar o <code>/etc/hosts</code>. Vamos atribuir um IP de loopback uma vez que o Apache está sendo executado na máquina local que é um ambiente de desenvolvimento.<br />
Segue abaixo o conteúdo do <code>/etc/hosts</code> já alterado. A primeira coluna é o IP do servidor virtual, a segunda é o hostname e a terceira é um alias para o hostname.<br />
<code><br />
gustavo@notebook:~# cat /etc/hosts<br />
127.0.0.1 localhost     notebook<br />
127.0.0.2 www2.meusite.com.br meusite<br />
</code><br />
Reparem que não configurei o hostname como www.meusite.com.br, pois senão o Apache responderia à toda requisição feita para essa URL, logo não conseguiríamos acessar o site na web. Se testarmos dar um ping para <code>meusite</code> vamos obter uma resposta sem perda de pacotes.</p>
<p>O próximo passo é criar um VirtualHost para nosso projeto. Com VirtualHost é possível hospedar vários sites no Apache, cada um sendo representado por um nome. Vamos criar nosso VirtualHost após a última linha do arquivo de configuração <code>/etc/apache2/sites-enabled/000-default</code> do Apache:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">WSGIRestrictStdout Off
&lt;virtualHost www2.meusite.com.br&gt;
  LimitInternalRecursion 1000
  ServerName meusite
  DocumentRoot /var/www/meusite/www/
&nbsp;
  ErrorLog /var/www/meusite/error.log
  LogLevel warn
&nbsp;
  &lt;directory &quot;/var/www/meusite/www/&quot;&gt;
    Order allow,deny
    Allow from all
  &lt;/directory&gt;
&nbsp;
  AddHandler wsgi-script .wsgi
  #WSGIScriptAlias /meusite/ &quot;/var/www/meusite/www/meusite.wsgi&quot;
  &lt;directory &quot;/var/www/meusite/apps_wsgi/&quot;&gt;
    AllowOverride FileInfo
    Options ExecCGI MultiViews FollowSymLinks
    MultiviewsMatch Handlers
    Order deny,allow
    Allow from all
  &lt;/directory&gt;
&lt;/virtualHost&gt;</pre></div></div>

<p>Então agora criamos o arquivo <code>/var/www/meusite/www/meusite.wsgi</code> com o seguinte conteúdo:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">path</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/var/www/meusite/apps_wsgi'</span><span style="color: black;">&#41;</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span>=<span style="color: #483d8b;">'meusite.settings'</span>
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>E para finalizar, vamos criar um arquivo <code>.htaccess</code> dentro do <code>DocumentRoot</code>. Esse arquivo utiliza o <code>mod_rewrite</code> do Apache para sobrescrever URLs. Nesse exemplo o <code>.htaccess</code> verifica todas as requisições feitas ao servidor, e caso não exista o arquivo dentro do diretório <code>www</code>, quem assume o controle é o Django.<br />
<code><br />
$ cat /var/www/meusite/www/.htaccess<br />
RewriteEngine On<br />
RewriteCond %{REQUEST_FILENAME} !-f<br />
RewriteRule ^(.*)$ /var/www/meusite/www/meusite.wsgi/$1 [QSA,PT,L]<br />
</code></p>
<h2>Conclusão</h2>
<p>Para testar, crie o arquivo <code>/var/www/meusite/www/info.php</code> contendo a seguinte linha:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #990000;">phpinfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Agora tente acessa a URL <code>http://www2.meusite.com.br/info.php</code> e será exibida na tela informações sobre o ambiente web instalado na máquina. Agora se tentar acessar um arquivo que não existe, tipo <code>http://www2.meusite.com.br/qualquercoisa</code>, o <code>.htaccess</code> redirecionará a requisição para o arquivo <code>urls.py</code> do projeto feito em Django.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Compartilhe esse artigo</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;submitHeadline=Configurando+Apache+para+trabalhar+com+Django+e+PHP&amp;submitSummary=" rel="nofollow" title="Adicionar ao&nbsp;Buzz"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/buzz.png" title="Adicionar ao&nbsp;Buzz" alt="Adicionar ao&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;title=Configurando+Apache+para+trabalhar+com+Django+e+PHP" rel="nofollow" title="Adicionar ao&nbsp;Del.icio.us"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/delicious.png" title="Adicionar ao&nbsp;Del.icio.us" alt="Adicionar ao&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;title=Configurando+Apache+para+trabalhar+com+Django+e+PHP" rel="nofollow" title="Adicionar ao&nbsp;digg"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/digg.png" title="Adicionar ao&nbsp;digg" alt="Adicionar ao&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F" rel="nofollow" title="Adicionar ao&nbsp;Facebook"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/facebook.png" title="Adicionar ao&nbsp;Facebook" alt="Adicionar ao&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;title=Configurando+Apache+para+trabalhar+com+Django+e+PHP" rel="nofollow" title="Adicionar ao&nbsp;Google Bookmarks"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/google.png" title="Adicionar ao&nbsp;Google Bookmarks" alt="Adicionar ao&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;bm_description=Configurando+Apache+para+trabalhar+com+Django+e+PHP" rel="nofollow" title="Adicionar ao&nbsp;Mister Wong"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Adicionar ao&nbsp;Mister Wong" alt="Adicionar ao&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;T=Configurando+Apache+para+trabalhar+com+Django+e+PHP" rel="nofollow" title="Adicionar ao&nbsp;Netscape"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/netscape.png" title="Adicionar ao&nbsp;Netscape" alt="Adicionar ao&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;title=Configurando+Apache+para+trabalhar+com+Django+e+PHP" rel="nofollow" title="Adicionar ao&nbsp;reddit"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/reddit.png" title="Adicionar ao&nbsp;reddit" alt="Adicionar ao&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;title=Configurando+Apache+para+trabalhar+com+Django+e+PHP" rel="nofollow" title="Adicionar ao&nbsp;Stumble Upon"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Adicionar ao&nbsp;Stumble Upon" alt="Adicionar ao&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F" rel="nofollow" title="Adicionar ao&nbsp;Technorati"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/technorati.png" title="Adicionar ao&nbsp;Technorati" alt="Adicionar ao&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F" rel="nofollow" title="Adicionar ao&nbsp;Tip'd"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/tipd.png" title="Adicionar ao&nbsp;Tip'd" alt="Adicionar ao&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Configurando+Apache+para+trabalhar+com+Django+e+PHP+@+http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F" rel="nofollow" title="Adicionar ao&nbsp;Twitter"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/twitter.png" title="Adicionar ao&nbsp;Twitter" alt="Adicionar ao&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2009%2F01%2Fservidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi%2F&amp;t=Configurando+Apache+para+trabalhar+com+Django+e+PHP" rel="nofollow" title="Adicionar ao&nbsp;Yahoo My Web"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Adicionar ao&nbsp;Yahoo My Web" alt="Adicionar ao&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.gustavohenrique.net/brogui/2009/01/servidor-compartilhado-django-php-configurando-virtualhost-no-apache-com-mod_wsgi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Roteamento em Linux com 2 links de internet</title>
		<link>http://www.gustavohenrique.net/brogui/2008/12/roteamento-em-linux-com-2-links-de-internet/</link>
		<comments>http://www.gustavohenrique.net/brogui/2008/12/roteamento-em-linux-com-2-links-de-internet/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 13:16:43 +0000</pubDate>
		<dc:creator>gustavohenrique</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[debian]]></category>
		<category><![CDATA[ip]]></category>
		<category><![CDATA[iproute2]]></category>
		<category><![CDATA[iptables]]></category>
		<category><![CDATA[roteamento]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.gustavohenrique.net/brogui/?p=50</guid>
		<description><![CDATA[Introdução
Como primeiro artigo sobre Linux, vou abordar um pouco sobre roteamento de pacotes usando iproute e iptables.
Imagine um cenário onde há um servidor que compartilha a internet para a rede interna, atuando como gateway, e por algum motivo foi contratado mais um link de internet. Ou então há 2 ou mais servidores proxy e se [...]]]></description>
			<content:encoded><![CDATA[<h2>Introdução</h2>
<p>Como primeiro artigo sobre Linux, vou abordar um pouco sobre roteamento de pacotes usando iproute e iptables.<br />
Imagine um cenário onde há um servidor que compartilha a internet para a rede interna, atuando como gateway, e por algum motivo foi contratado mais um link de internet. Ou então há 2 ou mais servidores proxy e se deseja definir para qual deles cada IP da rede interna deve seguir caminho. Outro cenário seria separar a parte de navegação dos demais serviços (msn, emule, e-mail&#8230;), cada uma usando um link diferente. Nesse artigo vou demonstrar que o Linux proporciona maneiras simples e eficientes de implementar esse tipo de roteamento. As distribuições utilizadas foram ubuntu server 5.04 e 5.10 e debian lenny.<br />
<span id="more-50"></span></p>
<h2>Funcionamento</h2>
<p>No exemplo em questão, o servidor que vou configurar é um gateway compartilhando a internet para vários clientes. O iptables realiza uma marcação para todos os pacotes oriundos de cada IP cliente e o iproute determina para qual link os pacotes deverão ser encaminhados de acordo com a marcação feita. Vou usar a marcação 1 para pacotes via link1, marcação 2 para pacotes à serem encaminhados para o link2 e a rota principal do próprio gateway será pelo link1.</p>
<h2>Instalação</h2>
<p>O iptables e iproute são pacotes presentes em quase todas as distribuições. Na maioria o iptables é instalado por padrão. Nesse caso, vou instalar apenas o iproute, via apt-get, que é o gerenciador de pacotes padrão das distribuições debian e ubuntu.<br />
<code>sudo apt-get install iproute2</code></p>
<h2>Criando a tabela de roteamento</h2>
<p>Vou criar 2 tabelas chamadas link1 e link2, adicionando duas linhas no final do arquivo <code>/etc/iproute2/rt_tables</code>, ficando assim:<br />
<code><br />
root@gateway# cat /etc/iproute2/rt_tables<br />
255     local<br />
254     main<br />
253     default<br />
0       unspec<br />
250     link1<br />
251     link2<br />
</code></p>
<p>Agora vou criar o script <code>rotas</code>, dentro do diretório <code>/etc/init.d/</code>, que utiliza o iproute para definir as rotas.<br />
<code><br />
root@gateway# vim /etc/init.d/rotas<br />
</code></p>
<p>conteúdo do script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># DEFINICAO DOS GATEWAYS (IPs FICTICIOS)</span>
<span style="color: #007800;">GW_LINK1</span>=189.12.34.1
<span style="color: #007800;">GW_LINK2</span>=200.56.78.1
&nbsp;
<span style="color: #666666; font-style: italic;"># PLACAS DE REDE</span>
<span style="color: #007800;">ETH_LINK1</span>=eth1
<span style="color: #007800;">ETH_LINK2</span>=eth2
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> start<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Limpa o cache de rotas</span>
  ip route flush cache
  <span style="color: #666666; font-style: italic;"># Pacotes com marcacao 1 vao para o link1</span>
  ip rule add fwmark <span style="color: #000000;">1</span> prio <span style="color: #000000;">20</span> table link1
  <span style="color: #666666; font-style: italic;"># Pacotes com marcacao 2 vao para o link2</span>
  ip rule add fwmark <span style="color: #000000;">2</span> prio <span style="color: #000000;">20</span> table link2
  <span style="color: #666666; font-style: italic;"># Associa a rota do link1 a interface de rede e tabela correspondentes</span>
  ip route add default via <span style="color: #007800;">$GW_LINK1</span> dev <span style="color: #007800;">$ETH_LINK1</span> table link1
  <span style="color: #666666; font-style: italic;"># Associa a rota do link1 a interface de rede e tabela correspondentes</span>
  ip route add default via <span style="color: #007800;">$GW_LINK2</span> dev <span style="color: #007800;">$ETH_LINK2</span> table link2
  <span style="color: #666666; font-style: italic;"># Adiciona a rota padrao ao link1</span>
  route add default gw <span style="color: #007800;">$GW_LINK1</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Tabela de roteamento criada.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> stop<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Limpa o cache</span>
  ip route flush cache
  <span style="color: #666666; font-style: italic;"># Deleta as regras de acordo com as marcacoes</span>
  ip rule del fwmark <span style="color: #000000;">2</span>
  ip rule del fwmark <span style="color: #000000;">3</span>
  <span style="color: #666666; font-style: italic;"># Deleta a rota padrao</span>
  route del default
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Limpeza da tabela de roteamento concluida.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> $<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">in</span>
  <span style="color: #ff0000;">'start'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #ff0000;">'stop'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> stop; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #ff0000;">'restart'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> stop; start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></td></tr></table></div>

<p>Reparem que o que o script <code>rotas</code> faz é dizer que pacotes marcados como 1 (fwmark 1) fazem parte da tabela link1 e marcados como 2 (fwmark 2) da tabela link2. Em seguida cria uma regra dizendo que os pacotes da tabela link1 seguem pelo gateway e interface de rede correspondentes. O mesmo para a tabela link2.<br />
O próximo passo é fazer a marcação dos pacotes.</p>
<h2>Marcando os pacotes com iptables</h2>
<p>Esse script de firewall é apenas para demonstrar como fazer a marcação de pacotes. As regras devem ser incorporadas à um firewall melhor configurado e mais seguro.<br />
O iptables na verdade é um software que age como frontend para manipulação do firewall do Linux, chamado de netfilter. Esse por sua vez possui uma tabela chamada <code>mangle</code> que é onde ficam armazenadas as regras de marcação de pacotes e outra chamada <code>nat</code>, que contém as regras de <a href="http://pt.wikipedia.org/wiki/Nat">NAT</a>.<br />
Assim como antes, vou colocar o script dentro do diretório <code>/etc/init.d</code><br />
<code><br />
root@gateway# vim /etc/init.d/firewall<br />
</code></p>
<p>E dentro do script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># modprobe eh usado para carregar modulos do kernel</span>
<span style="color: #007800;">MOD</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> modprobe<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># iptables</span>
<span style="color: #007800;">IPT</span>=$<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">which</span> iptables<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Interfaces de rede</span>
<span style="color: #007800;">I_LINK1</span>=<span style="color: #ff0000;">&quot;eth1&quot;</span>
<span style="color: #007800;">I_LINK2</span>=<span style="color: #ff0000;">&quot;eth2&quot;</span>
<span style="color: #007800;">I_LAN</span>=<span style="color: #ff0000;">&quot;eth0&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> stop<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Limpa a tabela mangle</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-F</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-X</span>
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Firewall parado.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> start<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># Carrega o modulo do kernel</span>
  <span style="color: #007800;">$MOD</span> ip_tables
&nbsp;
  <span style="color: #666666; font-style: italic;"># Limpa as regras anteriores</span>
  stop;
&nbsp;
  <span style="color: #666666; font-style: italic;"># Habilita redirecionamento de IP</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>proc<span style="color: #000000; font-weight: bold;">/</span>sys<span style="color: #000000; font-weight: bold;">/</span>net<span style="color: #000000; font-weight: bold;">/</span>ipv4<span style="color: #000000; font-weight: bold;">/</span>ip_forward
&nbsp;
  <span style="color: #666666; font-style: italic;"># Clientes para o link1</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.2 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">1</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.4 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">1</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Clientes para o link2</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.3 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.5 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.6 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># Apenas a navegacao vai para o link2. E-mail, msn e outros vao para o link1 que é a rota padrao.</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.7 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">2</span>
  <span style="color: #007800;">$IPT</span> <span style="color: #660033;">-t</span> mangle <span style="color: #660033;">-A</span> PREROUTING <span style="color: #660033;">-s</span> 192.168.0.7 <span style="color: #660033;">-i</span> <span style="color: #007800;">$I_LAN</span> <span style="color: #660033;">-p</span> tcp <span style="color: #660033;">--dport</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #000000;">80</span> <span style="color: #660033;">-j</span> MARK <span style="color: #660033;">--set-mark</span> <span style="color: #000000;">1</span>
&nbsp;
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Firewall iniciado.&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">case</span> $<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">in</span>
  <span style="color: #ff0000;">'start'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #ff0000;">'stop'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> stop; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
  <span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> start; <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000; font-weight: bold;">;;</span>
<span style="color: #000000; font-weight: bold;">esac</span></pre></td></tr></table></div>

<p>Por fim, vou colocar os dois scripts para carregarem durante a inicialização do sistema. Há muitas maneiras de fazer isso, então vou fazer da mais preguiçosa possível porque isso varia de acordo com o administrador e distribuição utilizada:<br />
<code><br />
root@gateway#: chmod +x /etc/init.d/rotas<br />
root@gateway#: chmod +x /etc/init.d/firewall<br />
root@gateway#: ln -s /etc/init.d/rotas /etc/rcS.d/S79rotas<br />
root@gateway#: ln -s /etc/init.d/firewall /etc/rcS.d/S80firewall<br />
</code></p>
<h2>Conclusão</h2>
<p>Apenas com esses 2 scripts não será possível compartilhar a internet. Porém o objetivo aqui é mostrar uma breve abordagem de como fazer roteamento no Linux. Artigos, dicas e scripts para compartilhar internet existem aos milhares na internet. Com uma rápida pesquisa no google e implementando o que foi descrito aqui, é possível criar um servidor de internet em Linux muito mais estável e eficiente do que um rodando Windows.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Compartilhe esse artigo</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://buzz.yahoo.com/submit?submitUrl=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;submitHeadline=Roteamento+em+Linux+com+2+links+de+internet&amp;submitSummary=" rel="nofollow" title="Adicionar ao&nbsp;Buzz"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/buzz.png" title="Adicionar ao&nbsp;Buzz" alt="Adicionar ao&nbsp;Buzz" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;title=Roteamento+em+Linux+com+2+links+de+internet" rel="nofollow" title="Adicionar ao&nbsp;Del.icio.us"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/delicious.png" title="Adicionar ao&nbsp;Del.icio.us" alt="Adicionar ao&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;title=Roteamento+em+Linux+com+2+links+de+internet" rel="nofollow" title="Adicionar ao&nbsp;digg"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/digg.png" title="Adicionar ao&nbsp;digg" alt="Adicionar ao&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F" rel="nofollow" title="Adicionar ao&nbsp;Facebook"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/facebook.png" title="Adicionar ao&nbsp;Facebook" alt="Adicionar ao&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;title=Roteamento+em+Linux+com+2+links+de+internet" rel="nofollow" title="Adicionar ao&nbsp;Google Bookmarks"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/google.png" title="Adicionar ao&nbsp;Google Bookmarks" alt="Adicionar ao&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.mister-wong.com/index.php?action=addurl&amp;bm_url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;bm_description=Roteamento+em+Linux+com+2+links+de+internet" rel="nofollow" title="Adicionar ao&nbsp;Mister Wong"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/misterwong.png" title="Adicionar ao&nbsp;Mister Wong" alt="Adicionar ao&nbsp;Mister Wong" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;T=Roteamento+em+Linux+com+2+links+de+internet" rel="nofollow" title="Adicionar ao&nbsp;Netscape"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/netscape.png" title="Adicionar ao&nbsp;Netscape" alt="Adicionar ao&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;title=Roteamento+em+Linux+com+2+links+de+internet" rel="nofollow" title="Adicionar ao&nbsp;reddit"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/reddit.png" title="Adicionar ao&nbsp;reddit" alt="Adicionar ao&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;title=Roteamento+em+Linux+com+2+links+de+internet" rel="nofollow" title="Adicionar ao&nbsp;Stumble Upon"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Adicionar ao&nbsp;Stumble Upon" alt="Adicionar ao&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F" rel="nofollow" title="Adicionar ao&nbsp;Technorati"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/technorati.png" title="Adicionar ao&nbsp;Technorati" alt="Adicionar ao&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://tipd.com/submit.php?url=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F" rel="nofollow" title="Adicionar ao&nbsp;Tip'd"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/tipd.png" title="Adicionar ao&nbsp;Tip'd" alt="Adicionar ao&nbsp;Tip'd" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Roteamento+em+Linux+com+2+links+de+internet+@+http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F" rel="nofollow" title="Adicionar ao&nbsp;Twitter"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/twitter.png" title="Adicionar ao&nbsp;Twitter" alt="Adicionar ao&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fwww.gustavohenrique.net%2Fbrogui%2F2008%2F12%2Froteamento-em-linux-com-2-links-de-internet%2F&amp;t=Roteamento+em+Linux+com+2+links+de+internet" rel="nofollow" title="Adicionar ao&nbsp;Yahoo My Web"><img class="social_img" src="http://www.gustavohenrique.net/brogui/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Adicionar ao&nbsp;Yahoo My Web" alt="Adicionar ao&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.gustavohenrique.net/brogui/2008/12/roteamento-em-linux-com-2-links-de-internet/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
