[英]Android VpnService, packets forwarding
我正在創建基於ToyVPN的應用程序來捕獲tcp / udp數據包。 我在我的應用程序中獲取傳出數據包后,我想將它們轉發到原始目標。 我已設法從標頭獲取目標IP和端口,但我不知道如何與遠程服務器通信,然后將響應寫回源。 我認為這是可能的,因為有這個應用程序 。 這是我的第一次嘗試:
private void runVpnConnection() throws Exception {
configure();
FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());
FileOutputStream out = new FileOutputStream(
mInterface.getFileDescriptor());
// Allocate the buffer for a single packet.
ByteBuffer packet = ByteBuffer.allocate(32767);
boolean ok = true;
while (ok) {
Socket tcpSocket = SocketChannel.open().socket();
try {
// Read the outgoing packet from the input stream.
int length = in.read(packet.array());
if (length > 0) {
Log.i(TAG, "-------------------New packet: " + length);
packet.limit(length);
// here i get destIP and destIP
InetAddress serverAddr = InetAddress.getByName(destIP);
SocketAddress socketadd = new InetSocketAddress(serverAddr,
destPort);
protect(tcpSocket);
OutputStream outBuffer = tcpSocket.getOutputStream();
outBuffer.write(packet.array());
outBuffer.flush();
// outBuffer.close();
packet.clear();
}
if (tcpSocket.isConnected()) {
InputStream inBuffer = tcpSocket.getInputStream();
DataInputStream inStream = new DataInputStream(inBuffer);
Log.i(TAG, "Response length " + inStream.available());
if (inStream.available() > 0) {
Log.i(TAG, "Server says " + inStream.readUTF());
inStream.readFully(packet.array());
out.write(packet.array());
inBuffer.close();
}
out.flush();
}
packet.clear();
// Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, e.toString());
ok = false;
}
tcpSocket.close();
}
in.close();
out.close();
}
顯然tPacketCapture將流量路由到手機上運行的另一個界面,並將所有這些流量發送到互聯網(這與使用VPNService的mobiwol,greyshirts和其他應用程序相同)。
如果您運行(並了解)ToyVPN,您就會知道來自手機的所有流量都會進入服務器(您的計算機),您可以在其中配置iptables以將所有流量發送到互聯網。
如果你想在沒有服務器的情況下運行,你必須在手機上做同樣的事情。 來自另一個問題:
當我看到mobiwol與“adb shell netcfg”的連接時,它會創建一個帶有10.2.3.4/32地址的tun0接口。 它將所有程序包路由到此專用網絡並發送到Internet。
因此,基本上從您的應用程序,您將必須配置手機充當它自己的服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.