gpt4 book ai didi

Android VpnService,数据包转发

转载 作者:可可西里 更新时间:2023-11-01 02:32:53 55 4
gpt4 key购买 nike

我正在创建基于 ToyVPN 的应用程序来捕获 tcp/udp 数据包。在我的应用程序中收到传出数据包后,我想将它们转发到原始目的地。我已经设法从 header 中获取目标 ip 和端口,但我不知道如何与远程服务器通信,然后将响应写回源。我认为这是可能的,因为有这个 app .这是我的第一次尝试:

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 将流量路由到手机上运行的另一个接口(interface),并将所有这些流量发送到互联网(这与 mobiwol、greyshirts 和其他使用 VPNService 的应用相同)。

如果您运行(并了解)ToyVPN,您就会知道来自手机的所有流量都会进入服务器(您的计算机),您在服务器中配置 iptables 以将所有流量发送到互联网。

如果你想在没有服务器的情况下运行,你必须在手机上做同样的事情。来自 another question:

When I look at mobiwol's connection with "adb shell netcfg" it creates a tun0 interface with 10.2.3.4/32 address. It routes all packages to this private network and send to internet.

所以基本上在您的应用程序中,您必须将手机配置为充当它自己的服务器。

关于Android VpnService,数据包转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21910082/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com