-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command Debug.go
356 lines (299 loc) · 13.4 KB
/
Command Debug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
File Name: Command Debug.go
Copyright: 2021 Peernet Foundation s.r.o.
Author: Peter Kleissner
*/
package main
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"sync"
"time"
"github.com/PeernetOfficial/core"
"github.com/PeernetOfficial/core/btcec"
"github.com/PeernetOfficial/core/dht"
"github.com/PeernetOfficial/core/protocol"
)
// debugCmdConnect connects to the node ID
func debugCmdConnect(backend *core.Backend, nodeID []byte, output io.Writer) {
fmt.Fprintf(output, "---------------- Connect to node %s ----------------\n", hex.EncodeToString(nodeID))
defer fmt.Fprintf(output, "---------------- done node %s ----------------\n", hex.EncodeToString(nodeID))
// in local DHT list?
_, peer := backend.IsNodeContact(nodeID)
if peer != nil {
fmt.Fprintf(output, "* In local routing table: Yes.\n")
} else {
fmt.Fprintf(output, "* In local routing table: No. Lookup via DHT. Timeout = 10 seconds.\n")
hashMonitorControl(nodeID, 0, output)
defer hashMonitorControl(nodeID, 1, nil)
// Discovery via DHT.
_, peer, _ = backend.FindNode(nodeID, time.Second*10)
if peer == nil {
fmt.Fprintf(output, "* Not found via DHT :(\n")
return
}
fmt.Fprintf(output, "* Successfully discovered via DHT.\n")
}
fmt.Fprintf(output, "* Peer details:\n")
fmt.Fprintf(output, " Uncontacted: %t\n", peer.IsVirtual())
fmt.Fprintf(output, " Root peer: %t\n", peer.IsRootPeer)
fmt.Fprintf(output, " User Agent: %s\n", peer.UserAgent)
fmt.Fprintf(output, " Firewall: %t\n", peer.IsFirewallReported())
// virtual peer?
if peer.IsVirtual() {
fmt.Fprintf(output, "* Peer is virtual and was not contacted before. Sending out ping.\n")
peer.Ping()
} else {
fmt.Fprintf(output, "* Connections:\n")
fmt.Fprintf(output, "%s", textPeerConnections(peer))
}
// ping via all connections TODO
//fmt.Fprintf(output, "* Sending ping:\n")
}
// ---- filter for outgoing DHT searches ----
// debug output of monitored keys searched in the DHT
var monitorKeys map[string]io.Writer = make(map[string]io.Writer)
var monitorKeysMutex sync.RWMutex
// hashMonitorControl adds (0), removes (1), or inverts (2) a hash on the list
func hashMonitorControl(key []byte, action int, output io.Writer) (added bool) {
monitorKeysMutex.Lock()
defer monitorKeysMutex.Unlock()
switch action {
case 0:
monitorKeys[string(key)] = output
added = true
case 1:
delete(monitorKeys, string(key))
case 2:
if _, ok := monitorKeys[string(key)]; !ok {
monitorKeys[string(key)] = output
added = true
} else {
delete(monitorKeys, string(key))
}
}
return
}
func hashIsMonitored(keys ...[]byte) (monitored bool, output io.Writer) {
monitorKeysMutex.Lock()
defer monitorKeysMutex.Unlock()
for _, key := range keys {
if output, monitored = monitorKeys[string(key)]; monitored {
return true, output
}
}
return false, nil
}
const keyMonitorAllSearches = "all searches" // special key to monitor all searches
func filterSearchStatus(client *dht.SearchClient, function, format string, v ...interface{}) {
monitored, output := hashIsMonitored(client.Key, []byte(keyMonitorAllSearches))
if !monitored {
return
}
keyA := client.Key
if len(keyA) > 8 {
keyA = keyA[:8]
}
intend := " ->"
switch function {
case "search.sendInfoRequest":
intend = " >"
case "dht.FindNode", "dht.Get", "dht.Store":
intend = " -"
case "search.startSearch":
intend = " >"
}
fmt.Fprintf(output, intend+" "+function+" ["+hex.EncodeToString(keyA)+"] "+format, v...)
}
// ---- filter for incoming information requests ----
const keyMonitorAllRequests = "all requests" // special key to monitor all info requests
func filterIncomingRequest(peer *core.PeerInfo, Action int, Key []byte, Info interface{}) {
monitored, output := hashIsMonitored(peer.NodeID, []byte(keyMonitorAllRequests))
if !monitored {
return
}
requestType := "UNKNOWN"
switch Action {
case protocol.ActionFindSelf:
requestType = "FIND_SELF"
case protocol.ActionFindPeer:
requestType = "FIND_PEER"
case protocol.ActionFindValue:
requestType = "FIND_VALUE"
case protocol.ActionInfoStore:
requestType = "INFO_STORE"
}
if Action == protocol.ActionFindSelf && bytes.Equal(peer.NodeID, Key) {
fmt.Fprintf(output, "Info request from %s %s\n", hex.EncodeToString(peer.NodeID), requestType)
} else {
fmt.Fprintf(output, "Info request from %s %s for key %s\n", hex.EncodeToString(peer.NodeID), requestType, hex.EncodeToString(Key))
}
}
// ---- filter for incoming and outgoing packets ----
func filterMessageIn(peer *core.PeerInfo, raw *protocol.MessageRaw, message interface{}) {
monitored, output := hashIsMonitored(peer.NodeID)
if !monitored {
// TODO: For Announcement/Response also check data, Traverse the final target
return
}
commandA := "Unknown"
switch raw.Command {
case protocol.CommandAnnouncement:
commandA = "Announcement"
case protocol.CommandResponse:
commandA = "Response"
case protocol.CommandPing:
commandA = "Ping"
case protocol.CommandPong:
commandA = "Pong"
case protocol.CommandLocalDiscovery:
commandA = "Local Discovery"
case protocol.CommandTraverse:
commandA = "Traverse"
case protocol.CommandChat:
commandA = "Chat"
}
text := fmt.Sprintf("-------- Node %s Incoming %s --------\n", hex.EncodeToString(peer.NodeID), commandA)
text += fmt.Sprintf("Sender Peer ID: %s\n", hex.EncodeToString(peer.PublicKey.SerializeCompressed()))
if !raw.SenderPublicKey.IsEqual(peer.PublicKey) {
text += fmt.Sprintf("WARNING: Mismatch of public keys, sender %s and packet indicates %s\n", hex.EncodeToString(peer.PublicKey.SerializeCompressed()), hex.EncodeToString(raw.SenderPublicKey.SerializeCompressed()))
}
if message == nil {
text += "(no message decoded)\n"
} else if announce, ok := message.(*protocol.MessageAnnouncement); ok {
text += fmt.Sprintf("Fields:\n Protocol supported %d\n", announce.Protocol)
text += fmt.Sprintf(" Feature bits %d\n", announce.Features)
text += fmt.Sprintf(" Action bits %d\n", announce.Actions)
text += fmt.Sprintf(" Blockchain Height %d\n", announce.BlockchainHeight)
text += fmt.Sprintf(" Blockchain Version %d\n", announce.BlockchainVersion)
text += fmt.Sprintf(" Port Internal %d\n", announce.PortInternal)
text += fmt.Sprintf(" Port External %d\n", announce.PortExternal)
text += fmt.Sprintf(" User Agent %s\n", announce.UserAgent)
if len(announce.FindPeerKeys) > 0 {
text += fmt.Sprintf("FIND_PEER %d records:\n", len(announce.FindPeerKeys))
}
for _, find := range announce.FindPeerKeys {
text += fmt.Sprintf(" - Find peer %s\n", hex.EncodeToString(find.Hash))
}
if len(announce.FindDataKeys) > 0 {
text += fmt.Sprintf("FIND_VALUE %d records:\n", len(announce.FindDataKeys))
}
for _, find := range announce.FindDataKeys {
text += fmt.Sprintf(" - Find data %s\n", hex.EncodeToString(find.Hash))
}
if len(announce.InfoStoreFiles) > 0 {
text += fmt.Sprintf("INFO_STORE %d records:\n", len(announce.InfoStoreFiles))
}
for _, info := range announce.InfoStoreFiles {
text += fmt.Sprintf(" - Info store %s, type %d, size %d\n", hex.EncodeToString(info.ID.Hash), info.Type, info.Size)
}
} else if response, ok := message.(*protocol.MessageResponse); ok {
text += fmt.Sprintf("Fields:\n Protocol supported %d\n", response.Protocol)
text += fmt.Sprintf(" Feature bits %d\n", response.Features)
text += fmt.Sprintf(" Action bits %d\n", response.Actions)
text += fmt.Sprintf(" Blockchain Height %d\n", response.BlockchainHeight)
text += fmt.Sprintf(" Blockchain Version %d\n", response.BlockchainVersion)
text += fmt.Sprintf(" Port Internal %d\n", response.PortInternal)
text += fmt.Sprintf(" Port External %d\n", response.PortExternal)
text += fmt.Sprintf(" User Agent %s\n", response.UserAgent)
for _, hash := range response.Hash2Peers {
isLast := ""
if hash.IsLast {
isLast = " [last result in sequence]"
}
text += fmt.Sprintf(" - Peers known for the hash %s%s\n", hex.EncodeToString(hash.ID.Hash), isLast)
for n := range hash.Closest {
text += fmt.Sprintf(" Close peer:\n%s\n", outputPeerRecord(&hash.Closest[n]))
}
for n := range hash.Storing {
text += fmt.Sprintf(" Peer stores:\n%s\n", outputPeerRecord(&hash.Storing[n]))
}
}
for _, find := range response.FilesEmbed {
text += fmt.Sprintf(" - File embedded %s (%d bytes)\n", hex.EncodeToString(find.ID.Hash), len(find.Data))
}
for _, hash := range response.HashesNotFound {
text += fmt.Sprintf(" - Hash not found %s\n", hex.EncodeToString(hash))
}
} else if traverse, ok := message.(*protocol.MessageTraverse); ok {
text += fmt.Sprintf("Fields:\n Target Peer %s\n", hex.EncodeToString(traverse.TargetPeer.SerializeCompressed()))
text += fmt.Sprintf(" Authorized Relay Peer %s\n", hex.EncodeToString(traverse.AuthorizedRelayPeer.SerializeCompressed()))
text += fmt.Sprintf(" Signer Public Key %s\n", hex.EncodeToString(traverse.SignerPublicKey.SerializeCompressed()))
text += fmt.Sprintf(" Expires %s\n", traverse.Expires.String())
text += fmt.Sprintf(" IPv4 %s\n", traverse.IPv4.String())
text += fmt.Sprintf(" Port IPv4 %d\n", traverse.PortIPv4)
text += fmt.Sprintf(" Port IPv4 Reported External %d\n", traverse.PortIPv4ReportedExternal)
text += fmt.Sprintf(" IPv6 %s\n", traverse.IPv6.String())
text += fmt.Sprintf(" Port IPv6 %d\n", traverse.PortIPv6)
text += fmt.Sprintf(" Port IPv6 Reported External %d\n", traverse.PortIPv6ReportedExternal)
}
text += "--------\n"
output.Write([]byte(text))
}
func outputPeerRecord(record *protocol.PeerRecord) (output string) {
output += fmt.Sprintf(" * Peer ID %s\n", hex.EncodeToString(record.PublicKey.SerializeCompressed()))
output += fmt.Sprintf(" Node ID %s\n", hex.EncodeToString(record.NodeID))
if record.IPv4 != nil && !record.IPv4.IsUnspecified() {
output += fmt.Sprintf(" IPv4 %s\n", record.IPv4.String())
output += fmt.Sprintf(" Port IPv4 %d\n", record.IPv4Port)
output += fmt.Sprintf(" Port IPv4 Reported Internal %d\n", record.IPv4PortReportedInternal)
output += fmt.Sprintf(" Port IPv4 Reported External %d\n", record.IPv4PortReportedExternal)
}
if record.IPv6 != nil && !record.IPv6.IsUnspecified() {
output += fmt.Sprintf(" IPv6 %s\n", record.IPv6.String())
output += fmt.Sprintf(" Port IPv6 %d\n", record.IPv6Port)
output += fmt.Sprintf(" Port IPv6 Reported Internal %d\n", record.IPv6PortReportedInternal)
output += fmt.Sprintf(" Port IPv6 Reported External %d\n", record.IPv6PortReportedExternal)
}
output += fmt.Sprintf(" Last Contact %s", record.LastContactT.Format(dateFormat))
return
}
func outputOutgoingMessage(peer *core.PeerInfo, packet *protocol.PacketRaw) {
monitored, output := hashIsMonitored(peer.NodeID)
if !monitored {
// TODO: For Announcement/Response also check data, Traverse the final target
return
}
commandA := "Unknown"
switch packet.Command {
case protocol.CommandAnnouncement:
commandA = "Announcement"
case protocol.CommandResponse:
commandA = "Response"
case protocol.CommandPing:
commandA = "Ping"
case protocol.CommandPong:
commandA = "Pong"
case protocol.CommandLocalDiscovery:
commandA = "Local Discovery"
case protocol.CommandTraverse:
commandA = "Traverse"
case protocol.CommandChat:
commandA = "Chat"
}
text := fmt.Sprintf("-------- Node %s Outgoing %s --------\n", hex.EncodeToString(peer.NodeID), commandA)
text += fmt.Sprintf("Receiver Peer ID: %s\n", hex.EncodeToString(peer.PublicKey.SerializeCompressed()))
// TODO: Decoding of payload data (done by caller of this function)
text += "--------\n"
output.Write([]byte(text))
}
func filterMessageOutAnnouncement(receiverPublicKey *btcec.PublicKey, peer *core.PeerInfo, packet *protocol.PacketRaw, findSelf bool, findPeer []protocol.KeyHash, findValue []protocol.KeyHash, files []protocol.InfoStore) {
if peer == nil {
peer = &core.PeerInfo{PublicKey: receiverPublicKey, NodeID: protocol.PublicKey2NodeID(receiverPublicKey)}
}
outputOutgoingMessage(peer, packet)
}
func filterMessageOutResponse(peer *core.PeerInfo, packet *protocol.PacketRaw, hash2Peers []protocol.Hash2Peer, filesEmbed []protocol.EmbeddedFileData, hashesNotFound [][]byte) {
outputOutgoingMessage(peer, packet)
}
func filterMessageOutTraverse(peer *core.PeerInfo, packet *protocol.PacketRaw, embeddedPacket *protocol.PacketRaw, receiverEnd *btcec.PublicKey) {
outputOutgoingMessage(peer, packet)
}
func filterMessageOutPing(peer *core.PeerInfo, packet *protocol.PacketRaw, connection *core.Connection) {
outputOutgoingMessage(peer, packet)
}
func filterMessageOutPong(peer *core.PeerInfo, packet *protocol.PacketRaw) {
outputOutgoingMessage(peer, packet)
}