<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Iptables on bramp.net</title>
    <link>https://blog.bramp.net/</link>
    <description>Recent content in Iptables on bramp.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-GB</language>
    <lastBuildDate>Tue, 26 Jan 2010 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://blog.bramp.net/tags/iptables/" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Redirect local traffic to a web cache with iptables</title>
      <link>https://blog.bramp.net/post/2010/01/26/redirect-local-traffic-to-a-web-cache-with-iptables/</link>
      <pubDate>Tue, 26 Jan 2010 00:00:00 +0000</pubDate>
      
      <guid>https://blog.bramp.net/post/2010/01/26/redirect-local-traffic-to-a-web-cache-with-iptables/</guid>
      <description><p>Very occasionally I come across a Linux application that does not respect the http_proxy variable. This stops the application from working while I’m at university as they forbid traffic on port 80 unless it goes via their webcache. So today I came up with a hack of a solution that involves iptables:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1"># IP address and port number of the webcache</span>
</span></span><span class="line"><span class="cl"><span class="nv">WEBCACHE</span><span class="o">=</span>194.80.32.10:8080
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Flush any previous rules</span>
</span></span><span class="line"><span class="cl">iptables -t nat --flush
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Delete and recreate the chain</span>
</span></span><span class="line"><span class="cl">iptables -t nat -X HTTPFORCE
</span></span><span class="line"><span class="cl">iptables -t nat -N HTTPFORCE
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Don&#39;t touch local traffic (localhost and internal network)</span>
</span></span><span class="line"><span class="cl">iptables -t nat -A HTTPFORCE -o lo -j RETURN
</span></span><span class="line"><span class="cl">iptables -t nat -A HTTPFORCE --dst 127.0.0.1/8 -j RETURN
</span></span><span class="line"><span class="cl">iptables -t nat -A HTTPFORCE --dst 10.0.0.0/8 -j RETURN
</span></span><span class="line"><span class="cl"><span class="c1"># Add any other local networks here.</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Now we have two options. Please uncomment out one of them</span>
</span></span><span class="line"><span class="cl"><span class="c1"># 1) Redirect packets on port 80 to the webcache</span>
</span></span><span class="line"><span class="cl"><span class="c1">#    This may not work unless the webcache is generous with its input</span>
</span></span><span class="line"><span class="cl"><span class="c1">#iptables -t nat -A HTTPFORCE -p tcp --dport 80 -j DNAT --to $WEBCACHE</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># 2) Redirect packets on port 80 to localhost port 1234</span>
</span></span><span class="line"><span class="cl"><span class="c1">#    On port 1234 you need to run a local web proxy, which forwards </span>
</span></span><span class="line"><span class="cl"><span class="c1">#    requests to the real webcache</span>
</span></span><span class="line"><span class="cl"><span class="c1">#iptables -t nat -A HTTPFORCE -p tcp --dport 80 -j REDIRECT --to-port 1234</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Capture all outgoing TCP syns</span>
</span></span><span class="line"><span class="cl">iptables -t nat -A OUTPUT -p tcp --syn -j HTTPFORCE
</span></span></code></pre></div><p>All outgoing TCP packets on port 80 which are not destined for a local network are captured and changed in one of two ways. The first option just manipulates the IP/TCP header, and the second redirects the traffic to a proxy running on localhost. The reason for the two options was that my university’s webcache seemed unable to deal with HTTP requests without the full URL in the GET line, even though the request contains a valid Host header. I think this is a misconfiguration of their squid proxy, so instead you can redirect to a local proxy which forwards the request on to the webcache.</p>
<p>This all seems a hassle but sometimes it is needed when a application does not respect the http_proxy environment. On a good note all libcurl applications should respect it by default.</p>
</description>
    </item>
    
  </channel>
</rss>
