From 75dc93ea6d3c7cc4ab3776e14d0fd2998fc1f77b Mon Sep 17 00:00:00 2001 From: enWILLYado Date: Wed, 31 Jan 2018 23:47:17 +0100 Subject: [PATCH] Include proxy (CONNECT) The 'config.json' and the '-o' command option allow to include a proxy: poolHost[:poolPort[@proxyHost[:proxyPort]]. --- src/config.json | 10 +++++++++- src/net/Client.cpp | 42 +++++++++++++++++++++++++++++++++++++++--- src/net/Client.h | 2 ++ src/net/Url.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- src/net/Url.h | 12 ++++++++++-- 5 files changed, 100 insertions(+), 9 deletions(-) diff --git a/src/config.json b/src/config.json index 7813d4b9..07570d8b 100644 --- a/src/config.json +++ b/src/config.json @@ -6,6 +6,7 @@ "cpu-affinity": null, // set process affinity to CPU core(s), mask "0x3" for cores 0 and 1 "cpu-priority": null, // set process priority (0 idle, 2 normal to 5 highest) "donate-level": 5, // donate level, mininum 1% + "debug": false, "log-file": null, // log all output to a file, example: "c:/some/path/xmrig.log" "max-cpu-usage": 75, // maximum CPU usage for automatic mode, usually limiting factor is CPU cache not this option. "print-time": 60, // print hashrate report every N seconds @@ -21,7 +22,14 @@ "pass": "x", // password for mining server "keepalive": true, // send keepalived for prevent timeout (need pool support) "nicehash": false // enable nicehash/xmrig-proxy support - } + }, + { + "url": "pool.minemonero.pro:5555@localhost:8080",// URL of mining server with localhost proxy (example) + "user": "", // username for mining server + "pass": "x", // password for mining server + "keepalive": true, // send keepalived for prevent timeout (need pool support) + "nicehash": false // enable nicehash/xmrig-proxy support + } ], "api": { "port": 0, // port for the miner API https://github.com/xmrig/xmrig/wiki/API diff --git a/src/net/Client.cpp b/src/net/Client.cpp index 8e8ebcfd..56a47329 100644 --- a/src/net/Client.cpp +++ b/src/net/Client.cpp @@ -274,7 +274,7 @@ int Client::resolve(const char *host) int64_t Client::send(size_t size) { LOG_DEBUG("[%s:%u] send (%d bytes): \"%s\"", m_url.host(), m_url.port(), size, m_sendBuf); - if (state() != ConnectedState || !uv_is_writable(m_stream)) { + if ((state() != ConnectedState && state() != ProxingState) || !uv_is_writable(m_stream)) { LOG_DEBUG_ERR("[%s:%u] send failed, invalid state: %d", m_url.host(), m_url.port(), m_state); return -1; } @@ -328,6 +328,27 @@ void Client::connect(struct sockaddr *addr) uv_tcp_connect(req, m_socket, reinterpret_cast(addr), Client::onConnect); } +void Client::prelogin() +{ + if (m_url.proxyHost()) + { + setState (ProxingState); + const std::string buffer = std::string ("CONNECT ") + m_url.finalHost() + ":" + std::to_string(m_url.finalPort()) + " HTTP/1.1\n"; + + const size_t size = buffer.size(); + memcpy (m_sendBuf, buffer.c_str(), size); + m_sendBuf[size] = '\n'; + m_sendBuf[size + 1] = '\0'; + + LOG_DEBUG("Prelogin send (%d bytes): \"%s\"", size, m_sendBuf); + send (size + 1); + } + else + { + setState (ConnectedState); + login(); + } +} void Client::login() { @@ -565,12 +586,11 @@ void Client::onConnect(uv_connect_t *req, int status) client->m_stream = static_cast(req->handle); client->m_stream->data = req->data; - client->setState(ConnectedState); uv_read_start(client->m_stream, Client::onAllocBuffer, Client::onRead); delete req; - client->login(); + client->prelogin(); } @@ -589,6 +609,22 @@ void Client::onRead(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) return client->close(); } + if (client->state() == ProxingState) + { + const char* const content = buf->base; + LOG_DEBUG("[%s:%u] received from proxy (%d bytes): \"%s\"", + client->m_url.host(), client->m_url.port(), + nread, content); + + if(content == strstr(content, "HTTP/1.1 200")) + { + LOG_INFO("[%s:%u] Proxy connected to %s:%u!", client->m_url.host(), client->m_url.port(), client->m_url.finalHost(), client->m_url.finalPort()); + client->setState(ConnectedState); + client->login(); + } + return; + } + client->m_recvBufPos += nread; char* end; diff --git a/src/net/Client.h b/src/net/Client.h index 699f4903..c61ccd10 100644 --- a/src/net/Client.h +++ b/src/net/Client.h @@ -46,6 +46,7 @@ public: UnconnectedState, HostLookupState, ConnectingState, + ProxingState, ConnectedState, ClosingState }; @@ -81,6 +82,7 @@ private: int64_t send(size_t size); void close(); void connect(struct sockaddr *addr); + void prelogin(); void login(); void parse(char *line, size_t len); void parseNotification(const char *method, const rapidjson::Value ¶ms, const rapidjson::Value &error); diff --git a/src/net/Url.cpp b/src/net/Url.cpp index dcbe82af..f45f94bb 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -41,7 +41,9 @@ Url::Url() : m_host(nullptr), m_password(nullptr), m_user(nullptr), - m_port(kDefaultPort) + m_port(kDefaultPort), + m_proxy_host(nullptr), + m_proxy_port(kDefaultProxyPort) { } @@ -63,7 +65,9 @@ Url::Url(const char *url) : m_host(nullptr), m_password(nullptr), m_user(nullptr), - m_port(kDefaultPort) + m_port(kDefaultPort), + m_proxy_host (nullptr), + m_proxy_port (kDefaultProxyPort) { parse(url); } @@ -74,7 +78,9 @@ Url::Url(const char *host, uint16_t port, const char *user, const char *password m_nicehash(nicehash), m_password(password ? strdup(password) : nullptr), m_user(user ? strdup(user) : nullptr), - m_port(port) + m_port(port), + m_proxy_host (nullptr), + m_proxy_port (kDefaultProxyPort) { m_host = strdup(host); } @@ -116,7 +122,27 @@ bool Url::parse(const char *url) memcpy(m_host, base, size - 1); m_host[size - 1] = '\0'; + const char* proxy = strchr(port, '@'); m_port = (uint16_t) strtol(port, nullptr, 10); + if (!proxy) { + m_port = (uint16_t) strtol(port, nullptr, 10); + return true; + } + + ++proxy; + + const char* proxyport = strchr(proxy, ':'); + if (!port) { + m_proxy_host = strdup(proxy); + return false; + } + + const size_t proxysize = proxyport++ - proxy + 1; + m_proxy_host = static_cast(malloc (proxysize)); + memcpy(m_proxy_host, proxy, proxysize - 1); + m_proxy_host[proxysize - 1] = '\0'; + m_proxy_port = (uint16_t) strtol(proxyport, nullptr, 10); + return true; } @@ -183,10 +209,21 @@ Url &Url::operator=(const Url *other) m_keepAlive = other->m_keepAlive; m_nicehash = other->m_nicehash; m_port = other->m_port; + m_proxy_port = other->m_proxy_port; free(m_host); m_host = strdup(other->m_host); + free (m_proxy_host); + if(other->m_proxy_host) + { + m_proxy_host = strdup (other->m_proxy_host); + } + else + { + m_proxy_host = nullptr; + } + setPassword(other->m_password); setUser(other->m_user); diff --git a/src/net/Url.h b/src/net/Url.h index a1982300..4c8b7d16 100644 --- a/src/net/Url.h +++ b/src/net/Url.h @@ -34,6 +34,7 @@ public: constexpr static const char *kDefaultPassword = "x"; constexpr static const char *kDefaultUser = "x"; constexpr static uint16_t kDefaultPort = 3333; + constexpr static uint16_t kDefaultProxyPort = 8080; Url(); Url(const char *url); @@ -43,10 +44,15 @@ public: inline bool isKeepAlive() const { return m_keepAlive; } inline bool isNicehash() const { return m_nicehash; } inline bool isValid() const { return m_host && m_port > 0; } - inline const char *host() const { return m_host; } + inline const char *host() const { return isProxyed() ? proxyHost() : finalHost(); } inline const char *password() const { return m_password ? m_password : kDefaultPassword; } inline const char *user() const { return m_user ? m_user : kDefaultUser; } - inline uint16_t port() const { return m_port; } + inline uint16_t port() const { return isProxyed() ? proxyPort() : finalPort(); } + inline bool isProxyed() const { return proxyHost(); } + inline const char* finalHost() const { return m_host; } + inline uint16_t finalPort() const { return m_port;} + inline const char* proxyHost() const { return m_proxy_host; } + inline uint16_t proxyPort() const { return m_proxy_port; } inline void setKeepAlive(bool keepAlive) { m_keepAlive = keepAlive; } inline void setNicehash(bool nicehash) { m_nicehash = nicehash; } @@ -65,6 +71,8 @@ private: char *m_password; char *m_user; uint16_t m_port; + char* m_proxy_host; + uint16_t m_proxy_port; }; #endif /* __URL_H__ */