我希望新的android(4.0)VpnService接口实现简单的数据包捕获和分析。我已成功从filedescriptor捕获数据包(IP数据包),但由于我没有服务器,我想直接将数据包发送到其原始目的地。我尝试从数据包中获取IP和端口,用于创建保护隧道和发送数据包。但是,当我使用Fiddler进行检测时,我什么都没得到。
private Thread mThread;
private ParcelFileDescriptor mInterface;
private static String TAG = "VPN_SERVICE";
Builder builder = new Builder();
@Override
public int onStartCommand(Intent intent, final int flags, int startId) {
mThread = new Thread(new Runnable() {
@Override
public void run() {
try {
mInterface = builder.setSession("MyVPNService")
.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 out1 = openFileOutput("216427.txt", Context.MODE_APPEND);
ByteBuffer packet = ByteBuffer.allocate(512);
int length;
while (true){
while ((length = in.read(packet.array())) > 0){
packet.limit(length);
out1.write(packet.array());
ResolvePacket resolvePacket = new ResolvePacket();
resolvePacket.debugPacket(packet);
String desIP = resolvePacket.DESTINATION_IP;
String desPORT = resolvePacket.DESTINATION_PORT;
Log.d("IP + PORT:", "*******" + desIP + "******" + desPORT + "******");
DatagramChannel tmp_tunnel = DatagramChannel.open();
tmp_tunnel.connect(new InetSocketAddress(desIP, Integer.parseInt(desPORT)));
protect(tmp_tunnel.socket());
tmp_tunnel.write(packet);
tmp_tunnel.close();
packet.clear();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(mInterface != null){
mInterface.close();
mInterface = null;
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}, "MyVpnRunnable");
mThread.start();
return START_STICKY;
}