33

I have access to a VPN and want to use it only for some applications, not all.

For example:
If I connect to a VPN I want only the applications Opera and Newsbin to use that VPN connection. All other applications, like f.e. Outlook, should use the normal internet connection (while the VPN is connected/open).

Afaik this isn't possible, but I'm not totally sure. So therefore my question: is it doable?

-1

No it isn't. Well not using normal means. Routing is routing. Routing operates lower down the OSI model. What you need to happen is for it to be aware at the applicaion level (higher up) so it can detect what program you are using etc. NORMAL routers do not allow you to configure routes based on applications or port numbers.

I think it is possible though. Routers can see ports so you can filter them by port number and send the traffic out different routes. i am sure i read something a while ago with Cisco routers that can do this. They don't come cheap though and they are business routers which require a lot of config and I'm guessing you want something simple for home use.

In summary it is the router that would need this feature and your bog standard ones for home use, even business don't support these features. Only the advanced line of Cisco and Sonicwalls offer these capabilities.

33

It is possible to accomplish this, at least on Linux (and I'm thinking on BSD and OS X as well). You can do so by:

  • Create an exra user for all VPN traffic.
  • Create an extra routing table with 1 default route via the VPN.
  • Configure Netfilter through Iptables to use the other routing table for all traffic originating from a specific User ID.
  • Run the applications that should use the VPN under their own user. For example with 'sudo'.

There are scripts for accomplishing the above steps here or there is another guide here.

Here is a detailed guide for routing Transmission via a VPN (using a VPN server that you own.

14

You could use the Windows Firewall to accomplish this (provided you are using Win 7 or Vista) - I wrote a guide on this

  1. Connect to your VPN as you normally would.

  2. Open the Network and Sharing Center - right-click on the Internet connection icon in the taskbar and choose "Open Network and Sharing Center" (see below)

  3. You should see (at least) two networks listed under "View Your Active Networks" - your VPN connection and one called "Network" - a.k.a. your ISP Connection. Ensure that your VPN is a "Public Network", and your ISP connection is "Home Network". If you need to change either connection, click it and an option window will appear (see below).

  4. Go to the Control Panel and click System and Security (see below).

  5. In the resulting window, click Windows Firewall (see below).

  6. In the Windows Firewall window, click Advanced Settings on the left pane (see below). Note: You must be logged in as an Adminstrator to make changes to the Firewall Settings.

  7. You should see a window titled Windows Firewall with Advanced Security. In this window, click Inbound Rules (see below).

  8. On the right pane, you will see an option for a New Rule. Click it (see below).

  9. In the New Inbound Rule Wizard (which should appear), do the following:

    • Choose Program and click Next.

    • Choose the program you wish to block all traffic to except on the VPN connection, and click next.

    • Choose Block the Connection.

    • Tick Domain and Private. Make sure Public is left unticked.

  10. Repeat Step 9 for Outbound Rules.

  • 10
    Does this actually answer the question? Yes, this stops the app from working if it isn't on the VPN, but all traffic still goes over the VPN does it not? – Jason Coyne Apr 20 '15 at 20:33
  • 3
    @pramble So does all traffic still go through the vpn? – nsij22 May 8 '15 at 21:47
  • Appears to work. Just tested it.. – Faiz Dec 11 '17 at 7:08
  • This doesn't fix it. This is a very simple scenario. I want all my work-related stuff to go over the VPN (SQL Server connections, Slack, etc.), but I want my web browser (Facebook, Soundcloud, etc., basically all port 80 traffic) to NOT go over the VPN. Windows sucks if this isn't possible. – Triynko Jan 8 '18 at 19:09
1

I've done this on Windows. The idea is to bind the outgoing network packages to VPN's interface. People suggest ForceBindIP for this, but thanks to this answer I've got an idea to use proxy. The downside of this method is that either your apps have to have proxy support or you'll have to use a proxifier (see here and here). The upside is that this way you'll be able to limit the use of VPN in the browser to specific domains using FoxyProxy or similar add-ons.

I use 3proxy in SOCKS mode and bind its external interface to VPN's IP. OpenVPN is used for the VPN connection.

In my .ovpn file (client, dev tun) I've added these lines:

route-nopull
route 0.0.0.0 0.0.0.0 vpn_gateway
pull-filter ignore "dhcp-option DNS "
script-security 2
up 'c:\path\to\up.cmd'
down 'c:\path\to\down.cmd'

route-nopull to ignore routes pushed from the server. In your case you might need to comment out redirect-gateway instead.

route to add a route for this interface, without this line it won't be used even if the app is bound to it.

pull-filter to preserve pushed DNS that otherwise will be dropped by route-nopull together with the pushed routes. This option is supported starting with OpenVPN 2.4, if you have to stick with OpenVPN 2.3 (latest release for Windows XP), you'll have to add two dhcp-option DNS x.x.x.x lines with hardcoded IPs instead.

script-security 2 to allow scripting.

up script:

cd %~dp0
echo auth none> 3proxy-openvpn.conf
echo internal 127.0.0.1>> 3proxy-openvpn.conf
echo external %4>> 3proxy-openvpn.conf
echo socks>> 3proxy-openvpn.conf
start /b 3proxy.exe 3proxy-openvpn.conf

down script:

taskkill /f /im 3proxy.exe

Thus, after you connect to VPN using this config, 3proxy.exe process will be started and a localhost-limited SOCKS5 proxy with DNS resolution capability will run on the 1080 port, now just configure your app to use localhost:1080 SOCKS proxy.

  • I am getting Options error: option 'route' cannot be used in this context ([PUSH-OPTIONS]) and Options error: option 'redirect-gateway' cannot be used in this context ([PUSH-OPTIONS]). None the less, this solution works. If you skip the apostrophes for down and up script, use double backslashes: c:\\path\\to\\script – chx Jan 16 '18 at 14:42
1

If you're on linux, and you use openVPN, VPNShift works beautifully.

  • Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places. – DavidPostill Jul 12 '18 at 17:20
  • 4
    Is the fact that it's "the same answer to multiple questions" so important? The question exists, it's not (yet) flagged as duplicate, and it's a good answer, so it deserves to exist. Thanks @thouliha 🙂. – Ronan Jouchet Oct 19 '18 at 12:34
0

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.

0

Just access the VPN through a virtual machine.

  1. Create a VM, then from inside the VM...
  2. Install the 'selected' applications
  3. Configure the VPN

Use the 'selected' applications from the VM instead of using them from the host machine.

P.S. You need to give network access to the VM through the host machine, of course.

  • This just mentions an approach already described in detail in other answers. – fixer1234 Jan 16 '18 at 3:46
  • 1
    @fixer1234: Nobody mentioned virtual machines. – drowa Jan 16 '18 at 3:49
  • Can you expand your answer into something more actionable (explain the how)? See the other answers on the thread for an idea of the level of detail. Just providing a hint for a direction to pursue would go in a comment, which requires a little more rep. – fixer1234 Jan 16 '18 at 4:05
  • I've tried a comment first but the system didn't allow me because I have less than 50 points. I will try to expand the answer then. – drowa Jan 16 '18 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy