Skip to content

Commit

Permalink
Fix tunnel parsing exception handling.
Browse files Browse the repository at this point in the history
Rather than using STFATAL and exiting, bubble up tunnel parsing
exception to main and display the responsible option arg and print help
menu so the user need not dig into /tmp/etclient-* to determine why et
aborted.

Fixes #491
  • Loading branch information
jshort committed Dec 2, 2022
1 parent ff2249e commit 5648709
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/terminal/TerminalClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ vector<PortForwardSourceRequest> parseRangesToRequests(const string& input) {
pfsrs.push_back(pfsr);
}
} catch (const std::logic_error& lr) {
STFATAL << "Logic error: " << lr.what();
exit(1);
string msg = "Invalid tunnel argument: " + input;
throw TunnelParseException(msg.c_str());
}
}
return pfsrs;
Expand Down
11 changes: 11 additions & 0 deletions src/terminal/TerminalClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,16 @@ class TerminalClient {
int keepaliveDuration;
};

class TunnelParseException : public std::exception {
public:
TunnelParseException(const char* msg) : message(msg) {}
const char* what() const noexcept override
{
return message.c_str();
}
private:
std::string message = " ";
};

} // namespace et
#endif // __ET_TERMINAL_CLIENT__
25 changes: 17 additions & 8 deletions src/terminal/TerminalClientMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ bool ping(SocketEndpoint socketEndpoint,
return true;
}

void handleParseException(std::exception& e, cxxopts::Options& options) {
CLOG(INFO, "stdout") << "Exception: " << e.what() << "\n" << endl;
CLOG(INFO, "stdout") << options.help({}) << endl;
exit(1);
}

int main(int argc, char** argv) {
WinsockContext context;
string tmpDir = GetTempDirectory();
Expand Down Expand Up @@ -334,17 +340,20 @@ int main(int argc, char** argv) {
}
TelemetryService::get()->logToDatadog("Session Started", el::Level::Info,
__FILE__, __LINE__);
string tunnel_arg =
result.count("tunnel") ? result["tunnel"].as<string>() : "";
string r_tunnel_arg =
result.count("reversetunnel") ? result["reversetunnel"].as<string>() : "";
TerminalClient terminalClient(
clientSocket, clientPipeSocket, socketEndpoint, id, passkey, console,
is_jumphost, result.count("t") ? result["t"].as<string>() : "",
result.count("r") ? result["r"].as<string>() : "", forwardAgent,
sshSocket, keepaliveDuration);
terminalClient.run(result.count("command") ? result["command"].as<string>()
: "");
is_jumphost, tunnel_arg, r_tunnel_arg,
forwardAgent, sshSocket, keepaliveDuration);
terminalClient.run(
result.count("command") ? result["command"].as<string>() : "");
} catch (TunnelParseException& tpe) {
handleParseException(tpe, options);
} catch (cxxopts::OptionException& oe) {
CLOG(INFO, "stdout") << "Exception: " << oe.what() << "\n" << endl;
CLOG(INFO, "stdout") << options.help({}) << endl;
exit(1);
handleParseException(oe, options);
}

#ifdef WIN32
Expand Down

0 comments on commit 5648709

Please sign in to comment.