1

我在 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);
4

0 回答 0