@@ -465,3 +465,92 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp)
465
465
obj.push_back (Pair (" warnings" , GetWarnings (" statusbar" )));
466
466
return obj;
467
467
}
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
+ " \n Attempts add or remove a IP from the banned list.\n "
479
+ " \n Arguments:\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
+ " \n Examples:\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
+ " \n List all banned IPs.\n "
522
+ " \n Examples:\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
+ " \n Clear all banned IPs.\n "
548
+ " \n Examples:\n "
549
+ + HelpExampleCli (" clearbanned" , " " )
550
+ + HelpExampleRpc (" clearbanned" , " " )
551
+ );
552
+
553
+ CNode::ClearBanned ();
554
+
555
+ return Value::null;
556
+ }
0 commit comments