Make your own Android VPN client with OpenVPN servers

Tanuj Singh Kushwah
3 min readMay 19, 2023

--

This article aims to provide steps to build your own end-to-end Android VPN client solution using OpenVPN servers. The client would be built on top of the OpenVPN open-source repository and servers would be deployed using OpenVPN AWS Machine Image.

Ether VPN Android App

Reference project Ether VPN built using OpenVPN module: https://github.com/tj4752/ether-vpn

Available on Play Store now➡️: https://play.google.com/store/apps/details?id=com.anonymous.ethervpn

Link for OpenVPN Server AWS EC2 free tier machine image: https://aws.amazon.com/marketplace/pp/prodview-y3m73u6jd5srk

Follow the steps given here to complete the setup of the OpenVPN server: https://openvpn.net/vpn-server-resources/amazon-web-services-ec2-tiered-appliance-quick-start-guide/

Congratulations, you have successfully set up your own VPN server. Now, let’s build the client app.

  1. Clone the ics-openvpn Android VPN client module git repo on your local machine.
  2. Create a blank project in Android Studio and import ics-openvpn repo as a module in your project.
Importing repo as a module in Android Studio

3. Download swig from https://www.swig.org/download.html and add its system variable path to Android Studio. Change the plugin id(“com.android.application”) to id(“com.android.library”) in OpenVPN module gradle file and then gradle sync the project.

4. Add the below permission to Android Manifest:

<uses-permission android:name="android.permission.INTERNET"/>

Add service components for VPN connect/disconnect service in Manifest:

<service
android:name="de.blinkt.openvpn.core.OpenVPNService"
android:permission="android.permission.BIND_VPN_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.net.VpnService" />
</intent-filter>
</service>

<activity
android:name="de.blinkt.openvpn.activities.DisconnectVPN"
android:excludeFromRecents="true"
android:noHistory="true"
android:taskAffinity=".DisconnectVPN"
android:theme="@style/blinkt.dialog" />

5. Create an assets folder in src/main and copy server ovpn configuration files. You can use your own custom ovpn server configs or download it from free sources such as vpngate/freeopenvpn. Then, add server adapter/model and interfaces in the main project.

ovpn assets directory structure

6. Create a fragment class in your main project and bind it to an activity. To start the VPN service in fragment class, use the below code:

protected IOpenVPNAPIService mService = null;

private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
// This is called when the connection with the service has been
// established, giving us the service object we can use to
// interact with the service. We are communicating with our
// service through an IDL interface, so get a client-side
// representation of that from the raw service object.

mService = IOpenVPNAPIService.Stub.asInterface(service);

try {
// Request permission to use the API
Intent i = mService.prepare(getActivity().getPackageName());
if (i!=null) {
startActivityForResult(i, ICS_OPENVPN_PERMISSION);
} else {
onActivityResult(ICS_OPENVPN_PERMISSION, Activity.RESULT_OK,null);
}

} catch (RemoteException e) {
logger.log("openvpn service connection failed: " + e.getMessage());
e.printStackTrace();
}
}

public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mService = null;

}
};

private void bindService() {

Intent icsopenvpnService = new Intent(IOpenVPNAPIService.class.getName());
icsopenvpnService.setPackage("your.package.name");

getActivity().bindService(icsopenvpnService, mConnection, Context.BIND_AUTO_CREATE);

}

7. To disconnect the VPN service, use the below code:

mService.disconnect();

8. Install and run the app on your Android device.

Congratulations, you have successfully built your own Android VPN solution. Now, your privacy is in your hands. 👏

For business queries, please drop an email to tanujsinghkushwah@gmail.com

Support the development for more free servers:

  • BTC — bc1qm7j9qsn55ue3ke54n2f92el9jx8rfa343yqxq7
  • ETH — 0x81466D108b0969DC26baE8AC040d15F706E9a231

--

--

Tanuj Singh Kushwah

Writes about Software Development, Blockchain, Networking and Security.