DrBrad / JTun2Socks

This works similar to network layer socks client, written completely in java.
MIT License
25 stars 7 forks source link

How to forward UDP? #4

Open trinhthu opened 1 year ago

trinhthu commented 1 year ago

Thanks for your efforts.

Now I want to support forwarding UDP. What time will you support UDP? If you don't have time to support UDP soon, please guide me(documents or any relate technical) to support forwarding UDP.

DrBrad commented 1 year ago

Hi, I havent implemented a small part. All you would need to do is take the forwarded udp packets and send them to the socks5 server. I believe that the udp packets are already being captured.

I may come back to this project in a month or so and fix the bugs.

trinhthu commented 1 year ago

Thanks for your answer. I'm trying to request UDP associate. But when I send VER CMD RSV ATYP DST.ADDR DST.PORT to establish a connection to send UDP packets, Socks server didn't send me a response and then I can't read BND.ADDR and BND.PORT to perform to connect DatagramSocket. Here is my code:

        `DatagramSocket clientSocket = new DatagramSocket();
        byte[] response = new byte[256];
        InetSocketAddress destination = new InetSocketAddress("1.1.1.1", 1080); //EDIT THIS WITH THE IP AND PORT OF THE PROXY

        Socket socketProxy = new Socket();
        socketProxy.bind(new InetSocketAddress(0));
        service.protect(socketProxy);
        socketProxy.setSoTimeout(5000);
        socketProxy.connect(destination, 5000);

        DataOutputStream writer = new DataOutputStream(new BufferedOutputStream(socketProxy.getOutputStream()));
        DataInputStream reader = new DataInputStream(new BufferedInputStream(socketProxy.getInputStream()));

        // connect/authorization request
        writer.writeByte(0x05); // VER
        writer.writeByte(0x01); // NMETHODS
        writer.writeByte(0x00); // METHODS
        writer.flush();

        // connect/authorization response
        reader.read(response); // RESPONSE IS: 5 0

        // udp associate request
        writer.writeByte(0x05); // VER
        writer.writeByte(0x03); // CMD - UDP ASSOCIATE
        writer.writeByte(0x00); // RSV
        writer.writeByte(0x01); // ATYP
        // is what I'm sending here as DST.ADDR & DST.PORT okay?
        writer.write(clientSocket.getLocalAddress().getAddress()); // DST.ADDR
        writer.writeShort(clientSocket.getLocalPort()); // DST.PORT
        writer.flush();

        // udp associate response
        Log.d(TAG, "udp associate response");
        reader.skipBytes(4); // skip VER, REP, RSV, ATYP bytes
        int relayAddress = reader.readInt();
        InetAddress relayInetAddress = InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(relayAddress).array());
        int relayPort = reader.readUnsignedShort(); // server returns some random port

        // connect to relay server via datagramsocket
        clientSocket.connect(relayInetAddress, relayPort);`

Please help me, I spent a lot of time, but I can't find the cause.

DrBrad commented 1 year ago

Hey, sorry for the late reply, the last couple months have been busy. Have you resolved this issue?

trinhthu commented 9 months ago

Hey, sorry for the late reply, the last couple months have been busy. Have you resolved this issue?

Hi, I'm so sorry for the late response. I changed to use another solution for this and don't use your lib.