-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathTunnelUtils.cpp
66 lines (62 loc) · 2.7 KB
/
TunnelUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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