You can do it with network namespaces on GNU/Linux.
Here's how to run OpenVPN and a single application in a separate namespace:
Create the net network namespace:
ip netns add myvpn
Start the loopback interface in the namespace (otherwise many things don't work as expected…)
ip netns exec myvpn ip addr add 127.0.0.1/8 dev lo
ip netns exec myvpn ip link set lo up
Create virtual network interfaces that will let OpenVPN (in the namespace) access the real network, and configure the interface in the namespace (vpn1) to use the interface out of the namespace (vpn0) as its default gateway
ip link add vpn0 type veth peer name vpn1
ip link set vpn0 up
ip link set vpn1 netns myvpn up
ip addr add 10.200.200.1/24 dev vpn0
ip netns exec myvpn ip addr add 10.200.200.2/24 dev vpn1
ip netns exec myvpn ip route add default via 10.200.200.1 dev vpn1
Enable IPv4 routing and NAT for the interface in the namespace. As my default interface is a wireless one, I use wl+ (which may match wlan0, wlp3s0, etc.) in iptables for the outgoing interface; if you use a wired interface you should probably use en+ (or br+ for a bridged interface)
iptables -A INPUT \! -i vpn0 -s 10.200.200.0/24 -j DROP
iptables -t nat -A POSTROUTING -s 10.200.200.0/24 -o wl+ -j MASQUERADE
sysctl -q net.ipv4.ip_forward=1
Configure the nameserver to use inside the namespace
mkdir -p /etc/netns/myvpn
echo 'nameserver 8.8.8.8' > /etc/netns/myvpn/resolv.conf
Almost done, now we should have full network access in the namespace
ip netns exec myvpn ping www.google.com
Finally start OpenVPN in the namespace
ip netns exec myvpn openvpn --config /etc/openvpn/myvpn.conf
Once tun0 is up in the namespace, you're ready to start the program you wanted!
while ! ip netns exec myvpn ip a show dev tun0 up; do sleep .5; done
ip netns exec myvpn sudo -u $MYSELF popcorntime
SOURCE article.
Also there is a wrapper script in the source article you can adapt for your needs.
3
related: Disable VPN for a given process/application
– slhck – 2011-06-07T12:54:09.247