how to log/check network traffic through my vpn service app in android

1k Views Asked by At

I have read https://developer.android.com/guide/topics/connectivity/vpn but i have a few questions about it:

  1. am i creating a vpn client according to the code i have pasted below, if so, where is the vpn server?
  2. My vpn service is working(as i can see it in the settings of the emulator), how do i know if the network traffic is flowing through my vpn service
  3. how to log details of the network traffic?(destination adrress of the network request etc.)

Here is the code:-

public class vpnService extends VpnService {
public vpnService() {
}
private Thread mThread;
private ParcelFileDescriptor mInterface;
Builder builder=new Builder();
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mThread=new Thread(new Runnable(){

        @Override
        public void run() {
            try{
                mInterface=builder.setSession("vpnService")
                        .addAddress("192.168.0.1",24)
                        .addDnsServer("8.8.8.8")
                        .addRoute("0.0.0.0",0).establish();

                FileInputStream in=new FileInputStream(mInterface.getFileDescriptor());
                FileOutputStream out=new FileOutputStream(mInterface.getFileDescriptor());
                DatagramChannel tunnel=DatagramChannel.open();
                tunnel.connect(new InetSocketAddress("127.0.0.1",8087));
                protect(tunnel.socket());

                while(true){
                    Thread.sleep(100);
                }
            }

            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                try{
                    if(mInterface!=null){
                        mInterface.close();
                        mInterface=null;
                    }
                }
                catch(Exception e){

                }
            }
        }
    },"vpnRunnable");
    mThread.start();
    return START_STICKY;

}

@Override
public void onDestroy() {
    if(mThread!=null){
        mThread.interrupt();
    }
    super.onDestroy();
}

}

1

There are 1 best solutions below

0
On

am i creating a vpn client according to the code i have pasted below, if so, where is the vpn server?

No, you are just instructing Android to send the network packets of the device to your VpnService

how do i know if the network traffic is flowing through my vpn service

You are currently not sending any data. To send data, you need to read IPv4 packets from the in FileOutputStream and write them to the tunnel channel

how to log details of the network traffic

After you read a packet from the in FileOutputStream, you can the pcap4j library to parse the packet and then log it to the logcat console. For example:

IpV4Packet pkt = IpV4Packet.newPacket(buf, 0, len);
  Log.d("PKT", (String.format("[%s] %s -> %s [%d B]\n",
            hdr.getProtocol(),
            hdr.getSrcAddr().getHostAddress(), hdr.getDstAddr().getHostAddress(),
            pkt.length())))

You can see find a VPNService example here