From f1fa9627973405d2692b4c79822e21af8c8c9638 Mon Sep 17 00:00:00 2001 From: SChernykh <15806605+SChernykh@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:14:43 +0200 Subject: [PATCH] Stratum: detect HTTP and send a response --- src/stratum_server.cpp | 28 ++++++++++++++++++++++++++++ src/stratum_server.h | 2 ++ src/tls.cpp | 6 +++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index 5e515e59..cf03f910 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -1167,11 +1167,20 @@ bool StratumServer::StratumClient::on_read(char* data, uint32_t size) char* line_start = m_stratumReadBuf; for (char *e = line_start + m_stratumReadBufBytes, *c = e - size; c < e; ++c) { if (*c == '\n') { + // Check if the line starts with "GET " (an HTTP request) + if ((c - line_start >= sizeof(uint32_t)) && (*reinterpret_cast(line_start) == 0x20544547U)) { + LOGINFO(5, "client " << log::Gray() << static_cast(m_addrString) << log::NoColor() << " sent an HTTP request"); + send_http_response(); + close(); + return true; + } + *c = '\0'; if (!process_request(line_start, static_cast(c - line_start))) { ban(DEFAULT_BAN_TIME); return false; } + line_start = c + 1; } } @@ -1365,6 +1374,25 @@ bool StratumServer::StratumClient::process_submit(rapidjson::Document& doc, uint return static_cast(m_owner)->on_submit(this, id, job_id.GetString(), nonce.GetString(), result.GetString()); } +bool StratumServer::StratumClient::send_http_response() +{ + return m_owner->send(this, [](uint8_t *buf, size_t buf_size) -> size_t { + static constexpr uint8_t data[] = + "HTTP/1.1 200 OK\r\n" + "Content-Length: 21\r\n" + "Content-Type: text/html\r\n" + "Connection: Closed\r\n\r\n" + "P2Pool Stratum online"; + + if (buf_size < sizeof(data)) { + return 0; + } + + memcpy(buf, data, sizeof(data)); + return sizeof(data); + }); +} + void StratumServer::api_update_local_stats(uint64_t timestamp) { if (!m_pool->api() || !m_pool->params().m_localStats || m_pool->stopped()) { diff --git a/src/stratum_server.h b/src/stratum_server.h index c9c137e7..ed094405 100644 --- a/src/stratum_server.h +++ b/src/stratum_server.h @@ -52,6 +52,8 @@ class StratumServer : public TCPServer [[nodiscard]] bool process_login(rapidjson::Document& doc, uint32_t id); [[nodiscard]] bool process_submit(rapidjson::Document& doc, uint32_t id); + bool send_http_response(); + alignas(8) char m_rawReadBuf[STRATUM_BUF_SIZE]; alignas(8) char m_stratumReadBuf[STRATUM_BUF_SIZE]; diff --git a/src/tls.cpp b/src/tls.cpp index aa9e5f29..5d4aa5e7 100644 --- a/src/tls.cpp +++ b/src/tls.cpp @@ -228,9 +228,6 @@ bool ServerTls::on_read_internal(char* data, uint32_t size, ReadCallback::Base&& return false; } - int bytes_read = 0; - char buf[1024]; - if (!SSL_is_init_finished(ssl)) { const int result = SSL_do_handshake(ssl); @@ -274,6 +271,9 @@ bool ServerTls::on_read_internal(char* data, uint32_t size, ReadCallback::Base&& } } + int bytes_read; + char buf[1024]; + while ((bytes_read = SSL_read(ssl, buf, sizeof(buf))) > 0) { if (!read_callback(buf, static_cast(bytes_read))) { return false;