简体   繁体   English

Android VpnService,包转发

[英]Android VpnService, packets forwarding

I'm creating application based on ToyVPN to capture tcp/udp packets. 我正在创建基于ToyVPN的应用程序来捕获tcp / udp数据包。 After i get outgoing packets in my apllication i would like to forward them to original destination. 我在我的应用程序中获取传出数据包后,我想将它们转发到原始目标。 I have managed to get destination ip and port from headers but i have no idea how to communicate with remote server and then write response back to the source. 我已设法从标头获取目标IP和端口,但我不知道如何与远程服务器通信,然后将响应写回源。 I think this is possible because there is this app . 我认为这是可能的,因为有这个应用程序 Here is my first first attempt: 这是我的第一次尝试:

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();
}

Apparently tPacketCapture routes the traffice into another interface running on the phone and sends all of this traffic to the internet (this is the same as mobiwol, greyshirts and other apps that use VPNService). 显然tPacketCapture将流量路由到手机上运行的另一个界面,并将所有这些流量发送到互联网(这与使用VPNService的mobiwol,greyshirts和其他应用程序相同)。

If you run (and understand) ToyVPN, you know that all of the traffic from the phone goes into a server (your computer) in which you configure iptables to send all the traffic to the internet. 如果您运行(并了解)ToyVPN,您就会知道来自手机的所有流量都会进入服务器(您的计算机),您可以在其中配置iptables以将所有流量发送到互联网。

If you want to run without a server, yo have to do the same thing on the phone. 如果你想在没有服务器的情况下运行,你必须在手机上做同样的事情。 From 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. 当我看到mobiwol与“adb shell netcfg”的连接时,它会创建一个带有10.2.3.4/32地址的tun0接口。 It routes all packages to this private network and send to internet. 它将所有程序包路由到此专用网络并发送到Internet。

So basically from your app you will have to configure the phone to act as it's own server. 因此,基本上从您的应用程序,您将必须配置手机充当它自己的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM