Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java VPN包旁路_Java_Android_Parsing_Ip_Vpn - Fatal编程技术网

Java VPN包旁路

Java VPN包旁路,java,android,parsing,ip,vpn,Java,Android,Parsing,Ip,Vpn,我正在创建一个模拟的VPN(实际上并没有创建到服务器的真正连接),以获取所有传入和传出的网络字节(信息) 现在我可以获取数据包并解析它们。例如,我得到的是: IP版本:4 收割台长度:20 全长:60 议定书:6 来源IP:10.0.2.0 目的地IP:5.20.5.59 主机名:clients4.google.com 我想知道我应该做什么以及如何连接到网站/应用程序(目前它不连接) 在本网站中:写道需要采取以下步骤: 从TUN获取IP数据包。与所有VPN服务相同 提取第4层信息。协议类型(如T

我正在创建一个模拟的VPN(实际上并没有创建到服务器的真正连接),以获取所有传入和传出的网络字节(信息)

现在我可以获取数据包并解析它们。例如,我得到的是:

IP版本:4 收割台长度:20 全长:60 议定书:6 来源IP:10.0.2.0 目的地IP:5.20.5.59 主机名:clients4.google.com

我想知道我应该做什么以及如何连接到网站/应用程序(目前它不连接)

在本网站中:写道需要采取以下步骤:

  • 从TUN获取IP数据包。与所有VPN服务相同
  • 提取第4层信息。协议类型(如TCP/UDP)及其 有效载荷是必须的。由于TCP中有一个握手过程,在 从中获取实际有效负载数据,我们需要写回 首先是握手包
  • 选择相应的套接字以发送有效负载。因为这一步是 正在处理第4层,因此我们需要保存套接字并尝试获取 稍后返回数据。如果有任何返回数据,我们需要传递 这些包裹要寄往敦
  • 从套接字获取数据包,并构建一个第3层数据包。首先,我们需要 构建有效的第4层数据包。UDP比4字节更简单 UDP标头仅包含源地址、源端口、目标 地址,目的港。TCP更为复杂,因为它是一种状态
  • 连接时,序列号和确认号应为 设置正确。然后,使用第4层数据包作为有效载荷,我们需要 构建一个有效的第3层数据包。将IP数据包写回TUN。等同于 所有的VPN服务都可以
  • 在第二步中,我从数据包中获取信息。但不要忽视应该如何进一步做。也许有人能给我详细解释一下。另外,当我有IP地址时,也许可以告诉我如何获得目的地端口。还有代码:

        public class VPN extends VpnService implements Handler.Callback, Runnable {
        private static final String TAG = "VpnService";
    
        private String mServerAddress = "127.0.0.1";
        private int mServerPort = 55555;
    
        private Handler mHandler;
        private Thread mThread;
    
        private ParcelFileDescriptor mInterface;
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (mHandler == null) {
                mHandler = new Handler(this);
            }
    
            if (mThread != null) {
                mThread.interrupt();
            }
            mThread = new Thread(this, "VpnThread");
            mThread.start();
            return START_STICKY;
        }
    
        @Override
        public void onDestroy() {
            if (mThread != null) {
                mThread.interrupt();
            }
            super.onDestroy();
        }
    
        @Override
        public boolean handleMessage(Message message) {
            if (message != null) {
                Toast.makeText(this, (String) message.obj, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    
        @Override
        public synchronized void run() {
            try {
                Log.i(TAG, "Starting");
                InetSocketAddress server = new InetSocketAddress(mServerAddress, mServerPort);
                run(server);
    
            } catch (Exception e) {
                Log.e(TAG, "Got " + e.toString());
                try {
                    mInterface.close();
                } catch (Exception e2) {  }
                Message msgObj = mHandler.obtainMessage();
                msgObj.obj = "Disconnected";
                mHandler.sendMessage(msgObj);
    
            } finally {
    
            }
        }
    
        DatagramChannel mTunnel = null;
    
    
        protected boolean run(InetSocketAddress server) throws Exception {
            boolean connected = false;
    
            mTunnel = DatagramChannel.open();
    
            if (!protect(mTunnel.socket())) {
                throw new IllegalStateException("Cannot protect the tunnel");
            }
    
            mTunnel.connect(server);
    
            mTunnel.configureBlocking(false);
            handshake();
    
            connected = true;
            Message msgObj = mHandler.obtainMessage();
            msgObj.obj = "Connected";
            mHandler.sendMessage(msgObj);
    
    
    
            new Thread ()
            {
    
                public void run ()
                {
                    FileInputStream in = new FileInputStream(mInterface.getFileDescriptor());
                    ByteBuffer packet = ByteBuffer.allocate(32767);
    
                    DatagramChannel tunnel = mTunnel;
                    FileOutputStream out = new FileOutputStream(mInterface.getFileDescriptor());
                    int length;
                    String destIP;
    
                    try
                    {
    
                        while (true)
                        {
                            while ((length = in.read(packet.array())) > 0) {
                                packet.limit(length);
                                Log.d(TAG, "Total Length:" + mTunnel.socket().getInetAddress());
    
                                mTunnel.write(packet);
                                packet.flip();
    
                                TCP_IP TCP_debug = new TCP_IP(packet);
                                TCP_debug.debug();
                                destIP = TCP_debug.getDestination();
    
                              //  InetAddress address = InetAddress.getByName(destIP);
                              //  System.out.println(address.getHostAddress()); // Gaunamas IP (185.11.24.36)
                              //  System.out.println(address.getHostName()); // www.15min.lt
    
    
    
                                out.write(packet.array(), 0, length);
                                packet.clear();
    
                                Thread.sleep(100);
    
                            }
                        }
    
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                }
    
            }.start();
    
    
            return connected;
        }
    
        private void makeConnection(String destination, int port) {
            try {
                run(new InetSocketAddress(destination, port));
            }
            catch (Exception e) {
                Log.d(TAG, "klaida jungiantis");
            }
        }
        private void handshake() throws Exception {
    
            if (mInterface == null)
            {
                Builder builder = new Builder();
    
                //builder.setMtu(1500);
                //builder.addAddress("10.0.2.0", 32);
               // builder.addRoute("0.0.0.0", 0);
                 builder.addAddress("192.168.0.1", 24);
                 builder.addDnsServer("8.8.8.8");
                 builder.addRoute("0.0.0.0", 0);
    
                try {
                    mInterface.close();
                } catch (Exception e) {
                    // ignore
                }
    
                mInterface = builder.setSession("VPN'as").establish();
            }
        }
    
    
    }
    
    
    
    public class TCP_IP extends VPN {
    
        private ByteBuffer packet;
        private String hostname;
        private String destIP;
        private String sourceIP;
        private int version;
        private int protocol;
        private int port;
    
    
        public TCP_IP(ByteBuffer pack) {
            this.packet = pack;
        }
    
        public void debug() {
    
    
            int buffer = packet.get();
            int headerlength;
            int temp;
    
            version = buffer >> 4;
            headerlength = buffer & 0x0F;
            headerlength *= 4;
            System.out.println("IP Version:"+version);
            System.out.println("Header Length:"+headerlength);
            String status = "";
            status += "Header Length:"+headerlength;
    
            buffer = packet.get();      //DSCP + EN
            buffer = packet.getChar();  //Total Length
    
            System.out.println( "Total Length:"+buffer);
    
            buffer = packet.getChar();  //Identification
            buffer = packet.getChar();  //Flags + Fragment Offset
            buffer = packet.get();      //Time to Live
            buffer = packet.get();      //Protocol
    
            protocol = buffer;
            System.out.println( "Protocol:"+buffer);
    
            status += "  Protocol:"+buffer;
    
            buffer = packet.getChar();  //Header checksum
    
    
            byte buff = (byte)buffer;
    
            sourceIP  = "";
            buff = packet.get();  //Source IP 1st Octet
            temp = ((int) buff) & 0xFF;
            sourceIP += temp;
            sourceIP += ".";
    
            buff = packet.get();  //Source IP 2nd Octet
            temp = ((int) buff) & 0xFF;
            sourceIP += temp;
            sourceIP += ".";
    
            buff = packet.get();  //Source IP 3rd Octet
            temp = ((int) buff) & 0xFF;
            sourceIP += temp;
            sourceIP += ".";
    
            buff = packet.get();  //Source IP 4th Octet
            temp = ((int) buff) & 0xFF;
            sourceIP += temp;
    
            System.out.println( "Source IP:"+sourceIP);
    
            status += "   Source IP:"+sourceIP;
    
    
            destIP  = "";
    
    
            buff = packet.get();  //Destination IP 1st Octet
            temp = ((int) buff) & 0xFF;
            destIP += temp;
            destIP += ".";
    
            buff = packet.get();  //Destination IP 2nd Octet
            temp = ((int) buff) & 0xFF;
            destIP += temp;
            destIP += ".";
    
            buff = packet.get();  //Destination IP 3rd Octet
            temp = ((int) buff) & 0xFF;
            destIP += temp;
            destIP += ".";
    
            buff = packet.get();  //Destination IP 4th Octet
            temp = ((int) buff) & 0xFF;
            destIP += temp;
    
            System.out.println( "Destination IP:" + destIP);
            status += "   Destination IP:"+destIP;
    
    
    
    
        }
    
        public String getDestination() {
            return destIP;
        }
    
        public int getProtocol() {
            return protocol;
        }
    
        public int getPort() {
            return port;
        }
    
        public String getHostname() {
            return hostname;
        }
    
        public int getIPversion() { return version; }
    
    }
    
    改变

    private String mServerAddress = "127.0.0.1";
    

    我还不知道答案并解释发生了什么,但我在Android Monitor中看到,我们不能将localhost(127.0.0.1)用于服务器地址


    另一个问题是HTTP上的SSL(HTTPS)。如果你连接到一个网站,它会工作,但如果你尝试,它不会工作。

    你有没有找到解决办法?我想做类似的事情。你有没有找到解决办法?我希望做类似的事情。@CikLinas TCP_IP从哪里来?那是图书馆吗?
    private String mServerAddress = "10.0.0.1";