VPN Interface

The VPN Interface allows you to configure and control VPN sessions using the UnifiedSDK class. This document provides an overview of the available methods and classes for managing VPN sessions.

SessionConfig

The SessionConfig class is used to configure VPN sessions. It provides the following Builder methods:

  • withTransport(transportName) - Set the transport name to start the session with. Useful when multiple transports are available.

  • withPolicy(appPolicy) - Define the app policy to use. Available options:

    • AppPolicy.forAll() - VPN will be available for all apps.

    • AppPolicy.newBuilder().policy(AppPolicy.POLICY_FOR_LIST) - Specify a list of apps allowed to use VPN.

    • AppPolicy.newBuilder().policy(AppPolicy.POLICY_EXCEPT_LIST) - Specify a list of apps not allowed to use VPN.

  • withReason(reason) - Define the reason for starting the session using unified.vpn.sdk.TrackingConstants.GprReasons. Used for analytics.

  • withLocation(location) - Define the virtual location for the session.

  • withCountry(country) - Define the virtual country for the session.

  • withPrivateGroup(group) - Define a private server for the session.

  • withVpnParams(params) - Define VPN tunnel parameters, such as DNS servers and additional routes.

  • addDnsRule(rule) - Add a DNS rule for the session. More on Traffic rules page. For Hydra transport only.

  • addProxyRule(rule) - Add a proxy rule for the session. More on Traffic rules page. For Hydra transport only.

  • withFireshieldConfig(config) - Define categorization service rules. For Hydra transport only

  • withTransportFallback(fallbackOrder) - Define the order of transports to fallback when an error occurs.

  • keepVpnOnReconnect() - Enable the kill switch to prevent traffic while the SDK is in the connection process or handling connection errors.

Examples

Start VPN with an optimal server

Start VPN for chrome only

Analytics Reasons

The following constants are used to specify the reason for starting the VPN session:

  • M_UI - Manually started from the user interface

  • M_SYSTEM - Manually started from the system

  • M_OTHER - Manually started from another place

  • A_APP_RUN - Automatically started on app run

  • A_RECONNECT - Automatically started on reconnect

  • A_ERROR - Automatically started after an error

  • A_SLEEP - Automatically started after sleep

  • A_NETWORK - Automatically started on a network event

  • A_OTHER - Automatically started for other reasons

These constants are defined in the TrackingConstants.GprReasons enum and can be used with the withReason method when building the SessionConfig.

Starting a VPN session

To start a VPN session:

  1. Obtain an instance of the UnifiedSdk class by calling the getInstance() method.

  2. Create a SessionConfig object using the SessionConfig.Builder class. You can customize the session configuration by setting various options in the builder.

  3. Start the VPN session by calling the start() method on the getVpn() instance of the UnifiedSdk.

  • The complete() method will be called when the VPN session starts successfully. You can add your logic to handle the successful start of the VPN here.

  • The error() method will be called if an error occurs during the VPN start process. You can handle the error case and take appropriate actions based on the VpnException received.

Make sure you have the necessary permissions and configurations in your Android manifest file for the VPN functionality to work properly.

Stopping a VPN Session

To stop a VPN session:

Restarting a VPN Session

To restart a VPN session with a new configuration without entering the IDLE state:

Updating Session Configuration

You can update certain VPN session configurations without restarting the VPN session. Currently, the SDK supports updating the following for the Hydra transport:

  • Fireshield rules

  • DNS Rules

  • Proxy Rules

Example:

Listening for VPN Status and Traffic Updates

To listen for VPN status and traffic updates:

Getting Current VPN State

To get the current VPN state:

VPN States

  • IDLE - No active VPN session.

  • CONNECTING_PERMISSIONS - Requesting system VPN permission.

  • CONNECTING_CREDENTIALS - Loading backend information to establish a connection.

  • CONNECTING_VPN - Transport connection in progress.

  • DISCONNECTING - Disconnection started.

  • CONNECTED - VPN session is active and connected.

  • PAUSED - VPN session paused due to an error, network change, or waiting to restore the VPN session.

  • UNKNOWN - Not used, reserved.

  • ERROR - Not used, reserved.

Checking VPN Permissions

To check if the app has system VPN permissions granted:

To request VPN permissions without connecting to the VPN:

Getting Current VPN Session Information

To get the current VPN session information:

Last updated

Was this helpful?