Commit 7e23960d authored by Pierre Kim's avatar Pierre Kim
Browse files

Add Cross-Origin Resource Sharing (CORS) option for RPC server

parent fc21277d
......@@ -47,6 +47,7 @@ namespace
const command_line::arg_descriptor<bool> arg_testnet_on = {"testnet", "Used to deploy test nets. Checkpoints and hardcoded seeds are ignored, "
"network id is changed. Use it with --data-dir flag. The wallet must be launched with --testnet flag.", false};
const command_line::arg_descriptor<bool> arg_print_genesis_tx = { "print-genesis-tx", "Prints genesis' block tx hex to insert it to config and exits" };
const command_line::arg_descriptor<std::vector<std::string>> arg_enable_cors = { "enable-cors", "Adds header 'Access-Control-Allow-Origin' to the daemon's RPC responses. Uses the value as domain. Use * for all" };
bool command_line_preprocessor(const boost::program_options::variables_map& vm, LoggerRef& logger);
......@@ -110,6 +111,7 @@ int main(int argc, char* argv[])
command_line::add_arg(desc_cmd_sett, arg_console);
command_line::add_arg(desc_cmd_sett, arg_testnet_on);
command_line::add_arg(desc_cmd_sett, arg_print_genesis_tx);
command_line::add_arg(desc_cmd_sett, arg_enable_cors);
......@@ -268,6 +270,8 @@ int main(int argc, char* argv[])
logger(INFO) << "Starting core rpc server on address " << rpcConfig.getBindAddress();
rpcServer.start(rpcConfig.bindIp, rpcConfig.bindPort);
rpcServer.enableCors(command_line::get_arg(vm, arg_enable_cors));
logger(INFO) << "Core rpc server started ok";
Tools::SignalHandler::install([&dch, &p2psrv] {
......@@ -110,6 +110,10 @@ void RpcServer::processRequest(const HttpRequest& request, HttpResponse& respons
response.setBody("Core is busy");
for (const auto& cors_domain: m_cors_domains) {
response.addHeader("Access-Control-Allow-Origin", cors_domain);
it->second.handler(this, request, response);
......@@ -644,5 +648,9 @@ bool RpcServer::on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER
return true;
bool RpcServer::enableCors(const std::vector<std::string> domains) {
m_cors_domains = domains;
return true;
......@@ -21,7 +21,7 @@ public:
RpcServer(System::Dispatcher& dispatcher, Logging::ILogger& log, core& c, NodeServer& p2p, const ICryptoNoteProtocolQuery& protocolQuery);
typedef std::function<bool(RpcServer*, const HttpRequest& request, HttpResponse& response)> HandlerFunction;
bool enableCors(const std::vector<std::string> domains);
template <class Handler>
......@@ -71,6 +71,7 @@ private:
core& m_core;
NodeServer& m_p2p;
const ICryptoNoteProtocolQuery& m_protocolQuery;
std::vector<std::string> m_cors_domains;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment