Stunnel
Encapsulate VPN connections with plain TLS to workaround firewall restrictions.
The problem
Most of corporate firewalls have a feature called Deep Packet Inspection (DPI), which blocks VPN connections.
Error when using OpenVPN with UDP protocol
When you OpenVPN client is trying to connect to the server using UDP, it returns the following error.
Error when using OpenVPN with TCP protocol
When you OpenVPN client is trying to connect to the server using TCP, it returns the following error.
The solution
To encapsulate VPN connections with plain TLS to workaround firewall restrictions.
The main idea is to create the following architecture:
Server | Client Openvpn (port 1194) <---> Stunnel (port 443) <---> | <---> Stunnel (port 443) <---> Openvpn (port 1194) |
Setup server
You need to have OpenVPN already running with TCP protocol.
Install Stunnel4
Create Openssl key
Config Stunnel
Redirect connections to OpenVPN
Create the file /etc/stunnel/stunnel.conf
With the following content:
What the config does is:
Line 3: Stunnel will accept (listen) connections on port 443;
Line 4: Stunnel will redirect all connections to Openvpn which is running on localhost port 1194.
Why Stunnel is running on port 443? Because most likely the client will be able to connect to it since most firewalls allow outbout connections on port 443. Although you can change it to any port you want.
Config iptables
Configure iptables to accept incoming traffic on port 443
Make sure your iptables config is persisted:
apt install iptables-persistent
Enable Stunnel to start on boot by default
Edit /etc/default/stunnel4
Replace ENABLED=0
with ENABLED=1
Restart Stunnel
Check Stunnel
Check stunnel process is up and running and listening to the correct port
Output example:
Setup client
Install Stunnel
Create Openssl key
Copy /etc/stunnel/stunnel.pem
from your server and paste it in your client in the same path.
Config Stunnel
Redirect connections to Stunnel server
Create the file /etc/stunnel/stunnel.conf
With the following content:
What the config does is:
Line 3: Stunnel will accept (listen) connections on port 443;
Line 4: Stunnel will redirect all connections to Stunnel server. Replace
PUT-YOUR-SERVER-HOST-HERE
with your server domain or IP.
Enable Stunnel to start on boot by default
Edit /etc/default/stunnel4
Replace ENABLED=0
with ENABLED=1
Restart Stunnel
Config OpenVPN
Edit your OpenVPN client config as follows:
Your OpenVPN client will actually connect to Stunnel running localhost on port 443, which will redirect to Stunnel server, then it will redirect to the OpenVPN server.
Test
On your client run ifconfig
. You should see your OpenVPN interface tunnel.
Last updated