|
@@ -91,9 +91,7 @@ session::~session() {
|
|
|
void session::parse_auth(void) {
|
|
|
// how many nulls should I be seeing?
|
|
|
// \0user\0pass\0terminal/SPEED\0
|
|
|
- // Maybe in the future I'll care about parsing this out. I don't right now.
|
|
|
-
|
|
|
- // Ok, yes I do! If I don't have a proper rlogin value here, it isn't going
|
|
|
+ // If I don't have a proper rlogin value here, it isn't going
|
|
|
// to work when I try to connect to the rlogin server.
|
|
|
|
|
|
if (rlogin_auth.size() > 10)
|
|
@@ -123,36 +121,59 @@ void session::on_connect(const boost::system::error_code error) {
|
|
|
server_read();
|
|
|
} else {
|
|
|
// TODO:
|
|
|
- std::string output = "Failed to connect : ";
|
|
|
- output += host;
|
|
|
- output += " : ";
|
|
|
- output += port;
|
|
|
- output += "\n\r";
|
|
|
+ std::string output =
|
|
|
+ str(boost::format("Failed to connect : %1% : %2%\n\r") % host % port);
|
|
|
to_client(output);
|
|
|
BOOST_LOG_TRIVIAL(error) << "Failed to connec to " << host << ":" << port;
|
|
|
-
|
|
|
- std::cout << "SHUTDOWN..." << std::endl;
|
|
|
+ BOOST_LOG_TRIVIAL(warning) << "socket.shutdown()";
|
|
|
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void session::dispatch_line(std::string line) {
|
|
|
// Does this have \n\r still on it? I don't want them.
|
|
|
- // BOOST_LOG_NAMED_SCOPE("session");
|
|
|
+
|
|
|
std::string temp = clean_string(line);
|
|
|
BOOST_LOG_TRIVIAL(info) << "SL: " << temp; // clean_string(line);
|
|
|
- // std::cout << "SL: " << line << std::endl;
|
|
|
- // is echo on? if so:
|
|
|
}
|
|
|
|
|
|
-void session::process_lines(void) {
|
|
|
+/*
|
|
|
+Call this with whatever we just received.
|
|
|
+
|
|
|
+That will allow me to send "just whatever I got"
|
|
|
+this time around, rather then trying to figure out
|
|
|
+what was just added to server_prompt.
|
|
|
+
|
|
|
+What about \r, \b ? Should that "reset" the server_prompt?
|
|
|
+
|
|
|
+ */
|
|
|
+void session::process_lines(std::string &received) {
|
|
|
// break server_prompt into lines and send/process one by one.
|
|
|
- size_t pos;
|
|
|
- // while ((pos = server_prompt.find("\n\r", 0, 2)) != std::string::npos) {
|
|
|
- // while ((pos = server_prompt.find("\r\n", 0, 2)) != std::string::npos) {
|
|
|
+
|
|
|
+ size_t pos, rpos;
|
|
|
+ server_prompt.append(received);
|
|
|
+
|
|
|
while ((pos = server_prompt.find('\n', 0)) != std::string::npos) {
|
|
|
- // line
|
|
|
- std::string line = server_prompt.substr(0, pos + 1);
|
|
|
+
|
|
|
+ std::string line;
|
|
|
+
|
|
|
+ // process "line" in received
|
|
|
+
|
|
|
+ rpos = received.find('\n', 0);
|
|
|
+
|
|
|
+ // get line to send to the client
|
|
|
+ if (show_client) {
|
|
|
+ // that is, if we're sending to the client!
|
|
|
+ line = received.substr(0, rpos + 1);
|
|
|
+ std::string clean = clean_string(line);
|
|
|
+ BOOST_LOG_TRIVIAL(error) << "rpos/show_client:" << clean;
|
|
|
+ to_client(line);
|
|
|
+ }
|
|
|
+ received = received.substr(rpos + 1);
|
|
|
+
|
|
|
+ // process "line" in server_prompt
|
|
|
+
|
|
|
+ line = server_prompt.substr(0, pos + 1);
|
|
|
server_prompt = server_prompt.substr(pos + 1);
|
|
|
|
|
|
// Remove \n for dispatching
|
|
@@ -163,33 +184,36 @@ void session::process_lines(void) {
|
|
|
};
|
|
|
|
|
|
// display on?
|
|
|
- to_client(line);
|
|
|
+ // to_client(line);
|
|
|
|
|
|
+ // NOTE: We get "TradeWars Game Server\n" (Missing \r)
|
|
|
+ // We add the \r with our injection line.
|
|
|
+
|
|
|
+ // TODO(stevet): MOVE TO DEFAULT dispatcher
|
|
|
// our first injection
|
|
|
if (line.find("TradeWars Game Server") != std::string::npos) {
|
|
|
to_client("\rTradeWars Proxy v2++ READY (~ to activate)\n\r");
|
|
|
}
|
|
|
|
|
|
- replace(part, "\r", "");
|
|
|
+ // How should I handle \r in lines? For now, remove it
|
|
|
+ // but LOG that we did.
|
|
|
+ if (replace(part, "\r", "")) {
|
|
|
+ BOOST_LOG_TRIVIAL(warning) << "\\r removed from line";
|
|
|
+ }
|
|
|
+
|
|
|
dispatch_line(part);
|
|
|
}
|
|
|
|
|
|
- // display on?
|
|
|
- if (server_sent != 0) {
|
|
|
- // send partial
|
|
|
- std::string part = server_prompt.substr(server_sent);
|
|
|
- to_client(part);
|
|
|
- server_sent = server_prompt.size();
|
|
|
- } else {
|
|
|
- // send all
|
|
|
- if (!server_prompt.empty()) {
|
|
|
- to_client(server_prompt);
|
|
|
- server_sent = server_prompt.size();
|
|
|
- }
|
|
|
- }
|
|
|
+ // Ok, we have sent all of the \n lines.
|
|
|
|
|
|
- // server_sent is the # of chars we've already sent of this.
|
|
|
+ if (!received.empty())
|
|
|
+ if (show_client) {
|
|
|
+ to_client(received);
|
|
|
+ std::string clean = clean_string(received);
|
|
|
+ BOOST_LOG_TRIVIAL(error) << "show_client/leftovers:" << clean;
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
void session::server_read(void) {
|
|
|
auto self(shared_from_this());
|
|
|
|
|
@@ -200,8 +224,14 @@ void session::server_read(void) {
|
|
|
if (!ec) {
|
|
|
server_buffer[length] = 0;
|
|
|
|
|
|
- server_prompt.append(server_buffer, length);
|
|
|
- process_lines();
|
|
|
+ // server_prompt.append(server_buffer, length);
|
|
|
+ std::string received(server_buffer, length);
|
|
|
+ process_lines(received);
|
|
|
+
|
|
|
+ /*
|
|
|
+ I don't believe I need to consume this,
|
|
|
+ I'm not async_reading from a stream.
|
|
|
+ */
|
|
|
|
|
|
/*
|
|
|
if (length) {
|
|
@@ -213,10 +243,8 @@ void session::server_read(void) {
|
|
|
|
|
|
server_read();
|
|
|
} else {
|
|
|
- std::cout << "S: read_failed: connection closed" << std::endl;
|
|
|
+ BOOST_LOG_TRIVIAL(warning) << "S: read_failed: socket.shutdown()";
|
|
|
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
|
|
- // socket_.async_shutdown(boost::bind(&session::on_shutdown, this,
|
|
|
- // boost::asio::placeholders::error));
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -239,11 +267,10 @@ void session::on_resolve(
|
|
|
// TO DO:
|
|
|
// BOOST_LOG_NAMED_SCOPE("session");
|
|
|
BOOST_LOG_TRIVIAL(error) << "Unable to resolve: " << host;
|
|
|
- std::string output = "Unable to resolve: ";
|
|
|
- output += host;
|
|
|
- output += "\n\r";
|
|
|
+ std::string output =
|
|
|
+ str(boost::format("Unable to resolve: %1%\n\r") % host);
|
|
|
to_client(output);
|
|
|
- std::cout << "SHUTDOWN ..." << std::endl;
|
|
|
+ BOOST_LOG_TRIVIAL(warning) << "socket.shutdown()";
|
|
|
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
|
|
}
|
|
|
}
|
|
@@ -284,39 +311,46 @@ void session::do_read(void) {
|
|
|
boost::asio::placeholders::iterator));
|
|
|
|
|
|
} else if (length) {
|
|
|
- // std::cout << length << std::endl;
|
|
|
-
|
|
|
// Proxy Active?
|
|
|
// BOOST_LOG_NAMED_SCOPE("session");
|
|
|
|
|
|
- to_server(read_buffer);
|
|
|
+ if (talk_direct)
|
|
|
+ to_server(read_buffer);
|
|
|
BOOST_LOG_TRIVIAL(info) << "C: " << read_buffer;
|
|
|
|
|
|
// do_write(output);
|
|
|
}
|
|
|
do_read();
|
|
|
} else {
|
|
|
- std::cout << "C: read_failed: connection closed" << std::endl;
|
|
|
- if (connected)
|
|
|
+ BOOST_LOG_TRIVIAL(warning) << "C: read_failed";
|
|
|
+ if (connected) {
|
|
|
+ BOOST_LOG_TRIVIAL(warning) << "server.shutdown()";
|
|
|
server_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
|
|
- // server_.async_shutdown(boost::bind(&session::on_shutdown, this,
|
|
|
- // boost::asio::placeholders::error));
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
void session::to_client(std::string message) {
|
|
|
auto self(shared_from_this());
|
|
|
+ // output the cleaned string (so I can see what we're sending in the
|
|
|
+ // logs)
|
|
|
+
|
|
|
+ std::string clean = clean_string(message);
|
|
|
+ BOOST_LOG_TRIVIAL(error) << "C: >>" << clean;
|
|
|
+
|
|
|
boost::asio::async_write(
|
|
|
socket_, boost::asio::buffer(message),
|
|
|
[this, self](boost::system::error_code ec, std::size_t /*length*/) {
|
|
|
if (!ec) {
|
|
|
|
|
|
} else {
|
|
|
- std::cout << "write failed? closed?" << std::endl;
|
|
|
- server_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
|
|
- // server_.async_shutdown(boost::bind(&session::on_shutdown, this,
|
|
|
- // boost::asio::placeholders::error));
|
|
|
+ BOOST_LOG_TRIVIAL(warning)
|
|
|
+ << "C: write failed? closed? server.shutdown()";
|
|
|
+ if (connected) {
|
|
|
+ BOOST_LOG_TRIVIAL(warning) << "server.shutdown()";
|
|
|
+ server_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -329,17 +363,18 @@ void session::to_server(std::string message) {
|
|
|
if (!ec) {
|
|
|
|
|
|
} else {
|
|
|
- std::cout << "write failed? closed?" << std::endl;
|
|
|
+ BOOST_LOG_TRIVIAL(warning)
|
|
|
+ << "S: write failed? closed? socket.shutdown()";
|
|
|
socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both);
|
|
|
- // socket_.async_shutdown(boost::bind(&session::on_shutdown, this,
|
|
|
- // boost::asio::placeholders::error));
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
void session::on_shutdown(boost::system::error_code ec) {
|
|
|
std::cout << "shutdown." << std::endl;
|
|
|
}
|
|
|
+*/
|
|
|
|
|
|
server::server(boost::asio::io_service &io_service,
|
|
|
const boost::asio::ip::tcp::endpoint &endpoint, std::string host,
|
|
@@ -360,6 +395,7 @@ void server::do_accept(void) {
|
|
|
acceptor_.async_accept([this](boost::system::error_code ec,
|
|
|
boost::asio::ip::tcp::socket socket) {
|
|
|
if (!ec) {
|
|
|
+ BOOST_LOG_TRIVIAL(info) << "server::do_accept()";
|
|
|
std::make_shared<session>(std::move(socket), io_service_, host_, port_)
|
|
|
->start();
|
|
|
}
|