mirror of
https://github.com/PeernetOfficial/Cmd.git
synced 2026-07-17 02:47:52 +01:00
Add "debug connect" command.
This commit is contained in:
93
Command Debug.go
Normal file
93
Command Debug.go
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
File Name: Command Debug.go
|
||||
Copyright: 2021 Peernet Foundation s.r.o.
|
||||
Author: Peter Kleissner
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/dht"
|
||||
)
|
||||
|
||||
// debugCmdConnect connects to the node ID
|
||||
func debugCmdConnect(nodeID []byte) {
|
||||
fmt.Printf("---------------- Connect to node %s ----------------\n", hex.EncodeToString(nodeID))
|
||||
defer fmt.Printf("---------------- done node %s ----------------\n", hex.EncodeToString(nodeID))
|
||||
|
||||
// in local DHT list?
|
||||
_, peer := core.IsNodeContact(nodeID)
|
||||
if peer != nil {
|
||||
fmt.Printf("* In local routing table: Yes.\n")
|
||||
} else {
|
||||
fmt.Printf("* In local routing table: No. Lookup via DHT. Timeout = 10 seconds.\n")
|
||||
|
||||
addKeyMonitor(nodeID)
|
||||
defer removeKeyMonitor(nodeID)
|
||||
|
||||
// Discovery via DHT.
|
||||
_, peer, _ = core.FindNode(nodeID, time.Second*10)
|
||||
if peer == nil {
|
||||
fmt.Printf("Not found via DHT :(\n")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Successfully discovered via DHT.\n")
|
||||
}
|
||||
|
||||
// virtual peer?
|
||||
if peer.IsVirtual() {
|
||||
fmt.Printf("* Peer is virtual and was not contacted before. It will show no active connections until contacted.\n")
|
||||
}
|
||||
|
||||
fmt.Printf("* Peer details:\n")
|
||||
fmt.Printf("%s", textPeerConnections(peer))
|
||||
|
||||
// ping via all connections TODO
|
||||
//fmt.Printf("* Sending ping:\n")
|
||||
}
|
||||
|
||||
// debug output of monitored keys searched in the DHT
|
||||
|
||||
var monitorKeys map[string]struct{}
|
||||
var monitorKeysMutex sync.RWMutex
|
||||
|
||||
func addKeyMonitor(key []byte) {
|
||||
monitorKeysMutex.Lock()
|
||||
monitorKeys[string(key)] = struct{}{}
|
||||
monitorKeysMutex.Unlock()
|
||||
}
|
||||
|
||||
func removeKeyMonitor(key []byte) {
|
||||
monitorKeysMutex.Lock()
|
||||
delete(monitorKeys, string(key))
|
||||
monitorKeysMutex.Unlock()
|
||||
}
|
||||
|
||||
func filterSearchStatus(client *dht.SearchClient, function, format string, v ...interface{}) {
|
||||
// check if the search client is actively being monitored
|
||||
monitorKeysMutex.Lock()
|
||||
_, ok := monitorKeys[string(client.Key)]
|
||||
monitorKeysMutex.Unlock()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
keyA := client.Key
|
||||
if len(keyA) > 8 {
|
||||
keyA = keyA[:8]
|
||||
}
|
||||
|
||||
intend := " ->"
|
||||
if function == "sendInfoRequest" {
|
||||
intend = " >"
|
||||
}
|
||||
|
||||
fmt.Printf(intend+" ["+function+" "+hex.EncodeToString(keyA)+"] "+format, v...)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"net"
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/btcsuite/btcd/btcec"
|
||||
)
|
||||
|
||||
func getUserOptionString(reader *bufio.Reader) (response string, valid bool) {
|
||||
@@ -88,6 +90,7 @@ func showHelp() {
|
||||
"peer list List current peers\n" +
|
||||
"debug key create Create Public-Private Key pair\n" +
|
||||
"debug key self List current Public-Private Key pair\n" +
|
||||
"debug connect Attempts to connect to the target peer\n" +
|
||||
"hash Create blake3 hash of input\n" +
|
||||
"warehouse get Get data from local warehouse by hash\n" +
|
||||
"warehouse store Store data into local warehouse\n" +
|
||||
@@ -144,7 +147,7 @@ func userCommands() {
|
||||
}
|
||||
userAgent := strings.ToValidUTF8(peer.UserAgent, "?")
|
||||
|
||||
fmt.Printf("* %s%s\n User Agent: %s\n\n%s Packets sent: %d\n Packets received: %d\n\n", hex.EncodeToString(peer.PublicKey.SerializeCompressed()), info, userAgent, textPeerConnections(peer), peer.StatsPacketSent, peer.StatsPacketReceived)
|
||||
fmt.Printf("* Peer ID %s%s\n Node ID %s\n User Agent: %s\n\n%s Packets sent: %d\n Packets received: %d\n\n", hex.EncodeToString(peer.PublicKey.SerializeCompressed()), info, hex.EncodeToString(peer.NodeID), userAgent, textPeerConnections(peer), peer.StatsPacketSent, peer.StatsPacketReceived)
|
||||
}
|
||||
|
||||
case "chat all", "chat":
|
||||
@@ -293,6 +296,48 @@ func userCommands() {
|
||||
config.ErrorOutput = number
|
||||
}
|
||||
|
||||
case "debug connect":
|
||||
fmt.Printf("Please specify the target peer to connect to via DHT lookup, either by peer ID or node ID:\n")
|
||||
text, valid := getUserOptionString(reader)
|
||||
if !valid || (len(text) != 66 && len(text) != 64) {
|
||||
fmt.Printf("Invalid peer ID or node ID. It must be hex-encoded and 66 (peer ID) or 64 characters (node ID) long.")
|
||||
break
|
||||
}
|
||||
|
||||
// node ID is required
|
||||
var nodeID []byte
|
||||
var err error
|
||||
|
||||
if len(text) == 66 {
|
||||
// Assume peer ID was supplied.
|
||||
publicKeyB, err := hex.DecodeString(text)
|
||||
if err != nil || len(publicKeyB) != 33 {
|
||||
fmt.Printf("Invalid peer ID encoding.")
|
||||
break
|
||||
}
|
||||
|
||||
publicKey, err := btcec.ParsePubKey(publicKeyB, btcec.S256())
|
||||
if err != nil {
|
||||
fmt.Printf("Invalid peer ID (public key decoding failed).")
|
||||
continue
|
||||
}
|
||||
|
||||
nodeID = core.PublicKey2NodeID(publicKey)
|
||||
} else {
|
||||
// Node ID was supplied.
|
||||
if nodeID, err = hex.DecodeString(text); err != nil || len(nodeID) != 256/8 {
|
||||
fmt.Printf("Invalid node ID encoding.")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// is self?
|
||||
if bytes.Equal(nodeID, core.SelfNodeID()) {
|
||||
fmt.Printf("Target node is self.")
|
||||
break
|
||||
}
|
||||
|
||||
debugCmdConnect(nodeID)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -466,13 +511,13 @@ func GetPeerlistSorted() (peers []*core.PeerInfo) {
|
||||
func logError(function, format string, v ...interface{}) {
|
||||
switch config.ErrorOutput {
|
||||
case 0:
|
||||
core.DefaultLogError(function, format, v)
|
||||
core.DefaultLogError(function, format, v...)
|
||||
|
||||
case 1:
|
||||
fmt.Printf("["+function+"] "+format, v...)
|
||||
|
||||
case 2:
|
||||
core.DefaultLogError(function, format, v)
|
||||
core.DefaultLogError(function, format, v...)
|
||||
fmt.Printf("["+function+"] "+format, v...)
|
||||
}
|
||||
}
|
||||
|
||||
3
Main.go
3
Main.go
@@ -41,8 +41,11 @@ func init() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
monitorKeys = make(map[string]struct{})
|
||||
|
||||
core.UserAgent = appName + "/" + core.Version
|
||||
core.Filters.LogError = logError
|
||||
core.Filters.DHTSearchStatus = filterSearchStatus
|
||||
|
||||
core.Init()
|
||||
}
|
||||
|
||||
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module github.com/PeernetOfficial/Cmd
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/PeernetOfficial/core v0.0.0-20210706113727-27fdafd23135
|
||||
require (
|
||||
github.com/PeernetOfficial/core v0.0.0-20210727233333-e5d09e70866a
|
||||
github.com/btcsuite/btcd v0.22.0-beta.0.20210625194946-86a17263b0ff
|
||||
)
|
||||
|
||||
9
go.sum
9
go.sum
@@ -1,11 +1,10 @@
|
||||
github.com/PeernetOfficial/core v0.0.0-20210706113727-27fdafd23135 h1:sTSVUqT6zja5hwjpQIodNxxiDTwFiPRw4Fm1rDT3QVY=
|
||||
github.com/PeernetOfficial/core v0.0.0-20210706113727-27fdafd23135/go.mod h1:NglFBSMGFtMqR9ugIR0H244XYPvE1eC5H0X6sergW5U=
|
||||
github.com/PeernetOfficial/core/dht v0.0.0-20210506124834-e929d6ba2f22 h1:FHBHtQR3ShxuxkG8tgE4ER86UmbNdXam/I1irdiusqg=
|
||||
github.com/PeernetOfficial/core/dht v0.0.0-20210506124834-e929d6ba2f22/go.mod h1:sb2H21VIVTohCXVrDFo/Skl2tN8Yzba+MXSS6NgCYXw=
|
||||
github.com/PeernetOfficial/core v0.0.0-20210727233333-e5d09e70866a h1:8yM6YODL7rvrYvuVEQ74wQVW310uKEqNxgbPaiax2YQ=
|
||||
github.com/PeernetOfficial/core v0.0.0-20210727233333-e5d09e70866a/go.mod h1:9aIe6MS6tefJnfmyEZFqaNF4NIeq7wJutV7EU+uowps=
|
||||
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
|
||||
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
|
||||
github.com/btcsuite/btcd v0.21.0-beta.0.20210401013323-36a96f6a0025 h1:aoVqvZk4mLyF3WZbqEVPq+vXnwL2wekZg4P4mjYJNLs=
|
||||
github.com/btcsuite/btcd v0.21.0-beta.0.20210401013323-36a96f6a0025/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA=
|
||||
github.com/btcsuite/btcd v0.22.0-beta.0.20210625194946-86a17263b0ff h1:7aWbvIwkXchSCIWgobzJ+a6l87s6WHkJ78sZqxSpQCk=
|
||||
github.com/btcsuite/btcd v0.22.0-beta.0.20210625194946-86a17263b0ff/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA=
|
||||
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
|
||||
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
|
||||
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o=
|
||||
|
||||
Reference in New Issue
Block a user