How to programmatically create a new VPN interface with Android 4.0?
Asked Answered
R

2

29

I'd like to know whether it's possible to create a VPN interface programmatically with the new VPN APIs in Android 4.0. I've looked through http://developer.android.com/resources/samples/ToyVpn but this is about creating a complete VPN client that handles the connections etc. I only want to add a new VPN configuration and possibly remove existing ones.

There are lots of questions like this but they all seem to be pre 4.0. I understand that previously this wasn't possible, but one would think that the new VPN APIs provided such functionality. So has anyone found a way to do this, or is it still not possible? If there is a way, please point me to the right direction.

Reprovable answered 19/1, 2012 at 5:29 Comment(0)
L
13

It appears that this is handled internally via the com.android.settings.vpn2.VpnDialog (and related) classes, which basically do KeyStore.getInstance().put("VPN_[VPN ID]", [encoded VpnProfile object]>) -- which then causes files with names like "/data/misc/vpn/1000_VPN_[VPN ID]" to get created (where 1000 is apparently the system process ID).

Using reflection (or one of the hacks to access the hidden / internal APIs) you can do this yourself, but because your process will not be running as the system process, the system VPN browser won't see them.

However, according to this (http://code.google.com/p/android/issues/detail?id=8915), there is at least one app that does this without a rooted phone, but the source doesn't seem to be available...so perhaps there's some way to do this that I can't figure out -- I'd definitely be interested if there is.

P.S. - Here's another similar question (though it's specifically asking about using a rooted device): Create VPN profile on Android

Leonteen answered 21/3, 2012 at 20:16 Comment(2)
I'll accept this answer because it sums up what I found out while testing and investigating the platform source codes pretty well. To access the VPN profiles on filesystem level you need root privileges. For the same reason I don't think a reflection hack would work either. So far all the VPN applications I've found use some local database for storing VPN configurations. If indeed there is an app that works on non rooted phones and uses the device's VPN configurations, I would be very interested.Reprovable
I just updated the answer with a link to the Google code ticket where I read that comment about the app that supposedly does this (code.google.com/p/android/issues/detail?id=8915). I'm still suspicious, but it might be worth looking into since it seems all other paths lead to dead ends.Leonteen
T
-2

Yes, there is a way. Use AndroidVpnService, it helps you create VPN interface on the device, there exist methods to configure that programmatically.

Tenrec answered 7/3, 2016 at 2:8 Comment(1)
What method, please make this a little more complete.Moralez

© 2022 - 2024 — McMap. All rights reserved.