From 081ad63301bc3a8f6ae3f619489df655158a148a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Kr=C3=BCger?= Date: Sat, 25 Apr 2020 16:07:41 +0200 Subject: [PATCH] Add user@address:port support --- src/libstore/ssh.cc | 14 ++++++++++---- src/libstore/ssh.hh | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/libstore/ssh.cc b/src/libstore/ssh.cc index 84548a6e4ebf..eba40a04ea48 100644 --- a/src/libstore/ssh.cc +++ b/src/libstore/ssh.cc @@ -2,14 +2,18 @@ namespace nix { -SSHMaster::SSHMaster(const std::string & host, const std::string & keyFile, bool useMaster, bool compress, int logFD) - : host(host) - , fakeSSH(host == "localhost") - , keyFile(keyFile) +SSHMaster::SSHMaster(const std::string & hostNport, const std::string & keyFile, bool useMaster, bool compress, int logFD) + : keyFile(keyFile) , useMaster(useMaster && !fakeSSH) , compress(compress) , logFD(logFD) { + auto res = tokenizeString>(hostNport, ":"); + port = res.size() > 1 ? atoi(res[1].c_str()) : 0; // if we have a port, parse that + host = res[0]; + + fakeSSH = host == "localhost" && !port; + if (host == "" || hasPrefix(host, "-")) throw Error("invalid SSH host name '%s'", host); } @@ -22,6 +26,8 @@ void SSHMaster::addCommonSSHOpts(Strings & args) args.insert(args.end(), {"-i", keyFile}); if (compress) args.push_back("-C"); + if (port != 0) + args.insert(args.end(), {"-p", std::to_string(port)}); } std::unique_ptr SSHMaster::startCommand(const std::string & command) diff --git a/src/libstore/ssh.hh b/src/libstore/ssh.hh index 4f0f0bd29f9f..bdffdaa14d82 100644 --- a/src/libstore/ssh.hh +++ b/src/libstore/ssh.hh @@ -9,12 +9,13 @@ class SSHMaster { private: - const std::string host; + std::string host; bool fakeSSH; const std::string keyFile; const bool useMaster; const bool compress; const int logFD; + int port; struct State {