Provides the essentials to allow your users to connect and disconnect from a server and view their connection status.
The VPN SDK provides a set of APIs to allow your users to establish and teardown a VPN connection with a server.
Establish a Connection
A connection to a server is established by calling the connect to server API using a server selected from the list servers API call.
In order to establish a connection, a SudoVPNServer is required.
Connection Configuration
Connection configuration is decided at the time of calling the connect API. It is up to you to build up a SudoVPNConfiguration object.
SudoVPNConfiguration
When the disconnect API is called, the configured server will be set back to undefined.
Both server and protocolType can be left as undefined. In the case that server is left undefined, the best server will be attempted to connect to, using the user's current geographical location. If protocolType is left undefined, the default protocol (IPSec (IKEv2) on mobile) will be used.
Protocol Types
When connecting to a VPN, a couple of different Protocol types are available. Depending on the platform, only a subset of these types may be supported.
In a scenario where network is lost whilst connected to an IPSec (IKEv2) or UDP protocol, the VPN tunnel will remain open. This allows the connection to stay open if there are changes in the network (i.e. moving from Wi-Fi to Mobile Data).
In order to see which protocols are currently available via the client, use the supportedProtocols() method. This method returns the list of supported protocols mentioned above currently available to the device calling the method.
An example implementation is:
let supportedProtocols = vpnClient.supportedProtocols()/// `supportedProtocols` contains an array of the protocols that can be used.
The listSupportedProtocols method is used to return a list of of supported protocols used as part of configuring a connection that are currently available to the client calling the method.
An example implementation of this is:
val supportedProtocols = vpnClient.listSupportedProtocols()// [supportedProtocols] contains a list of protocols that can be used to configure a connection.
Connect to the VPN
To connect to the VPN, use the connect(withConfiguration:completion:) method. The completion will be called either when the connection process has successfully begun, or an error occurred while attempting to do so.
If configuration is nil, the best server, and default protocol type will be used.
An example implementation of calling the connect method is:
vpnClient.connect { result inswitch result {caselet .failure(cause):/// Handle/notify user of error.case .success:/// Successfully begun connection. }
To watch for connection state events to see when the client has successfully connected (or failed to connect), see Connection Events.
The connect(notification, revokedNotification) method is used to establish a connection to the VPN network. The notification parameter allows you to attach a persistent notification once a connection is established whereas the revokedNotification parameter allows you to emit a notification when the VPN is revoked.
Before establishing a connection, the configurationclient property must be set. If configuration is null, the best server, and default protocol type will be used.
Android requires that the application request permission from the user the first time that the application attempts to create a connection with a VPN network.
Before attempting to connect to a VPN network. The prepare(fragment: Fragment) / prepare(activity: Activity) method must be called. This method will launch an intent containing a VPN connection permission dialogue requiring user action with the result coming back via the fragment or activity onActivityForResult method which can be overridden. If the result is RESULT_OK, the application is considered prepared. The isPrepared method is used to indicate whether the user has previously given permission to connect to VPN networks.
The execution of the prepare method is required in order to perform any VPN connection operations.
An example implementation of connecting to a selected server is:
classConnectionFragment : Fragment(), CoroutineScope {companionobject {privateconstval VPN_PREPARE =1000privateconstval NOTIFICATION_ID_VPN_STATUS =1privateconstval NOTIFICATION_ID_VPN_REVOKED =2 }// val server: SudoVPNServer // Retrieved via the list servers APIprivatefunconnect() {try {if (sudoVPNClient.isPrepared()) {// Set the configuration client property with selected [server]. sudoVPNClient.configuration =SudoVPNConfiguration(server, SudoVPNProtocol.IKEV2, false)// Build SudoVPNNotification objects.val vpnConnectionNotification = NotificationCompat.Builder(requireContext(),"vpnNotificationChannel" ) .setSmallIcon(R.drawable.vpn_logo) .build()val vpnRevokedNotification = NotificationCompat.Builder(requireContext(),"vpnNotificationChannel" ) .setSmallIcon(R.drawable.vpn_logo) .build()val sudoNotification =SudoVPNNotification(NOTIFICATION_ID_VPN_STATUS, vpnConnectionNotification)val sudoRevokedNotification =SudoVPNNotification(NOTIFICATION_ID_VPN_REVOKED, vpnRevokedNotification)launch {try {withContext(Dispatchers.IO) { sudoVPNClient.connect(sudoNotification, sudoRevokedNotification) } } catch (e: SudoVPNException) {// Handle/notify user of exception } } } else { sudoVPNClient.prepare(this) } } catch (e: ActivityNotFoundException) {// Handle/notify user if prepare method could not be executed. } }// Handle result from calling [sudoVPNClient.prepare] method.overridefunonActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {super.onActivityResult(requestCode, resultCode, data)if (resultCode == RESULT_OK && requestCode == VPN_PREPARE) {connect() } }}
To watch for connection state events to see when the client has successfully connected (or failed to connect), see Setting up a Subscription in the Android section of the Observe VPN Related Events page.
Disconnect from an Established Connection
Once already connected, a user may want to deliberately disconnect, or close the connection as part of a process.
To disconnect from an already established connection, use the disconnect(completion:) method.
An example implementation of calling the disconnect method is:
vpnClient.disconnect { result inswitch result {caselet .failure(cause):/// Handle/notify user of error.case .success:/// Successfully disconnected. }
To watch for connection state events to see when the client has successfully disconnected (or failed to disconnect), see Connection Events.
Disconnecting from an already established connection is performed by calling the disconnect method.
An example implementation of performing a disconnection is:
launch {try {withContext(Dispatchers.IO) { sudoVPNClient.disconnect() }// The disconnection process has begun. } catch (e: SudoVPNException) {// Handle/notify user of exception } }
To watch for connection state events to see when the client has successfully disconnected (or failed to disconnect), see Setting up a Subscription in the Android section of the Observe VPN Related Events page.