Skip to content

Commit

Permalink
Parse ssh port for remote connections
Browse files Browse the repository at this point in the history
Caching this will be done in a later commit.
  • Loading branch information
vimpostor committed Mar 25, 2024
1 parent bc614f2 commit 4c6630c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/Util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,20 @@ std::string get_username() {
return {};
#endif
}

int get_port() {
const auto env = std::getenv("SSH_CONNECTION");
if (env) {
std::string ssh_connection {env};
const auto n = ssh_connection.rfind(' ');
if (n != std::string::npos) {
try {
return std::stoi(ssh_connection.substr(n + 1));
} catch (std::invalid_argument const &) {
std::cerr << "Failed to parse port number " << ssh_connection << std::endl;
}
}
}
return -1;
}
}
1 change: 1 addition & 0 deletions src/Util/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ const char *home_dir();
std::string pwd();
bool get_local_domain(std::string &result);
std::string get_username();
int get_port();
}
2 changes: 1 addition & 1 deletion src/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ QUrl Path::get_url() const {
res.setScheme("sftp");
res.setUserName(QString::fromStdString(Util::get_username()));
res.setHost(QString::fromStdString(host));
// TODO: Consider supporting custom ports, e.g. parse $SSH_CLIENT
res.setPort(Util::get_port());
} else {
std::cerr << "Failed deducing remote prefix" << std::endl;
}
Expand Down

0 comments on commit 4c6630c

Please sign in to comment.