New Filters.IncomingRequest to intercept incoming information requests of all types

This commit is contained in:
Kleissner
2021-07-29 04:10:53 +02:00
parent fc1f0407f3
commit 3233bf5c1b
3 changed files with 17 additions and 2 deletions

View File

@@ -36,6 +36,8 @@ func (peer *PeerInfo) cmdAnouncement(msg *MessageAnnouncement) {
// FIND_SELF: Requesting peers close to the sender?
if msg.Actions&(1<<ActionFindSelf) > 0 {
Filters.IncomingRequest(peer, ActionFindSelf, peer.NodeID, nil)
selfD := Hash2Peer{ID: KeyHash{peer.NodeID}}
// do not respond the caller's own peer (add to ignore list)
@@ -55,6 +57,8 @@ func (peer *PeerInfo) cmdAnouncement(msg *MessageAnnouncement) {
// FIND_PEER: Find a different peer?
if msg.Actions&(1<<ActionFindPeer) > 0 && len(msg.FindPeerKeys) > 0 {
for _, findPeer := range msg.FindPeerKeys {
Filters.IncomingRequest(peer, ActionFindPeer, findPeer.Hash, nil)
details := Hash2Peer{ID: findPeer}
// Same as before, put self as ignoredNodes.
@@ -75,6 +79,8 @@ func (peer *PeerInfo) cmdAnouncement(msg *MessageAnnouncement) {
// Find a value?
if msg.Actions&(1<<ActionFindValue) > 0 {
for _, findHash := range msg.FindDataKeys {
Filters.IncomingRequest(peer, ActionFindValue, findHash.Hash, nil)
stored, data := announcementGetData(findHash.Hash)
if stored && len(data) > 0 {
filesEmbed = append(filesEmbed, EmbeddedFileData{ID: findHash, Data: data})
@@ -89,6 +95,10 @@ func (peer *PeerInfo) cmdAnouncement(msg *MessageAnnouncement) {
// Information about files stored by the sender?
if msg.Actions&(1<<ActionInfoStore) > 0 && len(msg.InfoStoreFiles) > 0 {
for n := range msg.InfoStoreFiles {
Filters.IncomingRequest(peer, ActionInfoStore, msg.InfoStoreFiles[n].ID.Hash, &msg.InfoStoreFiles[n])
}
peer.announcementStore(msg.InfoStoreFiles)
}

View File

@@ -30,6 +30,9 @@ var Filters struct {
// DHTSearchStatus is called with updates of searches in the DHT. It allows to see the live progress of searches.
DHTSearchStatus func(client *dht.SearchClient, function, format string, v ...interface{})
// IncomingRequest receives all incoming information requests. The action field is set accordingly.
IncomingRequest func(peer *PeerInfo, Action int, Key []byte, Info interface{})
}
func initFilters() {
@@ -45,10 +48,12 @@ func initFilters() {
if Filters.DHTSearchStatus == nil {
Filters.DHTSearchStatus = func(client *dht.SearchClient, function, format string, v ...interface{}) {}
}
if Filters.LogError == nil {
Filters.LogError = DefaultLogError
}
if Filters.IncomingRequest == nil {
Filters.IncomingRequest = func(peer *PeerInfo, Action int, Key []byte, Info interface{}) {}
}
}
// DefaultLogError is the default error logging function

View File

@@ -2,7 +2,7 @@
The core library which is needed for any Peernet application. It provides connectivity to the network and all basic functions. For details about Peernet see https://peernet.org/. For the current technical roadmap and upcoming releases see the [Talk forum](https://talk.peernet.org/discussion/10/technical-roadmap).
Current version: Alpha 2
Current version: Alpha 3
Current development status: Initial connectivity works. DHT functionality is in development.