Gintas_
Gintas_

Reputation: 5030

Why is my SOCKS5 UDP implementation not working?

I spent more than 50 hours on this nonsense, why is my UDP implementation not working? Problem is that the server does not receive UDP packet. I made server app to log events in console and I see it's not receiving anything. Here is SOCKS5 documentation I followed: https://www.ietf.org/rfc/rfc1928.txt

byte[] response = new byte[256];
int responseLength;
Socket socket = new Socket(proxyHost, proxyPort);
DatagramSocket clientSocket = new DatagramSocket();

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

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

// connect/authorization response
responseLength = 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.write(InetAddress.getByName(trackerHost).getAddress());
//writer.writeShort(trackerPort);
writer.flush();

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

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

// send package to destination server
ByteBuffer byteBuffer = ByteBuffer.allocate(16 + 10);
//header - 10 bytes
byteBuffer.putShort((short) 0); // RSV
byteBuffer.put((byte) 0); // FRAG
byteBuffer.put((byte) 1); // ATYP
byteBuffer.put(trackerInetAddress.getAddress()); // DST.ADDR
byteBuffer.putShort((short) targetPort); // DST.PORT
//packet data
byteBuffer.putLong(1);
byteBuffer.putInt(0);
byteBuffer.putInt(1);
// send packet
packet = byteBuffer.array();
DatagramPacket sendPacket = new DatagramPacket(packet, packet.length);//, relayInetAddress, relayPort);
clientSocket.send(sendPacket); // SERVER DOES NOT RECEIVE THIS

Target host is verified to be working 100% fine without proxy. Using semi-dedicated proxies from https://blazingseollc.com/proxy/ which support SOCKS5

Upvotes: -2

Views: 2087

Answers (1)

Gintas_
Gintas_

Reputation: 5030

Turns out the proxy provider does not support UDP ASSOCIATE of SOCKS5. Turns out majority of proxy providers do not support it.

Upvotes: 0

Related Questions