accept method

  1. @override
Future<Socket?> accept({
  1. bool? connect,
  2. bool? allowIPv6,
})
override

Accept connection.

TCP: If connect is true this function will open connection to requested host.

Mustn't throw any errors.

Implementation

@override
Future<Socket?> accept({
  bool? connect,
  bool? allowIPv6,
}) async {
  final Socket? target;

  final _connect = connect ?? false;
  final _allowIPv6 = allowIPv6 ?? false;

  if (_connect) {
    if(!_allowIPv6 && desiredAddress.type == InternetAddressType.IPv6) {
      add([
        0x05,
        CommandReplyCode.unsupportedAddressType.byte,
      ]);
      return null;
    }
    try {
      target = await Socket.connect(
        desiredAddress.type == InternetAddressType.unix
            ? ((await InternetAddress.lookup(desiredAddress.address))[0])
            : desiredAddress,
        desiredPort,
      );

      target.done.ignore();
    } catch (error) {
      print(error);
      await reject(CommandReplyCode.connectionRefused);
      return null;
    }
  } else {
    target = null;
  }

  add([
    0x05, // Socks version
    0x00, // Succeeded
    0x00, // Reserved byte
    0x01, // IPv4
    0x00, 0x00, 0x00, 0x00, // address 0.0.0.0
    0x00, 0x00, // port 0
  ]);
  await flush();

  return target;
}