Skip to content

Commit d930b26

Browse files
committed
[RPC] add setban/listbanned/clearbanned RPC commands
1 parent 2252fb9 commit d930b26

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

src/rpcclient.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
9393
{ "estimatepriority", 0 },
9494
{ "prioritisetransaction", 1 },
9595
{ "prioritisetransaction", 2 },
96+
{ "setban", 2 },
9697
};
9798

9899
class CRPCConvertTable

src/rpcnet.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,92 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)
465465
obj.push_back(Pair("warnings", GetWarnings("statusbar")));
466466
return obj;
467467
}
468+
469+
Value setban(const Array& params, bool fHelp)
470+
{
471+
string strCommand;
472+
if (params.size() >= 2)
473+
strCommand = params[1].get_str();
474+
if (fHelp || params.size() < 2 ||
475+
(strCommand != "add" && strCommand != "remove"))
476+
throw runtime_error(
477+
"setban \"node\" \"add|remove\" (bantime)\n"
478+
"\nAttempts add or remove a IP from the banned list.\n"
479+
"\nArguments:\n"
480+
"1. \"ip\" (string, required) The IP (see getpeerinfo for nodes ip)\n"
481+
"2. \"command\" (string, required) 'add' to add a IP to the list, 'remove' to remove a IP from the list\n"
482+
"1. \"bantime\" (numeric, optional) time in seconds how long the ip is banned (0 or empty means using the default time of 24h which can also be overwritten by the -bantime startup argument)\n"
483+
"\nExamples:\n"
484+
+ HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400")
485+
+ HelpExampleRpc("setban", "\"192.168.0.6\", \"add\" 86400")
486+
);
487+
488+
CNetAddr netAddr(params[0].get_str());
489+
if (!netAddr.IsValid())
490+
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Invalid IP Address");
491+
492+
if (strCommand == "add")
493+
{
494+
if (CNode::IsBanned(netAddr))
495+
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP already banned");
496+
497+
int64_t banTime = 0; //use standard bantime if not specified
498+
if (params.size() == 3 && !params[2].is_null())
499+
banTime = params[2].get_int64();
500+
501+
CNode::Ban(netAddr, banTime);
502+
503+
//disconnect possible nodes
504+
while(CNode *bannedNode = FindNode(netAddr))
505+
bannedNode->CloseSocketDisconnect();
506+
}
507+
else if(strCommand == "remove")
508+
{
509+
if (!CNode::Unban(netAddr))
510+
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Unban failed");
511+
}
512+
513+
return Value::null;
514+
}
515+
516+
Value listbanned(const Array& params, bool fHelp)
517+
{
518+
if (fHelp || params.size() != 0)
519+
throw runtime_error(
520+
"listbanned\n"
521+
"\nList all banned IPs.\n"
522+
"\nExamples:\n"
523+
+ HelpExampleCli("listbanned", "")
524+
+ HelpExampleRpc("listbanned", "")
525+
);
526+
527+
std::map<CNetAddr, int64_t> banMap;
528+
CNode::GetBanned(banMap);
529+
530+
Array bannedAddresses;
531+
for (std::map<CNetAddr, int64_t>::iterator it = banMap.begin(); it != banMap.end(); it++)
532+
{
533+
Object rec;
534+
rec.push_back(Pair("address", (*it).first.ToString()));
535+
rec.push_back(Pair("bannedtill", (*it).second));
536+
bannedAddresses.push_back(rec);
537+
}
538+
539+
return bannedAddresses;
540+
}
541+
542+
Value clearbanned(const Array& params, bool fHelp)
543+
{
544+
if (fHelp || params.size() != 0)
545+
throw runtime_error(
546+
"clearbanned\n"
547+
"\nClear all banned IPs.\n"
548+
"\nExamples:\n"
549+
+ HelpExampleCli("clearbanned", "")
550+
+ HelpExampleRpc("clearbanned", "")
551+
);
552+
553+
CNode::ClearBanned();
554+
555+
return Value::null;
556+
}

src/rpcserver.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ static const CRPCCommand vRPCCommands[] =
279279
{ "network", "getnettotals", &getnettotals, true },
280280
{ "network", "getpeerinfo", &getpeerinfo, true },
281281
{ "network", "ping", &ping, true },
282+
{ "network", "setban", &setban, true },
283+
{ "network", "listbanned", &listbanned, true },
284+
{ "network", "clearbanned", &clearbanned, true },
282285

283286
/* Block chain and UTXO */
284287
{ "blockchain", "getblockchaininfo", &getblockchaininfo, true },

src/rpcserver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ extern UniValue addnode(const UniValue& params, bool fHelp);
154154
extern UniValue disconnectnode(const UniValue& params, bool fHelp);
155155
extern UniValue getaddednodeinfo(const UniValue& params, bool fHelp);
156156
extern UniValue getnettotals(const UniValue& params, bool fHelp);
157+
extern UniValue setban(const json_spirit::Array& params, bool fHelp);
158+
extern UniValue listbanned(const json_spirit::Array& params, bool fHelp);
159+
extern UniValue clearbanned(const json_spirit::Array& params, bool fHelp);
157160

158161
extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp
159162
extern UniValue importprivkey(const UniValue& params, bool fHelp);

0 commit comments

Comments
 (0)