mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-07-06 21:02:32 +00:00

* [NOD-510] Change coinbase flags to kaspad. * [NOD-510] Removed superfluous spaces after periods in comments. * [NOD-510] Rename btcd -> kaspad in the root folder. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Rename BtcEncode -> KaspaEncode and BtcDecode -> KaspaDecode. * [NOD-510] Continue renaming btcd -> kaspad. * [NOD-510] Rename btcjson -> kaspajson. * [NOD-510] Rename file names inside kaspajson. * [NOD-510] Rename kaspajson -> jsonrpc. * [NOD-510] Finish renaming in addrmgr. * [NOD-510] Rename package btcec to ecc. * [NOD-510] Finish renaming stuff in blockdag. * [NOD-510] Rename stuff in cmd. * [NOD-510] Rename stuff in config. * [NOD-510] Rename stuff in connmgr. * [NOD-510] Rename stuff in dagconfig. * [NOD-510] Rename stuff in database. * [NOD-510] Rename stuff in docker. * [NOD-510] Rename stuff in integration. * [NOD-510] Rename jsonrpc to rpcmodel. * [NOD-510] Rename stuff in limits. * [NOD-510] Rename stuff in logger. * [NOD-510] Rename stuff in mempool. * [NOD-510] Rename stuff in mining. * [NOD-510] Rename stuff in netsync. * [NOD-510] Rename stuff in peer. * [NOD-510] Rename stuff in release. * [NOD-510] Rename stuff in rpcclient. * [NOD-510] Rename stuff in server. * [NOD-510] Rename stuff in signal. * [NOD-510] Rename stuff in txscript. * [NOD-510] Rename stuff in util. * [NOD-510] Rename stuff in wire. * [NOD-510] Fix failing tests. * [NOD-510] Fix merge errors. * [NOD-510] Fix go vet errors. * [NOD-510] Remove merged file that's no longer relevant. * [NOD-510] Add a comment above Op0. * [NOD-510] Fix some comments referencing Bitcoin Core. * [NOD-510] Fix some more comments referencing Bitcoin Core. * [NOD-510] Fix bitcoin -> kaspa. * [NOD-510] Fix more bitcoin -> kaspa. * [NOD-510] Fix comments, remove DisconnectBlock in addrindex. * [NOD-510] Rename KSPD to KASD. * [NOD-510] Fix comments and user agent.
114 lines
3.3 KiB
Go
114 lines
3.3 KiB
Go
package rpc
|
|
|
|
import (
|
|
"github.com/kaspanet/kaspad/logger"
|
|
"github.com/kaspanet/kaspad/rpcmodel"
|
|
"github.com/kaspanet/kaspad/server/serverutils"
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
// handleGetManualNodeInfo handles getManualNodeInfo commands.
|
|
func handleGetManualNodeInfo(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
|
c := cmd.(*rpcmodel.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.([]*rpcmodel.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, &rpcmodel.RPCError{
|
|
Code: rpcmodel.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([]*rpcmodel.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 rpcmodel.GetManualNodeInfoResult
|
|
result.ManualNode = peer.Addr()
|
|
result.Connected = rpcmodel.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.KaspadLookup(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([]rpcmodel.GetManualNodeInfoResultAddr, 0, len(ipList))
|
|
for _, ip := range ipList {
|
|
var addr rpcmodel.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
|
|
}
|