<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-CA">
	<id>http://nikosapi.org/w/index.php?feed=atom&amp;namespace=0&amp;title=Special%3ANewPages</id>
	<title>nikosapi.org wiki - New pages [en-ca]</title>
	<link rel="self" type="application/atom+xml" href="http://nikosapi.org/w/index.php?feed=atom&amp;namespace=0&amp;title=Special%3ANewPages"/>
	<link rel="alternate" type="text/html" href="http://nikosapi.org/w/index.php/Special:NewPages"/>
	<updated>2026-06-15T09:33:52Z</updated>
	<subtitle>From nikosapi.org wiki</subtitle>
	<generator>MediaWiki 1.35.0</generator>
	<entry>
		<id>http://nikosapi.org/w/index.php/Route_Squid_Proxy_Traffic_Through_an_OpenVPN_Gateway</id>
		<title>Route Squid Proxy Traffic Through an OpenVPN Gateway</title>
		<link rel="alternate" type="text/html" href="http://nikosapi.org/w/index.php/Route_Squid_Proxy_Traffic_Through_an_OpenVPN_Gateway"/>
		<updated>2014-03-02T05:09:30Z</updated>

		<summary type="html">&lt;p&gt;Nikosapi: /* iproute2 Configuration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Problem Description==&lt;br /&gt;
&lt;br /&gt;
When using a VPN, the default configuration is to route all traffic over the VPN tunnel. However, there are some instances where it is useful to only route application-specific traffic instead of forcing all traffic through the VPN. This isn&amp;#039;t always possible, but if application in question supports an HTTP proxy then we can use Squid to transparently pass that traffic off to an OpenVPN gateway.&lt;br /&gt;
&lt;br /&gt;
==Implementation Details==&lt;br /&gt;
&lt;br /&gt;
===iproute2 Configuration===&lt;br /&gt;
Permanently add a custom routing table for OpenVPN traffic:&lt;br /&gt;
 echo &amp;quot;100 openvpn&amp;quot; &amp;gt;&amp;gt; /etc/iproute2/rt_tables&lt;br /&gt;
&lt;br /&gt;
The addition/deletion of the actual routes will be added by the OpenVPN up/down scripts.&lt;br /&gt;
&lt;br /&gt;
===Squid Configuration===&lt;br /&gt;
Traffic which is accepted by Squid must be emitted on the OpenVPN tunnel interface in order for this to work. Since IP address for the interface is dynamic (assuming the server is not configured to provide static IP addresses) this would require updating the Squid config file every time the VPN connection is established (AFAIK, a hostname cannot be provided in place of the IP address). To get around this limitation, we will emit traffic on a random IP address and then use iptables to forward traffic to the real IP address.&lt;br /&gt;
&lt;br /&gt;
The following lines were added to the &amp;#039;&amp;#039;/etc/squid3/squid.conf&amp;#039;&amp;#039; config file:&lt;br /&gt;
 acl local_net src 192.168.0.0/255.255.255.0&lt;br /&gt;
 http_access allow local_net&lt;br /&gt;
 forwarded_for delete&lt;br /&gt;
 tcp_outgoing_address 10.202.101.1&lt;br /&gt;
&lt;br /&gt;
The first line defines my local network, the second line instructs Squid to accept connections from any computer on my local network, and the third line prevents Squid from including the X-Forwarded-For header in its outgoing connections. The last line defines the source IP address for which Squid will use to make requests on behalf of users. Note that this IP address should not belong to your local subnet or the OpenVPN subnet.&lt;br /&gt;
&lt;br /&gt;
===OpenVPN Client Configuration===&lt;br /&gt;
Add the following items to the client config file:&lt;br /&gt;
 route-noexec&lt;br /&gt;
 route-up route-up.sh&lt;br /&gt;
 down down.sh&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;route-noexec&amp;#039;&amp;#039; parameter prevents the client from applying the default routing configuration which is pushed from the server. The &amp;#039;&amp;#039;route-up&amp;#039;&amp;#039; parameter must point to a script which will be used to setup the custom routing and the &amp;#039;&amp;#039;down&amp;#039;&amp;#039; parameter must point to a script that will remove the custom routing once the VPN client is stopped.&lt;br /&gt;
&lt;br /&gt;
The content of &amp;#039;&amp;#039;route-up.sh&amp;#039;&amp;#039; should look like this:&lt;br /&gt;
 #!/bin/bash&lt;br /&gt;
 &lt;br /&gt;
 source &amp;quot;`dirname $0`/vpn_config.sh&amp;quot; &lt;br /&gt;
 &lt;br /&gt;
 logger &amp;quot;OpenVPN route-up.sh: bridge if: $VPNBRIDGE_IF addr: $VPNBRIDGE_ADDR table: $VPNBRIDGE_TBL&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 # Add the default route to our custom &amp;#039;openvpn&amp;#039; routing table&lt;br /&gt;
 ip route add default via $ifconfig_remote dev $dev table $VPNBRIDGE_TBL&lt;br /&gt;
 &lt;br /&gt;
 # Add new interface to act as a middleman between the fixed address used &lt;br /&gt;
 # by Squid and the dynamic address provided by OpenVPN&lt;br /&gt;
 ip tuntap add dev $VPNBRIDGE_IF mode tun&lt;br /&gt;
 ip addr add $VPNBRIDGE_ADDR dev $VPNBRIDGE_IF&lt;br /&gt;
 ip link set dev $VPNBRIDGE_IF up&lt;br /&gt;
 &lt;br /&gt;
 # All packets from Squid are passed to the custom routing table&lt;br /&gt;
 ip rule add from $VPNBRIDGE_ADDR lookup $VPNBRIDGE_TBL&lt;br /&gt;
 &lt;br /&gt;
 # Rewrite the source address to match the one provided by OpenVPN&lt;br /&gt;
 iptables -t nat -A POSTROUTING -s $VPNBRIDGE_ADDR -j SNAT --to-source $ifconfig_local&lt;br /&gt;
 &lt;br /&gt;
 logger &amp;quot;OpenVPN route-up.sh: local: $ifconfig_local remote: $ifconfig_remote device: $dev&amp;quot;&lt;br /&gt;
&lt;br /&gt;
The content of &amp;#039;&amp;#039;down.sh&amp;#039;&amp;#039; should look like this:&lt;br /&gt;
 #!/bin/bash&lt;br /&gt;
 &lt;br /&gt;
 source &amp;quot;`dirname $0`/vpn_config.sh&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 logger &amp;quot;OpenVPN down.sh: bridge if: $VPNBRIDGE_IF addr: $VPNBRIDGE_ADDR table: $VPNBRIDGE_TBL openvpn local: $ifconfig_local&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 ip rule del from $VPNBRIDGE_ADDR lookup $VPNBRIDGE_TBL&lt;br /&gt;
 iptables -t nat -D POSTROUTING -s $VPNBRIDGE_ADDR -j SNAT --to-source $ifconfig_local&lt;br /&gt;
 ip tuntap del dev $VPNBRIDGE_IF mode tun&lt;br /&gt;
&lt;br /&gt;
Finally, the common configurable parameters for both of these scripts is found in another script named &amp;#039;&amp;#039;vpn_config.sh&amp;#039;&amp;#039;. Its contents should look like this:&lt;br /&gt;
 #!/bin/bash&lt;br /&gt;
 &lt;br /&gt;
 export VPNBRIDGE_IF=vpnbridge0&lt;br /&gt;
 export VPNBRIDGE_ADDR=10.202.101.1&lt;br /&gt;
 export VPNBRIDGE_TBL=openvpn&lt;br /&gt;
&lt;br /&gt;
These three scripts should be placed in the same directory and must be marked as executable (chmod +x).&lt;br /&gt;
&lt;br /&gt;
==Theory of Operation==&lt;br /&gt;
This solution is probably overcomplicated, but this is the best I could do with the limited networking skills I have. That being said, I&amp;#039;ll attempt to explain the method here.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;route-up.sh&amp;#039;&amp;#039; script creates a new virtual tunnel network interface (named vpnbridge0) with the IP address which was provided for the tcp_outgoing_address parameter in the Squid configuration file (10.202.101.1). Additionally, the script instructs the kernel to route any traffic from that address to the custom &amp;#039;openvpn&amp;#039; routing table. So when traffic is emitted from the proxy, it gets passed to the openvpn routing table but since the source IP address doesn&amp;#039;t match the dynamic address provided by the VPN server, the request fails. To get around this, iptables NAT feature is used to rewrite the source address of the traffic to match that of the VPN client&amp;#039;s local IP address. This makes the VPN software happy, and the traffic can flow!&lt;br /&gt;
&lt;br /&gt;
As far as I can tell, this is how the traffic ends up flowing:&lt;br /&gt;
* A client sends a request to Squid.&lt;br /&gt;
* Squid binds to 10.202.101.1 and emits the request.&lt;br /&gt;
* The kernel&amp;#039;s routing chain prepares to send the traffic to the route defined in the custom &amp;#039;openvpn&amp;#039; routing table.&lt;br /&gt;
* The iptables POSTROUTING chain notices the packet has a source address of 10.202.101.1, so it rewrites it to match the $ifconfig_local address.&lt;br /&gt;
* The traffic is put onto the VPN&amp;#039;s virtual network interface and VPN server handles the rest.&lt;/div&gt;</summary>
		<author><name>Nikosapi</name></author>
	</entry>
	<entry>
		<id>http://nikosapi.org/w/index.php/Main_Page</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="http://nikosapi.org/w/index.php/Main_Page"/>
		<updated>2013-09-14T02:13:57Z</updated>

		<summary type="html">&lt;p&gt;Nikosapi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;big&amp;gt;&amp;#039;&amp;#039;&amp;#039;Welcome to nikosapi.org&amp;#039;&amp;#039;&amp;#039;&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This site is a place for me ([[User:Nikosapi|nikosapi]]) to put up stuff I find interesting or don&amp;#039;t want to forget.&lt;br /&gt;
&lt;br /&gt;
The wiki is currently closed for editing due to the massive amount of spam which was being added on a daily basis. If you&amp;#039;d like to edit something email me and I&amp;#039;ll provide you with an account.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Quick Links:&amp;#039;&amp;#039;&amp;#039; &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Thank you ablomen! --&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot; position: relative; height: 150px; overflow: hidden;&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;div style=&amp;quot;position: absolute; top: 0px; left: 0px;&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;lt;div style=&amp;quot;float: left&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;imagemap&amp;gt;&lt;br /&gt;
        Image:Software.png|150px&lt;br /&gt;
        default [[Software]]&lt;br /&gt;
        desc none&lt;br /&gt;
      &amp;lt;/imagemap&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;div style=&amp;quot;float: left&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;imagemap&amp;gt;&lt;br /&gt;
        Image:Hardware.png|150px&lt;br /&gt;
        default [[Hardware]]&lt;br /&gt;
        desc none&lt;br /&gt;
      &amp;lt;/imagemap&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
    &amp;lt;div style=&amp;quot;float: left;&amp;quot;&amp;gt;&lt;br /&gt;
      &amp;lt;imagemap&amp;gt;&lt;br /&gt;
        Image:blog.png|150px&lt;br /&gt;
        default [http://nikosapi.org/blog]&lt;br /&gt;
        desc none&lt;br /&gt;
      &amp;lt;/imagemap&amp;gt;&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>MediaWiki default</name></author>
	</entry>
</feed>