android DatagramChannel隧道必须连接到VpnService中的服务器?

时间:2019-06-29 11:26:14

标签: java android network-programming vpn android-vpn-service

我正在用android编码VPN,以观察本地网络流量(数据包),并在检查后让数据包通过。我的应用程序基于ToyVPN,到目前为止,我可以接收到数据包,但无法通过隧道发送它们。我看到隧道连接到('127.0.0.1',9040)。我的问题是,是否需要一台服务器绑定到('127.0.0.1',9040)以响应其他隧道侧?如果不?隧道连接到哪里?基本上,这个隧道如何运作?


参见部分代码:

  public void run() {
            try {
                //a. Configure the TUN and get the interface.
                mInterface = builder.setSession("MyVPNService")
                        .addAddress("192.168.1.0", 24)
                        .addDnsServer("8.8.8.8")
                        .addRoute("0.0.0.0", 0).establish();
                //b. Packets to be sent are queued in this input stream.
                FileInputStream in = new FileInputStream(
                        mInterface.getFileDescriptor());
                //b. Packets received need to be written to this output stream.
                FileOutputStream out = new FileOutputStream(
                        mInterface.getFileDescriptor());
                //c. The UDP channel can be used to pass/get ip package to/from server
                DatagramChannel tunnel = DatagramChannel.open();

                // Connect to the server, localhost is used for demonstration only.
                tunnel.connect(new InetSocketAddress("127.0.0.1", 9040));
                //d. Protect this socket, so package send by it will not be feedback to the vpn service.
                protect(tunnel.socket());
                tunnel.configureBlocking(false);
                ByteBuffer packet = ByteBuffer.allocate(MAX_PACKET_SIZE);
                Log.d("hixnal","tunnel open:" + tunnel.isOpen() + " connected:" + tunnel.isConnected());
                //e. Use a loop to pass packets.
                int timer = 0;
                int p=0;
                while (true) {
                    boolean idle = true;
                    int length= in.read(packet.array());
                    if (length > 0) {
                        p++;
                        Log.d("hixnal",p+"");
                        packet.limit(length);
                        //debugPacket(packet);
                        //tunnel.write(packet);

0 个答案:

没有答案