Added bans console command

This commit is contained in:
SChernykh 2022-02-21 19:59:57 +01:00
parent 9a09d15bea
commit 9593311332
5 changed files with 68 additions and 1 deletions

View file

@ -70,7 +70,7 @@ typedef struct cmd {
cmdfunc *func;
} cmd;
static cmdfunc do_help, do_status, do_loglevel, do_addpeers, do_droppeers, do_showpeers, do_outpeers, do_inpeers, do_start_mining, do_stop_mining, do_exit;
static cmdfunc do_help, do_status, do_loglevel, do_addpeers, do_droppeers, do_showpeers, do_showbans, do_outpeers, do_inpeers, do_start_mining, do_stop_mining, do_exit;
static cmd cmds[] = {
{ STRCONST("help"), "", "display list of commands", do_help },
@ -79,6 +79,7 @@ static cmd cmds[] = {
{ STRCONST("addpeers"), "<peeraddr>", "add peer", do_addpeers },
{ STRCONST("droppeers"), "", "disconnect all peers", do_droppeers },
{ STRCONST("peers"), "", "show all peers", do_showpeers },
{ STRCONST("bans"), "", "show all banned IPs", do_showbans },
{ STRCONST("outpeers"), "", "set maximum number of outgoing connections", do_outpeers },
{ STRCONST("inpeers"), "", "set maximum number of incoming connections", do_inpeers },
{ STRCONST("start_mining"), "<threads>", "start mining", do_start_mining },
@ -145,6 +146,17 @@ static int do_showpeers(p2pool* m_pool, const char* /* args */)
return 0;
}
static int do_showbans(p2pool* m_pool, const char* /* args */)
{
if (m_pool->stratum_server()) {
m_pool->stratum_server()->print_bans();
}
if (m_pool->p2p_server()) {
m_pool->p2p_server()->print_bans();
}
return 0;
}
static int do_outpeers(p2pool* m_pool, const char* args)
{
if (m_pool->p2p_server()) {

View file

@ -320,6 +320,29 @@ NOINLINE void Stream::writeCurrentTime()
m_numberWidth = 1;
}
NOINLINE void put_rawip(const raw_ip& value, Stream* wrapper)
{
const char* addr_str;
char addr_str_buf[64];
static constexpr uint8_t ipv4_prefix[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255 };
const bool is_v6 = (memcmp(value.data, ipv4_prefix, 12) != 0);
if (is_v6) {
addr_str = inet_ntop(AF_INET6, value.data, addr_str_buf, sizeof(addr_str_buf));
}
else {
addr_str = inet_ntop(AF_INET, value.data + 12, addr_str_buf, sizeof(addr_str_buf));
}
if (addr_str) {
*wrapper << addr_str;
}
else {
*wrapper << "N/A";
}
}
} // namespace log
} // namespace p2pool

View file

@ -384,6 +384,13 @@ template<> struct log::Stream::Entry<NetworkType>
}
};
void put_rawip(const raw_ip& value, Stream* wrapper);
template<> struct log::Stream::Entry<raw_ip>
{
static FORCEINLINE void put(const raw_ip& value, Stream* wrapper) { put_rawip(value, wrapper); }
};
namespace {
template<log::Severity severity> void apply_severity(log::Stream&);

View file

@ -48,6 +48,7 @@ public:
virtual void on_connect_failed(bool is_v6, const raw_ip& ip, int port);
void ban(const raw_ip& ip, uint64_t seconds);
virtual void print_bans();
struct Client
{

View file

@ -501,6 +501,30 @@ void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::ban(const raw_ip& ip, uint64_t se
m_bans[ip] = ban_time;
}
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
void TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::print_bans()
{
using namespace std::chrono;
const auto cur_time = steady_clock::now();
std::vector<std::pair<raw_ip, std::chrono::steady_clock::time_point>> bans;
{
MutexLock lock(m_bansLock);
bans.reserve(m_bans.size());
for (const auto& b : m_bans) {
bans.emplace_back(std::make_pair(b.first, b.second));
}
}
for (const auto& b : bans) {
if (cur_time < b.second) {
const uint64_t t = duration_cast<seconds>(b.second - cur_time).count();
LOGINFO(0, b.first << " is banned (" << t << " seconds left)");
}
}
}
template<size_t READ_BUF_SIZE, size_t WRITE_BUF_SIZE>
bool TCPServer<READ_BUF_SIZE, WRITE_BUF_SIZE>::send_internal(Client* client, SendCallbackBase&& callback)
{