setClient method

Future<String?> setClient(
  1. SSHModel ssh
)

Sets a client with the given ssh info.

Implementation

Future<String?> setClient(SSHModel ssh) async {
  String result = '';

  try {
    final socket = await SSHSocket.connect(ssh.host, ssh.port,
        // timeout: const Duration(seconds: 36000000));
        timeout: const Duration(seconds: 30));
    String? password;
    bool isAuthenticated = false;

    _client = SSHClient(
      socket,
      onPasswordRequest: () {
        password = ssh.passwordOrKey;

        return password;
      },
      username: ssh.username,
      onAuthenticated: () {
        isAuthenticated = true;
      },
      // keepAliveInterval: const Duration(seconds: 36000000),
      keepAliveInterval: const Duration(seconds: 30),
    );

    /// Add a delay before checking isAuthenticated
    await Future.delayed(const Duration(seconds: 10));

    if (isAuthenticated) {
    } else {
      // If the client is not authenticated, indicate a failed connection
      throw Exception('SSH authentication failed');
    }
  } catch (e) {
    result = "Failed to connect to the SSH server: $e";
  }
  return result;
}