From 1bb112b75159b178a5c9a8f7efbdb33127b30fb0 Mon Sep 17 00:00:00 2001 From: Aiwe Date: Wed, 6 Jul 2022 12:30:20 +0300 Subject: [PATCH 1/7] Replace httpCient with httplib's client --- CMakeLists.txt | 3 ++- src/CryptoNoteWrapper.cpp | 19 ++++++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 915705c74..e7a55e75f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -253,11 +253,11 @@ set(CRYPTONOTE_SOURCES cryptonote/src/System/Ipv4Address.cpp cryptonote/src/System/SocketStream.cpp cryptonote/src/System/TcpStream.cpp + cryptonote/src/HTTP/httplib.h cryptonote/src/HTTP/HttpRequest.cpp cryptonote/src/HTTP/HttpParser.cpp cryptonote/src/HTTP/HttpParserErrorCodes.cpp cryptonote/src/HTTP/HttpResponse.cpp - cryptonote/src/Rpc/HttpClient.cpp cryptonote/src/Rpc/JsonRpc.cpp cryptonote/src/P2p/NetNode.cpp cryptonote/src/P2p/LevinProtocol.cpp @@ -506,6 +506,7 @@ if (WIN32) endif () if (OPENSSL_FOUND) + add_definitions(-DCPPHTTPLIB_OPENSSL_SUPPORT) target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES}) if (MSVC) target_link_libraries(${PROJECT_NAME} Rpcrt4 ws2_32 advapi32 crypt32 gdi32 user32) diff --git a/src/CryptoNoteWrapper.cpp b/src/CryptoNoteWrapper.cpp index 7f5a2b304..41cb92f06 100644 --- a/src/CryptoNoteWrapper.cpp +++ b/src/CryptoNoteWrapper.cpp @@ -20,8 +20,9 @@ #include "CryptoNoteCore/Miner.h" #include "CryptoNoteCore/MinerConfig.h" #include "CryptoNoteCore/TransactionExtra.h" +#include "HTTP/httplib.h" #include "Rpc/CoreRpcServerCommandsDefinitions.h" -#include "Rpc/HttpClient.h" +#include "Rpc/JsonRpc.h" #include "CryptoNoteProtocol/CryptoNoteProtocolHandler.h" #include "InProcessNode/InProcessNode.h" #include "P2p/NetNode.h" @@ -214,8 +215,8 @@ class RpcNode : public CryptoNote::INodeObserver, public CryptoNote::INodeRpcPro CryptoNote::COMMAND_RPC_GETBLOCKTEMPLATE::response rsp = AUTO_VAL_INIT(rsp); req.miner_spend_key = Common::podToHex(acc.spendSecretKey); req.miner_view_key = Common::podToHex(acc.viewSecretKey); - CryptoNote::HttpClient httpClient(m_dispatcher, m_node.m_nodeHost, m_node.m_nodePort, false); - CryptoNote::invokeJsonRpcCommand(httpClient, "getblocktemplate", req, rsp); + httplib::Client httpClient (m_node.m_nodeHost, m_node.m_nodePort); + CryptoNote::JsonRpc::invokeJsonRpcCommand(httpClient, "getblocktemplate", req, rsp); std::string err = interpret_rpc_response(true, rsp.status); if (err.empty()) { if (!CryptoNote::fromBinaryArray(b, Common::fromHex(rsp.blocktemplate_blob))) { @@ -231,10 +232,6 @@ class RpcNode : public CryptoNote::INodeObserver, public CryptoNote::INodeRpcPro m_logger(Logging::INFO) << "Failed to invoke request: " << err; } } - catch (const CryptoNote::ConnectException&) { - m_logger(Logging::INFO) << "Wallet failed to connect to daemon."; - return false; - } catch (const std::exception& e) { m_logger(Logging::INFO) << "Failed to invoke RPC method: " << e.what(); return false; @@ -248,8 +245,8 @@ class RpcNode : public CryptoNote::INodeObserver, public CryptoNote::INodeRpcPro CryptoNote::COMMAND_RPC_SUBMITBLOCK::request req; req.emplace_back(Common::toHex(CryptoNote::toBinaryArray(b))); CryptoNote::COMMAND_RPC_SUBMITBLOCK::response res; - CryptoNote::HttpClient httpClient(m_dispatcher, m_node.m_nodeHost, m_node.m_nodePort, false); - CryptoNote::invokeJsonRpcCommand(httpClient, "submitblock", req, res); + httplib::Client httpClient(m_node.m_nodeHost, m_node.m_nodePort); + CryptoNote::JsonRpc::invokeJsonRpcCommand(httpClient, "submitblock", req, res); std::string err = interpret_rpc_response(true, res.status); if (err.empty()) { return true; @@ -258,10 +255,6 @@ class RpcNode : public CryptoNote::INodeObserver, public CryptoNote::INodeRpcPro m_logger(Logging::INFO) << "Failed to invoke request: " << err; } } - catch (const CryptoNote::ConnectException&) { - m_logger(Logging::INFO) << "Wallet failed to connect to daemon."; - return false; - } catch (const std::exception& e) { m_logger(Logging::INFO) << "Failed to invoke RPC method: " << e.what(); return false; From 868a1231e0fb45a15fcbae8b7a5517268ea9466c Mon Sep 17 00:00:00 2001 From: Aiwe Date: Fri, 22 Jul 2022 12:31:14 +0300 Subject: [PATCH 2/7] Run node RPC server too because we can --- CMakeLists.txt | 3 +++ src/CryptoNoteWrapper.cpp | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 915705c74..5d2a706b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -218,6 +218,9 @@ set(CRYPTONOTE_SOURCES cryptonote/src/NodeRpcProxy/NodeErrors.cpp cryptonote/src/NodeRpcProxy/NodeRpcProxy.cpp cryptonote/src/P2p/NetNodeConfig.cpp + cryptonote/src/Rpc/RpcServer.cpp + cryptonote/src/Rpc/RpcServerConfig.cpp + cryptonote/src/Rpc/HttpServer.cpp cryptonote/src/Serialization/BinaryInputStreamSerializer.cpp cryptonote/src/Serialization/BinaryOutputStreamSerializer.cpp cryptonote/src/Serialization/JsonInputValueSerializer.cpp diff --git a/src/CryptoNoteWrapper.cpp b/src/CryptoNoteWrapper.cpp index 7f5a2b304..6c3708c1a 100644 --- a/src/CryptoNoteWrapper.cpp +++ b/src/CryptoNoteWrapper.cpp @@ -22,6 +22,7 @@ #include "CryptoNoteCore/TransactionExtra.h" #include "Rpc/CoreRpcServerCommandsDefinitions.h" #include "Rpc/HttpClient.h" +#include "Rpc/RpcServer.h" #include "CryptoNoteProtocol/CryptoNoteProtocolHandler.h" #include "InProcessNode/InProcessNode.h" #include "P2p/NetNode.h" @@ -345,7 +346,7 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { m_coreConfig(coreConfig), m_netNodeConfig(netNodeConfig), m_protocolHandler(currency, m_dispatcher, m_core, nullptr, logManager), - m_core(currency, &m_protocolHandler, logManager, m_dispatcher, true), + m_core(currency, &m_protocolHandler, logManager, m_dispatcher, true, false, false), m_nodeServer(m_dispatcher, m_protocolHandler, logManager), m_node(m_core, m_protocolHandler) { @@ -393,6 +394,15 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { callback(make_error_code(CryptoNote::error::NOT_INITIALIZED)); return; } + + m_logger(Logging::INFO) << "Starting core rpc server..."; + m_rpcServer = new CryptoNote::RpcServer(m_dispatcher, m_logManager, m_core, m_nodeServer, m_protocolHandler); + m_rpcServer->start("127.0.0.1", + 32348, + 32448, + false); + m_logger(Logging::INFO) << "Core rpc server started ok"; + } catch (std::runtime_error& _err) { callback(make_error_code(CryptoNote::error::NOT_INITIALIZED)); return; @@ -405,6 +415,7 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { m_nodeServer.run(); m_nodeServer.deinit(); + m_rpcServer->stop(); m_core.deinit(); m_node.shutdown(); } @@ -554,6 +565,7 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { CryptoNote::InProcessNode m_node; std::future m_nodeServerFuture; Logging::LoggerRef m_logger; + CryptoNote::RpcServer* m_rpcServer; void peerCountUpdated(size_t count) override { m_callback.peerCountUpdated(*this, count); From 200be5a261b116c41cfcd065e11387d53d0eab4a Mon Sep 17 00:00:00 2001 From: Aiwe Date: Fri, 22 Jul 2022 12:43:31 +0300 Subject: [PATCH 3/7] Get us new core with new fancy rpc server --- cryptonote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptonote b/cryptonote index 5e0566b46..e43ece386 160000 --- a/cryptonote +++ b/cryptonote @@ -1 +1 @@ -Subproject commit 5e0566b4669352d2dfb951b091f3180dedb8e77d +Subproject commit e43ece3863ed221846d7e4cc5adfcbab01618908 From 3f0ece191fcfe86a2966e4731287942ebd7e3af3 Mon Sep 17 00:00:00 2001 From: Aiwe Date: Fri, 22 Jul 2022 16:35:54 +0300 Subject: [PATCH 4/7] Run builtin RPC server with minimal config i.e. SSL settings are not implemented as it is not intended to run via htttps --- src/CommandLineParser.cpp | 12 +++++++++ src/CommandLineParser.h | 4 +++ src/CryptoNoteWrapper.cpp | 15 +++++------ src/CryptoNoteWrapper.h | 4 ++- src/NodeAdapter.cpp | 54 ++++++++++++++++++++++++++++++++++----- src/NodeAdapter.h | 8 +++--- src/Settings.cpp | 10 ++++++++ src/Settings.h | 3 ++- 8 files changed, 90 insertions(+), 20 deletions(-) diff --git a/src/CommandLineParser.cpp b/src/CommandLineParser.cpp index 4fcfed33a..f2d3102bb 100644 --- a/src/CommandLineParser.cpp +++ b/src/CommandLineParser.cpp @@ -17,6 +17,8 @@ CommandLineParser::CommandLineParser(QObject* _parent) : QObject(_parent), m_par m_withoutCheckpointsOption("without-checkpoints", tr("Do not load checkpoints for full blocks validation")), m_p2pBindIpOption("p2p-bind-ip", tr("Interface for p2p network protocol"), tr("ip"), "0.0.0.0"), m_p2pBindPortOption("p2p-bind-port", tr("Port for p2p network protocol"), tr("port"), QString::number(CryptoNote::P2P_DEFAULT_PORT)), + m_rpcBindIpOption("rpc-bind-ip", tr("Interface for RPC server"), tr("ip"), "127.0.0.1"), + m_rpcBindPortOption("rpc-bind-port", tr("Port for RPC server"), tr("port"), QString::number(CryptoNote::RPC_DEFAULT_PORT)), m_p2pExternalOption("p2p-external-port", tr("External port for p2p network protocol (if port forwarding used with NAT)"), tr("port"), 0), m_allowLocalIpOption("allow-local-ip", tr("Allow local ip add to peer list, mostly in debug purposes")), @@ -39,6 +41,8 @@ CommandLineParser::CommandLineParser(QObject* _parent) : QObject(_parent), m_par m_parser.addOption(m_withoutCheckpointsOption); m_parser.addOption(m_p2pBindIpOption); m_parser.addOption(m_p2pBindPortOption); + m_parser.addOption(m_rpcBindIpOption); + m_parser.addOption(m_rpcBindPortOption); m_parser.addOption(m_p2pExternalOption); m_parser.addOption(m_allowLocalIpOption); m_parser.addOption(m_addPeerOption); @@ -117,6 +121,14 @@ quint16 CommandLineParser::getP2pBindPort() const { return m_parser.value(m_p2pBindPortOption).toUShort(); } +QString CommandLineParser::getRpcBindIp() const { + return m_parser.value(m_rpcBindIpOption); +} + +quint16 CommandLineParser::getRpcBindPort() const { + return m_parser.value(m_rpcBindPortOption).toUShort(); +} + quint16 CommandLineParser::getP2pExternalPort() const { return m_parser.value(m_p2pExternalOption).toUShort(); } diff --git a/src/CommandLineParser.h b/src/CommandLineParser.h index 9d1dd7f09..f2bfc85f3 100644 --- a/src/CommandLineParser.h +++ b/src/CommandLineParser.h @@ -33,6 +33,8 @@ class CommandLineParser : public QObject { QString getHelpText() const; QString getP2pBindIp() const; quint16 getP2pBindPort() const; + QString getRpcBindIp() const; + quint16 getRpcBindPort() const; quint16 getP2pExternalPort() const; QStringList getPeers() const; QStringList getPiorityNodes() const; @@ -49,6 +51,8 @@ class CommandLineParser : public QObject { QCommandLineOption m_withoutCheckpointsOption; QCommandLineOption m_p2pBindIpOption; QCommandLineOption m_p2pBindPortOption; + QCommandLineOption m_rpcBindIpOption; + QCommandLineOption m_rpcBindPortOption; QCommandLineOption m_p2pExternalOption; QCommandLineOption m_allowLocalIpOption; QCommandLineOption m_addPeerOption; diff --git a/src/CryptoNoteWrapper.cpp b/src/CryptoNoteWrapper.cpp index 352fa3057..ef5cf6edf 100644 --- a/src/CryptoNoteWrapper.cpp +++ b/src/CryptoNoteWrapper.cpp @@ -331,13 +331,14 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { public: Logging::LoggerManager& m_logManager; InprocessNode(const CryptoNote::Currency& currency, Logging::LoggerManager& logManager, const CryptoNote::CoreConfig& coreConfig, - const CryptoNote::NetNodeConfig& netNodeConfig, INodeCallback& callback) : + const CryptoNote::NetNodeConfig& netNodeConfig, const CryptoNote::RpcServerConfig& rpcServerConfig, INodeCallback& callback) : m_currency(currency), m_dispatcher(), m_callback(callback), m_logManager(logManager), m_logger(m_logManager, "InprocessNode"), m_coreConfig(coreConfig), m_netNodeConfig(netNodeConfig), + m_rpcServerConfig(rpcServerConfig), m_protocolHandler(currency, m_dispatcher, m_core, nullptr, logManager), m_core(currency, &m_protocolHandler, logManager, m_dispatcher, true, false, false), m_nodeServer(m_dispatcher, m_protocolHandler, logManager), @@ -389,11 +390,8 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { } m_logger(Logging::INFO) << "Starting core rpc server..."; - m_rpcServer = new CryptoNote::RpcServer(m_dispatcher, m_logManager, m_core, m_nodeServer, m_protocolHandler); - m_rpcServer->start("127.0.0.1", - 32348, - 32448, - false); + m_rpcServer = new CryptoNote::RpcServer(m_rpcServerConfig, m_dispatcher, m_logManager, m_core, m_nodeServer, m_protocolHandler); + m_rpcServer->start(); m_logger(Logging::INFO) << "Core rpc server started ok"; } catch (std::runtime_error& _err) { @@ -552,6 +550,7 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { System::Dispatcher m_dispatcher; CryptoNote::CoreConfig m_coreConfig; CryptoNote::NetNodeConfig m_netNodeConfig; + CryptoNote::RpcServerConfig m_rpcServerConfig; CryptoNote::Core m_core; CryptoNote::CryptoNoteProtocolHandler m_protocolHandler; CryptoNote::NodeServer m_nodeServer; @@ -583,8 +582,8 @@ Node* createRpcNode(const CryptoNote::Currency& currency, INodeCallback& callbac } Node* createInprocessNode(const CryptoNote::Currency& currency, Logging::LoggerManager& logManager, - const CryptoNote::CoreConfig& coreConfig, const CryptoNote::NetNodeConfig& netNodeConfig, INodeCallback& callback) { - return new InprocessNode(currency, logManager, coreConfig, netNodeConfig, callback); + const CryptoNote::CoreConfig& coreConfig, const CryptoNote::NetNodeConfig& netNodeConfig, const CryptoNote::RpcServerConfig& rpcServerConfig, INodeCallback& callback) { + return new InprocessNode(currency, logManager, coreConfig, netNodeConfig, rpcServerConfig, callback); } } diff --git a/src/CryptoNoteWrapper.h b/src/CryptoNoteWrapper.h index be37516c2..af3a734ce 100644 --- a/src/CryptoNoteWrapper.h +++ b/src/CryptoNoteWrapper.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace CryptoNote { @@ -19,6 +20,7 @@ class IWalletLegacy; class Currency; class CoreConfig; class NetNodeConfig; +class RpcServerConfig; } @@ -80,6 +82,6 @@ class INodeCallback { Node* createRpcNode(const CryptoNote::Currency& currency, INodeCallback& callback, Logging::LoggerManager& logManager, const std::string& nodeHost, unsigned short nodePort, bool enableSSL); Node* createInprocessNode(const CryptoNote::Currency& currency, Logging::LoggerManager& logManager, - const CryptoNote::CoreConfig& coreConfig, const CryptoNote::NetNodeConfig& netNodeConfig, INodeCallback& callback); + const CryptoNote::CoreConfig& coreConfig, const CryptoNote::NetNodeConfig& netNodeConfig, const CryptoNote::RpcServerConfig& rpcServerConfig, INodeCallback& callback); } diff --git a/src/NodeAdapter.cpp b/src/NodeAdapter.cpp index 37bbc3ac7..5c89c84a3 100644 --- a/src/NodeAdapter.cpp +++ b/src/NodeAdapter.cpp @@ -9,15 +9,17 @@ #include #include -#include -#include -#include +#include +#include +#include "CryptoNoteCore/CoreConfig.h" #include "CurrencyAdapter.h" #include "LoggerAdapter.h" #include "NodeAdapter.h" +#include "P2p/NetNodeConfig.h" +#include "Rpc/RpcServerConfig.h" #include "Settings.h" -#include +#include "Wallet/WalletErrors.h" namespace WalletGui { @@ -51,8 +53,8 @@ class InProcessNodeInitializer : public QObject { } void start(Node** _node, const CryptoNote::Currency* currency, INodeCallback* _callback, Logging::LoggerManager* _loggerManager, - const CryptoNote::CoreConfig& _coreConfig, const CryptoNote::NetNodeConfig& _netNodeConfig) { - (*_node) = createInprocessNode(*currency, *_loggerManager, _coreConfig, _netNodeConfig, *_callback); + const CryptoNote::CoreConfig& _coreConfig, const CryptoNote::NetNodeConfig& _netNodeConfig, const CryptoNote::RpcServerConfig& _rpcServerConfig) { + (*_node) = createInprocessNode(*currency, *_loggerManager, _coreConfig, _netNodeConfig, _rpcServerConfig, *_callback); try { (*_node)->init([this](std::error_code _err) { if (_err) { @@ -91,6 +93,7 @@ NodeAdapter::NodeAdapter() : QObject(), m_node(nullptr), m_nodeInitializerThread qRegisterMetaType("CryptoNote::CoreConfig"); qRegisterMetaType("CryptoNote::NetNodeConfig"); + qRegisterMetaType("CryptoNote::RpcServerConfig"); connect(m_nodeInitializer, &InProcessNodeInitializer::nodeInitCompletedSignal, this, &NodeAdapter::nodeInitCompletedSignal, Qt::QueuedConnection); connect(this, &NodeAdapter::initNodeSignal, m_nodeInitializer, &InProcessNodeInitializer::start, Qt::QueuedConnection); @@ -354,7 +357,8 @@ bool NodeAdapter::initInProcessNode() { m_nodeInitializerThread.start(); CryptoNote::CoreConfig coreConfig = makeCoreConfig(); CryptoNote::NetNodeConfig netNodeConfig = makeNetNodeConfig(); - Q_EMIT initNodeSignal(&m_node, &CurrencyAdapter::instance().getCurrency(), this, &LoggerAdapter::instance().getLoggerManager(), coreConfig, netNodeConfig); + CryptoNote::RpcServerConfig rpcServerConfig = makeRpcServerConfig(); + Q_EMIT initNodeSignal(&m_node, &CurrencyAdapter::instance().getCurrency(), this, &LoggerAdapter::instance().getLoggerManager(), coreConfig, netNodeConfig, rpcServerConfig); QEventLoop waitLoop; connect(m_nodeInitializer, &InProcessNodeInitializer::nodeInitCompletedSignal, &waitLoop, &QEventLoop::quit); connect(m_nodeInitializer, &InProcessNodeInitializer::nodeInitFailedSignal, &waitLoop, &QEventLoop::exit); @@ -441,6 +445,42 @@ CryptoNote::NetNodeConfig NodeAdapter::makeNetNodeConfig() const { return config; } +CryptoNote::RpcServerConfig NodeAdapter::makeRpcServerConfig() const { + CryptoNote::RpcServerConfig config; + boost::filesystem::path dataDir = std::string(Settings::instance().getDataDir().absolutePath().toLocal8Bit().data()); + config.setDataDir(dataDir.string()); + boost::program_options::variables_map options; + boost::any rpcBindIp = Settings::instance().getRpcBindIp().toStdString(); + boost::any rpcBindPort = static_cast(Settings::instance().getRpcBindPort()); + + options.insert(std::make_pair("rpc-bind-ip", boost::program_options::variable_value(rpcBindIp, false))); + options.insert(std::make_pair("rpc-bind-port", boost::program_options::variable_value(rpcBindPort, false))); + + // dummy defaults + std::string dummy = "", cors = "*"; + uint16_t sslport = CryptoNote::RPC_DEFAULT_SSL_PORT; + bool no = false, yes = true; + options.insert(std::make_pair("rpc-bind-ssl-port", boost::program_options::variable_value(sslport, false))); + options.insert(std::make_pair("rpc-bind-ssl-enable", boost::program_options::variable_value(no, false))); + options.insert(std::make_pair("rpc-chain-file", boost::program_options::variable_value(dummy, false))); + options.insert(std::make_pair("rpc-key-file", boost::program_options::variable_value(dummy, false))); + options.insert(std::make_pair("rpc-bind-ssl-enable", boost::program_options::variable_value(dummy, false))); + options.insert(std::make_pair("enable-cors", boost::program_options::variable_value(cors, false))); + options.insert(std::make_pair("contact", boost::program_options::variable_value(dummy, false))); + options.insert(std::make_pair("fee-address", boost::program_options::variable_value(dummy, false))); + options.insert(std::make_pair("fee-amount", boost::program_options::variable_value(dummy, false))); + options.insert(std::make_pair("view-key", boost::program_options::variable_value(dummy, false))); + + // We actually want to restrict it + // TODO: add to config but reversed + options.insert(std::make_pair("restricted-rpc", boost::program_options::variable_value(yes, false))); + + + config.init(options); + + return config; +} + bool NodeAdapter::isOffline() { return getConnectionsCount() == 0; } diff --git a/src/NodeAdapter.h b/src/NodeAdapter.h index ec171c2db..7a50ed9fe 100644 --- a/src/NodeAdapter.h +++ b/src/NodeAdapter.h @@ -11,6 +11,7 @@ #include #include #include "CryptoNoteWrapper.h" +#include "Rpc/RpcServerConfig.h" namespace CryptoNote { @@ -82,15 +83,16 @@ class NodeAdapter : public QObject, public INodeCallback { bool initInProcessNode(); CryptoNote::CoreConfig makeCoreConfig() const; CryptoNote::NetNodeConfig makeNetNodeConfig() const; + CryptoNote::RpcServerConfig makeRpcServerConfig() const; Q_SIGNALS: void localBlockchainUpdatedSignal(quint64 _height); void lastKnownBlockHeightUpdatedSignal(quint64 _height); void nodeInitCompletedSignal(); void peerCountUpdatedSignal(quintptr _count); - void initNodeSignal(Node** _node, const CryptoNote::Currency* currency, INodeCallback* _callback, Logging::LoggerManager* _loggerManager, - const CryptoNote::CoreConfig& _coreConfig, const CryptoNote::NetNodeConfig& _netNodeConfig); - void deinitNodeSignal(Node** _node); + void initNodeSignal(WalletGui::Node** _node, const CryptoNote::Currency* currency, INodeCallback* _callback, Logging::LoggerManager* _loggerManager, + const CryptoNote::CoreConfig& _coreConfig, const CryptoNote::NetNodeConfig& _netNodeConfig, const CryptoNote::RpcServerConfig& _rpcServerConfig); + void deinitNodeSignal(WalletGui::Node** _node); void connectionFailedSignal(); void connectionStatusUpdatedSignal(bool _connected); }; diff --git a/src/Settings.cpp b/src/Settings.cpp index 4db701b8b..95b7e7dc6 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -137,6 +137,16 @@ quint16 Settings::getP2pBindPort() const { return m_cmdLineParser->getP2pBindPort(); } +QString Settings::getRpcBindIp() const { + Q_ASSERT(m_cmdLineParser != nullptr); + return m_cmdLineParser->getRpcBindIp(); +} + +quint16 Settings::getRpcBindPort() const { + Q_ASSERT(m_cmdLineParser != nullptr); + return m_cmdLineParser->getRpcBindPort(); +} + quint16 Settings::getP2pExternalPort() const { Q_ASSERT(m_cmdLineParser != nullptr); return m_cmdLineParser->getP2pExternalPort(); diff --git a/src/Settings.h b/src/Settings.h index d72ffba2c..7f17b627c 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -40,9 +40,10 @@ class Settings : public QObject { bool alowReorg() const; QDir getDataDir() const; QString getP2pBindIp() const; - quint16 getLocalRpcPort() const; quint16 getP2pBindPort() const; quint16 getP2pExternalPort() const; + QString getRpcBindIp() const; + quint16 getRpcBindPort() const; quint16 getConnectionsCount() const; QStringList getExclusiveNodes() const; QStringList getPeers() const; From c3978e70cb351f1e72698e7f2f46e8cc8bf2d15e Mon Sep 17 00:00:00 2001 From: Aiwe Date: Fri, 22 Jul 2022 17:53:50 +0300 Subject: [PATCH 5/7] Update core again with RPC conf changes --- cryptonote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptonote b/cryptonote index e43ece386..71b6268c1 160000 --- a/cryptonote +++ b/cryptonote @@ -1 +1 @@ -Subproject commit e43ece3863ed221846d7e4cc5adfcbab01618908 +Subproject commit 71b6268c1bf10d305a5200b4422ea36d315f0b07 From 1c93e241d75b66aa77af5bd92f058bf95339129c Mon Sep 17 00:00:00 2001 From: Aiwe Date: Fri, 22 Jul 2022 18:17:37 +0300 Subject: [PATCH 6/7] Option to unrestrict rpc --- src/CommandLineParser.cpp | 6 ++++++ src/CommandLineParser.h | 2 ++ src/CryptoNoteWrapper.cpp | 2 ++ src/NodeAdapter.cpp | 7 ++----- src/Settings.cpp | 5 +++++ src/Settings.h | 1 + 6 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/CommandLineParser.cpp b/src/CommandLineParser.cpp index f2d3102bb..65d4224a3 100644 --- a/src/CommandLineParser.cpp +++ b/src/CommandLineParser.cpp @@ -19,6 +19,7 @@ CommandLineParser::CommandLineParser(QObject* _parent) : QObject(_parent), m_par m_p2pBindPortOption("p2p-bind-port", tr("Port for p2p network protocol"), tr("port"), QString::number(CryptoNote::P2P_DEFAULT_PORT)), m_rpcBindIpOption("rpc-bind-ip", tr("Interface for RPC server"), tr("ip"), "127.0.0.1"), m_rpcBindPortOption("rpc-bind-port", tr("Port for RPC server"), tr("port"), QString::number(CryptoNote::RPC_DEFAULT_PORT)), + m_unrestrictedRpcOption("unrestricted-rpc", tr("Allow all RPC server queries")), m_p2pExternalOption("p2p-external-port", tr("External port for p2p network protocol (if port forwarding used with NAT)"), tr("port"), 0), m_allowLocalIpOption("allow-local-ip", tr("Allow local ip add to peer list, mostly in debug purposes")), @@ -43,6 +44,7 @@ CommandLineParser::CommandLineParser(QObject* _parent) : QObject(_parent), m_par m_parser.addOption(m_p2pBindPortOption); m_parser.addOption(m_rpcBindIpOption); m_parser.addOption(m_rpcBindPortOption); + m_parser.addOption(m_unrestrictedRpcOption); m_parser.addOption(m_p2pExternalOption); m_parser.addOption(m_allowLocalIpOption); m_parser.addOption(m_addPeerOption); @@ -129,6 +131,10 @@ quint16 CommandLineParser::getRpcBindPort() const { return m_parser.value(m_rpcBindPortOption).toUShort(); } +bool CommandLineParser::hasUnrestrictedRpcOption() const { + return m_parser.isSet(m_unrestrictedRpcOption); +} + quint16 CommandLineParser::getP2pExternalPort() const { return m_parser.value(m_p2pExternalOption).toUShort(); } diff --git a/src/CommandLineParser.h b/src/CommandLineParser.h index f2bfc85f3..bdcbea64a 100644 --- a/src/CommandLineParser.h +++ b/src/CommandLineParser.h @@ -29,6 +29,7 @@ class CommandLineParser : public QObject { bool hasHideMyPortOption() const; bool hasPortableOption() const; bool hasAllowReorgOption() const; + bool hasUnrestrictedRpcOption() const; QString getErrorText() const; QString getHelpText() const; QString getP2pBindIp() const; @@ -53,6 +54,7 @@ class CommandLineParser : public QObject { QCommandLineOption m_p2pBindPortOption; QCommandLineOption m_rpcBindIpOption; QCommandLineOption m_rpcBindPortOption; + QCommandLineOption m_unrestrictedRpcOption; QCommandLineOption m_p2pExternalOption; QCommandLineOption m_allowLocalIpOption; QCommandLineOption m_addPeerOption; diff --git a/src/CryptoNoteWrapper.cpp b/src/CryptoNoteWrapper.cpp index ef5cf6edf..9573d9bdb 100644 --- a/src/CryptoNoteWrapper.cpp +++ b/src/CryptoNoteWrapper.cpp @@ -389,11 +389,13 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { return; } + m_logger(Logging::INFO) << "Starting core rpc server..."; m_rpcServer = new CryptoNote::RpcServer(m_rpcServerConfig, m_dispatcher, m_logManager, m_core, m_nodeServer, m_protocolHandler); m_rpcServer->start(); m_logger(Logging::INFO) << "Core rpc server started ok"; + } catch (std::runtime_error& _err) { callback(make_error_code(CryptoNote::error::NOT_INITIALIZED)); return; diff --git a/src/NodeAdapter.cpp b/src/NodeAdapter.cpp index 5c89c84a3..7b9373432 100644 --- a/src/NodeAdapter.cpp +++ b/src/NodeAdapter.cpp @@ -459,7 +459,7 @@ CryptoNote::RpcServerConfig NodeAdapter::makeRpcServerConfig() const { // dummy defaults std::string dummy = "", cors = "*"; uint16_t sslport = CryptoNote::RPC_DEFAULT_SSL_PORT; - bool no = false, yes = true; + bool no = false; options.insert(std::make_pair("rpc-bind-ssl-port", boost::program_options::variable_value(sslport, false))); options.insert(std::make_pair("rpc-bind-ssl-enable", boost::program_options::variable_value(no, false))); options.insert(std::make_pair("rpc-chain-file", boost::program_options::variable_value(dummy, false))); @@ -471,10 +471,7 @@ CryptoNote::RpcServerConfig NodeAdapter::makeRpcServerConfig() const { options.insert(std::make_pair("fee-amount", boost::program_options::variable_value(dummy, false))); options.insert(std::make_pair("view-key", boost::program_options::variable_value(dummy, false))); - // We actually want to restrict it - // TODO: add to config but reversed - options.insert(std::make_pair("restricted-rpc", boost::program_options::variable_value(yes, false))); - + options.insert(std::make_pair("restricted-rpc", boost::program_options::variable_value(!Settings::instance().unrestrictedRpc(), false))); config.init(options); diff --git a/src/Settings.cpp b/src/Settings.cpp index 95b7e7dc6..63dc30c35 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -147,6 +147,11 @@ quint16 Settings::getRpcBindPort() const { return m_cmdLineParser->getRpcBindPort(); } +bool Settings::unrestrictedRpc() const { + Q_ASSERT(m_cmdLineParser != nullptr); + return m_cmdLineParser->hasUnrestrictedRpcOption(); +} + quint16 Settings::getP2pExternalPort() const { Q_ASSERT(m_cmdLineParser != nullptr); return m_cmdLineParser->getP2pExternalPort(); diff --git a/src/Settings.h b/src/Settings.h index 7f17b627c..fbe2bebff 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -38,6 +38,7 @@ class Settings : public QObject { bool isTestnet() const; bool withoutCheckpoints() const; bool alowReorg() const; + bool unrestrictedRpc() const; QDir getDataDir() const; QString getP2pBindIp() const; quint16 getP2pBindPort() const; From 2f48ad189875fe508512414ad430c12d432461b9 Mon Sep 17 00:00:00 2001 From: Aiwe Date: Fri, 22 Jul 2022 18:32:20 +0300 Subject: [PATCH 7/7] Add command line option rpc-server to run RPC server by default it's not started also restrict rpc by option, insted of unrestricting since we explicitly tell wallet to run rpc server --- src/CommandLineParser.cpp | 14 ++++++++++---- src/CommandLineParser.h | 6 ++++-- src/CryptoNoteWrapper.cpp | 12 ++++++------ src/NodeAdapter.cpp | 2 +- src/Settings.cpp | 9 +++++++-- src/Settings.h | 3 ++- 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/CommandLineParser.cpp b/src/CommandLineParser.cpp index 65d4224a3..2abc8efb1 100644 --- a/src/CommandLineParser.cpp +++ b/src/CommandLineParser.cpp @@ -19,7 +19,8 @@ CommandLineParser::CommandLineParser(QObject* _parent) : QObject(_parent), m_par m_p2pBindPortOption("p2p-bind-port", tr("Port for p2p network protocol"), tr("port"), QString::number(CryptoNote::P2P_DEFAULT_PORT)), m_rpcBindIpOption("rpc-bind-ip", tr("Interface for RPC server"), tr("ip"), "127.0.0.1"), m_rpcBindPortOption("rpc-bind-port", tr("Port for RPC server"), tr("port"), QString::number(CryptoNote::RPC_DEFAULT_PORT)), - m_unrestrictedRpcOption("unrestricted-rpc", tr("Allow all RPC server queries")), + m_rpcOption("rpc-server", tr("Run RPC server")), + m_restrictedRpcOption("restricted-rpc", tr("Disallow some RPC server queries")), m_p2pExternalOption("p2p-external-port", tr("External port for p2p network protocol (if port forwarding used with NAT)"), tr("port"), 0), m_allowLocalIpOption("allow-local-ip", tr("Allow local ip add to peer list, mostly in debug purposes")), @@ -44,7 +45,8 @@ CommandLineParser::CommandLineParser(QObject* _parent) : QObject(_parent), m_par m_parser.addOption(m_p2pBindPortOption); m_parser.addOption(m_rpcBindIpOption); m_parser.addOption(m_rpcBindPortOption); - m_parser.addOption(m_unrestrictedRpcOption); + m_parser.addOption(m_rpcOption); + m_parser.addOption(m_restrictedRpcOption); m_parser.addOption(m_p2pExternalOption); m_parser.addOption(m_allowLocalIpOption); m_parser.addOption(m_addPeerOption); @@ -131,8 +133,12 @@ quint16 CommandLineParser::getRpcBindPort() const { return m_parser.value(m_rpcBindPortOption).toUShort(); } -bool CommandLineParser::hasUnrestrictedRpcOption() const { - return m_parser.isSet(m_unrestrictedRpcOption); +bool CommandLineParser::hasRpcOption() const { + return m_parser.isSet(m_rpcOption); +} + +bool CommandLineParser::hasRestrictedRpcOption() const { + return m_parser.isSet(m_restrictedRpcOption); } quint16 CommandLineParser::getP2pExternalPort() const { diff --git a/src/CommandLineParser.h b/src/CommandLineParser.h index bdcbea64a..ed4aef707 100644 --- a/src/CommandLineParser.h +++ b/src/CommandLineParser.h @@ -29,7 +29,8 @@ class CommandLineParser : public QObject { bool hasHideMyPortOption() const; bool hasPortableOption() const; bool hasAllowReorgOption() const; - bool hasUnrestrictedRpcOption() const; + bool hasRpcOption() const; + bool hasRestrictedRpcOption() const; QString getErrorText() const; QString getHelpText() const; QString getP2pBindIp() const; @@ -54,7 +55,8 @@ class CommandLineParser : public QObject { QCommandLineOption m_p2pBindPortOption; QCommandLineOption m_rpcBindIpOption; QCommandLineOption m_rpcBindPortOption; - QCommandLineOption m_unrestrictedRpcOption; + QCommandLineOption m_rpcOption; + QCommandLineOption m_restrictedRpcOption; QCommandLineOption m_p2pExternalOption; QCommandLineOption m_allowLocalIpOption; QCommandLineOption m_addPeerOption; diff --git a/src/CryptoNoteWrapper.cpp b/src/CryptoNoteWrapper.cpp index 9573d9bdb..cd6a6a990 100644 --- a/src/CryptoNoteWrapper.cpp +++ b/src/CryptoNoteWrapper.cpp @@ -389,12 +389,12 @@ class InprocessNode : public CryptoNote::INodeObserver, public Node { return; } - - m_logger(Logging::INFO) << "Starting core rpc server..."; - m_rpcServer = new CryptoNote::RpcServer(m_rpcServerConfig, m_dispatcher, m_logManager, m_core, m_nodeServer, m_protocolHandler); - m_rpcServer->start(); - m_logger(Logging::INFO) << "Core rpc server started ok"; - + if (Settings::instance().hasRunRpc()) { + m_logger(Logging::INFO) << "Starting core rpc server..."; + m_rpcServer = new CryptoNote::RpcServer(m_rpcServerConfig, m_dispatcher, m_logManager, m_core, m_nodeServer, m_protocolHandler); + m_rpcServer->start(); + m_logger(Logging::INFO) << "Core rpc server started ok"; + } } catch (std::runtime_error& _err) { callback(make_error_code(CryptoNote::error::NOT_INITIALIZED)); diff --git a/src/NodeAdapter.cpp b/src/NodeAdapter.cpp index 7b9373432..2036a3613 100644 --- a/src/NodeAdapter.cpp +++ b/src/NodeAdapter.cpp @@ -471,7 +471,7 @@ CryptoNote::RpcServerConfig NodeAdapter::makeRpcServerConfig() const { options.insert(std::make_pair("fee-amount", boost::program_options::variable_value(dummy, false))); options.insert(std::make_pair("view-key", boost::program_options::variable_value(dummy, false))); - options.insert(std::make_pair("restricted-rpc", boost::program_options::variable_value(!Settings::instance().unrestrictedRpc(), false))); + options.insert(std::make_pair("restricted-rpc", boost::program_options::variable_value(Settings::instance().hasRestrictedRpc(), false))); config.init(options); diff --git a/src/Settings.cpp b/src/Settings.cpp index 63dc30c35..94620b520 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -147,9 +147,14 @@ quint16 Settings::getRpcBindPort() const { return m_cmdLineParser->getRpcBindPort(); } -bool Settings::unrestrictedRpc() const { +bool Settings::hasRunRpc() const { Q_ASSERT(m_cmdLineParser != nullptr); - return m_cmdLineParser->hasUnrestrictedRpcOption(); + return m_cmdLineParser->hasRpcOption(); +} + +bool Settings::hasRestrictedRpc() const { + Q_ASSERT(m_cmdLineParser != nullptr); + return m_cmdLineParser->hasRestrictedRpcOption(); } quint16 Settings::getP2pExternalPort() const { diff --git a/src/Settings.h b/src/Settings.h index fbe2bebff..4fd744b94 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -38,7 +38,8 @@ class Settings : public QObject { bool isTestnet() const; bool withoutCheckpoints() const; bool alowReorg() const; - bool unrestrictedRpc() const; + bool hasRunRpc() const; + bool hasRestrictedRpc() const; QDir getDataDir() const; QString getP2pBindIp() const; quint16 getP2pBindPort() const;