Yes you can do this.
You need to connect to the VPN without it taking over your whole network, but still have the VPN's interface useable. I've done that with OpenVPN, by adding lines to the config file:
route-noexec # Don't add or remove routes automatically
script-security 2 # Allow user-defined scripts to be called
down down.sh # Run script called "down.sh" when connection goes down
up up.sh # Run script called "up.sh" when connection comes up
up.sh
script
#!/bin/sh
VPN_IP=$ifconfig_local
VPN_GATEWAY=$route_vpn_gateway
# Route packets from the VPN's IP address to the VPN's gateway
ip rule add from $VPN_IP table vpn
ip route add default via $VPN_GATEWAY table vpn
ip route flush cache
down.sh
script
#!/bin/sh
VPN_IP=$ifconfig_local
VPN_GATEWAY=$route_vpn_gateway
# Flush table and delete the rule
ip route flush table vpn
ip rule del from $VPN_IP table vpn
ip route flush cache
- For the scripts to work, you need to create a new routing table, called
vpn
, by issuing:
echo 1 vpn >> /etc/iproute2/rt_tables
- You have to do this only once. The scripts implement policy routing. Their purpose is to ensure any packets sent through the VPN tunnel are routed to the appropriate gateway address (i.e. the VPN's default route is correctly set up, but used only by programs that connect directly to the tunnel).
- Bring up the VPN connection by issuing:
openvpn config-file.conf
- Your computer will now have an extra interface you can use directly, called
tun0
. Some programs (qBittorrent for example) will allow you to specify the interface to connect to. Choosing tun0
ensures all of the program's network traffic uses the VPN.
To achieve the OP's requested goal, you can install the socks server called Dante.
- Part of the configuration for Dante is to set the internal and external interfaces.
- For the internal, and depending on from where you wish to connect, use either the loopback interface (i.e.
lo
) or your main network interface (i.e. eth0
).
- For the external, use
tun0
, ensuring all network traffic over socks is routed through the VPN.
Example openvpn.conf
client
dev tun0
remote some-openvpn-server.somewhere.net 53 # Replace this by a real server url
proto udp
nobind
persist-key
persist-tun
tls-auth Wdc.key 1
ca ca.crt
cipher AES-256-CBC
comp-lzo
verb 1
mute 20
float
route-method exe
route-delay 2
route-noexec # Stop the connection taking over the whole machine
up up.sh # Run the script called "up" when the connection comes up
down down.sh # Run the script called "down" when the connection goes down
script-security 2 # Allow scripts to be called
auth-user-pass me # Use the login credentials from the file called "me"
auth-retry interact
explicit-exit-notify 2
ifconfig-nowarn
auth-nocache