kaspad/server/rpc/handle_get_manual_node_info.go
stasatdaglabs 40342eb45a [NOD-275] Split rpcserver.go to separate files (#417)
* [NOD-275] Moved getBlockTemplate and related functionality to a separate file.

* [NOD-275] Started moving handlers to separate files.

* [NOD-275] Fixed merge errors.

* [NOD-275] Moved all handlers out of rpcserver.go.

* [NOD-275] Moved non-shared functions out of rpcserver.go.

* [NOD-275] Moved handleGetAllManualNodesInfo to a separate file.

* [NOD-275] Moved handlers out of rpcwebsocket.go to separate files.

* [NOD-275] Fixed import error.

* [NOD-275] Renamed all handler files to include underscores.

* [NOD-275] Moved common rpc helper functions to common.go.
2019-09-22 16:41:37 +03:00

114 lines
3.3 KiB
Go

package rpc
import (
"github.com/daglabs/btcd/btcjson"
"github.com/daglabs/btcd/logger"
"github.com/daglabs/btcd/server/serverutils"
"net"
"strings"
)
// handleGetManualNodeInfo handles getManualNodeInfo commands.
func handleGetManualNodeInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*btcjson.GetManualNodeInfoCmd)
results, err := getManualNodesInfo(s, c.Details, c.Node)
if err != nil {
return nil, err
}
if resultsNonDetailed, ok := results.([]string); ok {
return resultsNonDetailed[0], nil
}
resultsDetailed := results.([]*btcjson.GetManualNodeInfoResult)
return resultsDetailed[0], nil
}
// getManualNodesInfo handles getManualNodeInfo and getAllManualNodesInfo commands.
func getManualNodesInfo(s *Server, detailsArg *bool, node string) (interface{}, error) {
details := detailsArg == nil || *detailsArg
// Retrieve a list of persistent (manual) peers from the server and
// filter the list of peers per the specified address (if any).
peers := s.cfg.ConnMgr.PersistentPeers()
if node != "" {
found := false
for i, peer := range peers {
if peer.ToPeer().Addr() == node {
peers = peers[i : i+1]
found = true
}
}
if !found {
return nil, &btcjson.RPCError{
Code: btcjson.ErrRPCClientNodeNotAdded,
Message: "Node has not been added",
}
}
}
// Without the details flag, the result is just a slice of the addresses as
// strings.
if !details {
results := make([]string, 0, len(peers))
for _, peer := range peers {
results = append(results, peer.ToPeer().Addr())
}
return results, nil
}
// With the details flag, the result is an array of JSON objects which
// include the result of DNS lookups for each peer.
results := make([]*btcjson.GetManualNodeInfoResult, 0, len(peers))
for _, rpcPeer := range peers {
// Set the "address" of the peer which could be an ip address
// or a domain name.
peer := rpcPeer.ToPeer()
var result btcjson.GetManualNodeInfoResult
result.ManualNode = peer.Addr()
result.Connected = btcjson.Bool(peer.Connected())
// Split the address into host and port portions so we can do
// a DNS lookup against the host. When no port is specified in
// the address, just use the address as the host.
host, _, err := net.SplitHostPort(peer.Addr())
if err != nil {
host = peer.Addr()
}
var ipList []string
switch {
case net.ParseIP(host) != nil, strings.HasSuffix(host, ".onion"):
ipList = make([]string, 1)
ipList[0] = host
default:
// Do a DNS lookup for the address. If the lookup fails, just
// use the host.
ips, err := serverutils.BTCDLookup(host)
if err != nil {
ipList = make([]string, 1)
ipList[0] = host
break
}
ipList = make([]string, 0, len(ips))
for _, ip := range ips {
ipList = append(ipList, ip.String())
}
}
// Add the addresses and connection info to the result.
addrs := make([]btcjson.GetManualNodeInfoResultAddr, 0, len(ipList))
for _, ip := range ipList {
var addr btcjson.GetManualNodeInfoResultAddr
addr.Address = ip
addr.Connected = "false"
if ip == host && peer.Connected() {
addr.Connected = logger.DirectionString(peer.Inbound())
}
addrs = append(addrs, addr)
}
result.Addresses = &addrs
results = append(results, &result)
}
return results, nil
}