Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Gauci committed Oct 8, 2019
2 parents 06434ba + 4c9f133 commit d6dc9dd
Show file tree
Hide file tree
Showing 23 changed files with 413 additions and 85 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required (VERSION 3.0.2)
project (EternalTCP VERSION 6.0.3)
project (EternalTCP VERSION 6.0.4)

SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH})
FIND_PACKAGE(Sanitizers)
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ Eternal Terminal works under WSL (Windows Subsystem for Linux). Follow the ubun

Verify that the client is installed correctly by looking for the `et` executable: `which et`.

Verify that the server is installed correctly by checking the service status: `systemctl status et`. On some operating systems, you may need to enable and start the service manually: `sudo systemctl enable et; sudo systemctl start et`
Verify that the server is installed correctly by checking the service status: `systemctl status et`. On some operating systems, you may need to enable and start the service manually: `sudo systemctl enable et; sudo systemctl start et`.

You are ready to start using ET!

## Configuring

If you'd like to modify the server settings (e.g. to change the listening port), edit /etc/et.cfg.

## Using

ET uses ssh for handshaking and encryption, so you must be able to ssh into the machine from the client. Make sure that you can `ssh user@hostname`.
Expand Down
1 change: 0 additions & 1 deletion external/msgpack-c/external/boost/predef
Submodule predef deleted from 560ff5
1 change: 0 additions & 1 deletion external/msgpack-c/external/boost/preprocessor
Submodule preprocessor deleted from 56090c
1 change: 1 addition & 0 deletions proto/ETerminal.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum TerminalPacketType {
PORT_FORWARD_DATA = 7;
TERMINAL_USER_INFO = 8;
TERMINAL_INIT = 9;
JUMPHOST_INIT = 10;
}

message TerminalBuffer {
Expand Down
2 changes: 1 addition & 1 deletion src/base/ServerConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void ServerConnection::clientHandler(int clientSocketFd) {
{
lock_guard<std::recursive_mutex> guard(classMutex);
serverClientState.reset(new ServerClientConnection(
socketHandler, clientId, clientSocketFd, clientKeys[clientId]));
socketHandler, clientId, clientSocketFd, clientKeys.at(clientId)));
clientConnections.insert(std::make_pair(clientId, serverClientState));

if (!newClient(serverClientState)) {
Expand Down
7 changes: 4 additions & 3 deletions src/base/SocketHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SocketHandler {
}
}

inline Packet readPacket(int fd) {
inline bool readPacket(int fd, Packet* packet) {
int64_t length;
readAll(fd, (char*)&length, sizeof(int64_t), false);
if (length < 0 || length > 128 * 1024 * 1024) {
Expand All @@ -67,11 +67,12 @@ class SocketHandler {
throw std::runtime_error(s.c_str());
}
if (length == 0) {
return Packet("");
return false;
}
string s(length, '\0');
readAll(fd, &s[0], length, false);
return Packet(s);
*packet = Packet(s);
return true;
}

inline void writePacket(int fd, const Packet& packet) {
Expand Down
6 changes: 5 additions & 1 deletion src/terminal/SshSetupHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ string genCommand(const string &passkey, const string &id,
string SshSetupHandler::SetupSsh(const string &user, const string &host,
const string &host_alias, int port,
const string &jumphost, int jport, bool kill,
int vlevel, const string &cmd_prefix) {
int vlevel, const string &cmd_prefix,
const string &serverFifo) {
string clientTerm("xterm-256color");
auto envString = getenv("TERM");
if (envString != NULL) {
Expand All @@ -48,6 +49,9 @@ string SshSetupHandler::SetupSsh(const string &user, const string &host,
string passkey = genRandom(32);
string id = genRandom(16);
string cmdoptions{"--verbose=" + std::to_string(vlevel)};
if (!serverFifo.empty()) {
cmdoptions += " --serverfifo=" + serverFifo;
}

string SSH_SCRIPT_DST =
genCommand(passkey, id, clientTerm, user, kill, cmd_prefix, cmdoptions);
Expand Down
3 changes: 2 additions & 1 deletion src/terminal/SshSetupHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class SshSetupHandler {
static string SetupSsh(const string &user, const string &host,
const string &host_alias, int port,
const string &jumphost, int jport, bool kill,
int vlevel, const string &cmd_prefix);
int vlevel, const string &cmd_prefix,
const string &serverFifo);
};
} // namespace et
#endif // __ET_SSH_SETUP_HANDLER__
12 changes: 9 additions & 3 deletions src/terminal/TerminalClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ TerminalClient::TerminalClient(shared_ptr<SocketHandler> _socketHandler,
Packet(EtPacketType::INITIAL_PAYLOAD, protoToString(payload)));
fd_set rfd;
timeval tv;

for (int a = 0; a < 3; a++) {
FD_ZERO(&rfd);
int clientFd = connection->getSocketFd();
Expand Down Expand Up @@ -198,7 +197,13 @@ void TerminalClient::run(const string& command) {
cout << "ET running, feel free to background..." << endl;
}

while (!shuttingDown && !connection->isShuttingDown()) {
while (!connection->isShuttingDown()) {
{
lock_guard<recursive_mutex> guard(shutdownMutex);
if (shuttingDown) {
break;
}
}
// Data structures needed for select() and
// non-blocking I/O.
fd_set rfd;
Expand Down Expand Up @@ -253,7 +258,7 @@ void TerminalClient::run(const string& command) {
if (!connection->read(&packet)) {
break;
}
char packetType = packet.getHeader();
uint8_t packetType = packet.getHeader();
if (packetType == et::TerminalPacketType::PORT_FORWARD_DATA ||
packetType ==
et::TerminalPacketType::PORT_FORWARD_DESTINATION_REQUEST ||
Expand Down Expand Up @@ -341,6 +346,7 @@ void TerminalClient::run(const string& command) {
} catch (const runtime_error& re) {
LOG(ERROR) << "Error: " << re.what();
cout << "Connection closing because of error: " << re.what() << endl;
lock_guard<recursive_mutex> guard(shutdownMutex);
shuttingDown = true;
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/terminal/TerminalClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ class TerminalClient {
void handleWindowChanged(winsize* win);
// void handlePfwPacket(char packetType);
void run(const string& command);
void shutdown() { shuttingDown = true; }
void shutdown() {
lock_guard<recursive_mutex> guard(shutdownMutex);
shuttingDown = true;
}

protected:
shared_ptr<Console> console;
shared_ptr<ClientConnection> connection;
shared_ptr<PortForwardHandler> portForwardHandler;
bool shuttingDown;
recursive_mutex shutdownMutex;
};

} // namespace et
Expand Down
44 changes: 27 additions & 17 deletions src/terminal/TerminalClientMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ int main(int argc, char** argv) {
("silent", "Disable logging") //
("N,no-terminal", "Do not create a terminal") //
("f,forward-ssh-agent", "Forward ssh-agent socket") //
("serverfifo",
"If set, communicate to etserver on the matching fifo name", //
cxxopts::value<std::string>()->default_value("")) //
;

options.parse_positional({"host", "positional"});
Expand Down Expand Up @@ -105,9 +108,8 @@ int main(int argc, char** argv) {
if (result.count("username")) {
username = result["username"].as<string>();
}
int port = result["port"].as<int>();
LOG(INFO) << "Port initially set to " << port;
string host;
int destinationPort = result["port"].as<int>();
string destinationHost;

// Parse command-line argument
if (!result.count("host")) {
Expand All @@ -123,10 +125,10 @@ int main(int argc, char** argv) {
}
if (arg.find(':') != string::npos) {
int i = arg.find(':');
port = stoi(arg.substr(i + 1));
destinationPort = stoi(arg.substr(i + 1));
arg = arg.substr(0, i);
}
host = arg;
destinationHost = arg;

Options sshConfigOptions = {
NULL, // username
Expand All @@ -146,15 +148,16 @@ int main(int argc, char** argv) {
};

char* home_dir = ssh_get_user_home_dir();
string host_alias = host;
ssh_options_set(&sshConfigOptions, SSH_OPTIONS_HOST, host.c_str());
string host_alias = destinationHost;
ssh_options_set(&sshConfigOptions, SSH_OPTIONS_HOST,
destinationHost.c_str());
// First parse user-specific ssh config, then system-wide config.
parse_ssh_config_file(&sshConfigOptions,
string(home_dir) + USER_SSH_CONFIG_PATH);
parse_ssh_config_file(&sshConfigOptions, SYSTEM_SSH_CONFIG_PATH);
LOG(INFO) << "Parsed ssh config file, connecting to "
<< sshConfigOptions.host;
host = string(sshConfigOptions.host);
destinationHost = string(sshConfigOptions.host);

// Parse username: cmdline > sshconfig > localuser
if (username.empty()) {
Expand Down Expand Up @@ -184,28 +187,35 @@ int main(int argc, char** argv) {
}

bool is_jumphost = false;
SocketEndpoint socketEndpoint;
if (!jumphost.empty()) {
is_jumphost = true;
host = jumphost;
port = result["jport"].as<int>();
LOG(INFO) << "Setting port to jumphost port";
socketEndpoint.set_name(jumphost);
socketEndpoint.set_port(result["jport"].as<int>());
} else {
socketEndpoint.set_name(destinationHost);
socketEndpoint.set_port(destinationPort);
}
SocketEndpoint socketEndpoint;
socketEndpoint.set_name(host);
socketEndpoint.set_port(port);
shared_ptr<SocketHandler> clientSocket(new TcpSocketHandler());
shared_ptr<SocketHandler> clientPipeSocket(new PipeSocketHandler());

if (!ping(socketEndpoint, clientSocket)) {
cout << "Could not reach the ET server: " << host << ":" << port << endl;
cout << "Could not reach the ET server: " << socketEndpoint.name() << ":"
<< socketEndpoint.port() << endl;
exit(1);
}

int jport = result["jport"].as<int>();
string serverFifo = "";
if (result["serverfifo"].as<string>() != "") {
serverFifo = result["serverfifo"].as<string>();
}
string idpasskeypair = SshSetupHandler::SetupSsh(
username, host, host_alias, port, jumphost, jport,
username, destinationHost, host_alias, destinationPort, jumphost, jport,
result.count("x") > 0, result["v"].as<int>(),
result.count("prefix") ? result["prefix"].as<string>() : "");
result.count("prefix") ? result["prefix"].as<string>() : "",
serverFifo);

string id = "", passkey = "";
// Trim whitespace
Expand All @@ -227,7 +237,7 @@ int main(int argc, char** argv) {
console.reset(new PsuedoTerminalConsole());
}

TerminalClient terminalClient = TerminalClient(
TerminalClient terminalClient(
clientSocket, clientPipeSocket, socketEndpoint, id, passkey, console,
is_jumphost, result.count("t") ? result["t"].as<string>() : "",
result.count("r") ? result["r"].as<string>() : "", result.count("f"));
Expand Down
8 changes: 6 additions & 2 deletions src/terminal/TerminalMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ int main(int argc, char** argv) {
("v,verbose", "Enable verbose logging",
cxxopts::value<int>()->default_value("0")) //
("logtostdout", "Write log to stdout") //
("serverfifo",
"If set, connects to the etserver instance listening on the matching "
"fifo name", //
cxxopts::value<std::string>()->default_value(ROUTER_FIFO_NAME)) //
;

options.parse_positional({"host", "positional"});
Expand Down Expand Up @@ -135,7 +139,7 @@ int main(int argc, char** argv) {
LOG(FATAL) << "Error creating daemon: " << strerror(errno);
}
SocketEndpoint routerFifoEndpoint;
routerFifoEndpoint.set_name(ROUTER_FIFO_NAME);
routerFifoEndpoint.set_name(result["serverfifo"].as<string>());
SocketEndpoint destinationEndpoint;
destinationEndpoint.set_name(result["dsthost"].as<string>());
destinationEndpoint.set_port(result["dstport"].as<int>());
Expand Down Expand Up @@ -164,7 +168,7 @@ int main(int argc, char** argv) {
el::Helpers::installPreRollOutCallback(LogHandler::rolloutHandler);

SocketEndpoint routerEndpoint;
routerEndpoint.set_name(ROUTER_FIFO_NAME);
routerEndpoint.set_name(result["serverfifo"].as<string>());
UserTerminalHandler uth(ipcSocketHandler, term, true, routerEndpoint,
idpasskey);
cout << "IDPASSKEY:" << idpasskey << endl;
Expand Down
30 changes: 21 additions & 9 deletions src/terminal/TerminalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ void TerminalServer::run() {
maxCoreFd = max(maxCoreFd, terminalRouter->getServerFd());
numCoreFds++;

while (!halt) {
while (true) {
{
lock_guard<std::mutex> guard(terminalThreadMutex);
if (halt) {
break;
}
}
// Select blocks until there is something useful to do
fd_set rfds = coreFds;
int numFds = numCoreFds;
Expand Down Expand Up @@ -76,20 +82,24 @@ void TerminalServer::run() {
}

void TerminalServer::runJumpHost(
shared_ptr<ServerClientConnection> serverClientState) {
shared_ptr<ServerClientConnection> serverClientState,
const InitialPayload &payload) {
InitialResponse response;
serverClientState->writePacket(
Packet(uint8_t(EtPacketType::INITIAL_RESPONSE), protoToString(response)));
// set thread name
el::Helpers::setThreadName(serverClientState->getId());
bool run = true;

bool b[BUF_SIZE];
int terminalFd =
terminalRouter->getInfoForId(serverClientState->getId()).fd();
shared_ptr<SocketHandler> terminalSocketHandler =
terminalRouter->getSocketHandler();

terminalSocketHandler->writePacket(
terminalFd,
Packet(TerminalPacketType::JUMPHOST_INIT, protoToString(payload)));

while (!halt && run && !serverClientState->isShuttingDown()) {
fd_set rfd;
timeval tv;
Expand All @@ -108,18 +118,18 @@ void TerminalServer::runJumpHost(

try {
if (FD_ISSET(terminalFd, &rfd)) {
memset(b, 0, BUF_SIZE);
try {
Packet packet = terminalSocketHandler->readPacket(terminalFd);
serverClientState->writePacket(packet);
Packet packet;
if (terminalSocketHandler->readPacket(terminalFd, &packet)) {
serverClientState->writePacket(packet);
}
} catch (const std::runtime_error &ex) {
LOG(INFO) << "Terminal session ended" << ex.what();
run = false;
break;
}
}

VLOG(4) << "Jumphost serverclientFd: " << serverClientFd;
if (serverClientFd > 0 && FD_ISSET(serverClientFd, &rfd)) {
VLOG(4) << "Jumphost is selected";
if (serverClientState->hasData()) {
Expand Down Expand Up @@ -280,7 +290,7 @@ void TerminalServer::runTerminal(
if (!serverClientState->readPacket(&packet)) {
break;
}
char packetType = packet.getHeader();
uint8_t packetType = packet.getHeader();
if (packetType == et::TerminalPacketType::PORT_FORWARD_DATA ||
packetType ==
et::TerminalPacketType::PORT_FORWARD_DESTINATION_REQUEST ||
Expand Down Expand Up @@ -356,8 +366,10 @@ void TerminalServer::handleConnection(
}
InitialPayload payload = stringToProto<InitialPayload>(packet.getPayload());
if (payload.jumphost()) {
runJumpHost(serverClientState);
LOG(INFO) << "RUNNING JUMPHOST";
runJumpHost(serverClientState, payload);
} else {
LOG(INFO) << "RUNNING TERMINAL";
runTerminal(serverClientState, payload);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/terminal/TerminalServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ class TerminalServer : public ServerConnection {
std::shared_ptr<PipeSocketHandler> _pipeSocketHandler,
const SocketEndpoint &_routerEndpoint);
virtual ~TerminalServer();
void runJumpHost(shared_ptr<ServerClientConnection> serverClientState);
void runJumpHost(shared_ptr<ServerClientConnection> serverClientState,
const InitialPayload &payload);
void runTerminal(shared_ptr<ServerClientConnection> serverClientState,
const InitialPayload& payload);
const InitialPayload &payload);
void handleConnection(shared_ptr<ServerClientConnection> serverClientState);
virtual bool newClient(shared_ptr<ServerClientConnection> serverClientState);

Expand Down
Loading

0 comments on commit d6dc9dd

Please sign in to comment.