CoreAPI

Core methods that used for initialize tunnel

Login

Logins to the backend for fetch AccessToken, for instance.

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
if (loginResponse.Result.Equals(ResponseResult.Ok))
{
    Console.WriteLine(loginResponse.AccessToken);
}

Logout

Logouts from backend.

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var logoutResponse = await sdk.LogoutAsync(loginResponse.AccessToken).ConfigureAwait(false);
Console.WriteLine(logoutResponse.Result);

GetUserInfo

Gets user info, like Name, UserId, DevicesLimit etc.

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var userInfoResponse = await sdk.GetUserInfoAsync(loginResponse.AccessToken).ConfigureAwait(false);
if (userInfoResponse.Result.Equals(ResponseResult.Ok))
{
    Console.WriteLine(userInfoResponse.Name);
}

GetRemainingTraffic

This method allows users to retrieve their remaining traffic balance.

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var getRemainingTrafficResponse = await sdk.GetRemainingTrafficAsync(loginResponse.AccessToken).ConfigureAwait(false);
if (getRemainingTrafficResponse.Result.Equals(ResponseResult.Ok))
{
    Console.WriteLine(getRemainingTrafficResponse.TrafficRemaining);
}

GetRemoteConfig

Gets client configuration from backend (fireshield configuration for instance).

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var carrier = new Carrier(loginResponse);
var getRemoteConfigRequest = new GetRemoteConfigRequest
{
    Carrier = carrier,
    DeviceId = DEVICEID,
};

var getRemoteConfigResponse = await sdk.GetRemoteConfigAsync(getRemoteConfigRequest).ConfigureAwait(false);
if (getRemoteConfigResponse.Result.Equals(ResponseResult.Ok))
{
    Console.WriteLine(getRemoteConfigResponse.PatchData);
}

GetNodes and GetLocations

GetNodes

The GetNodes function is primarily used to retrieve a list of VPN countries.

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var carrier = new Carrier(loginResponse);
var getNodesRequest = new GetNodesRequest
{
    Carrier = carrier,
    Protocol = Core.Model.Enums.Protocol.HydraTcp,
};

var getNodesResponse = await sdk.GetNodesAsync(getNodesRequest).ConfigureAwait(false);

An example response looks as follows:

{
  "Message": "OK",
  "Metadata": {...},
  "Result": "Ok",
  "VpnCountries": [
    {
      "Carrier": {...},
      "CountryCode": "de",
      "IsAutomatic": false,
      "ServerRepresentation": "de",
      "Servers": 4,
     },
     ...   
    ],
  "VpnPrivateGroups": [],
}

GetLocations

The GetLocations function is recommended for multi-hop, custom DNS, and VPN profiles functionalities.

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var carrier = new Carrier(loginResponse);
var getLocationsRequest = new GetLocationsRequest
{
    Carrier = carrier,
    Protocol = Core.Model.Enums.Protocol.HydraTcp,
};

var getLocationsResponse = await sdk.GetLocationsAsync(getLocationsRequest).ConfigureAwait(false);

An example response looks as follows:

{
  "DnsServers": [...],
  "Locations": [
    {
      "Description": "Test location 1, includes all servers"
      "Labels": {
        "City": "",
        "Country": "",
        "Subdivision": "",
      },
      "Name": "test-1",
      "Status": "OK",
    },
    ...
  ],
  "Message": "OK",
  "Metadata": {...},
  "Profiles": [],
  "Result": "Ok",
  "VpnCustomLocations": [
    {
      Carrier: {...}
      IsAutomatic: false,
      Location: "test-1",
      ProxyLocation: null,
      ProxyOptimal: false,
      ServerRepresentation: "test-1",
    },
    ...
  ],
}

Connecting to Specific Locations using GetLocations

To connect to a specific location, you can use either the VpnCustomLocations property of the GetLocationsResponse. Here's how you can do it:

var selectedCustomLocation = getLocationsResponse.VpnCustomLocations[0];
  • The Locations[0] element points to the same location as VpnCustomLocations[0]. The Locations is useful for finding a location to display based on the connection's location index.

  • Only the VpnCustomLocations is used for establishing the connection. The Locations can be useful for displaying the connection location in the user interface.

Searching for a Specific Location

If you want to connect to a location based on its name, you can search through the VpnCustomLocations list:

var locationName = "YOUR_SELECTED_LOCATION";
var selectedLocation = getLocationsResponse.VpnCustomLocations
    .FirstOrDefault(loc => loc.Location == locationName);

if (selectedLocation != null)
{
    // Do something
}
else
{
    Console.WriteLine("Location not found");
}

Choosing Between GetNodes and GetLocations

While GetNodes provides a broader list of countries, GetLocations offers more detailed information that can be useful for advanced features. Choose the appropriate method based on your specific requirements and the level of detail you need to present to your users.

Function
When To Use

GetNodes

  • You only need country-level information

  • You want to display a list of countries in your UI

  • You need a broader range of connection options

GetLocations

  • You need more detailed location information (potentially including cities)

  • You're implementing multi-hop, custom DNS, or VPN profiles features

  • You want to display both country and city information in your UI

GetCredentials

Gets credentials for VPN tunnel, including HydraCertificate, OpenVpnCertificate, UserName and UserPassword etc.

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var carrier = new Carrier(loginResponse);
var getNodesRequest = new GetNodesRequest
{
    Carrier = carrier,
    Protocol = Core.Model.Enums.Protocol.HydraTcp,
};

var getNodesResponse = await sdk.GetNodesAsync(getNodesRequest).ConfigureAwait(false);

var getCredentialsRequest = new GetCredentialsRequest
{
    VpnNode = getNodesResponse.VpnCountries.FirstOrDefault(),
    WithSertificate = true,
    Protocol = Core.Model.Enums.Protocol.HydraTcp,
    AppVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString(),
};

var getCredentialsResponse = await sdk.GetCredentialsAsync(getCredentialsRequest).ConfigureAwait(false);
if (getCredentialsResponse.Result.Equals(ResponseResult.Ok))
{
    Console.WriteLine(getCredentialsResponse.Credentials);
}

StartVpn

All protocols work on the Wintun adapter, which is created anew each time.

var sdk = new SDK();
var loginRequest = new LoginRequest
{
    CarrierId = CARRIERID,
    DeviceId = DEVICEID,
    DeviceName = Environment.MachineName,
    Method = AuthenticationMethod.Anonymous,
    Token = null,
};

var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var carrier = new Carrier(loginResponse);
var getNodesRequest = new GetNodesRequest
{
    Carrier = carrier,
    Protocol = Core.Model.Enums.Protocol.HydraTcp,
};

var getNodesResponse = await sdk.GetNodesAsync(getNodesRequest).ConfigureAwait(false);

var getCredentialsRequest = new GetCredentialsRequest
{
    VpnNode = getNodesResponse.VpnCountries.FirstOrDefault(),
    WithSertificate = true,
    Protocol = Core.Model.Enums.Protocol.HydraTcp,
    AppVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString(),
};

var getCredentialsResponse = await sdk.GetCredentialsAsync(getCredentialsRequest).ConfigureAwait(false);

var startVpnRequest = new StartVpnRequest
{
    Credentials = getCredentialsResponse.Credentials,
    DeviceId = DEVICEID,
    EnableKillSwitch = false,
    EnableTunnelLogging = false,
    VpnNode = getNodesResponse.VpnCountries.FirstOrDefault(),
    WaitConnected = true,    
};

var startVpnResponse = await sdk.StartVpnAsync(startVpnRequest).ConfigureAwait(false);
if (startVpnResponse.Result == ResponseResult.Ok)
{
    Console.WriteLine(startVpnResponse.Message);
}

You have the ability to customize and apply the configuration template for the hydra protocol. To learn about the specific customization options available and to obtain a customized configuration please contact your sales representative.

Once you receive the configuration template you need to use the parameter HydraConfigTemplatein the StartVpnRequest.

With "StartVpnRequest" you can send "WaitConnected" flag which means:

True - "StartVpn" method will wait until the tunnel will be connected before sending the response. This value is for backward compatibility with previous versions.

Null/False (default) - "StartVpn" method will send response after connection will be initiated and later it can be canceled by "StopVpn" request. The connection status or error will be received through the 'StateChanged' event.

GetConnectionState

Gets current VPN connection state based on the VPN connection state stored in Unified SDK service.

var sdk = new SDK();
var stateResponse = await sdk.GetConnectionStateAsync().ConfigureAwait(false);
if (stateResponse.Result.Equals(ResponseResult.Ok))
{
    Console.WriteLine(stateResponse.OperationalState);
}

The VPN connection state represents an Enum:

  • TunnelIdle - either doing nothing or trying to reconnect over and over again with no luck.

  • TunnelConnecting - establishing the connection.

  • TunnelConnected - connected to one or more servers and ready to process diverted network traffic.

  • TunnelDisconnected - disconnected from all the connected servers (if any) and not diverting network traffic.

GetConnectedServerInfo

Returns the connected server information. It will be one of the servers received from GetCredentials response. Tunnel should be in the TunnelConnected state, otherwise it will return an error.

var sdk = new SDK();
var serverResponse = await sdk.GetConnectedServerInfoAsync().ConfigureAwait(false);
if (serverResponse.Result.Equals(ResponseResult.Ok))
{
    Console.Write(serverResponse.IPv4);
    Console.Write('-');
    Console.Write(serverResponse.Country);
    Console.Write('-');
    Console.WriteLine(serverResponse.Name);
}

StopVpn

Stops VPN.

var sdk = new SDK();
var stopVpnRequest = new StopVpnRequest
{
    KeepKillSwitchEnabled = false,
};
var stopVpnResponse = await sdk.StopVpnAsync(stopVpnRequest).ConfigureAwait(false);
Console.WriteLine(stopVpnResponse.ExitCode);

Last updated