Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/net.cpp
Show First 20 Lines • Show All 637 Lines • ▼ Show 20 Lines | if (request.fHelp || request.params.size() < 2 || | ||||
"4. \"absolute\" (boolean, optional) If set, the bantime must " | "4. \"absolute\" (boolean, optional) If set, the bantime must " | ||||
"be an absolute timestamp in seconds since epoch (Jan 1 1970 GMT)\n" | "be an absolute timestamp in seconds since epoch (Jan 1 1970 GMT)\n" | ||||
"\nExamples:\n" + | "\nExamples:\n" + | ||||
HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400") + | HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400") + | ||||
HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"") + | HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"") + | ||||
HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")); | HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")); | ||||
} | } | ||||
if (!g_connman) { | if (!g_banman) { | ||||
throw JSONRPCError( | throw JSONRPCError(RPC_DATABASE_ERROR, | ||||
RPC_CLIENT_P2P_DISABLED, | "Error: Ban database not loaded"); | ||||
"Error: Peer-to-peer functionality missing or disabled"); | |||||
} | } | ||||
CSubNet subNet; | CSubNet subNet; | ||||
CNetAddr netAddr; | CNetAddr netAddr; | ||||
bool isSubnet = false; | bool isSubnet = false; | ||||
if (request.params[0].get_str().find('/') != std::string::npos) { | if (request.params[0].get_str().find('/') != std::string::npos) { | ||||
isSubnet = true; | isSubnet = true; | ||||
} | } | ||||
if (!isSubnet) { | if (!isSubnet) { | ||||
CNetAddr resolved; | CNetAddr resolved; | ||||
LookupHost(request.params[0].get_str().c_str(), resolved, false); | LookupHost(request.params[0].get_str().c_str(), resolved, false); | ||||
netAddr = resolved; | netAddr = resolved; | ||||
} else { | } else { | ||||
LookupSubNet(request.params[0].get_str().c_str(), subNet); | LookupSubNet(request.params[0].get_str().c_str(), subNet); | ||||
} | } | ||||
if (!(isSubnet ? subNet.IsValid() : netAddr.IsValid())) { | if (!(isSubnet ? subNet.IsValid() : netAddr.IsValid())) { | ||||
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, | throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, | ||||
"Error: Invalid IP/Subnet"); | "Error: Invalid IP/Subnet"); | ||||
} | } | ||||
if (strCommand == "add") { | if (strCommand == "add") { | ||||
if (isSubnet ? g_connman->IsBanned(subNet) | if (isSubnet ? g_banman->IsBanned(subNet) | ||||
: g_connman->IsBanned(netAddr)) { | : g_banman->IsBanned(netAddr)) { | ||||
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, | throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, | ||||
"Error: IP/Subnet already banned"); | "Error: IP/Subnet already banned"); | ||||
} | } | ||||
// Use standard bantime if not specified. | // Use standard bantime if not specified. | ||||
int64_t banTime = 0; | int64_t banTime = 0; | ||||
if (request.params.size() >= 3 && !request.params[2].isNull()) { | if (request.params.size() >= 3 && !request.params[2].isNull()) { | ||||
banTime = request.params[2].get_int64(); | banTime = request.params[2].get_int64(); | ||||
} | } | ||||
bool absolute = false; | bool absolute = false; | ||||
if (request.params.size() == 4 && request.params[3].isTrue()) { | if (request.params.size() == 4 && request.params[3].isTrue()) { | ||||
absolute = true; | absolute = true; | ||||
} | } | ||||
if (isSubnet) { | if (isSubnet) { | ||||
g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute); | g_banman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute); | ||||
if (g_connman) { | |||||
g_connman->DisconnectNode(subNet); | g_connman->DisconnectNode(subNet); | ||||
} | |||||
} else { | } else { | ||||
g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute); | g_banman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute); | ||||
if (g_connman) { | |||||
g_connman->DisconnectNode(netAddr); | g_connman->DisconnectNode(netAddr); | ||||
} | } | ||||
} | |||||
} else if (strCommand == "remove") { | } else if (strCommand == "remove") { | ||||
if (!(isSubnet ? g_connman->Unban(subNet) | if (!(isSubnet ? g_banman->Unban(subNet) : g_banman->Unban(netAddr))) { | ||||
: g_connman->Unban(netAddr))) { | |||||
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, | throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, | ||||
"Error: Unban failed. Requested address/subnet " | "Error: Unban failed. Requested address/subnet " | ||||
"was not previously banned."); | "was not previously banned."); | ||||
} | } | ||||
} | } | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
static UniValue listbanned(const Config &config, | static UniValue listbanned(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
if (request.fHelp || request.params.size() != 0) { | if (request.fHelp || request.params.size() != 0) { | ||||
throw std::runtime_error("listbanned\n" | throw std::runtime_error("listbanned\n" | ||||
"\nList all banned IPs/Subnets.\n" | "\nList all banned IPs/Subnets.\n" | ||||
"\nExamples:\n" + | "\nExamples:\n" + | ||||
HelpExampleCli("listbanned", "") + | HelpExampleCli("listbanned", "") + | ||||
HelpExampleRpc("listbanned", "")); | HelpExampleRpc("listbanned", "")); | ||||
} | } | ||||
if (!g_connman) { | if (!g_banman) { | ||||
throw JSONRPCError( | throw JSONRPCError(RPC_DATABASE_ERROR, | ||||
RPC_CLIENT_P2P_DISABLED, | "Error: Ban database not loaded"); | ||||
"Error: Peer-to-peer functionality missing or disabled"); | |||||
} | } | ||||
banmap_t banMap; | banmap_t banMap; | ||||
g_connman->GetBanned(banMap); | g_banman->GetBanned(banMap); | ||||
UniValue bannedAddresses(UniValue::VARR); | UniValue bannedAddresses(UniValue::VARR); | ||||
for (const auto &entry : banMap) { | for (const auto &entry : banMap) { | ||||
const CBanEntry &banEntry = entry.second; | const CBanEntry &banEntry = entry.second; | ||||
UniValue rec(UniValue::VOBJ); | UniValue rec(UniValue::VOBJ); | ||||
rec.pushKV("address", entry.first.ToString()); | rec.pushKV("address", entry.first.ToString()); | ||||
rec.pushKV("banned_until", banEntry.nBanUntil); | rec.pushKV("banned_until", banEntry.nBanUntil); | ||||
rec.pushKV("ban_created", banEntry.nCreateTime); | rec.pushKV("ban_created", banEntry.nCreateTime); | ||||
Show All 10 Lines | static UniValue clearbanned(const Config &config, | ||||
if (request.fHelp || request.params.size() != 0) { | if (request.fHelp || request.params.size() != 0) { | ||||
throw std::runtime_error("clearbanned\n" | throw std::runtime_error("clearbanned\n" | ||||
"\nClear all banned IPs.\n" | "\nClear all banned IPs.\n" | ||||
"\nExamples:\n" + | "\nExamples:\n" + | ||||
HelpExampleCli("clearbanned", "") + | HelpExampleCli("clearbanned", "") + | ||||
HelpExampleRpc("clearbanned", "")); | HelpExampleRpc("clearbanned", "")); | ||||
} | } | ||||
if (!g_connman) { | if (!g_banman) { | ||||
throw JSONRPCError( | throw JSONRPCError( | ||||
RPC_CLIENT_P2P_DISABLED, | RPC_CLIENT_P2P_DISABLED, | ||||
"Error: Peer-to-peer functionality missing or disabled"); | "Error: Peer-to-peer functionality missing or disabled"); | ||||
} | } | ||||
g_connman->ClearBanned(); | g_banman->ClearBanned(); | ||||
return NullUniValue; | return NullUniValue; | ||||
} | } | ||||
static UniValue setnetworkactive(const Config &config, | static UniValue setnetworkactive(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
if (request.fHelp || request.params.size() != 1) { | if (request.fHelp || request.params.size() != 1) { | ||||
throw std::runtime_error( | throw std::runtime_error( | ||||
▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines |