Skip to content

Commit

Permalink
Add N option (#205)
Browse files Browse the repository at this point in the history
* add -N option

* Cleanup cerr/cout
  • Loading branch information
Jason Gauci authored Jul 22, 2019
1 parent d7708ad commit 8ce6897
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/base/ClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void ClientConnection::pollReconnect() {
if (response.status() != RETURNING_CLIENT) {
LOG(ERROR) << "Error reconnecting to server: " << response.status()
<< ": " << response.error();
cerr << "Error reconnecting to server: " << response.status()
cout << "Error reconnecting to server: " << response.status()
<< ": " << response.error() << endl;
socketHandler->close(newSocketFd);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/base/TcpSocketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ set<int> TcpSocketHandler::listen(const SocketEndpoint &endpoint) {
LOG(ERROR) << "Error binding " << p->ai_family << "/" << p->ai_socktype
<< "/" << p->ai_protocol << ": " << errno << " "
<< strerror(errno);
cerr << "Error binding " << p->ai_family << "/" << p->ai_socktype << "/"
cout << "Error binding " << p->ai_family << "/" << p->ai_socktype << "/"
<< p->ai_protocol << ": " << errno << " " << strerror(errno) << endl;
stringstream oss;
oss << "Error binding port " << port << ": " << errno << " "
Expand Down
2 changes: 1 addition & 1 deletion src/terminal/PortForwardHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void PortForwardHandler::handlePacket(const Packet& packet,
PortForwardSourceResponse pfsresponse =
stringToProto<PortForwardSourceResponse>(packet.getPayload());
if (pfsresponse.has_error()) {
cerr << "FATAL: A reverse tunnel has failed (probably because someone "
cout << "FATAL: A reverse tunnel has failed (probably because someone "
"else is already using that port on the destination server"
<< endl;
LOG(FATAL) << "Reverse tunnel request failed: " << pfsresponse.error();
Expand Down
31 changes: 0 additions & 31 deletions src/terminal/StdIoBuffer.hpp

This file was deleted.

96 changes: 57 additions & 39 deletions src/terminal/TerminalClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ vector<pair<int, int>> parseRangesToPairs(const string& input) {

void TerminalClient::run(const string& command, const string& tunnels,
const string& reverseTunnels) {
console->setup();
if (console) {
console->setup();
}

shared_ptr<TcpSocketHandler> socketHandler =
static_pointer_cast<TcpSocketHandler>(connection->getSocketHandler());
Expand Down Expand Up @@ -141,22 +143,30 @@ void TerminalClient::run(const string& command, const string& tunnels,
}
}
} catch (const std::runtime_error& ex) {
cerr << "Error establishing port forward: " << ex.what() << endl;
cout << "Error establishing port forward: " << ex.what() << endl;
LOG(FATAL) << "Error establishing port forward: " << ex.what();
}

TerminalInfo lastTerminalInfo;

if (!console.get()) {
cout << "ET running, feel free to background..." << endl;
}

while (!shuttingDown && !connection->isShuttingDown()) {
// Data structures needed for select() and
// non-blocking I/O.
fd_set rfd;
timeval tv;

FD_ZERO(&rfd);
int consoleFd = console->getFd();
int maxfd = consoleFd;
FD_SET(consoleFd, &rfd);
int maxfd = -1;
int consoleFd = -1;
if (console) {
consoleFd = console->getFd();
maxfd = consoleFd;
FD_SET(consoleFd, &rfd);
}
int clientFd = connection->getSocketFd();
if (clientFd > 0) {
FD_SET(clientFd, &rfd);
Expand All @@ -168,28 +178,30 @@ void TerminalClient::run(const string& command, const string& tunnels,
select(maxfd + 1, &rfd, NULL, NULL, &tv);

try {
// Check for data to send.
if (FD_ISSET(consoleFd, &rfd)) {
// Read from stdin and write to our client that will then send it to the
// server.
VLOG(4) << "Got data from stdin";
int rc = read(consoleFd, b, BUF_SIZE);
FATAL_FAIL(rc);
if (rc > 0) {
// VLOG(1) << "Sending byte: " << int(b) << " " << char(b) << " " <<
// connection->getWriter()->getSequenceNumber();
string s(b, rc);
et::TerminalBuffer tb;
tb.set_buffer(s);
if (console) {
// Check for data to send.
if (FD_ISSET(consoleFd, &rfd)) {
// Read from stdin and write to our client that will then send it to
// the server.
VLOG(4) << "Got data from stdin";
int rc = read(consoleFd, b, BUF_SIZE);
FATAL_FAIL(rc);
if (rc > 0) {
// VLOG(1) << "Sending byte: " << int(b) << " " << char(b) << " " <<
// connection->getWriter()->getSequenceNumber();
string s(b, rc);
et::TerminalBuffer tb;
tb.set_buffer(s);

connection->writePacket(
Packet(TerminalPacketType::TERMINAL_BUFFER, protoToString(tb)));
keepaliveTime = time(NULL) + CLIENT_KEEP_ALIVE_DURATION;
connection->writePacket(
Packet(TerminalPacketType::TERMINAL_BUFFER, protoToString(tb)));
keepaliveTime = time(NULL) + CLIENT_KEEP_ALIVE_DURATION;
}
}
}

if (clientFd > 0 && FD_ISSET(clientFd, &rfd)) {
VLOG(4) << "Cliendfd is selected";
VLOG(4) << "Clientfd is selected";
while (connection->hasData()) {
VLOG(4) << "connection has data";
Packet packet;
Expand All @@ -213,16 +225,18 @@ void TerminalClient::run(const string& command, const string& tunnels,
}
switch (packetType) {
case et::TerminalPacketType::TERMINAL_BUFFER: {
VLOG(3) << "Got terminal buffer";
// Read from the server and write to our fake terminal
et::TerminalBuffer tb =
stringToProto<et::TerminalBuffer>(packet.getPayload());
const string& s = tb.buffer();
// VLOG(5) << "Got message: " << s;
// VLOG(1) << "Got byte: " << int(b) << " " << char(b) << " " <<
// connection->getReader()->getSequenceNumber();
keepaliveTime = time(NULL) + CLIENT_KEEP_ALIVE_DURATION;
console->write(s);
if (console) {
VLOG(3) << "Got terminal buffer";
// Read from the server and write to our fake terminal
et::TerminalBuffer tb =
stringToProto<et::TerminalBuffer>(packet.getPayload());
const string& s = tb.buffer();
// VLOG(5) << "Got message: " << s;
// VLOG(1) << "Got byte: " << int(b) << " " << char(b) << " " <<
// connection->getReader()->getSequenceNumber();
keepaliveTime = time(NULL) + CLIENT_KEEP_ALIVE_DURATION;
console->write(s);
}
break;
}
case et::TerminalPacketType::KEEP_ALIVE:
Expand Down Expand Up @@ -254,13 +268,15 @@ void TerminalClient::run(const string& command, const string& tunnels,
waitingOnKeepalive = false;
}

TerminalInfo ti = console->getTerminalInfo();
if (console) {
TerminalInfo ti = console->getTerminalInfo();

if (ti != lastTerminalInfo) {
LOG(INFO) << "Window size changed: " << ti.DebugString();
lastTerminalInfo = ti;
connection->writePacket(
Packet(TerminalPacketType::TERMINAL_INFO, protoToString(ti)));
if (ti != lastTerminalInfo) {
LOG(INFO) << "Window size changed: " << ti.DebugString();
lastTerminalInfo = ti;
connection->writePacket(
Packet(TerminalPacketType::TERMINAL_INFO, protoToString(ti)));
}
}

vector<PortForwardDestinationRequest> requests;
Expand All @@ -285,7 +301,9 @@ void TerminalClient::run(const string& command, const string& tunnels,
shuttingDown = true;
}
}
console->teardown();
if (console) {
console->teardown();
}
cout << "Session terminated" << endl;
}
} // namespace et
8 changes: 6 additions & 2 deletions src/terminal/TerminalClientMain.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "TerminalClient.hpp"

#include "PsuedoTerminalConsole.hpp"
#include "ParseConfigFile.hpp"
#include "PsuedoTerminalConsole.hpp"

using namespace et;
namespace google {}
Expand Down Expand Up @@ -33,6 +33,7 @@ DEFINE_bool(silent, false, "If enabled, disable logging");
DEFINE_bool(noratelimit, false,
"There's 1024 lines/second limit, which can be "
"disabled based on different use case.");
DEFINE_bool(N, false, "Do not create a terminal");

using namespace et;

Expand Down Expand Up @@ -196,7 +197,10 @@ int main(int argc, char** argv) {
SocketEndpoint socketEndpoint =
SocketEndpoint(FLAGS_host, FLAGS_port, is_jumphost);
shared_ptr<SocketHandler> clientSocket(new TcpSocketHandler());
shared_ptr<Console> console(new PsuedoTerminalConsole());
shared_ptr<Console> console;
if (!FLAGS_N) {
console.reset(new PsuedoTerminalConsole());
}

TerminalClient terminalClient =
TerminalClient(clientSocket, socketEndpoint, id, passkey, console);
Expand Down
4 changes: 2 additions & 2 deletions src/terminal/TerminalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void TerminalServer::runJumpHost(
}
} catch (const runtime_error &re) {
LOG(ERROR) << "Jumphost Error: " << re.what();
cerr << "ERROR: " << re.what();
cout << "ERROR: " << re.what();
serverClientState->closeSocket();
}
}
Expand Down Expand Up @@ -277,7 +277,7 @@ void TerminalServer::runTerminal(
}
} catch (const runtime_error &re) {
LOG(ERROR) << "Error: " << re.what();
cerr << "Error: " << re.what();
cout << "Error: " << re.what();
serverClientState->closeSocket();
// If the client disconnects the session, it shouldn't end
// because the client may be starting a new one. TODO: Start a
Expand Down

0 comments on commit 8ce6897

Please sign in to comment.