P2PServer: add connection limits

#87 Added `--out-peers` and `--in-peers` command line options.
pull/112/head
SChernykh 2 years ago
parent 7d961ab3b9
commit f1b6212c82

@ -40,6 +40,8 @@ static void usage()
"--no-cache Disable p2pool.cache\n"
"--no-color Disable colors in console output\n"
"--no-randomx Disable internal RandomX hasher: p2pool will use RPC calls to monerod to check PoW hashes\n"
"--out-peers Maximum number of outgoing connections for p2p server (any value between 10 and 1000)\n"
"--in-peers Maximum number of incoming connections for p2p server (any value between 10 and 1000)\n"
"--help Show this help message\n\n"
"Example command line:\n\n"
"%s --host 127.0.0.1 --rpc-port 18081 --zmq-port 18083 --wallet YOUR_WALLET_ADDRESS --stratum 0.0.0.0:%d --p2p 0.0.0.0:%d\n\n",

@ -226,8 +226,8 @@ void P2PServer::update_peer_connections()
peer_list = m_peerList;
}
// Try to have at least 10 outgoing connections
for (uint32_t i = m_numConnections - m_numIncomingConnections; (i < 10) && !peer_list.empty();) {
// Try to have at least N outgoing connections (N defaults to 10, can be set via --out-peers command line parameter)
for (uint32_t i = m_numConnections - m_numIncomingConnections, n = m_pool->params().m_maxOutgoingPeers; (i < n) && !peer_list.empty();) {
const uint64_t k = get_random64() % peer_list.size();
const Peer& peer = peer_list[k];
@ -903,6 +903,15 @@ bool P2PServer::P2PClient::on_connect()
{
P2PServer* server = static_cast<P2PServer*>(m_owner);
if (!server || !server->m_pool) {
return false;
}
if (m_isIncoming && (server->m_numIncomingConnections > server->m_pool->params().m_maxIncomingPeers)) {
LOGINFO(5, "Connection from " << log::Gray() << static_cast<char*>(m_addrString) << log::NoColor() << " rejected (incoming connections limit was reached)");
return false;
}
// Don't allow multiple connections to/from the same IP
// server->m_clientsListLock is already locked here
for (P2PClient* client = static_cast<P2PClient*>(server->m_connectedClientsList->m_next); client != server->m_connectedClientsList; client = static_cast<P2PClient*>(client->m_next)) {

@ -30,11 +30,11 @@ Params::Params(int argc, char* argv[])
}
if ((strcmp(argv[i], "--rpc-port") == 0) && (i + 1 < argc)) {
m_rpcPort = static_cast<uint32_t>(atoi(argv[++i]));
m_rpcPort = strtoul(argv[++i], nullptr, 10);
}
if ((strcmp(argv[i], "--zmq-port") == 0) && (i + 1 < argc)) {
m_zmqPort = static_cast<uint32_t>(atoi(argv[++i]));
m_zmqPort = strtoul(argv[++i], nullptr, 10);
}
if (strcmp(argv[i], "--light-mode") == 0) {
@ -58,7 +58,7 @@ Params::Params(int argc, char* argv[])
}
if ((strcmp(argv[i], "--loglevel") == 0) && (i + 1 < argc)) {
const int level = std::min(std::max(atoi(argv[++i]), 0), log::MAX_GLOBAL_LOG_LEVEL);
const int level = std::min(std::max<int>(strtol(argv[++i], nullptr, 10), 0), log::MAX_GLOBAL_LOG_LEVEL);
log::GLOBAL_LOG_LEVEL = level;
}
@ -85,6 +85,14 @@ Params::Params(int argc, char* argv[])
if (strcmp(argv[i], "--no-randomx") == 0) {
m_disableRandomX = true;
}
if ((strcmp(argv[i], "--out-peers") == 0) && (i + 1 < argc)) {
m_maxOutgoingPeers = std::min(std::max(strtoul(argv[++i], nullptr, 10), 10UL), 1000UL);
}
if ((strcmp(argv[i], "--in-peers") == 0) && (i + 1 < argc)) {
m_maxIncomingPeers = std::min(std::max(strtoul(argv[++i], nullptr, 10), 10UL), 1000UL);
}
}
if (m_stratumAddresses.empty()) {

@ -40,6 +40,8 @@ struct Params
bool m_localStats = false;
bool m_blockCache = true;
bool m_disableRandomX = false;
uint32_t m_maxOutgoingPeers = 10;
uint32_t m_maxIncomingPeers = 1000;
};
} // namespace p2pool

Loading…
Cancel
Save