Skip to content

Commit

Permalink
Fix p2p changes after removal of debug commands/stat_info
Browse files Browse the repository at this point in the history
  • Loading branch information
who-biz committed Mar 21, 2019
1 parent 40b0e10 commit 4826319
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 138 deletions.
14 changes: 1 addition & 13 deletions src/p2p/net_node.h
Expand Up @@ -132,20 +132,15 @@ namespace nodetool
bool islimitup=false;
bool islimitdown=false;

typedef COMMAND_REQUEST_STAT_INFO_T<typename t_payload_net_handler::stat_info> COMMAND_REQUEST_STAT_INFO;

CHAIN_LEVIN_INVOKE_MAP2(p2p_connection_context); //move levin_commands_handler interface invoke(...) callbacks into invoke map
CHAIN_LEVIN_NOTIFY_MAP2(p2p_connection_context); //move levin_commands_handler interface notify(...) callbacks into nothing

BEGIN_INVOKE_MAP2(node_server)
HANDLE_INVOKE_T2(COMMAND_HANDSHAKE, &node_server::handle_handshake)
HANDLE_INVOKE_T2(COMMAND_TIMED_SYNC, &node_server::handle_timed_sync)
HANDLE_INVOKE_T2(COMMAND_PING, &node_server::handle_ping)
#ifdef ALLOW_DEBUG_COMMANDS
HANDLE_INVOKE_T2(COMMAND_REQUEST_STAT_INFO, &node_server::handle_get_stat_info)
HANDLE_INVOKE_T2(COMMAND_REQUEST_NETWORK_STATE, &node_server::handle_get_network_state)
HANDLE_INVOKE_T2(COMMAND_REQUEST_PEER_ID, &node_server::handle_get_peer_id)
#endif
HANDLE_INVOKE_T2(COMMAND_REQUEST_NETWORK_STATE, &node_server::handle_get_network_state)
HANDLE_INVOKE_T2(COMMAND_REQUEST_SUPPORT_FLAGS, &node_server::handle_get_support_flags)
CHAIN_INVOKE_MAP_TO_OBJ_FORCE_CONTEXT(m_payload_handler, typename t_payload_net_handler::connection_context&)
END_INVOKE_MAP2()
Expand All @@ -156,17 +151,13 @@ namespace nodetool
int handle_handshake(int command, typename COMMAND_HANDSHAKE::request& arg, typename COMMAND_HANDSHAKE::response& rsp, p2p_connection_context& context);
int handle_timed_sync(int command, typename COMMAND_TIMED_SYNC::request& arg, typename COMMAND_TIMED_SYNC::response& rsp, p2p_connection_context& context);
int handle_ping(int command, COMMAND_PING::request& arg, COMMAND_PING::response& rsp, p2p_connection_context& context);
#ifdef ALLOW_DEBUG_COMMANDS
int handle_get_stat_info(int command, typename COMMAND_REQUEST_STAT_INFO::request& arg, typename COMMAND_REQUEST_STAT_INFO::response& rsp, p2p_connection_context& context);
int handle_get_network_state(int command, COMMAND_REQUEST_NETWORK_STATE::request& arg, COMMAND_REQUEST_NETWORK_STATE::response& rsp, p2p_connection_context& context);
int handle_get_peer_id(int command, COMMAND_REQUEST_PEER_ID::request& arg, COMMAND_REQUEST_PEER_ID::response& rsp, p2p_connection_context& context);
#endif
int handle_get_support_flags(int command, COMMAND_REQUEST_SUPPORT_FLAGS::request& arg, COMMAND_REQUEST_SUPPORT_FLAGS::response& rsp, p2p_connection_context& context);
bool init_config();
bool make_default_peer_id();
bool make_default_config();
bool store_config();
bool check_trust(const proof_of_trust& tr);


//----------------- levin_commands_handler -------------------------------------------------------------
Expand Down Expand Up @@ -306,9 +297,6 @@ namespace nodetool

std::string m_bind_ip;
std::string m_port;
#ifdef ALLOW_DEBUG_COMMANDS
uint64_t m_last_stat_request_time;
#endif
std::list<epee::net_utils::network_address> m_priority_peers;
std::vector<epee::net_utils::network_address> m_exclusive_peers;
std::vector<epee::net_utils::network_address> m_seed_nodes;
Expand Down
58 changes: 0 additions & 58 deletions src/p2p/net_node.inl
Expand Up @@ -556,8 +556,6 @@ namespace nodetool
//only in case if we really sure that we have external visible ip
m_have_address = true;
m_ip_address = 0;
m_last_stat_request_time = 0;

//configure self
m_net_server.set_threads_prefix("P2P");
m_net_server.get_config_object().set_handler(this);
Expand Down Expand Up @@ -1391,64 +1389,9 @@ namespace nodetool
return true;
}
//-----------------------------------------------------------------------------------
#ifdef ALLOW_DEBUG_COMMANDS
template<class t_payload_net_handler>
bool node_server<t_payload_net_handler>::check_trust(const proof_of_trust& tr)
{
uint64_t local_time = time(NULL);
uint64_t time_delata = local_time > tr.time ? local_time - tr.time: tr.time - local_time;
if(time_delata > 24*60*60 )
{
MWARNING("check_trust failed to check time conditions, local_time=" << local_time << ", proof_time=" << tr.time);
return false;
}
if(m_last_stat_request_time >= tr.time )
{
MWARNING("check_trust failed to check time conditions, last_stat_request_time=" << m_last_stat_request_time << ", proof_time=" << tr.time);
return false;
}
if(m_config.m_peer_id != tr.peer_id)
{
MWARNING("check_trust failed: peer_id mismatch (passed " << tr.peer_id << ", expected " << m_config.m_peer_id<< ")");
return false;
}
crypto::public_key pk = AUTO_VAL_INIT(pk);
epee::string_tools::hex_to_pod(::config::P2P_REMOTE_DEBUG_TRUSTED_PUB_KEY, pk);
crypto::hash h = get_proof_of_trust_hash(tr);
if(!crypto::check_signature(h, pk, tr.sign))
{
MWARNING("check_trust failed: sign check failed");
return false;
}
//update last request time
m_last_stat_request_time = tr.time;
return true;
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
int node_server<t_payload_net_handler>::handle_get_stat_info(int command, typename COMMAND_REQUEST_STAT_INFO::request& arg, typename COMMAND_REQUEST_STAT_INFO::response& rsp, p2p_connection_context& context)
{
if(!check_trust(arg.tr))
{
drop_connection(context);
return 1;
}
rsp.connections_count = m_net_server.get_config_object().get_connections_count();
rsp.incoming_connections_count = rsp.connections_count - get_outgoing_connections_count();
rsp.version = MONERO_VERSION_FULL;
rsp.os_version = tools::get_os_version_string();
m_payload_handler.get_stat_info(rsp.payload_info);
return 1;
}
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
int node_server<t_payload_net_handler>::handle_get_network_state(int command, COMMAND_REQUEST_NETWORK_STATE::request& arg, COMMAND_REQUEST_NETWORK_STATE::response& rsp, p2p_connection_context& context)
{
if(!check_trust(arg.tr))
{
drop_connection(context);
return 1;
}
m_net_server.get_config_object().foreach_connection([&](const p2p_connection_context& cntxt)
{
connection_entry ce;
Expand All @@ -1471,7 +1414,6 @@ namespace nodetool
rsp.my_id = m_config.m_peer_id;
return 1;
}
#endif
//-----------------------------------------------------------------------------------
template<class t_payload_net_handler>
int node_server<t_payload_net_handler>::handle_get_support_flags(int command, COMMAND_REQUEST_SUPPORT_FLAGS::request& arg, COMMAND_REQUEST_SUPPORT_FLAGS::response& rsp, p2p_connection_context& context)
Expand Down
68 changes: 1 addition & 67 deletions src/p2p/p2p_protocol_defs.h
Expand Up @@ -37,9 +37,6 @@
#include "string_tools.h"
#include "time_helper.h"
#include "cryptonote_config.h"
#ifdef ALLOW_DEBUG_COMMANDS
#include "crypto/crypto.h"
#endif

namespace nodetool
{
Expand Down Expand Up @@ -162,7 +159,7 @@ namespace nodetool
KV_SERIALIZE(my_port)
END_KV_SERIALIZE_MAP()
};


#define P2P_COMMANDS_POOL_BASE 1000

Expand Down Expand Up @@ -321,57 +318,6 @@ namespace nodetool
};
};


#ifdef ALLOW_DEBUG_COMMANDS
//These commands are considered as insecure, and made in debug purposes for a limited lifetime.
//Anyone who feel unsafe with this commands can disable the ALLOW_GET_STAT_COMMAND macro.

struct proof_of_trust
{
peerid_type peer_id;
uint64_t time;
crypto::signature sign;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(peer_id)
KV_SERIALIZE(time)
KV_SERIALIZE_VAL_POD_AS_BLOB(sign)
END_KV_SERIALIZE_MAP()
};


template<class payload_stat_info>
struct COMMAND_REQUEST_STAT_INFO_T
{
const static int ID = P2P_COMMANDS_POOL_BASE + 4;

struct request
{
proof_of_trust tr;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tr)
END_KV_SERIALIZE_MAP()
};

struct response
{
std::string version;
std::string os_version;
uint64_t connections_count;
uint64_t incoming_connections_count;
payload_stat_info payload_info;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(version)
KV_SERIALIZE(os_version)
KV_SERIALIZE(connections_count)
KV_SERIALIZE(incoming_connections_count)
KV_SERIALIZE(payload_info)
END_KV_SERIALIZE_MAP()
};
};


/************************************************************************/
/* */
/************************************************************************/
Expand All @@ -381,9 +327,7 @@ namespace nodetool

struct request
{
proof_of_trust tr;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tr)
END_KV_SERIALIZE_MAP()
};

Expand Down Expand Up @@ -450,16 +394,6 @@ namespace nodetool
};
};

#endif


inline crypto::hash get_proof_of_trust_hash(const nodetool::proof_of_trust& pot)
{
std::string s;
s.append(reinterpret_cast<const char*>(&pot.peer_id), sizeof(pot.peer_id));
s.append(reinterpret_cast<const char*>(&pot.time), sizeof(pot.time));
return crypto::cn_fast_hash(s.data(), s.size());
}

}

Expand Down

0 comments on commit 4826319

Please sign in to comment.