socks5ConnectRequestBytes function

List<int> socks5ConnectRequestBytes(
  1. String targetHost,
  2. int targetPort
)

The CONNECT request for targetHost:targetPort: the hostname rides UNRESOLVED (ATYP 0x03, remote DNS at the proxy) unless it is already an IP literal (ATYP 0x01/0x04).

Implementation

List<int> socks5ConnectRequestBytes(String targetHost, int targetPort) {
  final address = InternetAddress.tryParse(targetHost);
  return [
    0x05,
    0x01, // CONNECT
    0x00,
    if (address != null)
      if (address.type == InternetAddressType.IPv4) ...[
        0x01,
        ...address.rawAddress,
      ] else ...[
        0x04,
        ...address.rawAddress,
      ]
    else ...[0x03, targetHost.length, ...ascii.encode(targetHost)],
    targetPort >> 8,
    targetPort & 0xFF,
  ];
}