SocksUpdPacket.parse constructor

SocksUpdPacket.parse(
  1. List<int> packetData
)

Implementation

SocksUpdPacket.parse(List<int> packetData) {
  final uint8ListPacketData = Uint8List.fromList(packetData);
  final bytes = uint8ListPacketData.buffer.asByteData();
  var offset = 2;

  if (bytes.getUint8(offset++) != 0) {
    throw Exception('Fragmented packets not supported');
  }

  final remoteAddressType = bytes.getUint8(offset++);

  int length;
  if (remoteAddressType == AddressType.domain.byte) {
    length = bytes.getUint8(offset) + 1;
  } else {
    length = remoteAddressType == AddressType.ipv4.byte ? 4 : 16;
  }

  remoteAddress = InternetAddress.fromRawAddress(
    bytes.buffer.asUint8List(
      offset,
      length,
    ),
  );
  offset += length;

  remotePort = bytes.getUint16(offset);
  offset += 2;

  data = bytes.buffer.asUint8List(offset);
}