I have read https://developer.android.com/guide/topics/connectivity/vpn but i have a few questions about it:
- am i creating a vpn client according to the code i have pasted below, if so, where is the vpn server?
- 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
- 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();
}
}
No, you are just instructing Android to send the network packets of the device to your VpnService
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 channelAfter 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:You can see find a VPNService example here