From 4de65da79a1ca86b3ae29c275e75a9c841e59eb1 Mon Sep 17 00:00:00 2001 From: Intel Date: Mon, 5 May 2014 17:04:38 -0400 Subject: [PATCH] Redirect for getwork --- src/server/poolserver/Main.cpp | 3 ++- src/server/poolserver/Stratum/Client.cpp | 11 +++++++++++ src/server/poolserver/Stratum/Client.h | 11 +++++++++++ src/server/poolserver/Stratum/Server.h | 4 ++-- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/server/poolserver/Main.cpp b/src/server/poolserver/Main.cpp index ac36887..417b6f0 100644 --- a/src/server/poolserver/Main.cpp +++ b/src/server/poolserver/Main.cpp @@ -43,7 +43,8 @@ bool InitConfig(int argc, char *argv[]) // Stratum descStratum.add_options() - ("StratumHost,sh", boost::program_options::value()->default_value("0.0.0.0"), "Stratum server host") + ("StratumHost,sh", boost::program_options::value()->default_value("0.0.0.0"), "Bind IP for stratum") + ("StratumRedirectHost", boost::program_options::value()->default_value("0.0.0.0"), "Where to redirect getwork requests") ("StratumPort,sp", boost::program_options::value()->default_value(3333), "Stratum server port") ("StratumBlockCheckTime", boost::program_options::value()->default_value(2000), "Time between block checks in ms") ("RetargetInterval", boost::program_options::value()->default_value(20), "Time between difficulty checks in seconds") diff --git a/src/server/poolserver/Stratum/Client.cpp b/src/server/poolserver/Stratum/Client.cpp index e8b4926..697cba5 100644 --- a/src/server/poolserver/Stratum/Client.cpp +++ b/src/server/poolserver/Stratum/Client.cpp @@ -1,5 +1,6 @@ #include "Server.h" #include "Client.h" +#include "Config.h" #include "BigNum.h" #include "DataMgr.h" #include "ShareLimiter.h" @@ -356,11 +357,21 @@ namespace Stratum char c; while (is.get(c)) { if (c == '\n') { + sLog.Debug(LOG_STRATUM, "Received message: %s", _recvMessage.c_str()); + + // Redirect getwork + if (!_subscribed) { + if (_recvMessage.compare("POST / HTTP/1.1\r") == 0) { + RedirectGetwork(); + } + } + try { OnMessage(JSON::FromString(_recvMessage)); } catch (std::exception& e) { sLog.Error(LOG_SERVER, "Exception caught while parsing json: %s", e.what()); } + _recvMessage.clear(); } else _recvMessage += c; diff --git a/src/server/poolserver/Stratum/Client.h b/src/server/poolserver/Stratum/Client.h index d7b6e8a..cb85372 100644 --- a/src/server/poolserver/Stratum/Client.h +++ b/src/server/poolserver/Stratum/Client.h @@ -135,6 +135,17 @@ namespace Stratum void Ban(uint32 time); void Disconnect(); + void RedirectGetwork() + { + sLog.Info(LOG_STRATUM, "Sending redirect to stratum for client %u", _ip); + std::string redirect(Util::FS("HTTP/1.1 200 OK\r\nX-Stratum: stratum+tcp://%s:%u\r\nConnection: Close\r\nContent-Length: 41\r\n\r\n{\"error\": null, \"result\": false, \"id\": 0}\n", sConfig.Get("StratumRedirectHost").c_str(), sConfig.Get("StratumPort"))); + boost::asio::async_write( + _socket, + boost::asio::buffer(redirect.c_str(), redirect.length()), + _ioStrand.wrap(boost::bind(&Client::_OnSend, shared_from_this(), boost::asio::placeholders::error))); + Disconnect(); + } + void CloseSocket() { boost::system::error_code ec; diff --git a/src/server/poolserver/Stratum/Server.h b/src/server/poolserver/Stratum/Server.h index a77246a..0589076 100644 --- a/src/server/poolserver/Stratum/Server.h +++ b/src/server/poolserver/Stratum/Server.h @@ -133,6 +133,8 @@ namespace Stratum void _OnAccept(ClientPtr client, const boost::system::error_code& error) { + _StartAccept(); + if (!error) { if (client->Start()) { _clients.insert(client); @@ -141,8 +143,6 @@ namespace Stratum } else { sLog.Debug(LOG_STRATUM, "Failed to accept stratum client"); } - - _StartAccept(); } private: