使用DatagramChannel和InetSocketAddress使用其配置文件连接到VPN服务器

时间:2019-04-28 17:44:40

标签: java android vpn

我正在构建一个简单的应用程序,只需单击一下即可连接到VPN服务器,我使用的是我在网上找到的有关工作项目和教程的代码,

如下面的代码所示,该api使您可以仅使用地址和端口连接到服务器,

我正在尝试使用vpn服务器配置文件进行连接

                //a. Configure the TUN and get the interface.
                mInterface = builder.setSession("MyVPNService")
                        .addAddress("192.168.4.1", 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.
                InetSocketAddress vpn_rout = new InetSocketAddress("128.0.0.1", 8001);
                tunnel.connect(vpn_rout);

                //d. Protect this socket, so package send by it will not be feedback to the vpn service.
                protect(tunnel.socket());
                //e. Use a loop to pass packets.
                while (true) {
                    //get packet with in
                    //put packet to tunnel
                    //get packet form tunnel
                    //return packet with out
                    //sleep is a must
                    Thread.sleep(100);
                }

是否可以将我的配置添加到代码中?

0 个答案:

没有答案