enterActiveMode method

Future<void> enterActiveMode(
  1. String parameters
)

Implementation

Future<void> enterActiveMode(String parameters) async {
  try {
    List<String> parts = parameters.split(',');
    if (parts.length < 6) {
      sendResponse('501 Syntax error in PORT parameters');
      return;
    }
    // Validate all 6 parts are valid byte values (0-255)
    final values = <int>[];
    for (final part in parts.take(6)) {
      final v = int.tryParse(part.trim());
      if (v == null || v < 0 || v > 255) {
        sendResponse('501 Syntax error in PORT parameters');
        return;
      }
      values.add(v);
    }
    String ip = values.take(4).join('.');
    int port = values[4] * 256 + values[5];

    if (protectionLevel == ProtectionLevel.private_ &&
        securityContext != null) {
      dataSocket = await SecureSocket.connect(
        ip,
        port,
        context: securityContext!,
      );
    } else {
      dataSocket = await Socket.connect(ip, port);
    }
    sendResponse('200 Active mode connection established');
  } catch (e) {
    if (e is TlsException || e is HandshakeException) {
      sendResponse('522 TLS negotiation failed on data connection');
      logger.generalLog('TLS negotiation failed on data connection: $e');
    } else {
      sendResponse('425 Can\'t enter active mode');
      logger.generalLog('Error entering active mode: $e');
    }
  }
}