socks5ConsumeReply function
Consumes the CONNECT reply (VER REP RSV ATYP BND.ADDR BND.PORT): throws on a malformed VER, a failure REP, or an unknown ATYP.
Implementation
Future<void> socks5ConsumeReply(Socks5HandshakeReader reader) async {
final head = await reader.readExactly(4);
if (head[0] != 0x05) {
throw const SocketException('SOCKS5 proxy sent a malformed reply');
}
if (head[1] != 0x00) {
throw SocketException('SOCKS5 CONNECT failed: ${socks5ReplyText(head[1])}');
}
var remaining = switch (head[3]) {
0x01 => 4 + 2,
0x04 => 16 + 2,
0x03 => -1, // sized by the name-length byte, read below
_ => throw SocketException(
'SOCKS5 proxy replied with unknown address type 0x'
'${head[3].toRadixString(16)}',
),
};
if (remaining < 0) {
remaining = (await reader.readExactly(1))[0] + 2;
}
if (remaining > 0) {
await reader.readExactly(remaining);
}
}