How to analyze a proxy socks package in java?

 1 year ago
source link: https://www.codesd.com/item/how-to-analyze-a-proxy-socks-package-in-java.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to analyze a proxy socks package in java?


Im trying to build a basic proxy 4 server, and I have to parse a packet and extract its informations that looks like this:

1 byte (version)
1 byte (command)
2 byte (port)
4 byte (ip)
X byte (userID, builds a string by looping until '\0' is found)

Here's my code so far:

InputStream reader = socket.getInputStream();

byte[] ver  = new byte[1];
byte[] cmd  = new byte[1];
byte[] port = new byte[2];
byte[] ip   = new byte[4];

reader.read(ver, 0, 1);  //should be: 4
reader.read(cmd, 1, 1);  //should be: 1
reader.read(port, 2, 2); //should be: 00, 80
reader.read(ip, 4, 4);   //should be: 217, 70, 182, 162

Here's the response I get from my code: [4, 1, 0, 80, -39, 70, -74, -94]

For some reason the IP part I get is always wrong, I really don't know why. My second issue would be: Is there an easy and clean way to get the last userID string part, without having to build a messy loop that could end up hanging for ever, if the \0 byte was not found?

Thank you.

Throw it all away and use the methods of DataInputStream. They will give you ints, shorts, longs, and fully-read byte arrays, and take care of network byte ordering for you as well.

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK