diff options
author | Jonas Schnelli <jonas.schnelli@include7.ch> | 2015-05-19 10:07:46 +0200 |
---|---|---|
committer | Jonas Schnelli <jonas.schnelli@include7.ch> | 2015-06-17 21:40:55 +0200 |
commit | d930b26a264ed7eae6ce239f3bfb4ff023df8195 (patch) | |
tree | cb7fbc9f01aac0da47b4ff2c67f8045c576ee4c6 /src | |
parent | 2252fb91cd19832c8baa63a10aaf7ce32bb400f8 (diff) |
[RPC] add setban/listbanned/clearbanned RPC commands
Diffstat (limited to 'src')
-rw-r--r-- | src/rpcclient.cpp | 1 | ||||
-rw-r--r-- | src/rpcnet.cpp | 89 | ||||
-rw-r--r-- | src/rpcserver.cpp | 3 | ||||
-rw-r--r-- | src/rpcserver.h | 3 |
4 files changed, 96 insertions, 0 deletions
diff --git a/src/rpcclient.cpp b/src/rpcclient.cpp index f254da5de0..1cc516e7be 100644 --- a/src/rpcclient.cpp +++ b/src/rpcclient.cpp @@ -93,6 +93,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "estimatepriority", 0 }, { "prioritisetransaction", 1 }, { "prioritisetransaction", 2 }, + { "setban", 2 }, }; class CRPCConvertTable diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp index aeaf54814f..6157a2d0a0 100644 --- a/src/rpcnet.cpp +++ b/src/rpcnet.cpp @@ -465,3 +465,92 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp) obj.push_back(Pair("warnings", GetWarnings("statusbar"))); return obj; } + +Value setban(const Array& params, bool fHelp) +{ + string strCommand; + if (params.size() >= 2) + strCommand = params[1].get_str(); + if (fHelp || params.size() < 2 || + (strCommand != "add" && strCommand != "remove")) + throw runtime_error( + "setban \"node\" \"add|remove\" (bantime)\n" + "\nAttempts add or remove a IP from the banned list.\n" + "\nArguments:\n" + "1. \"ip\" (string, required) The IP (see getpeerinfo for nodes ip)\n" + "2. \"command\" (string, required) 'add' to add a IP to the list, 'remove' to remove a IP from the list\n" + "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" + "\nExamples:\n" + + HelpExampleCli("setban", "\"192.168.0.6\" \"add\" 86400") + + HelpExampleRpc("setban", "\"192.168.0.6\", \"add\" 86400") + ); + + CNetAddr netAddr(params[0].get_str()); + if (!netAddr.IsValid()) + throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Invalid IP Address"); + + if (strCommand == "add") + { + if (CNode::IsBanned(netAddr)) + throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP already banned"); + + int64_t banTime = 0; //use standard bantime if not specified + if (params.size() == 3 && !params[2].is_null()) + banTime = params[2].get_int64(); + + CNode::Ban(netAddr, banTime); + + //disconnect possible nodes + while(CNode *bannedNode = FindNode(netAddr)) + bannedNode->CloseSocketDisconnect(); + } + else if(strCommand == "remove") + { + if (!CNode::Unban(netAddr)) + throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: Unban failed"); + } + + return Value::null; +} + +Value listbanned(const Array& params, bool fHelp) +{ + if (fHelp || params.size() != 0) + throw runtime_error( + "listbanned\n" + "\nList all banned IPs.\n" + "\nExamples:\n" + + HelpExampleCli("listbanned", "") + + HelpExampleRpc("listbanned", "") + ); + + std::map<CNetAddr, int64_t> banMap; + CNode::GetBanned(banMap); + + Array bannedAddresses; + for (std::map<CNetAddr, int64_t>::iterator it = banMap.begin(); it != banMap.end(); it++) + { + Object rec; + rec.push_back(Pair("address", (*it).first.ToString())); + rec.push_back(Pair("bannedtill", (*it).second)); + bannedAddresses.push_back(rec); + } + + return bannedAddresses; +} + +Value clearbanned(const Array& params, bool fHelp) +{ + if (fHelp || params.size() != 0) + throw runtime_error( + "clearbanned\n" + "\nClear all banned IPs.\n" + "\nExamples:\n" + + HelpExampleCli("clearbanned", "") + + HelpExampleRpc("clearbanned", "") + ); + + CNode::ClearBanned(); + + return Value::null; +} diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index c27bba519a..6d089c6738 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -279,6 +279,9 @@ static const CRPCCommand vRPCCommands[] = { "network", "getnettotals", &getnettotals, true }, { "network", "getpeerinfo", &getpeerinfo, true }, { "network", "ping", &ping, true }, + { "network", "setban", &setban, true }, + { "network", "listbanned", &listbanned, true }, + { "network", "clearbanned", &clearbanned, true }, /* Block chain and UTXO */ { "blockchain", "getblockchaininfo", &getblockchaininfo, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index fdd871d0b0..2b3a59a7ba 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -154,6 +154,9 @@ extern UniValue addnode(const UniValue& params, bool fHelp); extern UniValue disconnectnode(const UniValue& params, bool fHelp); extern UniValue getaddednodeinfo(const UniValue& params, bool fHelp); extern UniValue getnettotals(const UniValue& params, bool fHelp); +extern UniValue setban(const json_spirit::Array& params, bool fHelp); +extern UniValue listbanned(const json_spirit::Array& params, bool fHelp); +extern UniValue clearbanned(const json_spirit::Array& params, bool fHelp); extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp extern UniValue importprivkey(const UniValue& params, bool fHelp); |