diff --git a/Command Debug.go b/Command Debug.go index 19d366b..bacfaf0 100644 --- a/Command Debug.go +++ b/Command Debug.go @@ -34,11 +34,11 @@ func debugCmdConnect(nodeID []byte) { // Discovery via DHT. _, peer, _ = core.FindNode(nodeID, time.Second*10) if peer == nil { - fmt.Printf("Not found via DHT :(\n") + fmt.Printf("* Not found via DHT :(\n") return } - fmt.Printf("Successfully discovered via DHT.\n") + fmt.Printf("* Successfully discovered via DHT.\n") } // virtual peer? @@ -57,6 +57,7 @@ func debugCmdConnect(nodeID []byte) { var monitorKeys map[string]struct{} var monitorKeysMutex sync.RWMutex +var enableMonitorAll = false // Enables output for all searches. Otherwise it only monitors searches stored in monitorKeys. func addKeyMonitor(key []byte) { monitorKeysMutex.Lock() @@ -72,11 +73,13 @@ func removeKeyMonitor(key []byte) { 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 + if !enableMonitorAll { + monitorKeysMutex.Lock() + _, ok := monitorKeys[string(client.Key)] + monitorKeysMutex.Unlock() + if !ok { + return + } } keyA := client.Key @@ -85,9 +88,15 @@ func filterSearchStatus(client *dht.SearchClient, function, format string, v ... } intend := " ->" - if function == "sendInfoRequest" { + + switch function { + case "search.sendInfoRequest": intend = " >" + case "dht.FindNode", "dht.Get", "dht.Store": + intend = " -" + case "search.startSearch": + intend = " >" } - fmt.Printf(intend+" ["+function+" "+hex.EncodeToString(keyA)+"] "+format, v...) + fmt.Printf(intend+" "+function+" ["+hex.EncodeToString(keyA)+"] "+format, v...) } diff --git a/Command Line.go b/Command Line.go index 3b0c021..0a22aac 100644 --- a/Command Line.go +++ b/Command Line.go @@ -19,6 +19,7 @@ import ( "time" "github.com/PeernetOfficial/core" + "github.com/PeernetOfficial/core/dht" "github.com/btcsuite/btcd/btcec" ) @@ -83,20 +84,21 @@ func getUserOptionHash(reader *bufio.Reader) (hash []byte, valid bool) { func showHelp() { fmt.Print("Please enter a command:\n") - fmt.Print("help Show this help\n" + - "net list Lists all network adapters and their IPs\n" + - "status Get current status\n" + - "chat Send text to all peers\n" + - "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" + - "dht get Get data via DHT by hash\n" + - "dht store Store data into DHT\n" + - "log error Set error log output\n" + + fmt.Print("help Show this help\n" + + "net list Lists all network adapters and their IPs\n" + + "status Get current status\n" + + "chat Send text to all peers\n" + + "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" + + "debug watch searches Watches all outgoing DHT searches\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" + + "dht get Get data via DHT by hash\n" + + "dht store Store data into DHT\n" + + "log error Set error log output\n" + "\n") } @@ -147,7 +149,7 @@ func userCommands() { } userAgent := strings.ToValidUTF8(peer.UserAgent, "?") - 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) + fmt.Printf("* Peer ID %s%s\n Node ID %s\n User Agent: %s\n\n%s\n 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": @@ -267,7 +269,7 @@ func userCommands() { case "dht store": if text, valid := getUserOptionString(reader); valid { - if err := core.StoreDataDHT([]byte(text)); err != nil { + if err := core.StoreDataDHT([]byte(text), 5); err != nil { fmt.Printf("Error storing data: %s\n", err.Error()) break } @@ -291,7 +293,7 @@ func userCommands() { case "log error": fmt.Printf("Please choose the target output of error messages:\n0 = Log file (default)\n1 = Command line\n2 = Log file + command line\n3 = None\n") if number, valid := getUserOptionInt(reader); !valid || number < 0 || number > 3 { - fmt.Printf("Invalid option.") + fmt.Printf("Invalid option.\n") } else { config.ErrorOutput = number } @@ -300,7 +302,7 @@ func userCommands() { 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.") + fmt.Printf("Invalid peer ID or node ID. It must be hex-encoded and 66 (peer ID) or 64 characters (node ID) long.\n") break } @@ -312,13 +314,13 @@ func userCommands() { // Assume peer ID was supplied. publicKeyB, err := hex.DecodeString(text) if err != nil || len(publicKeyB) != 33 { - fmt.Printf("Invalid peer ID encoding.") + fmt.Printf("Invalid peer ID encoding.\n") break } publicKey, err := btcec.ParsePubKey(publicKeyB, btcec.S256()) if err != nil { - fmt.Printf("Invalid peer ID (public key decoding failed).") + fmt.Printf("Invalid peer ID (public key decoding failed).\n") continue } @@ -326,18 +328,35 @@ func userCommands() { } else { // Node ID was supplied. if nodeID, err = hex.DecodeString(text); err != nil || len(nodeID) != 256/8 { - fmt.Printf("Invalid node ID encoding.") + fmt.Printf("Invalid node ID encoding.\n") break } } // is self? if bytes.Equal(nodeID, core.SelfNodeID()) { - fmt.Printf("Target node is self.") + fmt.Printf("Target node is self.\n") break } debugCmdConnect(nodeID) + + case "debug watch searches": + fmt.Printf("Enable (1) or disable (0) watching of all outgoing DHT searches? (current setting: %t)\n", enableMonitorAll) + if number, valid := getUserOptionInt(reader); !valid || number < 0 || number > 1 { + fmt.Printf("Invalid option.\n") + } else { + enableMonitorAll = number == 1 + } + + case "debug bucket refresh": + fmt.Printf("Disable (1) or enable (0) bucket refresh. This can be useful to disable bucket refresh when debugging outgoing DHT searches. (current setting: %t)\n", dht.DisableBucketRefresh) + if number, valid := getUserOptionInt(reader); !valid || number < 0 || number > 1 { + fmt.Printf("Invalid option.\n") + } else { + dht.DisableBucketRefresh = number == 1 + } + } } } @@ -464,8 +483,6 @@ func textPeerConnections(peer *core.PeerInfo) (text string) { } } - text += " --\n" - return text } diff --git a/Main.go b/Main.go index bf79bcd..7b511be 100644 --- a/Main.go +++ b/Main.go @@ -25,19 +25,19 @@ func init() { if status, err := core.LoadConfigOut(configFile, &config); err != nil { switch status { case 0: - fmt.Printf("Unknown error accessing config file '%s': %s", configFile, err.Error()) + fmt.Printf("Unknown error accessing config file '%s': %s\n", configFile, err.Error()) case 1: - fmt.Printf("Error reading config file '%s': %s", configFile, err.Error()) + fmt.Printf("Error reading config file '%s': %s\n", configFile, err.Error()) case 2: - fmt.Printf("Error parsing config file '%s' (make sure it is valid YAML format): %s", configFile, err.Error()) + fmt.Printf("Error parsing config file '%s' (make sure it is valid YAML format): %s\n", configFile, err.Error()) case 3: - fmt.Printf("Unknown error loading config file '%s': %s", configFile, err.Error()) + fmt.Printf("Unknown error loading config file '%s': %s\n", configFile, err.Error()) } os.Exit(1) } if err := core.InitLog(); err != nil { - fmt.Printf("Error opening log file: %s", err.Error()) + fmt.Printf("Error opening log file: %s\n", err.Error()) os.Exit(1) } diff --git a/go.mod b/go.mod index 38cef4e..adf8167 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/PeernetOfficial/Cmd go 1.16 require ( - github.com/PeernetOfficial/core v0.0.0-20210727233333-e5d09e70866a + github.com/PeernetOfficial/core v0.0.0-20210729011714-fc1f0407f3af github.com/btcsuite/btcd v0.22.0-beta.0.20210625194946-86a17263b0ff ) diff --git a/go.sum b/go.sum index 00570cb..6f05bbe 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -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/PeernetOfficial/core v0.0.0-20210729011714-fc1f0407f3af h1:oEWnUdo7Ymqc53CzpCZCI5aDmzGT69znMAeEl6G8L64= +github.com/PeernetOfficial/core v0.0.0-20210729011714-fc1f0407f3af/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/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA=