-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix tunnel parsing exception handling. (#550)
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
Showing
7 changed files
with
145 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include "TunnelUtils.hpp" | ||
|
||
namespace et { | ||
vector<PortForwardSourceRequest> parseRangesToRequests(const string& input) { | ||
vector<PortForwardSourceRequest> pfsrs; | ||
auto j = split(input, ','); | ||
for (auto& pair : j) { | ||
vector<string> sourceDestination = split(pair, ':'); | ||
if (sourceDestination.size() < 2) { | ||
throw TunnelParseException( | ||
"Tunnel argument must have source and destination between a ':'"); | ||
} | ||
try { | ||
if (sourceDestination[0].find_first_not_of("0123456789-") != | ||
string::npos && | ||
sourceDestination[1].find_first_not_of("0123456789-") != | ||
string::npos) { | ||
PortForwardSourceRequest pfsr; | ||
pfsr.set_environmentvariable(sourceDestination[0]); | ||
pfsr.mutable_destination()->set_name(sourceDestination[1]); | ||
pfsrs.push_back(pfsr); | ||
} else if (sourceDestination[0].find('-') != string::npos && | ||
sourceDestination[1].find('-') != string::npos) { | ||
vector<string> sourcePortRange = split(sourceDestination[0], '-'); | ||
int sourcePortStart = stoi(sourcePortRange[0]); | ||
int sourcePortEnd = stoi(sourcePortRange[1]); | ||
|
||
vector<string> destinationPortRange = split(sourceDestination[1], '-'); | ||
int destinationPortStart = stoi(destinationPortRange[0]); | ||
int destinationPortEnd = stoi(destinationPortRange[1]); | ||
|
||
if (sourcePortEnd - sourcePortStart != | ||
destinationPortEnd - destinationPortStart) { | ||
throw TunnelParseException( | ||
"source/destination port range must have same length"); | ||
} else { | ||
int portRangeLength = sourcePortEnd - sourcePortStart + 1; | ||
for (int i = 0; i < portRangeLength; ++i) { | ||
PortForwardSourceRequest pfsr; | ||
pfsr.mutable_source()->set_port(sourcePortStart + i); | ||
pfsr.mutable_destination()->set_port(destinationPortStart + i); | ||
pfsrs.push_back(pfsr); | ||
} | ||
} | ||
} else if (sourceDestination[0].find('-') != string::npos || | ||
sourceDestination[1].find('-') != string::npos) { | ||
throw TunnelParseException( | ||
"Invalid port range syntax: if source is a range, " | ||
"destination must be a range (and vice versa)"); | ||
} else { | ||
PortForwardSourceRequest pfsr; | ||
pfsr.mutable_source()->set_port(stoi(sourceDestination[0])); | ||
pfsr.mutable_destination()->set_port(stoi(sourceDestination[1])); | ||
pfsrs.push_back(pfsr); | ||
} | ||
} catch (const TunnelParseException& e) { | ||
throw e; | ||
} catch (const std::logic_error& lr) { | ||
throw TunnelParseException("Invalid tunnel argument '" + input + | ||
"': " + lr.what()); | ||
} | ||
} | ||
return pfsrs; | ||
} | ||
|
||
} // namespace et |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef __ET_TUNNEL_UTILS__ | ||
#define __ET_TUNNEL_UTILS__ | ||
|
||
#include "ETerminal.pb.h" | ||
|
||
namespace et { | ||
|
||
vector<PortForwardSourceRequest> parseRangesToRequests(const string& input); | ||
|
||
class TunnelParseException : public std::exception { | ||
public: | ||
TunnelParseException(const string& msg) : message(msg) {} | ||
const char* what() const noexcept override { return message.c_str(); } | ||
|
||
private: | ||
std::string message = " "; | ||
}; | ||
|
||
} // namespace et | ||
#endif // __ET_TUNNEL_UTILS__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters