How can I programmatically connect and disconnect vpn connections by using android's "openvpn connect" app in combination with intents?
Asked Answered
P

1

9

I'm working on an android app that should start an OpenVPN Connect session automatically when needed.

How can I programmatically connect and disconnect vpn connections by using android's "openvpn connect" app in combination with intents ?

Edit: Meanwhile I found this approach - it works for me:

private void startVPN() {
    Intent openVPN = new Intent("android.intent.action.VIEW");
    openVPN.setPackage("net.openvpn.openvpn");
    openVPN.setClassName("net.openvpn.openvpn", net.openvpn.openvpn.OpenVPNClient");
    openVPN.putExtra("net.openvpn.openvpn.AUTOSTART_PROFILE_NAME", "10.10.10.10 [profilename]");
    startActivityForResult(openVPN, 0);
}

This starts the "OpenVPN Connect" app and uses the profilename to do an auto-connect.

If successfull the app goes to background by itself.

Is there even a way to do this completly in background ?
Stopping the VPN-connection does everything in background.

private void stopVPN() {
    Intent openVPN = new Intent("android.intent.action.VIEW");
    openVPN.setPackage("net.openvpn.openvpn");
    openVPN.setClassName("net.openvpn.openvpn", "net.openvpn.openvpn.OpenVPNDisconnect");
    startActivityForResult(openVPN, 0);
}
Perdomo answered 2/3, 2015 at 18:17 Comment(1)
i'm interested in this, wonder where you found this so i could dig more, for example, passing a "*.ovpn" file as profile ...Villenage
D
2

OpenVPN's official Android client can be invoked through AIDL. There's an entire sample app available, with source code. It even has a relatively friendly license.

Dupaix answered 2/3, 2015 at 18:36 Comment(2)
thanks for this info; would like to stick to "OpenVPN Connect" as long as possible. Since there is an iOS version as well and a future app porting should be a little smoother :)Perdomo
"future app porting should be a little smoother" -- I wouldn't expect that at all. They're very different platforms.Dupaix

© 2022 - 2024 — McMap. All rights reserved.