SOCKS5 port fields are two unsigned bytes in network byte order: the most significant byte comes first. In Go, encode an already validated uint16 directly:

var field [2]byte
binary.BigEndian.PutUint16(field[:], port)

Import encoding/binary. For port 1080, the resulting hexadecimal bytes are 04 38. For port 4, they are 00 04; a leading zero is part of the fixed-width field.

If the input begins as a larger integer or a string, validate its numeric range before converting to uint16. Conversion can discard high bits rather than report an out-of-range value.

In a SOCKS5 reply, serialize BND.PORT, the server’s bound-port field. Do not copy the requested destination port into that field merely because both occupy two bytes. Place the encoded field after the reply’s address, whose length depends on ATYP.

متعلقہ مضامین