Skip to content

Commit

Permalink
Make user feedback during ssh login less verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaraslaut committed Apr 9, 2024
1 parent f18103b commit 9a02c26
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/vtpty/SshSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,9 @@ void SshSession::processState()
void SshSession::start()
{
if (_config.port == 22)
logInfo("Starting SSH session to host: {}@{}", _config.username, _config.hostname);
logInfoWithInject("Starting SSH session to host: {}@{}", _config.username, _config.hostname);
else
logInfo("Starting SSH session to host: {}@{}:{}", _config.username, _config.hostname, _config.port);
logInfoWithInject("Starting SSH session to host: {}@{}:{}", _config.username, _config.hostname, _config.port);

assert(_state == State::Initial);
// auto const _ = std::lock_guard { _mutex };
Expand Down Expand Up @@ -897,16 +897,26 @@ void SshSession::injectRead(std::string_view buf)
_injectCV.notify_all();
}

void SshSession::logInject(std::string_view message) const
{
const_cast<SshSession*>(this)->injectRead(fmt::format("\U0001F511 \033[1;33m{}\033[m\r\n", message));
}

void SshSession::logInfo(std::string_view message) const
{
sshLog()("{}", message);
const_cast<SshSession*>(this)->injectRead(fmt::format("\U0001F511 \033[1;33m{}\033[m\r\n", message));
}

void SshSession::logInfoWithInject(std::string_view message) const
{
logInfo(message);
logInject(message);
}


void SshSession::logError(std::string_view message) const
{
errorLog()("{}", message);
const_cast<SshSession*>(this)->injectRead(fmt::format("\U0001F511 \033[1;31m{}\033[m\r\n", message));
}

bool SshSession::connect(std::string_view host, int port)
Expand Down Expand Up @@ -968,9 +978,9 @@ bool SshSession::connect(std::string_view host, int port)
auto const addrAndPort =
port == 22 ? std::string(addrStr) : fmt::format("{}:{}", addrStr, port);
if (host != addrStr)
logInfo("Connected to {} ({})", host, addrAndPort);
logInfoWithInject("Connected to {} ({})", host, addrAndPort);
else
logInfo("Connected to {}", addrAndPort);
logInfoWithInject("Connected to {}", addrAndPort);
return true;
}

Expand Down Expand Up @@ -1196,7 +1206,7 @@ void SshSession::authenticateWithPrivateKey()
return;
}

logInfo("Successfully authenticated with private key.");
logInfoWithInject("Successfully authenticated with private key.");

setState(State::OpenChannel);
}
Expand Down Expand Up @@ -1234,7 +1244,7 @@ void SshSession::authenticateWithPassword()
return;
}

logInfo("Successfully authenticated with password.");
logInfoWithInject("Successfully authenticated with password.");

setState(State::OpenChannel);
}
Expand Down Expand Up @@ -1289,7 +1299,7 @@ bool SshSession::authenticateWithAgent()
}
if (rc == LIBSSH2_ERROR_NONE)
{
logInfo("Successfully authenticated with SSH agent with identity: {}", identity->comment);
logInfoWithInject("Successfully authenticated with SSH agent with identity: {}", identity->comment);
setState(State::OpenChannel);
return true;
}
Expand Down
8 changes: 8 additions & 0 deletions src/vtpty/SshSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ class SshSession final: public Pty
void setState(State nextState);

void logInfo(std::string_view message) const;
void logInject(std::string_view message) const;
void logInfoWithInject(std::string_view message) const;

template <typename... Args>
void logInfoWithInject(fmt::format_string<Args...> fmt, Args&&... args) const
{
logInfoWithInject(fmt::format(fmt, std::forward<Args>(args)...));
}

template <typename... Args>
void logInfo(fmt::format_string<Args...> fmt, Args&&... args) const
Expand Down

0 comments on commit 9a02c26

Please sign in to comment.