如何使用 Android 4.0 以编程方式创建新的 VPN 接口?
我想知道是否可以使用 Android 4.0 中的新 VPN API 以编程方式创建 VPN 接口。我浏览过 http://developer.android.com/resources/samples/ToyVpn 但这是关于创建一个完整的 VPN 客户端来处理连接等。我只想添加新的 VPN 配置并可能删除现有的配置。
像这样的问题有很多,但似乎都是 4.0 之前的问题。我知道以前这是不可能的,但人们会认为新的 VPN API 提供了这样的功能。那么有没有人找到一种方法来做到这一点,或者仍然不可能?如果有办法,请指出我正确的方向。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来这是通过 com.android.settings.vpn2.VpnDialog (及相关)类在内部处理的,这些类基本上执行
KeyStore.getInstance().put("VPN_[VPN ID]", [encoded VpnProfile object ]>)
——然后会创建名称类似于“/data/misc/vpn/1000_VPN_[VPN ID]”的文件(其中 1000 是显然是系统进程ID)。使用反射(或访问隐藏/内部 API 的黑客之一),您可以自己执行此操作,但由于您的进程不会作为系统进程运行,因此系统 VPN 浏览器不会看到它们。
但是,根据此(http://code.google.com/ p/android/issues/detail?id=8915),至少有一个应用程序可以在没有 root 的手机的情况下执行此操作,但源似乎不可用......所以也许有某种方法可以做我做不到的事弄清楚——如果有的话我肯定会感兴趣。
PS - 这是另一个类似的问题(尽管它专门询问如何使用 root 设备):在 Android 上创建 VPN 配置文件
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
是的,有办法。使用 AndroidVpnService,它可以帮助您在设备上创建 VPN 接口,存在以编程方式配置该接口的方法。
Yes, there is a way. Use AndroidVpnService, it helps you create VPN interface on the device, there exist methods to configure that programmatically.