October 26, 2024
Chicago 12, Melborne City, USA
Android

how to passthrough traffic or bypass vpn while intercepting network traffic in android app with java?


Hi anyone please help how to route or bypass vpn while intercepting network traffic!

I am creating an Android app with JAVA that intercepts network traffic while running in background.

Currently it can intercept traffic but not able to pass through or route it, and the device cannot access the Internet.

*The problem I am facing is: *
While the app is running in background, and accessing any url through browser it stucks on loading screen and then showing No Internet.

Please have a look at the code

Permissions

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


Builder builder = new Builder()
                    .setSession("TrafficMonitorService")
                    .addAddress(VPN_ADDRESS, 24)
                    .addRoute(VPN_ROUTE, 0)
                    .addDnsServer(DNS_PRIMARY)
                    .addDnsServer(DNS_SECONDARY);
          

            // Exclude TrafficMonitorService itself from VPN
            try {
                builder.addDisallowedApplication(getPackageName());
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            mInterface = builder.establish();
            FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());
            FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());

            byte[] packet = new byte[32767];
            while (isRunning && !Thread.currentThread().isInterrupted()) {
                int length = in.read(packet);
                if (length > 0) {
                    logPacket(packet, length);
                    out.write(packet, 0, length); // Forward packet
                }
            }

Also tried by adding this code –
// Allow all apps to bypass the VPN

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
          builder.allowBypass();
      }



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video