<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>squid</title>
  <link rel="alternate" type="text/html" href="http://tumbleweed.org.za/tags/squid"/>
  <link rel="self" type="application/atom+xml" href="http://tumbleweed.org.za/taxonomy/term/150/atom/feed"/>
  <id>http://tumbleweed.org.za/taxonomy/term/150/atom/feed</id>
  <updated>2008-09-19T00:16:45+00:00</updated>
  <entry>
    <title>Easy home transparent proxy</title>
    <link rel="alternate" type="text/html" href="http://tumbleweed.org.za/2008/01/19/easy-home-transparent-proxy" />
    <id>http://tumbleweed.org.za/2008/01/19/easy-home-transparent-proxy</id>
    <published>2008-01-19T16:01:29+00:00</published>
    <updated>2008-09-19T00:16:45+00:00</updated>
    <author>
      <name>tumbleweed</name>
    </author>
    <category term="howto" />
    <category term="proxy" />
    <category term="squid" />
    <category term="technical" />
    <summary type="html"><![CDATA[<p>Everyone in South Africa wants to save a little more bandwidth, as low traffic caps are the rule of the day (esp if you are hanging off an expensive&nbsp;3G&nbsp;connection).</p>

<p>While the &#8220;correct&#8221; thing to do is to use <a href="http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol">wpad autodetection</a>, and thus politely request that users use your proxy, this isn&#8217;t always&nbsp;an&nbsp;option:</p>

<ul>
<li>Firefox doesn&#8217;t Autodetect Proxies&nbsp;by&nbsp;default</li>
<li>Autodetection doesn&#8217;t behave well for many roaming users (firefox should talk&nbsp;to&nbsp;network-manager)</li>
<li>Many programs simply don&#8217;t&nbsp;support&nbsp;wpad.</li>
<li>Your upstream <span class="caps"><span class="caps">ISP</span></span> transparently proxies <em>anyway</em> (the norm in <span class="caps"><span class="caps">ZA</span></span>), so it&#8217;s not like we have any <a href="http://en.wikipedia.org/wiki/End-to-end_principle">end-to-endness</a>&nbsp;to&nbsp;protect.</li>
</ul>

<p>So, here&#8217;s how you&nbsp;do&nbsp;it:</p>

<ol>
<li>Lets assume your network is 10.1.1.0/24, and the squid box is 10.1.1.1&nbsp;on&nbsp;eth0</li>
<li>Install squid (<span class="geshifilter"><code class="geshifilter-text">aptitude install squid</code></span>), configure it to have a reasonably large storage pool, give it some sane&nbsp;ACLs,&nbsp;etc.</li>
<li>Add <span class="geshifilter"><code class="geshifilter-text">http_port 8080 transparent</code></span> to <span class="geshifilter"><code class="geshifilter-text">squid.conf</code></span>(or <span class="geshifilter"><code class="geshifilter-text">http_port 10.1.1.1:8080 transparent</code></span> if you are using explicit <span class="geshifilter"><code class="geshifilter-text">http_port</code></span>&nbsp;options)</li>
<li><span class="geshifilter"><code class="geshifilter-text">invoke-rc.d squid reload</code></span></li>
<li>Add the following to your&nbsp;iptables&nbsp;script:</li>
</ol>

<div class="geshifilter"><div class="bash geshifilter-bash" style="font-family:monospace;">iptables <span class="re5">-t</span> nat <span class="re5">-A</span> <span class="caps"><span class="caps">PREROUTING</span></span> <span class="re5">-i</span> eth0 <span class="re5">-s</span> 10.1.1.0<span class="sy0">/</span><span class="nu0">24</span> <span class="re5">-d</span> <span class="sy0">!</span> 10.20.1.1 <span class="re5">-p</span> tcp <span class="re5">&#8212;dport</span> <span class="nu0">80</span> <span class="re5">-j</span> <span class="caps"><span class="caps">REDIRECT</span></span> <span class="re5">&#8212;to</span> <span class="nu0">8080</span></div></div>

<p>If you run squid on your network&#8217;s default gateway, then you are done. Otherwise, if you have a separate router, you need to do the following on&nbsp;the&nbsp;router:</p>

<ol>
<li>Add a new <span class="geshifilter"><code class="geshifilter-text">transprox</code></span> table to <span class="geshifilter"><code class="geshifilter-text">/etc/iproute2/rt_tables</code></span>, i.e. <span class="geshifilter"><code class="geshifilter-text">1 transprox</code></span></li>
<li>Pick a new netfilter <span class="caps"><span class="caps">MARK</span></span> value,&nbsp;i.e.&nbsp;0x04</li>
<li>Add the following to the router&#8217;s&nbsp;iptables&nbsp;script:</li>
</ol>

<div class="geshifilter"><div class="bash geshifilter-bash" style="font-family:monospace;"><span class="co0"># Transparent proxy</span><br />
iptables <span class="re5">-t</span> mangle <span class="re5">-F</span> <span class="caps"><span class="caps">PREROUTING</span></span><br />
iptables <span class="re5">-t</span> mangle <span class="re5">-A</span> <span class="caps"><span class="caps">PREROUTING</span></span> <span class="re5">-i</span> br-lan <span class="re5">-s</span> <span class="sy0">!</span> 10.1.1.1 <span class="re5">-d</span> <span class="sy0">!</span> 10.1.1.0<span class="sy0">/</span><span class="nu0">24</span> <span class="re5">-p</span> tcp <span class="re5">&#8212;dport</span> <span class="nu0">80</span> <span class="re5">-j</span> <span class="caps"><span class="caps">MARK</span></span> <span class="re5">&#8212;set-mark</span> 0x04<br />
ip route del table transprox<br />
ip route add default via 10.1.1.1 table transprox<br />
ip rule del table transprox<br />
ip rule add fwmark 0x04 pref <span class="nu0">10</span> table transprox</div></div>

<ol>
<li>Done: test and tail your&nbsp;squid&nbsp;logs</li>
</ol>

<p>The reason we use <span class="geshifilter"><code class="geshifilter-text">iproute</code></span> rules rather than <span class="geshifilter"><code class="geshifilter-text">iptables DNAT</code></span> is that you lose destination-<span class="caps">IP</span> information with a <span class="caps">DNAT</span> (like the envelope of&nbsp;an&nbsp;e-mail).</p>

<p>An alternative solution is to run tinyproxy on the router (with the transparent option, enabled in ubuntu but not debian), use the <span class="caps"><span class="caps">REDIRECT</span></span> rule above on the router, to redirect to the tinyproxy, and have that <span class="geshifilter"><code class="geshifilter-text">upstream</code></span> to the squid. But tinyproxy requires some <span class="caps">RAM</span>, and on a <span class="caps">WRT54</span> or the likes, you don&#8217;t have any of that&nbsp;to&nbsp;spare&#8230;</p>

<p>Should you need to temporarily disable this for&nbsp;any&nbsp;reason:</p>

<ul>
<li>With all-in-one-router: <span class="geshifilter"><code class="geshifilter-text">iptables -t nat -F PREROUTING</code></span></li>
<li>With the separate router: <span class="geshifilter"><code class="geshifilter-text">iptables -t mangle -F PREROUTING</code></span></li>
</ul>
    ]]></summary>
    <content type="html"><![CDATA[<p>Everyone in South Africa wants to save a little more bandwidth, as low traffic caps are the rule of the day (esp if you are hanging off an expensive 3G&nbsp;connection).</p>

<p>While the &#8220;correct&#8221; thing to do is to use <a href="http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol">wpad autodetection</a>, and thus politely request that users use your proxy, this isn&#8217;t always an&nbsp;option:</p>

<ul>
<li>Firefox doesn&#8217;t Autodetect Proxies by&nbsp;default</li>
<li>Autodetection doesn&#8217;t behave well for many roaming users (firefox should talk to&nbsp;network-manager)</li>
<li>Many programs simply don&#8217;t support&nbsp;wpad.</li>
<li>Your upstream <span class="caps">ISP</span> transparently proxies <em>anyway</em> (the norm in <span class="caps">ZA</span>), so it&#8217;s not like we have any <a href="http://en.wikipedia.org/wiki/End-to-end_principle">end-to-endness</a> to&nbsp;protect.</li>
</ul>

<p>So, here&#8217;s how you do&nbsp;it:</p>

<ol>
<li>Lets assume your network is 10.1.1.0/24, and the squid box is 10.1.1.1 on&nbsp;eth0</li>
<li>Install squid (<code>aptitude install squid</code>), configure it to have a reasonably large storage pool, give it some sane ACLs,&nbsp;etc.</li>
<li>Add <code>http_port 8080 transparent</code> to <code>squid.conf</code>(or <code>http_port 10.1.1.1:8080 transparent</code> if you are using explicit <code>http_port</code>&nbsp;options)</li>
<li><code>invoke-rc.d squid reload</code></li>
<li>Add the following to your iptables&nbsp;script:</li>
</ol>

<div class="geshifilter"><div class="bash geshifilter-bash" style="font-family:monospace;">iptables <span class="re5">-t</span> nat <span class="re5">-A</span> <span class="caps">PREROUTING</span> <span class="re5">-i</span> eth0 <span class="re5">-s</span> 10.1.1.0<span class="sy0">/</span><span class="nu0">24</span> <span class="re5">-d</span> <span class="sy0">!</span> 10.20.1.1 <span class="re5">-p</span> tcp <span class="re5">&#8212;dport</span> <span class="nu0">80</span> <span class="re5">-j</span> <span class="caps">REDIRECT</span> <span class="re5">&#8212;to</span> <span class="nu0">8080</span></div></div>

<p>If you run squid on your network&#8217;s default gateway, then you are done. Otherwise, if you have a separate router, you need to do the following on the&nbsp;router:</p>

<ol>
<li>Add a new <code>transprox</code> table to <code>/etc/iproute2/rt_tables</code>, i.e. <code>1 transprox</code></li>
<li>Pick a new netfilter <span class="caps">MARK</span> value, i.e.&nbsp;0x04</li>
<li>Add the following to the router&#8217;s iptables&nbsp;script:</li>
</ol>

<div class="geshifilter"><div class="bash geshifilter-bash" style="font-family:monospace;"><span class="co0"># Transparent proxy</span><br />
iptables <span class="re5">-t</span> mangle <span class="re5">-F</span> <span class="caps">PREROUTING</span><br />
iptables <span class="re5">-t</span> mangle <span class="re5">-A</span> <span class="caps">PREROUTING</span> <span class="re5">-i</span> br-lan <span class="re5">-s</span> <span class="sy0">!</span> 10.1.1.1 <span class="re5">-d</span> <span class="sy0">!</span> 10.1.1.0<span class="sy0">/</span><span class="nu0">24</span> <span class="re5">-p</span> tcp <span class="re5">&#8212;dport</span> <span class="nu0">80</span> <span class="re5">-j</span> <span class="caps">MARK</span> <span class="re5">&#8212;set-mark</span> 0x04<br />
ip route del table transprox<br />
ip route add default via 10.1.1.1 table transprox<br />
ip rule del table transprox<br />
ip rule add fwmark 0x04 pref <span class="nu0">10</span> table transprox</div></div>

<ol>
<li>Done: test and tail your squid&nbsp;logs</li>
</ol>

<p>The reason we use <code>iproute</code> rules rather than <code>iptables DNAT</code> is that you lose destination-IP information with a DNAT (like the envelope of an&nbsp;e-mail).</p>

<p>An alternative solution is to run tinyproxy on the router (with the transparent option, enabled in ubuntu but not debian), use the <span class="caps">REDIRECT</span> rule above on the router, to redirect to the tinyproxy, and have that <code>upstream</code> to the squid. But tinyproxy requires some RAM, and on a WRT54 or the likes, you don&#8217;t have any of that to&nbsp;spare&#8230;</p>

<p>Should you need to temporarily disable this for any&nbsp;reason:</p>

<ul>
<li>With all-in-one-router: <code>iptables -t nat -F PREROUTING</code></li>
<li>With the separate router: <code>iptables -t mangle -F PREROUTING</code></li>
</ul>
    ]]></content>
  </entry>
</feed>
