Bind HTTP server to specified addresses */
| 296 | |
| 297 | /** Bind HTTP server to specified addresses */ |
| 298 | static bool HTTPBindAddresses(struct evhttp* http) |
| 299 | { |
| 300 | uint16_t http_port{static_cast<uint16_t>(gArgs.GetIntArg("-rpcport", BaseParams().RPCPort()))}; |
| 301 | std::vector<std::pair<std::string, uint16_t>> endpoints; |
| 302 | |
| 303 | // Determine what addresses to bind to |
| 304 | if (!(gArgs.IsArgSet("-rpcallowip") && gArgs.IsArgSet("-rpcbind"))) { // Default to loopback if not allowing external IPs |
| 305 | endpoints.push_back(std::make_pair("::1", http_port)); |
| 306 | endpoints.push_back(std::make_pair("127.0.0.1", http_port)); |
| 307 | if (gArgs.IsArgSet("-rpcallowip")) { |
| 308 | LogPrintf("WARNING: option -rpcallowip was specified without -rpcbind; this doesn't usually make sense\n"); |
| 309 | } |
| 310 | if (gArgs.IsArgSet("-rpcbind")) { |
| 311 | LogPrintf("WARNING: option -rpcbind was ignored because -rpcallowip was not specified, refusing to allow everyone to connect\n"); |
| 312 | } |
| 313 | } else if (gArgs.IsArgSet("-rpcbind")) { // Specific bind address |
| 314 | for (const std::string& strRPCBind : gArgs.GetArgs("-rpcbind")) { |
| 315 | uint16_t port{http_port}; |
| 316 | std::string host; |
| 317 | SplitHostPort(strRPCBind, port, host); |
| 318 | endpoints.push_back(std::make_pair(host, port)); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // Bind addresses |
| 323 | for (std::vector<std::pair<std::string, uint16_t> >::iterator i = endpoints.begin(); i != endpoints.end(); ++i) { |
| 324 | LogPrint(BCLog::HTTP, "Binding RPC on address %s port %i\n", i->first, i->second); |
| 325 | evhttp_bound_socket *bind_handle = evhttp_bind_socket_with_handle(http, i->first.empty() ? nullptr : i->first.c_str(), i->second); |
| 326 | if (bind_handle) { |
| 327 | CNetAddr addr; |
| 328 | if (i->first.empty() || (LookupHost(i->first, addr, false) && addr.IsBindAny())) { |
| 329 | LogPrintf("WARNING: the RPC server is not safe to expose to untrusted networks such as the public internet\n"); |
| 330 | } |
| 331 | boundSockets.push_back(bind_handle); |
| 332 | } else { |
| 333 | LogPrintf("Binding RPC on address %s port %i failed.\n", i->first, i->second); |
| 334 | } |
| 335 | } |
| 336 | return !boundSockets.empty(); |
| 337 | } |
| 338 | |
| 339 | /** Simple wrapper to set thread name and run work queue */ |
| 340 | static void HTTPWorkQueueRun(WorkQueue<HTTPClosure>* queue, int worker_num) |
no test coverage detected