Android DatagramChannel туннель должен подключаться к серверу в VpnService? - PullRequest
1 голос
/ 29 июня 2019

Я кодирую VPN в Android для просмотра трафика локальной сети (пакетов) и отпускаю пакеты после проверки. мое приложение основано на 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);
...