diff --git a/libp2p/Host.h b/libp2p/Host.h index 0fe6da5f9..d015a2f65 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -105,6 +105,20 @@ private: SharedMutex mutable x_nodes; }; +struct NodeInfo +{ + NodeInfo() = default; + NodeInfo(NodeId const& _id, std::string const& _address, unsigned _port, std::string const& _version): + id(_id), address(_address), port(_port), version(_version) {} + + std::string enode() const { return "enode://" + id.hex() + "@" + address + ":" + toString(port); } + + NodeId id; + std::string address; + unsigned port; + std::string version; +}; + /** * @brief The Host class * Capabilities should be registered prior to startNetwork, since m_capabilities is not thread-safe. @@ -197,14 +211,24 @@ public: /// @returns if network is started and interactive. bool haveNetwork() const { return m_run && !!m_nodeTable; } - NodeId id() const { return m_alias.pub(); } - /// Validates and starts peer session, taking ownership of _io. Disconnects and returns false upon error. void startPeerSession(Public const& _id, RLP const& _hello, RLPXFrameCoder* _io, std::shared_ptr const& _s); /// Get session by id std::shared_ptr peerSession(NodeId const& _id) { RecursiveGuard l(x_sessions); return m_sessions.count(_id) ? m_sessions[_id].lock() : std::shared_ptr(); } + /// Get our current node ID. + NodeId id() const { return m_alias.pub(); } + + /// Get the public TCP endpoint. + bi::tcp::endpoint const& tcpPublic() const { return m_tcpPublic; } + + /// Get the public endpoint information. + std::string enode() const { return "enode://" + id().hex() + "@" + (networkPreferences().publicIPAddress.empty() ? m_tcpPublic.address().to_string() : networkPreferences().publicIPAddress) + ":" + toString(m_tcpPublic.port()); } + + /// Get the node information. + p2p::NodeInfo nodeInfo() const { return NodeInfo(id(), (networkPreferences().publicIPAddress.empty() ? m_tcpPublic.address().to_string() : networkPreferences().publicIPAddress), m_tcpPublic.port(), m_clientVersion); } + protected: void onNodeTableEvent(NodeId const& _n, NodeTableEventType const& _e); diff --git a/libweb3jsonrpc/WebThreeStubServerBase.cpp b/libweb3jsonrpc/WebThreeStubServerBase.cpp index bf0576039..76d84ccc4 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.cpp +++ b/libweb3jsonrpc/WebThreeStubServerBase.cpp @@ -558,6 +558,20 @@ Json::Value WebThreeStubServerBase::admin_net_peers(std::string const& _session) return ret; } +Json::Value WebThreeStubServerBase::admin_net_nodeInfo(const string& _session) +{ + ADMIN; + Json::Value ret; + p2p::NodeInfo i = network()->nodeInfo(); + ret["name"] = i.version; + ret["port"] = i.port; + ret["address"] = i.address; + ret["listenAddr"] = i.address + ":" + toString(i.port); + ret["id"] = i.id.hex(); + ret["enode"] = i.enode(); + return ret; +} + bool WebThreeStubServerBase::admin_eth_setMining(bool _on, std::string const& _session) { ADMIN; diff --git a/libweb3jsonrpc/WebThreeStubServerBase.h b/libweb3jsonrpc/WebThreeStubServerBase.h index 4230f7237..98eff9b6e 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.h +++ b/libweb3jsonrpc/WebThreeStubServerBase.h @@ -165,6 +165,7 @@ public: virtual bool admin_net_stop(std::string const& _session); virtual bool admin_net_connect(std::string const& _node, std::string const& _session); virtual Json::Value admin_net_peers(std::string const& _session); + virtual Json::Value admin_net_nodeInfo(std::string const& _session); virtual bool admin_eth_setMining(bool _on, std::string const& _session); virtual Json::Value admin_eth_blockQueueStatus(std::string const& _session) { (void)_session; return Json::Value(); } diff --git a/libwebthree/WebThree.h b/libwebthree/WebThree.h index 6eefa6a4b..5e251f974 100644 --- a/libwebthree/WebThree.h +++ b/libwebthree/WebThree.h @@ -53,6 +53,9 @@ namespace bzz { class Interface; } class WebThreeNetworkFace { public: + /// Get information concerning this node. + virtual p2p::NodeInfo nodeInfo() const = 0; + /// Get information on the current peer set. virtual std::vector peers() = 0; @@ -90,7 +93,8 @@ public: /// Is network working? there may not be any peers yet. virtual bool isNetworkStarted() const = 0; - std::string enode() const { return "enode://" + toHex(id().ref()) + "@" + (networkPreferences().publicIPAddress.empty() ? "127.0.0.1" : networkPreferences().publicIPAddress) + ":" + toString(networkPreferences().listenPort); } + /// Get enode string. + virtual std::string enode() const = 0; }; @@ -173,8 +177,12 @@ public: void setNetworkPreferences(p2p::NetworkPreferences const& _n, bool _dropPeers = false) override; + p2p::NodeInfo nodeInfo() const override { return m_net.nodeInfo(); } + p2p::NodeId id() const override { return m_net.id(); } + std::string enode() const override { return m_net.enode(); } + /// Gets the nodes. p2p::Peers nodes() const override { return m_net.getPeers(); }