Skip to content
codingtube

codingtube

Coding and Programming tutorials

  • javascript
  • React
  • ES6
  • React js
  • coding
  • ffmpeg
  • java
  • programming
  • information
  • coding
  • Privacy Policy
  • Twitter trends
  • Age Calculatore
  • Codingtube Community
  • YouTube Tags Generator
  • About
  • Toggle search form

Make Android VPN App using CakeTube VPN SDK

Posted on December 4, 2021December 4, 2021 By christo No Comments on Make Android VPN App using CakeTube VPN SDK

If you want to make an Android VPN app to publish on Google Play Store then use this simple caketube VPN SDK to make VPN application

Follow these steps to implement Anchorfree CakeTube VPN SDK in Android Studio

Adding SDK to project

  1. Add the JitPack repository to your build file
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
  1. Add the dependency, see badge on top SDK version
    dependencies {
        compile 'com.github.AnchorFreePartner:cake-tube-sdk-android:{VERSION}'
    }

Note: In case of conflicts, use transitive = false feature

Usage and core classes

SDK contains two core classes that must be implemented via Builder.

  1. AFClientService
  2. AFConnectionService

AFClientService

Base class that must be implemented as singleton is AFClientService.
This instance manages client user: authentication, vpn credentials retrieval, user licensing info, session management.
Session will be saved after first successful sign in and destroyed and cleaned up after logout.

Example usage:

    AFClientService api = AFClientService.newBuilder(Application_Context)
        .setCarrierId(YourCarrierId)
        .setConnectionRetries(3)
        .setHostUrl(YourHostUrl)
        .build();

Login may require OAuth Access Token and Authentication Method. This example uses GitHub and Anonymous for demonstration.

    api.login(oauthAccessToken, authMethod, new ResponseCallback<LoginResponse>() {
        @Override
        public void success(LoginResponse loginResponse) {
            // Process Response;
        }

        @Override
        public void failure(ApiException e) {
            // Handle Exception;
        }
    });

Example with anonymous login:

    api.login(null, "anonymous", new ResponseCallback<LoginResponse>() {
        @Override
        public void success(LoginResponse loginResponse) {
            // Process Response;
        }

        @Override
        public void failure(ApiException e) {
            // Handle Exception;
        }
    });

LoginResponse can return different results.
In case it’s “OK”, you can proceed with enabling VPN.

    "OK".equalsIgnoreCase(loginResponse.getResult())

Otherwise, handle Result or Exception and try to login again.

AFConnectionService

This service wraps ServiceConnection which is used to communicate with VPN client service and manage connection.

Service implementation requires these base Activity callbacks for implementation:

    private AFConnectionService connectionService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        connectionService = AFConnectionService.newBuilder(Activity_Context)
            .addConnectionCallbacksListener(new ConnectionCallbacksListener)
            .addVPNConnectionStateListener(new VPNConnectionStateListener)
            .setName(Application_name)
            .build();
        ...
    }

    @Override
    protected void onStart() {
        connectionService.onStart(); // NOTE: this is a mandatory call
        super.onStart();
    }

    @Override
    protected void onStop() {
        connectionService.onStop(); // NOTE: this is a mandatory call
        super.onStop();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // NOTE: service needs to know if VPN permission been granted by user
        connectionService.onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

Note: all these methods are mandatory for proper SDK work.

After this setup u can connect to VPN with connect() call:

    Protocol protocol = Protocol.AUTO;
    void connectVpn() {
        AFClientService clientService = ...;
        clientService.getCredentials(protocol, new ResponseCallback<CredentialsResponse>() {
            @Override
            public void success(CredentialsResponse credentialsResponse) {
                connectionService.connect(credentialsResponse, Activity.this);
            }

            @Override
            public void failure(ApiException e) {
                // Handle Exception
            }
        });
    }

Disconnect from VPN with:

    private void disconnectVPN() {
        connectionService.disconnect();
    }

Change country

Getting server list from AFConnectionService instance:

    api.getServers(new ResponseCallback<ServersResponse>() {
        @Override
        public void success(ServersResponse serversResponse) {
            //Process Response
        }

        @Override
        public void failure(ApiException e) {
            //Handle Exception
        }
    });

ServerResponse contains list of ServerItem. Response must also be checked for “OK” status.
ServerItem contains available information(like Country and Servers count) and is used to change configuration:

    api.setCountry(serverItem);

Note: changing connection configuration requires to restart VPN service.

Check remaining traffic limit

User can check remaining traffic limit if it is set:

    api.getRemainingTraffic(new ResponseCallback<RemainingTrafficResponse>() {
        @Override
        public void success(RemainingTrafficResponse remainingTrafficResponse) {
            Long trafficLimit = remainingTrafficResponse.getTrafficLimit();
        }

        @Override
        public void failure(ApiException e) {
            // Handle Exception
        }
    });

RemainingTrafficResponse contains information about:

  • Traffic start – beggining session time.
  • Traffic limit – limit for traffic usage in bytes.
  • Traffic used – used traffic for subscriber.
  • Traffic remaining – remaining traffic in bytes traffic

OAuth or Anonymous authorization

This example application uses two types of client authorization: with OAuth token and Anonymous.
Usage:

    api.login(oauthAccessToken, authMethod, new ResponseCallback<LoginResponse>() {
        @Override
        public void success(LoginResponse loginResponse) {
            Process Response;
        }

        @Override
        public void failure(ApiException e) {
            Handle Exception;
        }
    });

oauthAccessToken – valid token from OAuth server or null for anonymous.

authMethod – one of the valid authentication methods:

  • “github”, “facebook”, “twitter”, “firebase” – for public authentication servers,
  • “anonymous” – for anonymous authentication,
  • “oauth” – for custom authentication server.

Log out user with:

    api.logout(new ResponseCallback<LogoutResponse>() {
        @Override
        public void success(LogoutResponse logoutResponse) {    
            // Process Response;
        }

        @Override
        public void failure(ApiException e) {
            // Handle Exception;
        }
    });
Full Project
android Tags:Android, VPN App

Post navigation

Previous Post: array_unshift in PHP
Next Post: Operating system

Related Posts

Easy Splash Screen in Android android
How to Filter a RecyclerView with SearchView in Android android
Android active link of url in TextView android
Application I/O Interface in operating system android
Android to create OnItemClickListener interface to handle click events in Recycleview android
How to make VerticalSeekBar in Android android

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Affiliate Marketing Principles
  • The Basics You Need to Know About Affiliate Marketing
  • Affiliate Marketing Options
  • All About Affiliate Marketing
  • Classification of Database Management Systems
  • Three-Tier and n-Tier Architectures
    for Web Applications
  • Two-Tier Client/Server Architectures for DBMSs
  • Basic Client/Server Architectures in DBMS
  • Centralized DBMSs Architecture in DBMS
  • Tools, Application Environments, and Communications Facilities in DBMS

Categories

  • Affiliate marketing (5)
  • Algorithm (43)
  • amp (3)
  • android (223)
  • Android App (8)
  • Android app review (4)
  • android tutorial (60)
  • Artificial intelligence (61)
  • AWS (3)
  • bitcoin (8)
  • blockchain (1)
  • c (5)
  • c language (105)
  • cloud computing (4)
  • coding (4)
  • coding app (4)
  • complex number (1)
  • Computer Graphics (66)
  • data compression (65)
  • data structure (188)
  • DBMS (44)
  • digital marketing (9)
  • distributed systems (11)
  • ffmpeg (26)
  • game (3)
  • html (6)
  • image processing (35)
  • Inequalities (1)
  • information (4)
  • java (212)
  • java network (1)
  • javascript (9)
  • kotlin (4)
  • leetcode (1)
  • math (21)
  • maven (1)
  • mysql (1)
  • Node.js (8)
  • operating system (109)
  • php (310)
  • Principle Of Mathematical Induction (1)
  • programming (6)
  • Python (4)
  • Python data structure (9)
  • React native (1)
  • React.js (22)
  • Redux (1)
  • seo (4)
  • set (12)
  • trigonometry (6)
  • vue.js (35)
  • XML (3)

sitemap

sitemap of videos

sitemap of webstories

sitemap of website

  • Affiliate marketing
  • Algorithm
  • amp
  • android
  • Android App
  • Android app review
  • android tutorial
  • Artificial intelligence
  • AWS
  • bitcoin
  • blockchain
  • c
  • c language
  • cloud computing
  • coding
  • coding app
  • complex number
  • Computer Graphics
  • data compression
  • data structure
  • DBMS
  • digital marketing
  • distributed systems
  • ffmpeg
  • game
  • html
  • image processing
  • Inequalities
  • information
  • java
  • java network
  • javascript
  • kotlin
  • leetcode
  • math
  • maven
  • mysql
  • Node.js
  • operating system
  • php
  • Principle Of Mathematical Induction
  • programming
  • Python
  • Python data structure
  • React native
  • React.js
  • Redux
  • seo
  • set
  • trigonometry
  • vue.js
  • XML
  • Blog
  • Data compression tutorial - codingpoint
  • How to change mbstring in php 5.6
  • How to diagnose out of memory killed PHP-FPM
  • Introduction to jQuery
  • Privacy
  • Affiliate marketing
  • Algorithm
  • amp
  • android
  • Android App
  • Android app review
  • android tutorial
  • Artificial intelligence
  • AWS
  • bitcoin
  • blockchain
  • c
  • c language
  • cloud computing
  • coding
  • coding app
  • complex number
  • Computer Graphics
  • data compression
  • data structure
  • DBMS
  • digital marketing
  • distributed systems
  • ffmpeg
  • game
  • html
  • image processing
  • Inequalities
  • information
  • java
  • java network
  • javascript
  • kotlin
  • leetcode
  • math
  • maven
  • mysql
  • Node.js
  • operating system
  • php
  • Principle Of Mathematical Induction
  • programming
  • Python
  • Python data structure
  • React native
  • React.js
  • Redux
  • seo
  • set
  • trigonometry
  • vue.js
  • XML
  • Blog
  • Data compression tutorial - codingpoint
  • How to change mbstring in php 5.6
  • How to diagnose out of memory killed PHP-FPM
  • Introduction to jQuery
  • Privacy

© codingtube.tech