Search…
Manage Connection
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

Property
Description
server
The SudoVPNServer that the client will/is connected to.
protocolType
The SudoVPNProtocol that the client will/is using to connect to the VPN.
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).
Protocol
Description
iOS
Android
L2TP
β€‹βŒ ​
β€‹βŒ ​
PPTP
β€‹βŒ ​
β€‹βŒ ​
IPSec
β€‹βœ…β€‹
β€‹βŒ ​
IPSec (IKEv2)
β€‹βœ…β€‹
β€‹βœ…β€‹
OpenVPN UDP
​Open VPN over UDP​
β€‹βŒβ€‹
β€‹βœ…β€‹
OpenVPN TCP
​Open VPN over TCP​
β€‹βŒβ€‹
β€‹βœ…β€‹
WireGuard
​Wire Guard​
β€‹βŒβ€‹
β€‹βœ…β€‹
iOS
Android
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:
1
let supportedProtocols = vpnClient.supportedProtocols()
2
/// `supportedProtocols` contains an array of the protocols that can be used.
Copied!
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:
1
val supportedProtocols = vpnClient.listSupportedProtocols()
2
// [supportedProtocols] contains a list of protocols that can be used to configure a connection.
Copied!

Connect to the VPN

iOS
Android
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:
1
vpnClient.connect { result in
2
switch result {
3
case let .failure(cause):
4
/// Handle/notify user of error.
5
case .success:
6
/// Successfully begun connection.
7
}
Copied!
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:
1
class ConnectionFragment : Fragment(), CoroutineScope {
2
​
3
companion object {
4
private const val VPN_PREPARE = 1000
5
private const val NOTIFICATION_ID_VPN_STATUS = 1
6
private const val NOTIFICATION_ID_VPN_REVOKED = 2
7
}
8
​
9
// val server: SudoVPNServer // Retrieved via the list servers API
10
private fun connect() {
11
try {
12
if (sudoVPNClient.isPrepared()) {
13
​
14
// Set the configuration client property with selected [server].
15
sudoVPNClient.configuration = SudoVPNConfiguration(server, SudoVPNProtocol.IKEV2, false)
16
​
17
// Build SudoVPNNotification objects.
18
val vpnConnectionNotification = NotificationCompat.Builder(
19
requireContext(),
20
"vpnNotificationChannel"
21
)
22
.setSmallIcon(R.drawable.vpn_logo)
23
.build()
24
val vpnRevokedNotification = NotificationCompat.Builder(
25
requireContext(),
26
"vpnNotificationChannel"
27
)
28
.setSmallIcon(R.drawable.vpn_logo)
29
.build()
30
val sudoNotification = SudoVPNNotification(NOTIFICATION_ID_VPN_STATUS, vpnConnectionNotification)
31
val sudoRevokedNotification = SudoVPNNotification(NOTIFICATION_ID_VPN_REVOKED, vpnRevokedNotification)
32
​
33
launch {
34
try {
35
withContext(Dispatchers.IO) {
36
sudoVPNClient.connect(sudoNotification, sudoRevokedNotification)
37
}
38
} catch (e: SudoVPNException) {
39
// Handle/notify user of exception
40
}
41
}
42
} else {
43
sudoVPNClient.prepare(this)
44
}
45
} catch (e: ActivityNotFoundException) {
46
// Handle/notify user if prepare method could not be executed.
47
}
48
}
49
​
50
// Handle result from calling [sudoVPNClient.prepare] method.
51
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
52
super.onActivityResult(requestCode, resultCode, data)
53
if (resultCode == RESULT_OK && requestCode == VPN_PREPARE) {
54
connect()
55
}
56
}
57
}
Copied!
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.
iOS
Android
To disconnect from an already established connection, use the disconnect(completion:) method.
An example implementation of calling the disconnect method is:
1
vpnClient.disconnect { result in
2
switch result {
3
case let .failure(cause):
4
/// Handle/notify user of error.
5
case .success:
6
/// Successfully disconnected.
7
}
Copied!
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:
1
launch {
2
try {
3
withContext(Dispatchers.IO) {
4
sudoVPNClient.disconnect()
5
}
6
// The disconnection process has begun.
7
} catch (e: SudoVPNException) {
8
// Handle/notify user of exception
9
}
10
}
Copied!
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.
Last modified 6mo ago